Skip to content

Commit ace53e5

Browse files
committed
fix: handle CLI usage as a library
1 parent dfbc087 commit ace53e5

18 files changed

+64
-43
lines changed

lib/commands/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export abstract class BuildCommandBase extends ValidatePlatformCommandBase {
6565
this.$errors.fail(`Applications for platform ${platform} can not be built on this OS`);
6666
}
6767

68-
this.$bundleValidatorHelper.validate();
68+
this.$bundleValidatorHelper.validate(this.$projectData);
6969
}
7070

7171
protected async validateArgs(args: string[], platform: string): Promise<ICanExecuteCommandOutput> {

lib/commands/debug.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class DebugPlatformCommand extends ValidatePlatformCommandBase implements
6969
}
7070

7171
const minSupportedWebpackVersion = this.$options.hmr ? LiveSyncCommandHelper.MIN_SUPPORTED_WEBPACK_VERSION_WITH_HMR : null;
72-
this.$bundleValidatorHelper.validate(minSupportedWebpackVersion);
72+
this.$bundleValidatorHelper.validate(this.$projectData, minSupportedWebpackVersion);
7373

7474
const result = await super.canExecuteCommandBase(this.platform, { validateOptions: true, notConfiguredEnvOptions: { hideCloudBuildOption: true, hideSyncToPreviewAppOption: true } });
7575
return result;

lib/commands/deploy.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export class DeployOnDeviceCommand extends ValidatePlatformCommandBase implement
1414
$platformsData: IPlatformsData,
1515
private $bundleValidatorHelper: IBundleValidatorHelper,
1616
private $androidBundleValidatorHelper: IAndroidBundleValidatorHelper) {
17-
super($options, $platformsData, $platformService, $projectData);
18-
this.$projectData.initializeProjectData();
17+
super($options, $platformsData, $platformService, $projectData);
18+
this.$projectData.initializeProjectData();
1919
}
2020

2121
public async execute(args: string[]): Promise<void> {
@@ -26,7 +26,7 @@ export class DeployOnDeviceCommand extends ValidatePlatformCommandBase implement
2626

2727
public async canExecute(args: string[]): Promise<boolean | ICanExecuteCommandOutput> {
2828
this.$androidBundleValidatorHelper.validateNoAab();
29-
this.$bundleValidatorHelper.validate();
29+
this.$bundleValidatorHelper.validate(this.$projectData);
3030
if (!args || !args.length || args.length > 1) {
3131
return false;
3232
}

lib/commands/preview.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class PreviewCommand implements ICommand {
4646
}
4747

4848
await this.$networkConnectivityValidator.validate();
49-
this.$bundleValidatorHelper.validate(PreviewCommand.MIN_SUPPORTED_WEBPACK_VERSION);
49+
this.$bundleValidatorHelper.validate(this.$projectData, PreviewCommand.MIN_SUPPORTED_WEBPACK_VERSION);
5050
return true;
5151
}
5252
}

lib/declarations.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -940,13 +940,13 @@ interface IBundleValidatorHelper {
940940
* @param {string} minSupportedVersion the minimum supported version of nativescript-dev-webpack
941941
* @return {void}
942942
*/
943-
validate(minSupportedVersion?: string): void;
943+
validate(projectData: IProjectData, minSupportedVersion?: string): void;
944944

945945
/**
946946
* Returns the installed bundler version.
947947
* @return {string}
948948
*/
949-
getBundlerDependencyVersion(bundlerName?: string): string;
949+
getBundlerDependencyVersion(projectData: IProjectData, bundlerName?: string): string;
950950
}
951951

952952

lib/helpers/bundle-validator-helper.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ export class BundleValidatorHelper extends VersionValidatorHelper implements IBu
77
webpack: "nativescript-dev-webpack"
88
};
99

