Skip to content

Commit 72c7f22

Browse files
chore: Ensure App_Resources are copied correctly from default template
In case the used template does not have App_Resources, we need to copy them from the default template. Ensure the location of the App_Resources in the default template is calculated correctly based on its templateVersion property.
1 parent 1983062 commit 72c7f22

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

lib/definitions/project.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ interface IProjectTemplatesService {
219219
* @return {ITemplateData} Data describing the template - location where it is installed and its NativeScript version.
220220
*/
221221
prepareTemplate(templateName: string, projectDir: string): Promise<ITemplateData>;
222+
223+
/**
224+
* Gives information for the nativescript specific version of the template, for example v1, v2, etc.
225+
* Defaults to v1 in case there's no version specified.
226+
* @param {string} templatePath Full path to the template.
227+
* @returns {string} The version, for example v1 or v2.
228+
*/
229+
getTemplateVersion(templatePath: string): string;
222230
}
223231

224232
interface IPlatformProjectServiceBase {

lib/services/project-service.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ export class ProjectService implements IProjectService {
113113
this.$logger.trace(`Template version is ${templateVersion}`);
114114
let destinationDir = "";
115115
switch (templateVersion) {
116-
case constants.TemplateVersions.v2:
117-
destinationDir = projectDir;
118-
break;
119116
case constants.TemplateVersions.v1:
120-
default:
121117
const appDestinationPath = this.$projectData.getAppDirectoryPath(projectDir);
122118
this.$fs.createDirectory(appDestinationPath);
123119
destinationDir = appDestinationPath;
124120
break;
121+
case constants.TemplateVersions.v2:
122+
default:
123+
destinationDir = projectDir;
124+
break;
125125
}
126126

127127
this.$logger.trace(`Copying application from '${realTemplatePath}' into '${destinationDir}'.`);
@@ -146,20 +146,25 @@ export class ProjectService implements IProjectService {
146146
ignoreScripts: false
147147
});
148148

149-
const obsoleteAppResourcesPath = path.join(projectDir,
150-
constants.NODE_MODULES_FOLDER_NAME,
151-
defaultTemplateName,
152-
constants.APP_RESOURCES_FOLDER_NAME);
153-
154-
const defaultTemplateAppResourcesPath = path.join(projectDir,
155-
constants.NODE_MODULES_FOLDER_NAME,
156-
defaultTemplateName,
157-
constants.APP_FOLDER_NAME,
158-
constants.APP_RESOURCES_FOLDER_NAME);
149+
const defaultTemplatePath = path.join(projectDir, constants.NODE_MODULES_FOLDER_NAME, defaultTemplateName);
150+
const defaultTemplateVersion = this.$projectTemplatesService.getTemplateVersion(defaultTemplatePath);
151+
152+
let defaultTemplateAppResourcesPath: string = null;
153+
switch (defaultTemplateVersion) {
154+
case constants.TemplateVersions.v1:
155+
defaultTemplateAppResourcesPath = path.join(projectDir,
156+
constants.NODE_MODULES_FOLDER_NAME,
157+
defaultTemplateName,
158+
constants.APP_RESOURCES_FOLDER_NAME);
159+
break;
160+
case constants.TemplateVersions.v2:
161+
default:
162+
const defaultTemplateProjectData = this.$projectDataService.getProjectData(defaultTemplatePath);
163+
defaultTemplateAppResourcesPath = defaultTemplateProjectData.appResourcesDirectoryPath;
164+
}
159165

160-
const pathToAppResources = this.$fs.exists(defaultTemplateAppResourcesPath) ? defaultTemplateAppResourcesPath : obsoleteAppResourcesPath;
161-
if (this.$fs.exists(pathToAppResources)) {
162-
shelljs.cp('-R', pathToAppResources, appPath);
166+
if (defaultTemplateAppResourcesPath && this.$fs.exists(defaultTemplateAppResourcesPath)) {
167+
shelljs.cp('-R', defaultTemplateAppResourcesPath, appPath);
163168
}
164169

165170
await this.$npm.uninstall(defaultTemplateName, { save: true }, projectDir);

lib/services/project-templates-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class ProjectTemplatesService implements IProjectTemplatesService {
4545
return { templatePath, templateVersion };
4646
}
4747

48-
private getTemplateVersion(templatePath: string): string {
48+
public getTemplateVersion(templatePath: string): string {
4949
this.$logger.trace(`Checking the NativeScript version of the template located at ${templatePath}.`);
5050
const pathToPackageJson = path.join(templatePath, constants.PACKAGE_JSON_FILE_NAME);
5151
if (this.$fs.exists(pathToPackageJson)) {

0 commit comments

Comments
 (0)