Skip to content

Commit 2c7bafb

Browse files
committed
fix: validate current bundle version from package.json when tns preview --bundle command is executed
1 parent 8036ba3 commit 2c7bafb

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

lib/commands/preview.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export class PreviewCommand implements ICommand {
22
public allowedParameters: ICommandParameter[] = [];
3+
private static MIN_SUPPORTED_WEBPACK_VERSION = "0.17.0";
34

45
constructor(private $bundleValidatorHelper: IBundleValidatorHelper,
56
private $liveSyncService: ILiveSyncService,
@@ -30,7 +31,7 @@ export class PreviewCommand implements ICommand {
3031

3132
public async canExecute(args: string[]): Promise<boolean> {
3233
await this.$networkConnectivityValidator.validate();
33-
this.$bundleValidatorHelper.validate();
34+
this.$bundleValidatorHelper.validate(PreviewCommand.MIN_SUPPORTED_WEBPACK_VERSION);
3435
return true;
3536
}
3637
}

lib/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,8 @@ export class IosProjectConstants {
227227
public static XcodeProjExtName = ".xcodeproj";
228228
public static XcodeSchemeExtName = ".xcscheme";
229229
}
230+
231+
export class BundleValidatorMessages {
232+
public static MissingBundlePlugin = "Passing --bundle requires a bundling plugin. No bundling plugin found or the specified bundling plugin is invalid.";
233+
public static NotSupportedVersion = `The NativeScript CLI requires nativescript-dev-webpack %s or later to work properly.`;
234+
}

lib/declarations.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,11 @@ interface IDeployCommandHelper {
841841
interface IBundleValidatorHelper {
842842
/**
843843
* Validates bundling options.
844+
* In case when minSupportedVersion is provided, gets the current version of nativescript-dev-webpack from package json and compares with the provided version.
845+
* @param {string} minSupportedVersion the minimum supported version of nativescript-dev-webpack
844846
* @return {void}
845847
*/
846-
validate(): void;
848+
validate(minSupportedVersion?: string): void;
847849
}
848850

849851
interface INativeScriptCloudExtensionService {

lib/helpers/bundle-validator-helper.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import * as semver from "semver";
2+
import * as util from "util";
3+
import { BundleValidatorMessages } from "../constants";
4+
15
export class BundleValidatorHelper implements IBundleValidatorHelper {
26
private bundlersMap: IStringDictionary = {
37
webpack: "nativescript-dev-webpack"
@@ -9,13 +13,21 @@ export class BundleValidatorHelper implements IBundleValidatorHelper {
913
this.$projectData.initializeProjectData();
1014
}
1115

12-
public validate(): void {
16+
public validate(minSupportedVersion?: string): void {
1317
if (this.$options.bundle) {
1418
const bundlePluginName = this.bundlersMap[this.$options.bundle];
1519
const hasBundlerPluginAsDependency = this.$projectData.dependencies && this.$projectData.dependencies[bundlePluginName];
1620
const hasBundlerPluginAsDevDependency = this.$projectData.devDependencies && this.$projectData.devDependencies[bundlePluginName];
1721
if (!bundlePluginName || (!hasBundlerPluginAsDependency && !hasBundlerPluginAsDevDependency)) {
18-
this.$errors.fail("Passing --bundle requires a bundling plugin. No bundling plugin found or the specified bundling plugin is invalid.");
22+
this.$errors.fail(BundleValidatorMessages.MissingBundlePlugin);
23+
}
24+
25+
if (minSupportedVersion) {
26+
const currentVersion = hasBundlerPluginAsDependency || hasBundlerPluginAsDevDependency;
27+
const isBundleSupported = semver.gte(semver.coerce(currentVersion), semver.coerce(minSupportedVersion));
28+
if (!isBundleSupported) {
29+
this.$errors.fail(util.format(BundleValidatorMessages.NotSupportedVersion, minSupportedVersion));
30+
}
1931
}
2032
}
2133
}

0 commit comments

Comments
 (0)