Skip to content

Commit b9890bf

Browse files
committed
refactor: address pr comments, replace app_resources usage for whatever
the name of the app_resources directory basename is
1 parent b557596 commit b9890bf

7 files changed

+46
-35
lines changed

lib/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const OUTPUTS_DIR = "outputs";
2929
export const APK_DIR = "apk";
3030
export const RESOURCES_DIR = "res";
3131
export const CONFIG_NS_FILE_NAME = "nsconfig.json";
32-
export const CONFIG_NS_APP_RESOURCES_ENTRY = "app_resources";
32+
export const CONFIG_NS_APP_RESOURCES_ENTRY = "appResourcesPath";
3333

3434
export class PackageVersion {
3535
static NEXT = "next";

lib/project-data.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export class ProjectData implements IProjectData {
9393
projectDir = this.projectDir;
9494
}
9595

96+
if (!projectDir) {
97+
return null;
98+
}
99+
96100
const configNSFilePath = path.join(projectDir, constants.CONFIG_NS_FILE_NAME);
97101
let absoluteAppResourcesDirPath: string;
98102

@@ -103,10 +107,13 @@ export class ProjectData implements IProjectData {
103107
const appResourcesDirPath = configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY];
104108

105109
absoluteAppResourcesDirPath = path.resolve(projectDir, appResourcesDirPath);
110+
111+
return absoluteAppResourcesDirPath;
106112
}
107113
}
108114

109-
return absoluteAppResourcesDirPath || path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
115+
// if no nsconfig is present default to app/App_Resources
116+
return path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
110117
}
111118

