Skip to content

Commit 1122be6

Browse files
petekanevKristianDD
authored andcommitted
refactor: address pr comments, replace app_resources usage for whatever
the name of the app_resources directory basename is
1 parent 1aba3c1 commit 1122be6

File tree

7 files changed

+46
-35
lines changed

7 files changed

+46
-35
lines changed

lib/constants.ts

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 1 deletion
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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export class AppFilesUpdater {
1111
) {
1212
}
1313

14-
public updateApp(updateAppOptions: IUpdateAppOptions): void {
14+
public updateApp(updateAppOptions: IUpdateAppOptions, projectData: IProjectData): void {
1515
this.cleanDestinationApp(updateAppOptions);
16-
const sourceFiles = updateAppOptions.filesToSync || this.resolveAppSourceFiles();
16+
const sourceFiles = updateAppOptions.filesToSync || this.resolveAppSourceFiles(projectData);
1717

1818
updateAppOptions.beforeCopyAction(sourceFiles);
1919
this.copyAppSourceFiles(sourceFiles);
@@ -52,15 +52,19 @@ export class AppFilesUpdater {
5252
this.fs.deleteDirectory(path.join(this.appDestinationDirectoryPath, directoryItem));
5353
}
5454

55-
protected readSourceDir(): string[] {
55+
protected readSourceDir(projectData: IProjectData): string[] {
5656
const tnsDir = path.join(this.appSourceDirectoryPath, constants.TNS_MODULES_FOLDER_NAME);
57-
const defaultAppResourcesDir = path.join(this.appSourceDirectoryPath, constants.APP_RESOURCES_FOLDER_NAME);
58-
return this.fs.enumerateFilesInDirectorySync(this.appSourceDirectoryPath, null, { includeEmptyDirectories: true }).filter(dirName => dirName !== tnsDir).filter(dirName => !dirName.startsWith(defaultAppResourcesDir));
57+
58+
return this.fs.enumerateFilesInDirectorySync(this.appSourceDirectoryPath, null, { includeEmptyDirectories: true }).filter(dirName => dirName !== tnsDir);
5959
}
6060

61-
protected resolveAppSourceFiles(): string[] {
62-
// Copy all files from app dir, but make sure to exclude tns_modules and App_Resources
63-
let sourceFiles = this.readSourceDir();
61+
protected resolveAppSourceFiles(projectData: IProjectData): string[] {
62+
if (this.options.bundle) {
63+
return [];
64+
}
65+
66+
// Copy all files from app dir, but make sure to exclude tns_modules and application resources
67+
let sourceFiles = this.readSourceDir(projectData);
6468

6569
if (this.options.release) {
6670
const testsFolderPath = path.join(this.appSourceDirectoryPath, 'tests');
@@ -72,9 +76,11 @@ export class AppFilesUpdater {
7276
constants.LIVESYNC_EXCLUDED_FILE_PATTERNS.forEach(pattern => sourceFiles = sourceFiles.filter(file => !minimatch(file, pattern, { nocase: true })));
7377
}
7478

75-
if (this.options.bundle) {
76-
sourceFiles = sourceFiles.filter(file => minimatch(file, "**/App_Resources/**", { nocase: true }));
77-
}
79+
// exclude the app_resources directory from being enumerated
80+
// for copying if it is present in the application sources dir
81+
const appResourcesPath = projectData.appResourcesDirectoryPath;
82+
sourceFiles = sourceFiles.filter(dirName => !path.normalize(dirName).startsWith(path.normalize(appResourcesPath)));
83+
7884
return sourceFiles;
7985
}
8086

lib/services/prepare-platform-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ export class PreparePlatformService {
3030
filesToSync: copyAppFilesData.filesToSync,
3131
filesToRemove: copyAppFilesData.filesToRemove
3232
};
33-
appUpdater.updateApp(appUpdaterOptions);
33+
appUpdater.updateApp(appUpdaterOptions, copyAppFilesData.projectData);
3434
}
3535
}

test/app-files-updates.ts

Lines changed: 16 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 15 deletions
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)