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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 6 additions & 8 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 11 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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);

0 commit comments

Comments
 (0)