10-
constructor(protected $projectData: IProjectData,
11-
protected $errors: IErrors,
10+
constructor(protected $errors: IErrors,
1211
protected $options: IOptions) {
1312
super();
14-
this.$projectData.initializeProjectData();
1513
}
1614

17-
public validate(minSupportedVersion?: string): void {
15+
public validate(projectData: IProjectData, minSupportedVersion?: string): void {
1816
if (this.$options.bundle) {
19-
const currentVersion = this.getBundlerDependencyVersion();
17+
const currentVersion = this.getBundlerDependencyVersion(projectData);
2018
if (!currentVersion) {
2119
this.$errors.failWithoutHelp(BundleValidatorMessages.MissingBundlePlugin);
2220
}
@@ -28,11 +26,11 @@ export class BundleValidatorHelper extends VersionValidatorHelper implements IBu
2826
}
2927
}
3028

31-
public getBundlerDependencyVersion(bundlerName?: string): string {
29+
public getBundlerDependencyVersion(projectData: IProjectData, bundlerName?: string): string {
3230
let dependencyVersion = null;
3331
const bundlePluginName = bundlerName || this.bundlersMap[this.$options.bundle];
34-
const bundlerVersionInDependencies = this.$projectData.dependencies && this.$projectData.dependencies[bundlePluginName];
35-
const bundlerVersionInDevDependencies = this.$projectData.devDependencies && this.$projectData.devDependencies[bundlePluginName];
32+
const bundlerVersionInDependencies = projectData.dependencies && projectData.dependencies[bundlePluginName];
33+
const bundlerVersionInDevDependencies = projectData.devDependencies && projectData.devDependencies[bundlePluginName];
3634
dependencyVersion = bundlerVersionInDependencies || bundlerVersionInDevDependencies;
3735

3836
return dependencyVersion;

lib/helpers/livesync-command-helper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class LiveSyncCommandHelper implements ILiveSyncCommandHelper {
152152
}
153153

154154
const minSupportedWebpackVersion = this.$options.hmr ? LiveSyncCommandHelper.MIN_SUPPORTED_WEBPACK_VERSION_WITH_HMR : null;
155-
this.$bundleValidatorHelper.validate(minSupportedWebpackVersion);
155+
this.$bundleValidatorHelper.validate(this.$projectData, minSupportedWebpackVersion);
156156

157157
return result;
158158
}

lib/services/project-data-service.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface IProjectFileData {
1919
}
2020

2121
export class ProjectDataService implements IProjectDataService {
22-
private defaultProjectDir = "";
22+
private defaultProjectDir: string;
2323
private static DEPENDENCIES_KEY_NAME = "dependencies";
2424
private projectDataCache: IDictionary<IProjectData> = {};
2525

@@ -28,12 +28,16 @@ export class ProjectDataService implements IProjectDataService {
2828
private $logger: ILogger,
2929
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
3030
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
31-
private $injector: IInjector,
32-
$projectData: IProjectData) {
33-
// add the ProjectData of the default projectDir to the projectData cache
34-
$projectData.initializeProjectData();
35-
this.defaultProjectDir = $projectData.projectDir;
36-
this.projectDataCache[this.defaultProjectDir] = $projectData;
31+
private $injector: IInjector) {
32+
try {
33+
// add the ProjectData of the default projectDir to the projectData cache
34+
const projectData = this.$injector.resolve("projectData");
35+
projectData.initializeProjectData();
36+
this.defaultProjectDir = projectData.projectDir;
37+
this.projectDataCache[this.defaultProjectDir] = projectData;
38+
} catch (e) {
39+
// the CLI is required as a lib from a non-project folder
40+
}
3741
}
3842

3943
public getNSValue(projectDir: string, propertyName: string): any {

lib/services/workflow-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class WorkflowService implements IWorkflowService {
6868
const validWebpackPluginTags = ["*", "latest", "next", "rc"];
6969

7070
let isInstalledVersionSupported = true;
71-
const installedVersion = this.$bundleValidatorHelper.getBundlerDependencyVersion(webpackPluginName);
71+
const installedVersion = this.$bundleValidatorHelper.getBundlerDependencyVersion(projectData, webpackPluginName);
7272
this.$logger.trace(`Updating to webpack workflow: Found ${webpackPluginName} v${installedVersion}`);
7373
if (validWebpackPluginTags.indexOf(installedVersion) === -1) {
7474
const isInstalledVersionValid = !!semver.valid(installedVersion) || !!semver.coerce(installedVersion);

test/debug.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { SettingsService } from "../lib/common/test/unit-tests/stubs";
1515
function createTestInjector(): IInjector {
1616
const testInjector: IInjector = new yok.Yok();
1717

18+
testInjector.register("workflowService", stubs.WorkflowServiceStub);
1819
testInjector.register("debug|android", DebugAndroidCommand);
1920
testInjector.register("config", Configuration);
2021
testInjector.register("staticConfig", StaticConfig);

test/helpers/bundle-validator-helper.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,18 @@ describe("BundleValidatorHelper", () => {
9090
]);
9191
});
9292

93-
_.each(testCases, (testCase: any) => {
94-
const deps = {
95-
"nativescript-dev-webpack": testCase.currentWebpackVersion
96-
};
93+
_.each(testCases, (testCase: any) => {
94+
const deps = {
95+
"nativescript-dev-webpack": testCase.currentWebpackVersion
96+
};
9797

98-
it(`${testCase.name}`, async () => {
99-
const injector = createTestInjector({ dependencies: testCase.isDependency ? deps : null, devDependencies: !testCase.isDependency ? deps : null });
100-
const bundleValidatorHelper = injector.resolve("bundleValidatorHelper");
101-
bundleValidatorHelper.validate(testCase.minSupportedWebpackVersion);
98+
it(`${testCase.name}`, async () => {
99+
const injector = createTestInjector({ dependencies: testCase.isDependency ? deps : null, devDependencies: !testCase.isDependency ? deps : null });
100+
const bundleValidatorHelper = injector.resolve("bundleValidatorHelper");
101+
const projectData = injector.resolve("projectData");
102+
bundleValidatorHelper.validate(projectData, testCase.minSupportedWebpackVersion);
102103

103-
assert.deepEqual(error, testCase.expectedError);
104-
});
104+
assert.deepEqual(error, testCase.expectedError);
105105
});
106+
});
106107
});

test/package-installation-manager.ts

+3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ import * as yok from "../lib/common/yok";
1414
import ChildProcessLib = require("../lib/common/child-process");
1515
import { SettingsService } from "../lib/common/test/unit-tests/stubs";
1616
import { ProjectDataService } from "../lib/services/project-data-service";
17+
import { WorkflowServiceStub, ProjectDataStub } from "./stubs";
1718

1819
function createTestInjector(): IInjector {
1920
const testInjector = new yok.Yok();
2021

22+
testInjector.register("workflowService", WorkflowServiceStub);
23+
testInjector.register("projectData", ProjectDataStub);
2124
testInjector.register("config", ConfigLib.Configuration);
2225
testInjector.register("logger", LoggerLib.Logger);
2326
testInjector.register("errors", ErrorsLib.Errors);

test/platform-commands.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as ChildProcessLib from "../lib/common/child-process";
2020
import ProjectChangesLib = require("../lib/services/project-changes-service");
2121
import { Messages } from "../lib/common/messages/messages";
2222
import { SettingsService } from "../lib/common/test/unit-tests/stubs";
23+
import { WorkflowServiceStub } from "./stubs";
2324

2425
let isCommandExecuted = true;
2526

@@ -97,6 +98,7 @@ function createTestInjector() {
9798
const testInjector = new yok.Yok();
9899

99100
testInjector.register("injector", testInjector);
101+
testInjector.register("workflowService", WorkflowServiceStub);
100102
testInjector.register("hooksService", stubs.HooksServiceStub);
101103
testInjector.register("staticConfig", StaticConfigLib.StaticConfig);
102104
testInjector.register("nodeModulesDependenciesBuilder", {});

test/platform-service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ temp.track();
3232
function createTestInjector() {
3333
const testInjector = new yok.Yok();
3434

35+
testInjector.register("workflowService", stubs.WorkflowServiceStub);
3536
testInjector.register('platformService', PlatformServiceLib.PlatformService);
3637
testInjector.register('errors', stubs.ErrorsStub);
3738
testInjector.register('logger', stubs.LoggerStub);

test/services/playground/preview-app-livesync-service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Yok } from "../../../lib/common/yok";
22
import * as _ from 'lodash';
3-
import { LoggerStub, ErrorsStub } from "../../stubs";
3+
import { LoggerStub, ErrorsStub, WorkflowServiceStub } from "../../stubs";
44
import { FilePayload, Device, FilesPayload } from "nativescript-preview-sdk";
55
import { PreviewAppLiveSyncService } from "../../../lib/services/livesync/playground/preview-app-livesync-service";
66
import * as chai from "chai";
@@ -101,6 +101,7 @@ function createTestInjector(options?: {
101101
options = options || {};
102102

103103
const injector = new Yok();
104+
injector.register("workflowService", WorkflowServiceStub);
104105
injector.register("logger", LoggerMock);
105106
injector.register("hmrStatusService", {});
106107
injector.register("errors", ErrorsStub);

test/services/project-data-service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Yok } from "../../lib/common/yok";
22
import { assert } from "chai";
33
import { ProjectDataService } from "../../lib/services/project-data-service";
4-
import { LoggerStub } from "../stubs";
4+
import { LoggerStub, WorkflowServiceStub, ProjectDataStub } from "../stubs";
55
import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER, PACKAGE_JSON_FILE_NAME, AssetConstants, ProjectTypes } from '../../lib/constants';
66
import { DevicePlatformsConstants } from "../../lib/common/mobile/device-platforms-constants";
77
import { basename, join } from "path";
@@ -43,6 +43,8 @@ const testData: any = [
4343

4444
const createTestInjector = (readTextData?: string): IInjector => {
4545
const testInjector = new Yok();
46+
testInjector.register("workflowService", WorkflowServiceStub);
47+
testInjector.register("projectData", ProjectDataStub);
4648
testInjector.register("staticConfig", {
4749
CLIENT_NAME_KEY_IN_PROJECT_FILE: CLIENT_NAME_KEY_IN_PROJECT_FILE,
4850
PROJECT_FILE_NAME: "package.json"

test/stubs.ts

+7
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,12 @@ export class PerformanceService implements IPerformanceService {
916916
processExecutionData() { }
917917
}
918918

919+
export class WorkflowServiceStub implements IWorkflowService {
920+
handleLegacyWorkflow(projectDir: string, settings: IWebpackWorkflowSettings, skipWarnings?: boolean, force?: boolean): Promise<void> {
921+
return;
922+
}
923+
}
924+
919925
export class InjectorStub extends Yok implements IInjector {
920926
constructor() {
921927
super();
@@ -954,5 +960,6 @@ export class InjectorStub extends Yok implements IInjector {
954960
getDevice: (): Mobile.IDevice => undefined,
955961
getDeviceByIdentifier: (): Mobile.IDevice => undefined
956962
});
963+
this.register("workflowService", WorkflowServiceStub);
957964
}
958965
}

test/update.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function createTestInjector(
2525
): IInjector {
2626
const testInjector: IInjector = new yok.Yok();
2727
testInjector.register("logger", stubs.LoggerStub);
28+
testInjector.register("workflowService", stubs.WorkflowServiceStub);
2829
testInjector.register("options", Options);
2930
testInjector.register('fs', stubs.FileSystemStub);
3031
testInjector.register("analyticsService", {
@@ -49,10 +50,10 @@ function createTestInjector(
4950
});
5051
testInjector.register("pluginVariablesService", {});
5152
testInjector.register("platformService", {
52-
getInstalledPlatforms: function(): string[] {
53+
getInstalledPlatforms: function (): string[] {
5354
return installedPlatforms;
5455
},
55-
getAvailablePlatforms: function(): string[] {
56+
getAvailablePlatforms: function (): string[] {
5657
return availablePlatforms;
5758
},
5859
removePlatforms: async (): Promise<void> => undefined,
@@ -66,9 +67,9 @@ function createTestInjector(
6667
getPlatformData: () => {
6768
return {
6869
platformProjectService: {
69-
validate
70-
}
71-
};
70+
validate
71+
}
72+
};
7273
}
7374
});
7475
testInjector.register("settingsService", SettingsService);
@@ -161,7 +162,7 @@ describe("update command method tests", () => {
161162
const fs = testInjector.resolve("fs");
162163
const copyFileStub = sandbox.stub(fs, "copyFile");
163164
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
164-
return updateCommand.execute(["3.3.0"]).then( () => {
165+
return updateCommand.execute(["3.3.0"]).then(() => {
165166
assert.isTrue(copyFileStub.calledWith(path.join(projectFolder, "package.json")));
166167
for (const folder of UpdateCommand.folders) {
167168
assert.isTrue(copyFileStub.calledWith(path.join(projectFolder, folder)));

0 commit comments

Comments
 (0)