Skip to content

Commit 9a211ff

Browse files
Merge pull request #1658 from NativeScript/milanov/change-warning-for-project-with-name-app
Change the warning when creating project with name app
2 parents 3ea1378 + fa3742c commit 9a211ff

File tree

4 files changed

+47
-55
lines changed

4 files changed

+47
-55
lines changed

lib/commands/create-project.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,11 @@
33

44
import * as constants from "../constants";
55

6-
export class ProjectCommandParameter implements ICommandParameter {
7-
constructor(private $errors: IErrors,
8-
private $logger: ILogger,
9-
private $projectNameValidator: IProjectNameValidator) { }
10-
11-
mandatory = true;
12-
validate(value: string): IFuture<boolean> {
13-
return (() => {
14-
if(!value) {
15-
this.$errors.fail("You must specify <App name> when creating a new project.");
16-
}
17-
18-
if (value.toUpperCase() === "APP") {
19-
this.$logger.warn("You cannot build applications named 'app' in Xcode. Consider creating a project with different name.");
20-
}
21-
22-
return this.$projectNameValidator.validate(value);
23-
}).future<boolean>()();
24-
}
25-
}
26-
276
export class CreateProjectCommand implements ICommand {
287
constructor(private $projectService: IProjectService,
298
private $errors: IErrors,
30-
private $logger: ILogger,
31-
private $projectNameValidator: IProjectNameValidator,
32-
private $options: IOptions) { }
9+
private $options: IOptions,
10+
private $stringParameterBuilder: IStringParameterBuilder) { }
3311

3412
public enableHooks = false;
3513

@@ -45,6 +23,7 @@ export class CreateProjectCommand implements ICommand {
4523
}).future<void>()();
4624
}
4725

48-
allowedParameters = [new ProjectCommandParameter(this.$errors, this.$logger, this.$projectNameValidator) ];
26+
public allowedParameters: ICommandParameter[] = [this.$stringParameterBuilder.createMandatoryParameter("Project name cannot be empty.")];
4927
}
28+
5029
$injector.registerCommand("create", CreateProjectCommand);

lib/services/project-name-service.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,25 @@ export class ProjectNameService implements IProjectNameService {
1515
}
1616

1717
if (!this.$projectNameValidator.validate(projectName)) {
18-
return this.promptForNewName(projectName, validateOptions).wait();
18+
return this.promptForNewName("The project name is invalid.", projectName, validateOptions).wait();
1919
}
2020

