-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathapp-files-updater.ts
93 lines (79 loc) · 3.16 KB
/
app-files-updater.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import * as path from "path";
import * as minimatch from "minimatch";
import * as constants from "../constants";
import * as fs from "fs";
import Future = require("fibers/future");
export class AppFilesUpdater {
constructor(
private appSourceDirectoryPath: string,
private appDestinationDirectoryPath: string,
public options: IOptions,
public fs: IFileSystem
) {
}
public updateApp(beforeCopyAction: (sourceFiles: string[]) => void): void {
this.cleanDestinationApp();
const sourceFiles = this.resolveAppSourceFiles();
beforeCopyAction(sourceFiles);
this.copyAppSourceFiles(sourceFiles);
}
public cleanDestinationApp(): void {
if (this.options.bundle) {
//Assuming an the bundle has updated the dest folder already.
//Skip cleaning up completely.
return;
}
// Delete the destination app in order to prevent EEXIST errors when symlinks are used.
let destinationAppContents = this.readDestinationDir();
destinationAppContents = destinationAppContents.filter(
(directoryName: string) => directoryName !== constants.TNS_MODULES_FOLDER_NAME);
_(destinationAppContents).each((directoryItem: string) => {
this.deleteDestinationItem(directoryItem);
});
}
protected readDestinationDir(): string[] {
if (this.fs.exists(this.appDestinationDirectoryPath).wait()) {
return this.fs.readDirectory(this.appDestinationDirectoryPath).wait();
} else {
return [];
}
}
protected deleteDestinationItem(directoryItem: string): void {
this.fs.deleteDirectory(path.join(this.appDestinationDirectoryPath, directoryItem)).wait();
}
protected readSourceDir(): string[] {
let tnsDir = path.join(this.appSourceDirectoryPath, constants.TNS_MODULES_FOLDER_NAME);
return this.fs.enumerateFilesInDirectorySync(this.appSourceDirectoryPath, null, { includeEmptyDirectories: true }).filter(dirName => dirName !== tnsDir);
}
protected resolveAppSourceFiles(): string[] {
// Copy all files from app dir, but make sure to exclude tns_modules
let sourceFiles = this.readSourceDir();
if (this.options.release) {
let testsFolderPath = path.join(this.appSourceDirectoryPath, 'tests');
sourceFiles = sourceFiles.filter(source => source.indexOf(testsFolderPath) === -1);
}
// Remove .ts and .js.map files in release
if (this.options.release) {
constants.LIVESYNC_EXCLUDED_FILE_PATTERNS.forEach(pattern => sourceFiles = sourceFiles.filter(file => !minimatch(file, pattern, { nocase: true })));
}
if (this.options.bundle) {
sourceFiles = sourceFiles.filter(file => minimatch(file, "**/App_Resources/**", {nocase: true}));
}
return sourceFiles;
}
protected copyAppSourceFiles(sourceFiles: string[]): void {
let copyFileFutures = sourceFiles.map(source => {
let destinationPath = path.join(this.appDestinationDirectoryPath, path.relative(this.appSourceDirectoryPath, source));
let exists = fs.lstatSync(source);
if (exists.isSymbolicLink()) {
source = fs.realpathSync(source);
exists = fs.lstatSync(source);
}
if (exists.isDirectory()) {
return this.fs.createDirectory(destinationPath);
}
return this.fs.copyFile(source, destinationPath);
});
Future.wait(copyFileFutures);
}
}