forked from DevExpress/devextreme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.angular.js
More file actions
170 lines (138 loc) · 5.45 KB
/
application.angular.js
File metadata and controls
170 lines (138 loc) · 5.45 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const getLayoutInfo = require('../utility/prompts/layout');
const path = require('path');
const moduleWorker = require('../utility/module');
const runCommand = require('../utility/run-command');
const semver = require('semver').SemVer;
const fs = require('fs');
const dasherize = require('../utility/string').dasherize;
const ngVersion = require('../utility/ng-version');
const latestVersions = require('../utility/latest-versions');
const { extractDepsVersionTag, depsVersionTagOptionName } = require('../utility/extract-deps-version-tag');
const { getPackageJsonPath } = require('../utility/package-json-utils');
const modifyJson = require('../utility/modify-json-file');
const schematicsVersion = latestVersions['devextreme-schematics'] || 'latest';
const minNgCliVersion = new semver('17.0.0');
async function runSchematicCommand(schematicCommand, options, evaluatingOptions) {
const collectionName = 'devextreme-schematics';
let collectionPath = `${collectionName}@${schematicsVersion}`;
if(options['c']) {
collectionPath = `${path.join(process.cwd(), options['c'])}`;
delete options['c'];
}
if(!localPackageExists(collectionName)) {
await runNgCommand(['add', collectionPath, '--skip-confirmation=true'], options, evaluatingOptions);
}
const commandArguments = ['g', `${collectionName}:${schematicCommand}`];
const { [depsVersionTagOptionName]: _, ...optionsToArguments } = options; // eslint-disable-line no-unused-vars
for(let option in optionsToArguments) {
commandArguments.push(`--${dasherize(option)}=${options[option]}`);
}
runNgCommand(commandArguments, options, evaluatingOptions);
}
async function runNgCommand(commandArguments, commandOptions, commandConfig) {
const hasNg = await hasSutableNgCli();
const depsVersionTag = extractDepsVersionTag(commandOptions);
const npmCommandName = hasNg && !depsVersionTag ? 'ng' : 'npx';
const [minCliLtsVersion] = minNgCliVersion.version.split('.');
const ngCommandArguments = hasNg && !depsVersionTag
? []
: ['-p', `@angular/cli@${depsVersionTag || `v${minCliLtsVersion}-lts`}`, 'ng'];
ngCommandArguments.push(...commandArguments);
return runCommand(npmCommandName, ngCommandArguments, commandConfig);
}
function localPackageExists(packageName) {
const nodeModulesPath = path.join(process.cwd(), 'node_modules');
if(!fs.existsSync(nodeModulesPath)) {
return;
}
const packageJsonPath = path.join(nodeModulesPath, packageName, 'package.json');
return fs.existsSync(packageJsonPath);
}
const hasSutableNgCli = async() => {
const localVersion = ngVersion.getLocalNgVersion();
if(!localVersion) {
return false;
}
const isSupportVersion = localVersion.compare(minNgCliVersion) >= 0;
return isSupportVersion;
};
const install = async(options) => {
runSchematicCommand('install', {
...options
});
};
const bumpAngular = (appPath, versionTag) => {
modifyJson(getPackageJsonPath(appPath), ({ dependencies, devDependencies, ...rest }) => {
const bump = (section) => {
for(const depName in section) {
section[depName] = depName.startsWith('@angular') ? versionTag : section[depName];
}
};
return {
dependencies: bump(dependencies),
devDependencies: bump(devDependencies),
...rest,
};
});
};
const create = async(appName, options) => {
const layout = await getLayoutInfo(options.layout);
const depsVersionTag = extractDepsVersionTag(options);
const commandArguments = [
'new',
appName,
'--style=scss',
'--routing=false',
'--skip-tests=true',
'--skip-install=true',
'--standalone=false',
'--ssr=false'
];
await runNgCommand(commandArguments, options);
const appPath = path.join(process.cwd(), appName);
if(depsVersionTag) {
bumpAngular(appPath, depsVersionTag);
}
options.resolveConflicts = 'override';
options.updateBudgets = true;
options.layout = layout;
addTemplate(appName, options, {
cwd: appPath
});
changeMainTs(appPath);
};
const addTemplate = async(appName, options, evaluatingOptions) => {
const schematicOptions = {
...(appName && { project: appName }),
...options,
globalNgCliVersion: ngVersion.getNgCliVersion().version
};
runSchematicCommand('add-app-template', schematicOptions, evaluatingOptions);
};
const addView = (viewName, options) => {
const schematicOptions = Object.assign({
name: viewName
}, options);
schematicOptions.name = viewName;
runSchematicCommand('add-view', schematicOptions);
};
const changeMainTs = (appPath) => {
const filePath = path.join(appPath, 'src', 'main.ts');
moduleWorker.insertImport(filePath, 'devextreme/ui/themes', 'themes', true);
const fileContent = fs.readFileSync(filePath).toString();
const bootstrapPattern = /platformBrowser(?:Dynamic)?\(\)\.bootstrapModule\(\s*AppModule\s*(?:,\s*\{[^}]*\})?\s*\)/;
const firstChaptStr = fileContent.match(bootstrapPattern)[0];
const lastChaptStr = '.catch(err => console.error(err));';
fs.writeFileSync(
filePath,
fileContent
.replace(firstChaptStr, `themes.initialized(() => {\n ${firstChaptStr}`)
.replace(lastChaptStr, ` ${lastChaptStr}\n});`)
);
};
module.exports = {
install,
create,
addTemplate,
addView
};