UNPKG

2.62 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var fs_1 = require("fs");
4var path_1 = require("path");
5var util_1 = require("../deep-linking/util");
6var config_1 = require("../util/config");
7var Constants = require("../util/constants");
8var glob_util_1 = require("../util/glob-util");
9var helpers_1 = require("../util/helpers");
10var typescript_utils_1 = require("../util/typescript-utils");
11function getTsFilePaths(context) {
12 var tsFileGlobString = path_1.join(context.srcDir, '**', '*.ts');
13 return glob_util_1.globAll([tsFileGlobString]).then(function (results) {
14 return results.map(function (result) { return result.absolutePath; });
15 });
16}
17exports.getTsFilePaths = getTsFilePaths;
18function readTsFiles(context, tsFilePaths) {
19 var promises = tsFilePaths.map(function (tsFilePath) {
20 var promise = helpers_1.readFileAsync(tsFilePath);
21 promise.then(function (fileContent) {
22 context.fileCache.set(tsFilePath, { path: tsFilePath, content: fileContent });
23 });
24 return promise;
25 });
26 return Promise.all(promises);
27}
28exports.readTsFiles = readTsFiles;
29function generateAndWriteNgModules(fileCache) {
30 fileCache.getAll().forEach(function (file) {
31 var sourceFile = typescript_utils_1.getTypescriptSourceFile(file.path, file.content);
32 var deepLinkDecoratorData = util_1.getDeepLinkDecoratorContentForSourceFile(sourceFile);
33 if (deepLinkDecoratorData) {
34 // we have a valid DeepLink decorator
35 var correspondingNgModulePath = util_1.getNgModulePathFromCorrespondingPage(file.path);
36 var ngModuleFile = fileCache.get(correspondingNgModulePath);
37 if (!ngModuleFile) {
38 // the ngModule file does not exist, so go ahead and create a default one
39 var defaultNgModuleContent = util_1.generateDefaultDeepLinkNgModuleContent(file.path, deepLinkDecoratorData.className);
40 var ngModuleFilePath = helpers_1.changeExtension(file.path, helpers_1.getStringPropertyValue(Constants.ENV_NG_MODULE_FILE_NAME_SUFFIX));
41 fs_1.writeFileSync(ngModuleFilePath, defaultNgModuleContent);
42 }
43 }
44 });
45}
46exports.generateAndWriteNgModules = generateAndWriteNgModules;
47function run() {
48 var context = config_1.generateContext();
49 // find out what files to read
50 return getTsFilePaths(context).then(function (filePaths) {
51 // read the files
52 return readTsFiles(context, filePaths);
53 }).then(function () {
54 generateAndWriteNgModules(context.fileCache);
55 });
56}
57run();