112119
private getProjectType(): string {

lib/services/app-files-updater.ts

+17-11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export class AppFilesUpdater {
1111
) {
1212
}
1313

14-
public updateApp(beforeCopyAction: (sourceFiles: string[]) => void, filesToSync?: string[]): void {
15-
const sourceFiles = filesToSync || this.resolveAppSourceFiles();
14+
public updateApp(beforeCopyAction: (sourceFiles: string[]) => void, projectData: IProjectData, filesToSync?: string[]): void {
15+
const sourceFiles = filesToSync || this.resolveAppSourceFiles(projectData);
1616

1717
beforeCopyAction(sourceFiles);
1818
this.copyAppSourceFiles(sourceFiles);
@@ -41,15 +41,19 @@ export class AppFilesUpdater {
4141
this.fs.deleteDirectory(path.join(this.appDestinationDirectoryPath, directoryItem));
4242
}
4343

44-
protected readSourceDir(): string[] {
44+
protected readSourceDir(projectData: IProjectData): string[] {
4545
const tnsDir = path.join(this.appSourceDirectoryPath, constants.TNS_MODULES_FOLDER_NAME);
46-
const defaultAppResourcesDir = path.join(this.appSourceDirectoryPath, constants.APP_RESOURCES_FOLDER_NAME);
47-
return this.fs.enumerateFilesInDirectorySync(this.appSourceDirectoryPath, null, { includeEmptyDirectories: true }).filter(dirName => dirName !== tnsDir).filter(dirName => !dirName.startsWith(defaultAppResourcesDir));
46+
47+
return this.fs.enumerateFilesInDirectorySync(this.appSourceDirectoryPath, null, { includeEmptyDirectories: true }).filter(dirName => dirName !== tnsDir);
4848
}
4949

50-
protected resolveAppSourceFiles(): string[] {
51-
// Copy all files from app dir, but make sure to exclude tns_modules and App_Resources
52-
let sourceFiles = this.readSourceDir();
50+
protected resolveAppSourceFiles(projectData: IProjectData): string[] {
51+
if (this.options.bundle) {
52+
return [];
53+
}
54+
55+
// Copy all files from app dir, but make sure to exclude tns_modules and application resources
56+
let sourceFiles = this.readSourceDir(projectData);
5357

5458
if (this.options.release) {
5559
const testsFolderPath = path.join(this.appSourceDirectoryPath, 'tests');
@@ -61,9 +65,11 @@ export class AppFilesUpdater {
6165
constants.LIVESYNC_EXCLUDED_FILE_PATTERNS.forEach(pattern => sourceFiles = sourceFiles.filter(file => !minimatch(file, pattern, { nocase: true })));
6266
}
6367

64-
if (this.options.bundle) {
65-
sourceFiles = sourceFiles.filter(file => minimatch(file, "**/App_Resources/**", { nocase: true }));
66-
}
68+
// exclude the app_resources directory from being enumerated
69+
// for copying if it is present in the application sources dir
70+
const appResourcesPath = projectData.appResourcesDirectoryPath;
71+
sourceFiles = sourceFiles.filter(dirName => !path.normalize(dirName).startsWith(path.normalize(appResourcesPath)));
72+
6773
return sourceFiles;
6874
}
6975

lib/services/prepare-platform-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ export class PreparePlatformService {
2525
const appUpdater = new AppFilesUpdater(appSourceDirectoryPath, appDestinationDirectoryPath, copyAppFilesData.appFilesUpdaterOptions, this.$fs);
2626
appUpdater.updateApp(sourceFiles => {
2727
this.$xmlValidator.validateXmlFiles(sourceFiles);
28-
}, copyAppFilesData.filesToSync);
28+
}, copyAppFilesData.projectData, copyAppFilesData.filesToSync);
2929
}
3030
}

test/app-files-updates.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { assert } from "chai";
22
import { AppFilesUpdater } from "../lib/services/app-files-updater";
3+
import * as yok from "../lib/common/yok";
34

45
require("should");
56

7+
function createTestInjector(): IInjector {
8+
const testInjector = new yok.Yok();
9+
10+
testInjector.register("projectData", { appResourcesDirectoryPath: "App_Resources"});
11+
12+
return testInjector;
13+
}
14+
615
describe("App files cleanup", () => {
716
class CleanUpAppFilesUpdater extends AppFilesUpdater {
817
public deletedDestinationItems: string[] = [];
@@ -54,23 +63,25 @@ describe("App files copy", () => {
5463
}
5564

5665
public copy(): void {
57-
this.copiedDestinationItems = this.resolveAppSourceFiles();
66+
const injector = createTestInjector();
67+
const projectData = <IProjectData>injector.resolve("projectData");
68+
this.copiedDestinationItems = this.resolveAppSourceFiles(projectData);
5869
}
5970
}
6071

61-
it("copies all app files when not bundling", () => {
72+
it("copies all app files but app_resources when not bundling", () => {
6273
const updater = new CopyAppFilesUpdater([
6374
"file1", "dir1/file2", "App_Resources/Android/blah.png"
6475
], { bundle: false });
6576
updater.copy();
66-
assert.deepEqual(["file1", "dir1/file2", "App_Resources/Android/blah.png"], updater.copiedDestinationItems);
77+
assert.deepEqual(["file1", "dir1/file2"], updater.copiedDestinationItems);
6778
});
6879

69-
it("skips copying non-App_Resource files when bundling", () => {
80+
it("skips copying files when bundling", () => {
7081
const updater = new CopyAppFilesUpdater([
7182
"file1", "dir1/file2", "App_Resources/Android/blah.png"
7283
], { bundle: true });
7384
updater.copy();
74-
assert.deepEqual(["App_Resources/Android/blah.png"], updater.copiedDestinationItems);
85+
assert.deepEqual([], updater.copiedDestinationItems);
7586
});
7687
});

test/ios-entitlements-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe("IOSEntitlements Service Tests", () => {
4747
injector = createTestInjector();
4848

4949
platformsData = injector.resolve("platformsData");
50-
projectData = injector.resolve("projectData");
50+
projectData = $injector.resolve<IProjectData>("projectData");
5151
projectData.projectName = 'testApp';
5252

5353
projectData.platformsDir = temp.mkdirSync("platformsDir");

test/stubs.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as util from "util";
44
import * as chai from "chai";
55
import { EventEmitter } from "events";
66

7-
import * as fs from "fs";
87
import * as path from "path";
98
import * as constants from "./../lib/constants";
109

@@ -267,20 +266,8 @@ export class ProjectDataStub implements IProjectData {
267266
projectDir = this.projectDir;
268267
}
269268

270-
const configNSFilePath = path.join(projectDir, constants.CONFIG_NS_FILE_NAME);
271-
let absoluteAppResourcesDirPath: string;
272-
273-
if (fs.existsSync(configNSFilePath)) {
274-
const configNS = JSON.parse(fs.readFileSync(configNSFilePath).toString());
275-
276-
if (configNS && configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY]) {
277-
const appResourcesDirPath = configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY];
278-
279-
absoluteAppResourcesDirPath = path.resolve(projectDir, appResourcesDirPath);
280-
}
281-
}
282-
283-
return absoluteAppResourcesDirPath || path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
269+
// always return app/App_Resources
270+
return path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
284271
}
285272
}
286273

0 commit comments

Comments
 (0)