Skip to content

Commit 159c1e7

Browse files
committed
feat(nsconfig): read app folder path from nsconfig
chore(tests): fix tests after making appDirectoryPath property with getter refactor(project-data): add getNsConfig private method
1 parent e6770d1 commit 159c1e7

11 files changed

+59
-25
lines changed

lib/commands/test-init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class TestInitCommand implements ICommand {
6868

6969
await this.$pluginsService.add('nativescript-unit-test-runner', this.$projectData);
7070

71-
const testsDir = path.join(projectDir, 'app/tests');
71+
const testsDir = path.join(this.$projectData.appDirectoryPath, 'tests');
7272
let shouldCreateSampleTests = true;
7373
if (this.$fs.exists(testsDir)) {
7474
this.$logger.info('app/tests/ directory already exists, will not create an example test project.');

lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const APK_DIR = "apk";
3030
export const RESOURCES_DIR = "res";
3131
export const CONFIG_NS_FILE_NAME = "nsconfig.json";
3232
export const CONFIG_NS_APP_RESOURCES_ENTRY = "app_resources";
33+
export const CONFIG_NS_APP_ENTRY = "app_folder";
3334

3435
export class PackageVersion {
3536
static NEXT = "next";

lib/definitions/project.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ interface IProjectData extends IProjectDir {
6969
* @returns {void}
7070
*/
7171
initializeProjectData(projectDir?: string): void;
72+
getAppDirectoryPath(projectDir?: string): string;
7273
getAppResourcesDirectoryPath(projectDir?: string): string;
7374
}
7475

lib/project-data.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ export class ProjectData implements IProjectData {
3232
public projectFilePath: string;
3333
public projectId: string;
3434
public projectName: string;
35-
public appDirectoryPath: string;
35+
get appDirectoryPath(): string {
36+
return this.getAppDirectoryPath();
37+
}
3638
get appResourcesDirectoryPath(): string {
3739
return this.getAppResourcesDirectoryPath();
3840
}
@@ -70,7 +72,6 @@ export class ProjectData implements IProjectData {
7072
this.projectName = this.$projectHelper.sanitizeName(path.basename(projectDir));
7173
this.platformsDir = path.join(projectDir, constants.PLATFORMS_DIR_NAME);
7274
this.projectFilePath = projectFilePath;
73-
this.appDirectoryPath = path.join(projectDir, constants.APP_FOLDER_NAME);
7475
this.projectId = data.id;
7576
this.dependencies = fileContent.dependencies;
7677
this.devDependencies = fileContent.devDependencies;
@@ -93,20 +94,43 @@ export class ProjectData implements IProjectData {
9394
projectDir = this.projectDir;
9495
}
9596

96-
const configNSFilePath = path.join(projectDir, constants.CONFIG_NS_FILE_NAME);
97+
const configNS = this.getNsConfig(projectDir);
9798
let absoluteAppResourcesDirPath: string;
9899

99-
if (this.$fs.exists(configNSFilePath)) {
100-
const configNS = this.$fs.readJson(configNSFilePath);
100+
if (configNS && configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY]) {
101+
const appResourcesDirPath = configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY];
101102

102-
if (configNS && configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY]) {
103-
const appResourcesDirPath = configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY];
103+
absoluteAppResourcesDirPath = path.resolve(projectDir, appResourcesDirPath);
104+
}
104105

105-
absoluteAppResourcesDirPath = path.resolve(projectDir, appResourcesDirPath);
106-
}
106+
return absoluteAppResourcesDirPath || path.join(this.getAppDirectoryPath(projectDir), constants.APP_RESOURCES_FOLDER_NAME);
107+
}
108+
109+
public getAppDirectoryPath(projectDir?: string): string {
110+
if (!projectDir) {
111+
projectDir = this.projectDir;
112+
}
113+
114+
const configNS = this.getNsConfig(projectDir);
115+
let absoluteAppDirPath: string;
116+
117+
if (configNS && configNS[constants.CONFIG_NS_APP_ENTRY]) {
118+
const appDirPath = configNS[constants.CONFIG_NS_APP_ENTRY];
119+
120+
absoluteAppDirPath = path.resolve(projectDir, appDirPath);
121+
}
122+
123+
return absoluteAppDirPath || path.join(projectDir, constants.APP_FOLDER_NAME);
124+
}
125+
126+
private getNsConfig(projectDir: string): Object {
127+
const configNSFilePath = path.join(projectDir, constants.CONFIG_NS_FILE_NAME);
128+
129+
if (!this.$fs.exists(configNSFilePath)) {
130+
return null;
107131
}
108132

109-
return absoluteAppResourcesDirPath || path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
133+
return this.$fs.readJson(configNSFilePath);
110134
}
111135

112136
private getProjectType(): string {

lib/providers/project-files-provider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ export class ProjectFilesProvider extends ProjectFilesProviderBase {
1616
const platformData = this.$platformsData.getPlatformData(platform.toLowerCase(), projectData);
1717
const parsedFilePath = this.getPreparedFilePath(filePath, projectFilesConfig);
1818
let mappedFilePath = "";
19+
let relativePath;
1920
if (parsedFilePath.indexOf(constants.NODE_MODULES_FOLDER_NAME) > -1) {
20-
const relativePath = path.relative(path.join(projectData.projectDir, constants.NODE_MODULES_FOLDER_NAME), parsedFilePath);
21+
relativePath = path.relative(path.join(projectData.projectDir, constants.NODE_MODULES_FOLDER_NAME), parsedFilePath);
2122
mappedFilePath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, constants.TNS_MODULES_FOLDER_NAME, relativePath);
2223
} else {
23-
mappedFilePath = path.join(platformData.appDestinationDirectoryPath, path.relative(projectData.projectDir, parsedFilePath));
24+
relativePath = path.relative(projectData.appDirectoryPath, parsedFilePath);
25+
mappedFilePath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, relativePath);
2426
}
2527

2628
const appResourcesDirectoryPath = projectData.appResourcesDirectoryPath;

lib/services/livesync/livesync-service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as choki from "chokidar";
33
import { EOL } from "os";
44
import { EventEmitter } from "events";
55
import { hook } from "../../common/helpers";
6-
import { APP_FOLDER_NAME, PACKAGE_JSON_FILE_NAME, LiveSyncTrackActionNames, USER_INTERACTION_NEEDED_EVENT_NAME, DEBUGGER_ATTACHED_EVENT_NAME, DEBUGGER_DETACHED_EVENT_NAME, TrackActionNames } from "../../constants";
6+
import { PACKAGE_JSON_FILE_NAME, LiveSyncTrackActionNames, USER_INTERACTION_NEEDED_EVENT_NAME, DEBUGGER_ATTACHED_EVENT_NAME, DEBUGGER_DETACHED_EVENT_NAME, TrackActionNames } from "../../constants";
77
import { FileExtensions, DeviceTypes, DeviceDiscoveryEventNames } from "../../common/constants";
88
import { cache } from "../../common/decorators";
99

@@ -515,7 +515,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
515515
}
516516

