Skip to content

Commit 0d3e921

Browse files
authored
add App_Resources from default template, if not found in the specified template #2513 (#2609)
1 parent 4cb7ebf commit 0d3e921

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lib/services/project-service.ts

+23
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export class ProjectService implements IProjectService {
4848
let templatePath = await this.$projectTemplatesService.prepareTemplate(selectedTemplate, projectDir);
4949
await this.extractTemplate(projectDir, templatePath);
5050

51+
await this.ensureAppResourcesExist(projectDir);
52+
5153
let packageName = constants.TNS_CORE_MODULES_NAME;
5254
await this.$npm.install(packageName, projectDir, { save: true, "save-exact": true });
5355

@@ -102,6 +104,27 @@ export class ProjectService implements IProjectService {
102104
this.$fs.createDirectory(path.join(projectDir, "platforms"));
103105
}
104106

107+
private async ensureAppResourcesExist(projectDir: string): Promise<void> {
108+
let appPath = path.join(projectDir, constants.APP_FOLDER_NAME),
109+
appResourcesDestinationPath = path.join(appPath, constants.APP_RESOURCES_FOLDER_NAME);
110+
111+
if (!this.$fs.exists(appResourcesDestinationPath)) {
112+
this.$fs.createDirectory(appResourcesDestinationPath);
113+
114+
// the template installed doesn't have App_Resources -> get from a default template
115+
let defaultTemplateName = constants.RESERVED_TEMPLATE_NAMES["default"];
116+
await this.$npm.install(defaultTemplateName, projectDir, { save: true, });
117+
let defaultTemplateAppResourcesPath = path.join(projectDir, constants.NODE_MODULES_FOLDER_NAME,
118+
defaultTemplateName, constants.APP_RESOURCES_FOLDER_NAME);
119+
120+
if (this.$fs.exists(defaultTemplateAppResourcesPath)) {
121+
shelljs.cp('-R', defaultTemplateAppResourcesPath, appPath);
122+
}
123+
124+
await this.$npm.uninstall(defaultTemplateName, { save: true }, projectDir);
125+
}
126+
}
127+
105128
private removeMergedDependencies(projectDir: string, templatePackageJsonData: any): void {
106129
let extractedTemplatePackageJsonPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.PACKAGE_JSON_FILE_NAME);
107130
for (let key in templatePackageJsonData) {

test/project-service.ts

+29
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ describe("Project Service Tests", () => {
138138
describe("project service integration tests", () => {
139139
let defaultTemplatePath: string;
140140
let defaultSpecificVersionTemplatePath: string;
141+
let noAppResourcesTemplatePath: string;
141142
let angularTemplatePath: string;
142143
let typescriptTemplatePath: string;
143144

145+
const noAppResourcesTemplateName = "tns-template-hello-world-ts";
146+
144147
before(async () => {
145148
let projectIntegrationTest = new ProjectIntegrationTest();
146149
let fs: IFileSystem = projectIntegrationTest.testInjector.resolve("fs");
@@ -205,6 +208,23 @@ describe("Project Service Tests", () => {
205208
typescriptTemplatePath = path.join(typescriptTemplateDir, "node_modules", constants.RESERVED_TEMPLATE_NAMES["typescript"]);
206209

207210
fs.deleteDirectory(path.join(typescriptTemplatePath, "node_modules"));
211+
let noAppResourcesTemplateDir = temp.mkdirSync("noAppResources");
212+
fs.writeJson(path.join(noAppResourcesTemplateDir, "package.json"), {
213+
"name": "blankTemplate",
214+
"version": "1.0.0",
215+
"description": "dummy",
216+
"license": "MIT",
217+
"readme": "dummy",
218+
"repository": "dummy"
219+
});
220+
221+
await npmInstallationManager.install(noAppResourcesTemplateName, noAppResourcesTemplateDir, {
222+
dependencyType: "save",
223+
version: "2.0.0"
224+
});
225+
noAppResourcesTemplatePath = path.join(noAppResourcesTemplateDir, "node_modules", noAppResourcesTemplateName);
226+
227+
fs.deleteDirectory(path.join(noAppResourcesTemplatePath, "node_modules"));
208228
});
209229

210230
it("creates valid project from default template", async () => {
@@ -234,6 +254,15 @@ describe("Project Service Tests", () => {
234254
await projectIntegrationTest.assertProject(tempFolder, projectName, "org.nativescript.myapp", defaultSpecificVersionTemplatePath);
235255
});
236256

257+
it("creates valid project from a template without App_Resources", async () => {
258+
let projectIntegrationTest = new ProjectIntegrationTest();
259+
let tempFolder = temp.mkdirSync("project");
260+
let projectName = "myapp";
261+
262+
await projectIntegrationTest.createProject({ projectName: projectName, template: noAppResourcesTemplateName + "@2.0.0", pathToProject: tempFolder });
263+
await projectIntegrationTest.assertProject(tempFolder, projectName, "org.nativescript.myapp", noAppResourcesTemplatePath);
264+
});
265+
237266
it("creates valid project from typescript template", async () => {
238267
let projectIntegrationTest = new ProjectIntegrationTest();
239268
let tempFolder = temp.mkdirSync("projectTypescript");

0 commit comments

Comments
 (0)