Skip to content

Commit 4f19063

Browse files
authored
Plamen5kov/remove obsolete code (#2461)
* refactoring refactored create project functionality removed --copy-from option, removed --tns-modules-version removed --copy-from and --link-to options prepare template now expects template name will never be undefined * lint * update after review
1 parent 9b0f965 commit 4f19063

7 files changed

+47
-113
lines changed

lib/declarations.ts

-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ interface IOptions extends ICommonOptions {
6565
baseConfig: string;
6666
client: boolean;
6767
compileSdk: number;
68-
copyFrom: string;
6968
copyTo: string;
7069
debugTransport: boolean;
7170
emulator: boolean;
@@ -81,7 +80,6 @@ interface IOptions extends ICommonOptions {
8180
keyStoreAliasPassword: string;
8281
keyStorePassword: string;
8382
keyStorePath: string;
84-
linkTo: string;
8583
ng: boolean;
8684
tsc: boolean;
8785
androidTypings: boolean;
@@ -90,7 +88,6 @@ interface IOptions extends ICommonOptions {
9088
port: Number;
9189
production: boolean; //npm flag
9290
sdk: string;
93-
tnsModulesVersion: string;
9491
teamId: string;
9592
syncAllFiles: boolean;
9693
liveEdit: boolean;

lib/options.ts

-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ export class Options extends commonOptionsLibPath.OptionsBase {
1414
frameworkName: { type: OptionType.String },
1515
framework: { type: OptionType.String },
1616
frameworkVersion: { type: OptionType.String },
17-
copyFrom: { type: OptionType.String },
18-
linkTo: { type: OptionType.String },
1917
forDevice: { type: OptionType.Boolean },
2018
provision: { type: OptionType.Object },
2119
client: { type: OptionType.Boolean, default: true },
@@ -27,7 +25,6 @@ export class Options extends commonOptionsLibPath.OptionsBase {
2725
keyStoreAliasPassword: { type: OptionType.String },
2826
ignoreScripts: { type: OptionType.Boolean },
2927
disableNpmInstall: { type: OptionType.Boolean },
30-
tnsModulesVersion: { type: OptionType.String },
3128
compileSdk: { type: OptionType.Number },
3229
port: { type: OptionType.Number },
3330
copyTo: { type: OptionType.String },

lib/services/init-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class InitService implements IInitService {
6565

6666
// In case console is interactive and --force is not specified, do not read the version from package.json, show all available versions to the user.
6767
let tnsCoreModulesVersionInPackageJson = this.useDefaultValue ? projectData.dependencies[constants.TNS_CORE_MODULES_NAME] : null;
68-
projectData.dependencies[constants.TNS_CORE_MODULES_NAME] = this.$options.tnsModulesVersion || tnsCoreModulesVersionInPackageJson || (await this.getVersionData(constants.TNS_CORE_MODULES_NAME))["version"];
68+
projectData.dependencies[constants.TNS_CORE_MODULES_NAME] = tnsCoreModulesVersionInPackageJson || (await this.getVersionData(constants.TNS_CORE_MODULES_NAME))["version"];
6969

7070
this.$fs.writeJson(this.projectFilePath, projectData);
7171
} catch (err) {

lib/services/project-service.ts

+27-82
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as constants from "../constants";
2-
import * as osenv from "osenv";
32
import * as path from "path";
43
import * as shelljs from "shelljs";
54

@@ -19,72 +18,37 @@ export class ProjectService implements IProjectService {
1918
if (!projectName) {
2019
this.$errors.fail("You must specify <App name> when creating a new project.");
2120
}
22-
2321
projectName = await this.$projectNameService.ensureValidName(projectName, { force: this.$options.force });
2422

25-
let projectId = this.$options.appid || this.$projectHelper.generateDefaultAppId(projectName, constants.DEFAULT_APP_IDENTIFIER_PREFIX);
26-
2723
let projectDir = path.join(path.resolve(this.$options.path || "."), projectName);
2824
this.$fs.createDirectory(projectDir);
2925
if (this.$fs.exists(projectDir) && !this.$fs.isEmptyDir(projectDir)) {
3026
this.$errors.fail("Path already exists and is not empty %s", projectDir);
3127
}
3228

29+
let projectId = this.$options.appid || this.$projectHelper.generateDefaultAppId(projectName, constants.DEFAULT_APP_IDENTIFIER_PREFIX);
3330
this.createPackageJson(projectDir, projectId);
3431

35-
let customAppPath = this.getCustomAppPath();
36-
if (customAppPath) {
37-
customAppPath = path.resolve(customAppPath);
38-
if (!this.$fs.exists(customAppPath)) {
39-
this.$errors.failWithoutHelp(`The specified path "${customAppPath}" doesn't exist. Check that you specified the path correctly and try again.`);
40-
}
41-
42-
let customAppContents = this.$fs.enumerateFilesInDirectorySync(customAppPath);
43-
if (customAppContents.length === 0) {
44-
this.$errors.failWithoutHelp(`The specified path "${customAppPath}" is empty directory.`);
45-
}
32+
this.$logger.trace(`Creating a new NativeScript project with name ${projectName} and id ${projectId} at location ${projectDir}`);
33+
if (!selectedTemplate) {
34+
selectedTemplate = constants.RESERVED_TEMPLATE_NAMES["default"];
4635
}
4736

48-
this.$logger.trace("Creating a new NativeScript project with name %s and id %s at location %s", projectName, projectId, projectDir);
49-
50-
let projectAppDirectory = path.join(projectDir, constants.APP_FOLDER_NAME);
51-
let appPath: string = null;
52-
if (customAppPath) {
53-
this.$logger.trace("Using custom app from %s", customAppPath);
54-
55-
// Make sure that the source app/ is not a direct ancestor of a target app/
56-
let relativePathFromSourceToTarget = path.relative(customAppPath, projectAppDirectory);
57-
// path.relative returns second argument if the paths are located on different disks
58-
// so in this case we don't need to make the check for direct ancestor
59-
if (relativePathFromSourceToTarget !== projectAppDirectory) {
60-
let doesRelativePathGoUpAtLeastOneDir = relativePathFromSourceToTarget.split(path.sep)[0] === "..";
61-
if (!doesRelativePathGoUpAtLeastOneDir) {
62-
this.$errors.fail("Project dir %s must not be created at/inside the template used to create the project %s.", projectDir, customAppPath);
63-
}
64-
}
65-
this.$logger.trace("Copying custom app into %s", projectAppDirectory);
66-
appPath = customAppPath;
67-
} else {
37+
try {
6838
let templatePath = await this.$projectTemplatesService.prepareTemplate(selectedTemplate, projectDir);
69-
this.$logger.trace(`Copying application from '${templatePath}' into '${projectAppDirectory}'.`);
70-
let templatePackageJson = this.$fs.readJson(path.join(templatePath, "package.json"));
71-
selectedTemplate = templatePackageJson.name;
72-
appPath = templatePath;
73-
}
39+
await this.extractTemplate(projectDir, templatePath);
7440

75-
try {
76-
//TODO: plamen5kov: move copy of template and npm uninstall in prepareTemplate logic
77-
await this.createProjectCore(projectDir, appPath, projectId);
78-
let templatePackageJsonData = this.getDataFromJson(appPath);
41+
let packageName = constants.TNS_CORE_MODULES_NAME;
42+
await this.$npm.install(packageName, projectDir, { save: true, "save-exact": true });
43+
44+
let templatePackageJsonData = this.getDataFromJson(templatePath);
7945
this.mergeProjectAndTemplateProperties(projectDir, templatePackageJsonData); //merging dependencies from template (dev && prod)
8046
this.removeMergedDependencies(projectDir, templatePackageJsonData);
47+
8148
await this.$npm.install(projectDir, projectDir, { "ignore-scripts": this.$options.ignoreScripts });
82-
selectedTemplate = selectedTemplate || "";
83-
let templateName = (constants.RESERVED_TEMPLATE_NAMES[selectedTemplate.toLowerCase()] || selectedTemplate/*user template*/) || constants.RESERVED_TEMPLATE_NAMES["default"];
84-
await this.$npm.uninstall(selectedTemplate, { save: true }, projectDir);
8549

86-
// TODO: plamen5kov: remove later (put only so tests pass (need to fix tests))
87-
this.$logger.trace(`Using NativeScript verified template: ${templateName} with version undefined.`);
50+
let templatePackageJson = this.$fs.readJson(path.join(templatePath, "package.json"));
51+
await this.$npm.uninstall(templatePackageJson.name, { save: true }, projectDir);
8852
} catch (err) {
8953
this.$fs.deleteDirectory(projectDir);
9054
throw err;
@@ -103,6 +67,18 @@ export class ProjectService implements IProjectService {
10367
return null;
10468
}
10569

70+
private async extractTemplate(projectDir: string, realTemplatePath: string): Promise<void> {
71+
this.$fs.ensureDirectoryExists(projectDir);
72+
73+
let appDestinationPath = path.join(projectDir, constants.APP_FOLDER_NAME);
74+
this.$fs.createDirectory(appDestinationPath);
75+
76+
this.$logger.trace(`Copying application from '${realTemplatePath}' into '${appDestinationPath}'.`);
77+
shelljs.cp('-R', path.join(realTemplatePath, "*"), appDestinationPath);
78+
79+
this.$fs.createDirectory(path.join(projectDir, "platforms"));
80+
}
81+
10682
private removeMergedDependencies(projectDir: string, templatePackageJsonData: any): void {
10783
let extractedTemplatePackageJsonPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.PACKAGE_JSON_FILE_NAME);
10884
for (let key in templatePackageJsonData) {
@@ -129,6 +105,8 @@ export class ProjectService implements IProjectService {
129105
}
130106
this.$logger.trace("New project package.json data: ", projectPackageJsonData);
131107
this.$fs.writeJson(projectPackageJsonPath, projectPackageJsonData);
108+
} else {
109+
this.$errors.failWithoutHelp(`Couldn't find package.json data in installed template`);
132110
}
133111
}
134112

@@ -147,42 +125,9 @@ export class ProjectService implements IProjectService {
147125
return sortedDeps;
148126
}
149127

150-
private async createProjectCore(projectDir: string, appSourcePath: string, projectId: string): Promise<void> {
151-
this.$fs.ensureDirectoryExists(projectDir);
152-
153-
let appDestinationPath = path.join(projectDir, constants.APP_FOLDER_NAME);
154-
this.$fs.createDirectory(appDestinationPath);
155-
156-
shelljs.cp('-R', path.join(appSourcePath, "*"), appDestinationPath);
157-
158-
this.$fs.createDirectory(path.join(projectDir, "platforms"));
159-
160-
let tnsModulesVersion = this.$options.tnsModulesVersion;
161-
let packageName = constants.TNS_CORE_MODULES_NAME;
162-
if (tnsModulesVersion) {
163-
packageName = `${packageName}@${tnsModulesVersion}`;
164-
}
165-
await this.$npm.install(packageName, projectDir, { save: true, "save-exact": true });
166-
}
167-
168128
private createPackageJson(projectDir: string, projectId: string): void {
169129
this.$projectDataService.initialize(projectDir);
170130
this.$projectDataService.setValue("id", projectId);
171131
}
172-
173-
private getCustomAppPath(): string {
174-
let customAppPath = this.$options.copyFrom || this.$options.linkTo;
175-
if (customAppPath) {
176-
if (customAppPath.indexOf("http://") === 0) {
177-
this.$errors.fail("Only local paths for custom app are supported.");
178-
}
179-
180-
if (customAppPath.substr(0, 1) === '~') {
181-
customAppPath = path.join(osenv.home(), customAppPath.substr(1));
182-
}
183-
}
184-
185-
return customAppPath;
186-
}
187132
}
188133
$injector.register("projectService", ProjectService);

lib/services/project-templates-service.ts

+12-21
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,28 @@ temp.track();
55

66
export class ProjectTemplatesService implements IProjectTemplatesService {
77

8-
public constructor(private $errors: IErrors,
9-
private $fs: IFileSystem,
8+
public constructor(private $fs: IFileSystem,
109
private $logger: ILogger,
1110
private $npmInstallationManager: INpmInstallationManager) { }
1211

1312
public async prepareTemplate(originalTemplateName: string, projectDir: string): Promise<string> {
1413
let realTemplatePath: string;
15-
if (originalTemplateName) {
16-
// support <reserved_name>@<version> syntax
17-
let data = originalTemplateName.split("@"),
18-
name = data[0],
19-
version = data[1];
14+
// support <reserved_name>@<version> syntax
15+
let data = originalTemplateName.split("@"),
16+
name = data[0],
17+
version = data[1];
2018

21-
if (constants.RESERVED_TEMPLATE_NAMES[name.toLowerCase()]) {
22-
realTemplatePath = await this.prepareNativeScriptTemplate(constants.RESERVED_TEMPLATE_NAMES[name.toLowerCase()], version, projectDir);
23-
} else {
24-
// Use the original template name, specified by user as it may be case-sensitive.
25-
realTemplatePath = await this.prepareNativeScriptTemplate(name, version, projectDir);
26-
}
19+
if (constants.RESERVED_TEMPLATE_NAMES[name.toLowerCase()]) {
20+
realTemplatePath = await this.prepareNativeScriptTemplate(constants.RESERVED_TEMPLATE_NAMES[name.toLowerCase()], version, projectDir);
2721
} else {
28-
realTemplatePath = await this.prepareNativeScriptTemplate(constants.RESERVED_TEMPLATE_NAMES["default"], null/*version*/, projectDir);
22+
// Use the original template name, specified by user as it may be case-sensitive.
23+
realTemplatePath = await this.prepareNativeScriptTemplate(originalTemplateName, version, projectDir);
2924
}
3025

31-
if (realTemplatePath) {
32-
//this removes dependencies from templates so they are not copied to app folder
33-
this.$fs.deleteDirectory(path.join(realTemplatePath, constants.NODE_MODULES_FOLDER_NAME));
34-
return realTemplatePath;
35-
}
26+
//this removes dependencies from templates so they are not copied to app folder
27+
this.$fs.deleteDirectory(path.join(realTemplatePath, constants.NODE_MODULES_FOLDER_NAME));
3628

37-
this.$errors.failWithoutHelp("Unable to find the template in temp directory. " +
38-
`Please open an issue at https://github.com/NativeScript/nativescript-cli/issues and send the output of the same command executed with --log trace.`);
29+
return realTemplatePath;
3930
}
4031

4132
/**

test/project-service.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class ProjectIntegrationTest {
3939

4040
public async createProject(projectName: string, template?: string): Promise<void> {
4141
let projectService = this.testInjector.resolve("projectService");
42+
if (!template) {
43+
template = constants.RESERVED_TEMPLATE_NAMES["default"];
44+
}
4245
return projectService.createProject(projectName, template);
4346
}
4447

@@ -67,7 +70,7 @@ class ProjectIntegrationTest {
6770
let tnsCoreModulesRecord = packageJsonContent["dependencies"][constants.TNS_CORE_MODULES_NAME];
6871
assert.isTrue(tnsCoreModulesRecord !== null);
6972

70-
let sourceDir = projectSourceDirectory || options.copyFrom;
73+
let sourceDir = projectSourceDirectory || options.template;
7174

7275
let expectedFiles = fs.enumerateFilesInDirectorySync(sourceDir);
7376
let actualFiles = fs.enumerateFilesInDirectorySync(appDirectoryPath);
@@ -436,7 +439,7 @@ describe("Project Service Tests", () => {
436439
options.path = tempFolder;
437440

438441
await projectIntegrationTest.createProject(projectName);
439-
options.copyFrom = defaultTemplatePath;
442+
options.template = defaultTemplatePath;
440443

441444
await projectIntegrationTest.assertProject(tempFolder, projectName, `org.nativescript.${projectName}`, null);
442445
});

test/project-templates-service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ProjectTemplatesService } from "../lib/services/project-templates-servi
44
import { assert } from "chai";
55
import * as path from "path";
66
import temp = require("temp");
7+
import * as constants from "../lib/constants";
78

89
let isDeleteDirectoryCalledForNodeModulesDir = false;
910
let nativeScriptValidatedTemplatePath = "nsValidatedTemplatePath";
@@ -92,7 +93,7 @@ describe("project-templates-service", () => {
9293
testInjector = createTestInjector({ shouldNpmInstallThrow: false, npmInstallationDirContents: [], npmInstallationDirNodeModulesContents: [] });
9394
projectTemplatesService = testInjector.resolve("projectTemplatesService");
9495
let tempFolder = temp.mkdirSync("preparetemplate");
95-
let actualPathToTemplate = await projectTemplatesService.prepareTemplate(undefined, tempFolder);
96+
let actualPathToTemplate = await projectTemplatesService.prepareTemplate(constants.RESERVED_TEMPLATE_NAMES["default"], tempFolder);
9697
assert.strictEqual(path.basename(actualPathToTemplate), nativeScriptValidatedTemplatePath);
9798
assert.strictEqual(isDeleteDirectoryCalledForNodeModulesDir, true, "When correct path is returned, template's node_modules directory should be deleted.");
9899
});

0 commit comments

Comments
 (0)