21+
let userCanInteract = isInteractive();
22+
2123
if (!this.checkIfNameStartsWithLetter(projectName)) {
22-
if (!isInteractive()) {
24+
if (!userCanInteract) {
2325
this.$errors.fail("The project name does not start with letter and will fail to build for Android. If You want to create project with this name add --force to the create command.");
24-
return;
2526
}
2627

27-
return this.promptForNewName(projectName, validateOptions).wait();
28+
return this.promptForNewName("The project name does not start with letter and will fail to build for Android.", projectName, validateOptions).wait();
29+
}
30+
31+
if (projectName.toUpperCase() === "APP") {
32+
if (!userCanInteract) {
33+
this.$errors.fail("You cannot build applications named 'app' in Xcode. Consider creating a project with different name. If You want to create project with this name add --force to the create command.");
34+
}
35+
36+
return this.promptForNewName("You cannot build applications named 'app' in Xcode. Consider creating a project with different name.", projectName, validateOptions).wait();
2837
}
2938

3039
return projectName;
@@ -36,9 +45,9 @@ export class ProjectNameService implements IProjectNameService {
3645
return startsWithLetterExpression.test(projectName);
3746
}
3847

39-
private promptForNewName(projectName: string, validateOptions?: { force: boolean }): IFuture<string> {
48+
private promptForNewName(warningMessage: string, projectName: string, validateOptions?: { force: boolean }): IFuture<string> {
4049
return (() => {
41-
if (this.promptForForceNameConfirm().wait()) {
50+
if (this.promptForForceNameConfirm(warningMessage).wait()) {
4251
return projectName;
4352
}
4453

@@ -47,9 +56,9 @@ export class ProjectNameService implements IProjectNameService {
4756
}).future<string>()();
4857
}
4958

50-
private promptForForceNameConfirm(): IFuture<boolean> {
59+
private promptForForceNameConfirm(warningMessage: string): IFuture<boolean> {
5160
return (() => {
52-
this.$logger.warn("The project name does not start with letter and will fail to build for Android.");
61+
this.$logger.warn(warningMessage);
5362

5463
return this.$prompter.confirm("Do you want to create the project with this name?").wait();
5564
}).future<boolean>()();

test/project-commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { Yok } from "../lib/common/yok";
55
import * as stubs from "./stubs";
66
import { CreateProjectCommand } from "../lib/commands/create-project";
7+
import { StringParameterBuilder } from "../lib/common/command-params";
78
import * as constants from "../lib/constants";
89
import {assert} from "chai";
910

@@ -40,6 +41,7 @@ function createTestInjector() {
4041
template: undefined
4142
});
4243
testInjector.register("createCommand", CreateProjectCommand);
44+
testInjector.register("stringParameterBuilder", StringParameterBuilder);
4345

4446
return testInjector;
4547
}

test/project-name-service.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe("Project Name Service Tests", () => {
3333
let testInjector: IInjector;
3434
let projectNameService: IProjectNameService;
3535
let validProjectName = "valid";
36-
let invalidProjectName = "1invalid";
36+
let invalidProjectNames = ["1invalid", "app"];
3737

3838
beforeEach(() => {
3939
testInjector = createTestInjector();
@@ -46,33 +46,35 @@ describe("Project Name Service Tests", () => {
4646
assert.deepEqual(actualProjectName, validProjectName);
4747
});
4848

49-
it("returns correct name when invalid name is entered several times and then valid name is entered", () => {
50-
let prompter = testInjector.resolve("prompter");
51-
prompter.confirm = (message: string): IFuture<boolean> => Future.fromResult(false);
49+
_.each(invalidProjectNames, invalidProjectName => {
50+
it(`returns correct name when "${invalidProjectName}" is entered several times and then valid name is entered`, () => {
51+
let prompter = testInjector.resolve("prompter");
52+
prompter.confirm = (message: string): IFuture<boolean> => Future.fromResult(false);
5253

53-
let incorrectInputsLimit = 5;
54-
let incorrectInputsCount = 0;
54+
let incorrectInputsLimit = 5;
55+
let incorrectInputsCount = 0;
5556

56-
prompter.getString = (message: string): IFuture<string> => {
57-
return (() => {
58-
if (incorrectInputsCount < incorrectInputsLimit) {
59-
incorrectInputsCount++;
57+
prompter.getString = (message: string): IFuture<string> => {
58+
return (() => {
59+
if (incorrectInputsCount < incorrectInputsLimit) {
60+
incorrectInputsCount++;
6061

61-
return invalidProjectName;
62-
} else {
63-
return validProjectName;
64-
}
65-
}).future<string>()();
66-
};
62+
return invalidProjectName;
63+
} else {
64+
return validProjectName;
65+
}
66+
}).future<string>()();
67+
};
6768

68-
let actualProjectName = projectNameService.ensureValidName(invalidProjectName).wait();
69+
let actualProjectName = projectNameService.ensureValidName(invalidProjectName).wait();
6970

70-
assert.deepEqual(actualProjectName, validProjectName);
71-
});
71+
assert.deepEqual(actualProjectName, validProjectName);
72+
});
7273

73-
it("returns the invalid name when invalid name is entered and --force flag is present", () => {
74-
let actualProjectName = projectNameService.ensureValidName(validProjectName, { force: true }).wait();
74+
it(`returns the invalid name when "${invalidProjectName}" is entered and --force flag is present`, () => {
75+
let actualProjectName = projectNameService.ensureValidName(validProjectName, { force: true }).wait();
7576

76-
assert.deepEqual(actualProjectName, validProjectName);
77+
assert.deepEqual(actualProjectName, validProjectName);
78+
});
7779
});
7880
});

0 commit comments

Comments
 (0)