-
Notifications
You must be signed in to change notification settings - Fork 1k
/
setup-project.ts
201 lines (172 loc) · 6.09 KB
/
setup-project.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { parseJson } from '@angular-devkit/core';
import { chain, Rule, SchematicContext, Tree, SchematicsException } from '@angular-devkit/schematics';
import {
addModuleImportToRootModule,
getProjectFromWorkspace,
getProjectMainFile,
getProjectTargetOptions,
getSourceFile,
hasNgModuleImport,
} from '../utils';
import { WorkspaceProject, WorkspaceSchema } from '@angular-devkit/core/src/experimental/workspace';
import { getWorkspace } from '@schematics/angular/utility/config';
import { addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils';
import { getAppModulePath } from '@schematics/angular/utility/ng-ast-utils';
import { InsertChange } from '@schematics/angular/utility/change';
import { Schema } from './schema';
/** JSON */
interface UpdateJsonFn<T> {
(obj: T): T | void;
}
type TsLintPartialType = {
rules: {
[key: string]: [
boolean,
string | string[],
string | string[],
string
]
};
}
/** Name of the ngx-onsenui module. */
const moduleName = 'OnsenModule';
/**
* Scaffolds the basics of a Angular Material application, this includes:
* - Add Packages to package.json
* - Adds pre-built themes to styles.ext
* - Adds OnsenModule to app.module
*/
export default function(options: Schema): Rule {
return chain([
addCustomElementsSchema(options),
addOnsenModule(options),
addOnsenStyles(options),
updateTsLint(),
showCompleteMessage()
]);
}
/**
* Updates JSON file
*/
function updateJsonFile<T>(host: Tree, path: string, callback: UpdateJsonFn<T>): Tree {
const source = host.read(path);
if (source) {
const sourceText = source.toString('utf-8');
const json = parseJson(sourceText);
callback(json as {} as T);
host.overwrite(path, JSON.stringify(json, null, 4));
}
return host;
}
/**
* Updates tslint.json
*/
function updateTsLint() {
return (host: Tree) => {
if (!host.exists('src/tslint.json')) { return host; }
return updateJsonFile(host, 'src/tslint.json', (tslint: TsLintPartialType) => {
if (!tslint.rules) {
tslint.rules = {};
}
if (!tslint.rules['component-selector']) {
tslint.rules['component-selector'] = [
true,
'element',
'app',
'kebab-case'
];
}
const s = tslint.rules['component-selector'][2];
if (Array.isArray(s)) {
tslint.rules['component-selector'][2] = [...s, 'ons-page'];
} else {
tslint.rules['component-selector'][2] = [s, 'ons-page'];
}
});
};
}
/**
* Adds an animation module to the root module of the specified project.
*/
function addOnsenModule(options: Schema) {
return (host: Tree, context: SchematicContext) => {
const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
const appModulePath = getAppModulePath(host, getProjectMainFile(project));
if (!hasNgModuleImport(host, appModulePath, moduleName)) {
// Do not add the OnsenModule module if the project already explicitly uses
// the BrowserAnimationsModule.
addModuleImportToRootModule(host, moduleName, 'ngx-onsenui', project);
}
context.logger.log('info', `✅️ ${moduleName} is imported`);
return host;
};
}
/**
* Adds CUSTOM_ELEMENTS_SCHEMA to the root module of the specified project.
*/
function addCustomElementsSchema(options: Schema) {
return (host: Tree) => {
const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
const appModulePath = getAppModulePath(host, getProjectMainFile(project));
const moduleSource = getSourceFile(host, appModulePath);
if (!moduleSource) {
throw new SchematicsException(`Module not found: ${appModulePath}`);
}
const changes = addSymbolToNgModuleMetadata(moduleSource, appModulePath, 'schemas', 'CUSTOM_ELEMENTS_SCHEMA', '@angular/core');
const declarationRecorder = host.beginUpdate(appModulePath);
for (const change of changes) {
if (change instanceof InsertChange) {
declarationRecorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(declarationRecorder);
return host;
}
}
/**
* Adds Onsen UI styles to the specified project.
*/
function addOnsenStyles(options: Schema) {
return (host: Tree, context: SchematicContext) => {
const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
const onsenTheme = `./node_modules/onsenui/css/onsenui.css`;
const onsenComponentsTheme = `./node_modules/onsenui/css/onsen-css-components.css`;
addThemeStyleToTarget(project, 'build', host, onsenTheme, workspace);
addThemeStyleToTarget(project, 'build', host, onsenComponentsTheme, workspace);
addThemeStyleToTarget(project, 'test', host, onsenTheme, workspace);
addThemeStyleToTarget(project, 'test', host, onsenComponentsTheme, workspace);
context.logger.log('info', `✅️ Added Onsen UI theme to styles`);
return host;
};
}
/** Adds a theming style entry to the given project target options. */
function addThemeStyleToTarget(project: WorkspaceProject, targetName: string, host: Tree,
assetPath: string, workspace: WorkspaceSchema) {
const targetOptions = getProjectTargetOptions(project, targetName);
if (!targetOptions.styles) {
targetOptions.styles = [assetPath];
} else {
const existingStyles = targetOptions.styles.map((s: any) => typeof s === 'string' ? s : s.input);
for (let stylePath of existingStyles.entries()) {
// If the given asset is already specified in the styles, we don't need to do anything.
if (stylePath === assetPath) {
return;
}
}
targetOptions.styles.unshift(assetPath);
}
host.overwrite('angular.json', JSON.stringify(workspace, null, 2));
}
/**
* Show complete message.
*/
function showCompleteMessage() {
return (host: Tree, context: SchematicContext) => {
context.logger.log('info', `🎉 Hooray! ngx-onsenui is successfully installed.`);
context.logger.log('info', `♨️ For more information, see https://onsen.io/v2/api/angular2/`);
return host;
};
}