1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.maybeJs = exports.updateTsConfigsToJs = exports.toJS = void 0;
|
4 | const schematics_1 = require("@angular-devkit/schematics");
|
5 | const core_1 = require("@angular-devkit/core");
|
6 | const ast_utils_1 = require("../ast-utils");
|
7 | const typescript_1 = require("../../utilities/typescript");
|
8 | let tsModule;
|
9 | function toJS() {
|
10 | if (!tsModule) {
|
11 | tsModule = (0, typescript_1.ensureTypescript)();
|
12 | }
|
13 | const { transpile, JsxEmit, ScriptTarget } = tsModule;
|
14 | return (0, schematics_1.chain)([
|
15 | (0, schematics_1.forEach)((0, schematics_1.when)((path) => path.endsWith('.ts') || path.endsWith('.tsx'), (entry) => {
|
16 | const original = entry.content.toString('utf-8');
|
17 | const result = transpile(original, {
|
18 | allowJs: true,
|
19 | jsx: JsxEmit.Preserve,
|
20 | target: ScriptTarget.ESNext,
|
21 | });
|
22 | return {
|
23 | content: Buffer.from(result, 'utf-8'),
|
24 | path: (0, core_1.normalize)(entry.path.replace(/\.tsx?$/, '.js')),
|
25 | };
|
26 | })),
|
27 | ]);
|
28 | }
|
29 | exports.toJS = toJS;
|
30 | function updateTsConfigsToJs(options) {
|
31 | const paths = {
|
32 | tsConfig: (0, core_1.normalize)(`${options.projectRoot}/tsconfig.json`),
|
33 | tsConfigLib: (0, core_1.normalize)(`${options.projectRoot}/tsconfig.lib.json`),
|
34 | tsConfigApp: (0, core_1.normalize)(`${options.projectRoot}/tsconfig.app.json`),
|
35 | };
|
36 | const getProjectType = (tree) => {
|
37 | if (tree.exists(paths.tsConfigApp)) {
|
38 | return 'application';
|
39 | }
|
40 | if (tree.exists(paths.tsConfigLib)) {
|
41 | return 'library';
|
42 | }
|
43 | throw new schematics_1.SchematicsException(`project is missing tsconfig.lib.json or tsconfig.app.json`);
|
44 | };
|
45 | const getConfigFileForUpdate = (tree) => {
|
46 | const projectType = getProjectType(tree);
|
47 | if (projectType === 'library') {
|
48 | return paths.tsConfigLib;
|
49 | }
|
50 | if (projectType === 'application') {
|
51 | return paths.tsConfigApp;
|
52 | }
|
53 | };
|
54 | return (0, schematics_1.chain)([
|
55 | (0, ast_utils_1.updateJsonInTree)(paths.tsConfig, (json) => {
|
56 | if (json.compilerOptions) {
|
57 | json.compilerOptions.allowJs = true;
|
58 | }
|
59 | else {
|
60 | json.compilerOptions = { allowJs: true };
|
61 | }
|
62 | return json;
|
63 | }),
|
64 | (tree) => {
|
65 | const updateConfigPath = getConfigFileForUpdate(tree);
|
66 | return (0, ast_utils_1.updateJsonInTree)(updateConfigPath, (json) => {
|
67 | json.include = uniq([...json.include, '**/*.js']);
|
68 | json.exclude = uniq([...json.exclude, '**/*.spec.js']);
|
69 | return json;
|
70 | });
|
71 | },
|
72 | ]);
|
73 | }
|
74 | exports.updateTsConfigsToJs = updateTsConfigsToJs;
|
75 | const uniq = (value) => [...new Set(value)];
|
76 | function maybeJs(options, path) {
|
77 | return options.js && (path.endsWith('.ts') || path.endsWith('.tsx'))
|
78 | ? path.replace(/\.tsx?$/, '.js')
|
79 | : path;
|
80 | }
|
81 | exports.maybeJs = maybeJs;
|
82 |
|
\ | No newline at end of file |