Skip to content

Commit a32e8f1

Browse files
authored
Merge pull request #4856 from NativeScript/fatme/fix-argv
fix: respect correctly `hmr: false` from command's dashed options
2 parents 1f6beef + 214311f commit a32e8f1

File tree

6 files changed

+70
-124
lines changed

6 files changed

+70
-124
lines changed

lib/common/definitions/yargs.d.ts

-100
This file was deleted.

lib/common/services/commands-service.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ export class CommandsService implements ICommandsService {
2727
private $staticConfig: Config.IStaticConfig,
2828
private $helpService: IHelpService,
2929
private $extensibilityService: IExtensibilityService,
30-
private $optionsTracker: IOptionsTracker,
31-
private $projectDataService: IProjectDataService) {
30+
private $optionsTracker: IOptionsTracker) {
3231
}
3332

3433
public allCommands(opts: { includeDevCommands: boolean }): string[] {
@@ -106,15 +105,8 @@ export class CommandsService implements ICommandsService {
106105
private async tryExecuteCommandAction(commandName: string, commandArguments: string[]): Promise<boolean | ICanExecuteCommandOutput> {
107106
const command = this.$injector.resolveCommand(commandName);
108107
if (!command || !command.isHierarchicalCommand) {
109-
let projectData = null;
110-
try {
111-
projectData = this.$projectDataService.getProjectData();
112-
} catch (err) {
113-
this.$logger.trace(`Error while trying to get project data. More info: ${err}`);
114-
}
115-
116108
const dashedOptions = command ? command.dashedOptions : null;
117-
this.$options.validateOptions(dashedOptions, projectData);
109+
this.$options.validateOptions(dashedOptions);
118110
}
119111

120112
return this.canExecuteCommand(commandName, commandArguments);

lib/options.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class Options {
66
private static NONDASHED_OPTION_REGEX = /(.+?)[-]([a-zA-Z])(.*)/;
77

88
private optionsWhiteList = ["ui", "recursive", "reporter", "require", "timeout", "_", "$0"]; // These options shouldn't be validated
9-
public argv: IYargArgv;
9+
private yargsArgv: yargs.Argv;
1010
private globalOptions: IDictionary<IDashedOption> = {
1111
log: { type: OptionType.String, hasSensitiveValue: false },
1212
verbose: { type: OptionType.Boolean, alias: "v", hasSensitiveValue: false },
@@ -19,24 +19,23 @@ export class Options {
1919
_: { type: OptionType.String, hasSensitiveValue: false }
2020
};
2121

22+
public argv: yargs.Arguments;
2223
public options: IDictionary<IDashedOption>;
2324

24-
public setupOptions(projectData: IProjectData, commandSpecificDashedOptions?: IDictionary<IDashedOption>): void {
25+
public setupOptions(commandSpecificDashedOptions?: IDictionary<IDashedOption>): void {
2526
if (commandSpecificDashedOptions) {
2627
_.extend(this.options, commandSpecificDashedOptions);
2728
this.setArgv();
2829
}
2930

30-
if (this.argv.release && this.argv.hmr) {
31+
this.argv.bundle = "webpack";
32+
33+
// Check if the user has explicitly provide --hmr and --release options from command line
34+
if (this.yargsArgv.argv.release && this.yargsArgv.argv.hmr) {
3135
this.$errors.failWithoutHelp("The options --release and --hmr cannot be used simultaneously.");
3236
}
3337

34-
this.argv.bundle = "webpack";
35-
36-
const parsed = require("yargs-parser")(process.argv.slice(2), { 'boolean-negation': false });
37-
// --no-hmr -> hmr: false or --hmr false -> hmr: 'false'
38-
const noHmr = parsed && (parsed.hmr === false || parsed.hmr === 'false');
39-
if (!noHmr) {
38+
if (this.argv.hmr) {
4039
this.argv.hmr = !this.argv.release;
4140
}
4241

@@ -111,7 +110,7 @@ export class Options {
111110
pluginName: { type: OptionType.String, hasSensitiveValue: false },
112111
includeTypeScriptDemo: { type: OptionType.String, hasSensitiveValue: false },
113112
includeAngularDemo: { type: OptionType.String, hasSensitiveValue: false },
114-
hmr: { type: OptionType.Boolean, hasSensitiveValue: false },
113+
hmr: { type: OptionType.Boolean, hasSensitiveValue: false, default: true },
115114
collection: { type: OptionType.String, alias: "c", hasSensitiveValue: false },
116115
json: { type: OptionType.Boolean, hasSensitiveValue: false },
117116
avd: { type: OptionType.String, hasSensitiveValue: true },
@@ -159,8 +158,8 @@ export class Options {
159158
return this.argv[optionName];
160159
}
161160

162-
public validateOptions(commandSpecificDashedOptions?: IDictionary<IDashedOption>, projectData?: IProjectData): void {
163-
this.setupOptions(projectData, commandSpecificDashedOptions);
161+
public validateOptions(commandSpecificDashedOptions?: IDictionary<IDashedOption>): void {
162+
this.setupOptions(commandSpecificDashedOptions);
164163
const parsed = Object.create(null);
165164
// DO NOT REMOVE { } as when they are missing and some of the option values is false, the each stops as it thinks we have set "return false".
166165
_.each(_.keys(this.argv), optionName => {
@@ -251,12 +250,13 @@ export class Options {
251250
opts[this.getDashedOptionName(key)] = value;
252251
});
253252

254-
this.argv = yargs(process.argv.slice(2)).options(opts).argv;
253+
this.yargsArgv = yargs(process.argv.slice(2));
254+
this.argv = this.yargsArgv.options(<any>opts).argv;
255255

256256
// For backwards compatibility
257257
// Previously profileDir had a default option and calling `this.$options.profileDir` always returned valid result.
258258
// Now the profileDir should be used from $settingsService, but ensure the `this.$options.profileDir` returns the same value.
259-
this.$settingsService.setSettings({ profileDir: this.argv.profileDir });
259+
this.$settingsService.setSettings({ profileDir: <string>this.argv.profileDir });
260260
this.argv.profileDir = this.argv["profile-dir"] = this.$settingsService.getProfileDir();
261261

262262
// if justlaunch is set, it takes precedence over the --watch flag and the default true value

npm-shrinkwrap.json

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"@types/universal-analytics": "0.4.1",
116116
"@types/ws": "4.0.1",
117117
"@types/xml2js": "0.4.3",
118+
"@types/yargs": "13.0.0",
118119
"chai": "4.0.2",
119120
"chai-as-promised": "7.0.0",
120121
"grunt": "1.0.1",

test/options.ts

+38
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,44 @@ describe("options", () => {
286286
assert.deepEqual(actualError, testCase.expectedError);
287287
});
288288
});
289+
290+
describe("hmr option", () => {
291+
const testCases = [{
292+
name: "should set hmr to true by default",
293+
expectedHmrValue: true
294+
}, {
295+
name: "set set hmr to false when --no-hmr is provided",
296+
args: ["--no-hmr"],
297+
expectedHmrValue: false
298+
}, {
299+
name: "should set hmr to false when provided through dashed options from command",
300+
commandSpecificDashedOptions: {
301+
hmr: { type: OptionType.Boolean, default: false, hasSensitiveValue: false },
302+
},
303+
expectedHmrValue: false
304+
}, {
305+
name: "should set hmr to false by default when --release option is provided",
306+
args: ["--release"],
307+
expectedHmrValue: false
308+
}, {
309+
name: "should set hmr to false by default when --debug-brk option is provided",
310+
args: ["--debugBrk"],
311+
expectedHmrValue: false
312+
}];
313+
314+
_.each(testCases, testCase => {
315+
it(testCase.name, () => {
316+
(testCase.args || []).forEach(arg => process.argv.push(arg));
317+
318+
const options: any = createOptions(testInjector);
319+
options.setupOptions(testCase.commandSpecificDashedOptions);
320+
321+
assert.equal(testCase.expectedHmrValue, options.argv.hmr);
322+
323+
(testCase.args || []).forEach(arg => process.argv.pop());
324+
});
325+
});
326+
});
289327
});
290328
});
291329

0 commit comments

Comments
 (0)