-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathinit.ts
executable file
·125 lines (106 loc) · 3.36 KB
/
init.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
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
import {
addDependenciesToPackageJson,
createProjectGraphAsync,
ensurePackage,
formatFiles,
type GeneratorCallback,
logger,
readNxJson,
type Tree,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { assertNotUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { createNodesV2 } from '../../plugins/plugin';
import { getInstalledPackageVersion, versions } from '../utils/version-utils';
import { Schema } from './schema';
export async function angularInitGenerator(
tree: Tree,
options: Schema
): Promise<GeneratorCallback> {
assertNotUsingTsSolutionSetup(tree, 'angular', 'init');
ignoreAngularCacheDirectory(tree);
const installTask = installAngularDevkitCoreIfMissing(tree, options);
// For Angular inference plugin, we only want it during import since our
// generators do not use `angular.json`, and `nx init` should split
// `angular.json` into multiple `project.json` files -- as this is preferred
// by most folks we've talked to.
options.addPlugin ??= process.env.NX_RUNNING_NX_IMPORT === 'true';
if (options.addPlugin) {
await addPlugin(
tree,
await createProjectGraphAsync(),
'@nx/angular/plugin',
createNodesV2,
{
targetNamePrefix: ['', 'angular:', 'angular-'],
},
options.updatePackageScripts
);
}
if (!options.skipFormat) {
await formatFiles(tree);
}
return installTask;
}
function installAngularDevkitCoreIfMissing(
tree: Tree,
options: Schema
): GeneratorCallback {
const packageVersion = getInstalledPackageVersion(
tree,
'@angular-devkit/core'
);
if (!packageVersion) {
const pkgVersions = versions(tree);
const devkitVersion =
getInstalledPackageVersion(tree, '@angular-devkit/build-angular') ??
pkgVersions.angularDevkitVersion;
try {
ensurePackage('@angular-devkit/core', devkitVersion);
} catch {
// @schematics/angular cannot be required so this fails but this will still allow wrapping the schematic later on
}
if (!options.skipPackageJson) {
return addDependenciesToPackageJson(
tree,
{},
{ ['@angular-devkit/core']: devkitVersion },
undefined,
options.keepExistingVersions
);
}
}
return () => {};
}
function ignoreAngularCacheDirectory(tree: Tree): void {
const { cli } = readNxJson(tree);
// angular-specific cli config is supported though is not included in the
// NxJsonConfiguration type
const angularCacheDir = (cli as any)?.cache?.path ?? '.angular';
addGitIgnoreEntry(tree, angularCacheDir);
addPrettierIgnoreEntry(tree, angularCacheDir);
}
function addGitIgnoreEntry(tree: Tree, entry: string): void {
if (tree.exists('.gitignore')) {
let content = tree.read('.gitignore', 'utf-8');
if (/^\.angular$/gm.test(content)) {
return;
}
content = `${content}\n${entry}\n`;
tree.write('.gitignore', content);
} else {
logger.warn(`Couldn't find .gitignore file to update`);
}
}
function addPrettierIgnoreEntry(tree: Tree, entry: string): void {
if (!tree.exists('.prettierignore')) {
return;
}
let content = tree.read('.prettierignore', 'utf-8');
if (/^\.angular(\/cache)?$/gm.test(content)) {
return;
}
content = `${content}\n${entry}\n`;
tree.write('.prettierignore', content);
}
export default angularInitGenerator;