517517
private async startWatcher(projectData: IProjectData, liveSyncData: ILiveSyncInfo): Promise<void> {
518-
const patterns = [APP_FOLDER_NAME];
518+
const patterns = [path.relative(projectData.projectDir, projectData.appDirectoryPath)];
519519

520520
if (liveSyncData.watchAllFiles) {
521521
const productionDependencies = this.$nodeModulesDependenciesBuilder.getProductionDependencies(projectData.projectDir);
@@ -527,7 +527,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
527527
}
528528
}
529529

530-
pattern.push(projectData.appResourcesDirectoryPath);
530+
patterns.push(projectData.appResourcesDirectoryPath);
531531

532532
const currentWatcherInfo = this.liveSyncProcessesInfo[liveSyncData.projectDir].watcherInfo;
533533
const areWatcherPatternsDifferent = () => _.xor(currentWatcherInfo.patterns, patterns).length;

lib/services/platform-service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,9 @@ export class PlatformService extends EventEmitter implements IPlatformService {
571571
public async cleanDestinationApp(platformInfo: IPreparePlatformInfo): Promise<void> {
572572
await this.ensurePlatformInstalled(platformInfo.platform, platformInfo.platformTemplate, platformInfo.projectData, platformInfo.config);
573573

574-
const appSourceDirectoryPath = path.join(platformInfo.projectData.projectDir, constants.APP_FOLDER_NAME);
575574
const platformData = this.$platformsData.getPlatformData(platformInfo.platform, platformInfo.projectData);
576575
const appDestinationDirectoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
577-
const appUpdater = new AppFilesUpdater(appSourceDirectoryPath, appDestinationDirectoryPath, platformInfo.appFilesUpdaterOptions, this.$fs);
576+
const appUpdater = new AppFilesUpdater(platformInfo.projectData.appDirectoryPath, appDestinationDirectoryPath, platformInfo.appFilesUpdaterOptions, this.$fs);
578577
appUpdater.cleanDestinationApp();
579578
}
580579

lib/services/prepare-platform-service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ export class PreparePlatformService {
2020

2121
// Copy app folder to native project
2222
this.$fs.ensureDirectoryExists(appDestinationDirectoryPath);
23-
const appSourceDirectoryPath = path.join(copyAppFilesData.projectData.projectDir, constants.APP_FOLDER_NAME);
2423

25-
const appUpdater = new AppFilesUpdater(appSourceDirectoryPath, appDestinationDirectoryPath, copyAppFilesData.appFilesUpdaterOptions, this.$fs);
24+
const appUpdater = new AppFilesUpdater(copyAppFilesData.projectData.appDirectoryPath, appDestinationDirectoryPath, copyAppFilesData.appFilesUpdaterOptions, this.$fs);
2625
appUpdater.updateApp(sourceFiles => {
2726
this.$xmlValidator.validateXmlFiles(sourceFiles);
2827
}, copyAppFilesData.filesToSync);

lib/services/project-service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class ProjectService implements IProjectService {
101101
private async extractTemplate(projectDir: string, realTemplatePath: string): Promise<void> {
102102
this.$fs.ensureDirectoryExists(projectDir);
103103

104-
const appDestinationPath = path.join(projectDir, constants.APP_FOLDER_NAME);
104+
const appDestinationPath = this.$projectData.getAppDirectoryPath(projectDir);
105105
this.$fs.createDirectory(appDestinationPath);
106106

107107
this.$logger.trace(`Copying application from '${realTemplatePath}' into '${appDestinationPath}'.`);
@@ -111,7 +111,7 @@ export class ProjectService implements IProjectService {
111111
}
112112

113113
private async ensureAppResourcesExist(projectDir: string): Promise<void> {
114-
const appPath = path.join(projectDir, constants.APP_FOLDER_NAME),
114+
const appPath = this.$projectData.getAppDirectoryPath(projectDir),
115115
appResourcesDestinationPath = this.$projectData.getAppResourcesDirectoryPath(projectDir);
116116

117117
if (!this.$fs.exists(appResourcesDestinationPath)) {
@@ -138,7 +138,7 @@ export class ProjectService implements IProjectService {
138138
}
139139

140140
private removeMergedDependencies(projectDir: string, templatePackageJsonData: any): void {
141-
const extractedTemplatePackageJsonPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.PACKAGE_JSON_FILE_NAME);
141+
const extractedTemplatePackageJsonPath = path.join(this.$projectData.getAppDirectoryPath(projectDir), constants.PACKAGE_JSON_FILE_NAME);
142142
for (const key in templatePackageJsonData) {
143143
if (constants.PackageJsonKeysToKeep.indexOf(key) === -1) {
144144
delete templatePackageJsonData[key];

test/platform-service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ describe('Platform Service Tests', () => {
454454

455455
const projectData = testInjector.resolve("projectData");
456456
projectData.projectDir = testDirData.tempFolder;
457-
projectData.appDirectoryPath = testDirData.appFolderPath;
458457
projectData.projectName = "app";
459458

460459
platformService = testInjector.resolve("platformService");

test/stubs.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ export class ProjectDataStub implements IProjectData {
253253
projectFilePath: string;
254254
projectId: string;
255255
dependencies: any;
256-
appDirectoryPath: string;
256+
get appDirectoryPath(): string {
257+
return this.getAppDirectoryPath();
258+
}
257259
devDependencies: IStringDictionary;
258260
projectType: string;
259261
get appResourcesDirectoryPath(): string {
@@ -282,6 +284,13 @@ export class ProjectDataStub implements IProjectData {
282284

283285
return absoluteAppResourcesDirPath || path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
284286
}
287+
public getAppDirectoryPath(projectDir?: string): string {
288+
if (!projectDir) {
289+
projectDir = this.projectDir;
290+
}
291+
292+
return path.join(projectDir, "app") || "";
293+
}
285294
}
286295

287296
export class PlatformProjectServiceStub extends EventEmitter implements IPlatformProjectService {

0 commit comments

Comments
 (0)