Repository navigation
Expand file tree
/
Copy pathdeep-linking.ts
More file actions
129 lines (108 loc) · 4.33 KB
/
Copy pathdeep-linking.ts
File metadata and controls
129 lines (108 loc) · 4.33 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { extname } from 'path';
import { scanSrcTsFiles } from './build/util';
import { Logger } from './logger/logger';
import * as Constants from './util/constants';
import { BuildError } from './util/errors';
import { getParsedDeepLinkConfig, getStringPropertyValue, readAndCacheFile, setParsedDeepLinkConfig } from './util/helpers';
import { BuildContext, BuildState, ChangedFile, DeepLinkConfigEntry } from './util/interfaces';
import {
convertDeepLinkConfigEntriesToString,
getDeepLinkData,
hasExistingDeepLinkConfig
} from './deep-linking/util';
export let existingDeepLinkConfigString: string = null;
export function setExistingDeepLinkConfig(newString: string) {
existingDeepLinkConfigString = newString;
}
export function deepLinking(context: BuildContext) {
const logger = new Logger(`deeplinks`);
return deepLinkingWorker(context).then((map: Map<string, DeepLinkConfigEntry>) => {
setParsedDeepLinkConfig(map);
logger.finish();
})
.catch((err: Error) => {
const error = new BuildError(err.message);
error.isFatal = true;
throw logger.fail(error);
});
}
function deepLinkingWorker(context: BuildContext) {
return deepLinkingWorkerImpl(context, []);
}
export async function deepLinkingWorkerImpl(context: BuildContext, changedFiles: ChangedFile[]): Promise<Map<string, DeepLinkConfigEntry>> {
// get the app.module.ts content from ideally the cache, but fall back to disk if needed
const appNgModulePath = getStringPropertyValue(Constants.ENV_APP_NG_MODULE_PATH);
const appNgModuleFileContent = await getAppMainNgModuleFile(appNgModulePath);
// is there is an existing (legacy) deep link config, just move on and don't look for decorators
const hasExisting = hasExistingDeepLinkConfig(appNgModulePath, appNgModuleFileContent);
if (hasExisting) {
return new Map<string, DeepLinkConfigEntry>();
}
// okay cool, we need to get the data from each file
const results = getDeepLinkData(appNgModulePath, context.fileCache, context.runAot) || new Map<string, DeepLinkConfigEntry>();
const newDeepLinkString = convertDeepLinkConfigEntriesToString(results);
if (!existingDeepLinkConfigString || newDeepLinkString !== existingDeepLinkConfigString || hasAppModuleChanged(changedFiles, appNgModulePath)) {
existingDeepLinkConfigString = newDeepLinkString;
if (changedFiles) {
changedFiles.push({
event: 'change',
filePath: appNgModulePath,
ext: extname(appNgModulePath).toLowerCase()
});
}
}
return results;
}
export function deepLinkingUpdate(changedFiles: ChangedFile[], context: BuildContext) {
if (context.deepLinkState === BuildState.RequiresBuild) {
return deepLinkingWorkerFullUpdate(context);
} else {
return deepLinkingUpdateImpl(changedFiles, context);
}
}
export function deepLinkingUpdateImpl(changedFiles: ChangedFile[], context: BuildContext) {
const tsFiles = changedFiles.filter(changedFile => changedFile.ext === '.ts');
if (tsFiles.length === 0) {
return Promise.resolve();
}
const logger = new Logger('deeplinks update');
return deepLinkingWorkerImpl(context, changedFiles).then((map: Map<string, DeepLinkConfigEntry>) => {
// okay, now that the existing config is updated, go ahead and reset it
setParsedDeepLinkConfig(map);
logger.finish();
}).catch((err: Error) => {
Logger.warn(err.message);
const error = new BuildError(err.message);
throw logger.fail(error);
});
}
export function deepLinkingWorkerFullUpdate(context: BuildContext) {
const logger = new Logger(`deeplinks update`);
return deepLinkingWorker(context).then((map: Map<string, DeepLinkConfigEntry>) => {
setParsedDeepLinkConfig(map);
logger.finish();
})
.catch((err: Error) => {
Logger.warn(err.message);
const error = new BuildError(err.message);
throw logger.fail(error);
});
}
export async function getAppMainNgModuleFile(appNgModulePath: string) {
try {
return await readAndCacheFile(appNgModulePath);
} catch (ex) {
throw new Error(`The main app NgModule was not found at the following path: ${appNgModulePath}`);
}
}
export function hasAppModuleChanged(changedFiles: ChangedFile[], appNgModulePath: string) {
if (!changedFiles) {
changedFiles = [];
}
for (const changedFile of changedFiles) {
if (changedFile.filePath === appNgModulePath) {
return true;
}
}
return false;
}