forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.ts
222 lines (201 loc) · 6.41 KB
/
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { readFile, writeFile, replaceInFile } from './fs';
import { execAndWaitForOutputToMatch, silentNpm, ng } from './process';
import { getGlobalVariable } from './env';
const packages = require('../../../lib/packages').packages;
const tsConfigPath = 'src/tsconfig.app.json';
export function updateJsonFile(filePath: string, fn: (json: any) => any | void) {
return readFile(filePath)
.then(tsConfigJson => {
const tsConfig = JSON.parse(tsConfigJson);
const result = fn(tsConfig) || tsConfig;
return writeFile(filePath, JSON.stringify(result, null, 2));
});
}
export function updateTsConfig(fn: (json: any) => any | void) {
return updateJsonFile(tsConfigPath, fn);
}
export function ngServe(...args: string[]) {
return execAndWaitForOutputToMatch('ng',
['serve', ...args],
/webpack: bundle is now VALID|webpack: Compiled successfully./);
}
export function createProject(name: string, ...args: string[]) {
const argv: any = getGlobalVariable('argv');
return Promise.resolve()
.then(() => process.chdir(getGlobalVariable('tmp-root')))
.then(() => ng('new', name, '--skip-install', ...args))
.then(() => process.chdir(name))
.then(() => updateJsonFile('package.json', json => {
Object.keys(packages).forEach(pkgName => {
json['dependencies'][pkgName] = packages[pkgName].dist;
});
}))
.then(() => useCIChrome())
.then(() => argv['ng2'] ? useNg2() : Promise.resolve())
.then(() => {
if (argv.nightly || argv['ng-sha']) {
const label = argv['ng-sha'] ? `#2.0.0-${argv['ng-sha']}` : '';
return updateJsonFile('package.json', json => {
// Install over the project with nightly builds.
Object.keys(json['dependencies'] || {})
.filter(name => name.match(/^@angular\//))
.forEach(name => {
const pkgName = name.split(/\//)[1];
if (pkgName == 'cli') {
return;
}
json['dependencies'][`@angular/${pkgName}`]
= `github:angular/${pkgName}-builds${label}`;
});
Object.keys(json['devDependencies'] || {})
.filter(name => name.match(/^@angular\//))
.forEach(name => {
const pkgName = name.split(/\//)[1];
if (pkgName == 'cli') {
return;
}
json['devDependencies'][`@angular/${pkgName}`]
= `github:angular/${pkgName}-builds${label}`;
});
});
}
})
.then(() => console.log(`Project ${name} created... Installing npm.`))
.then(() => silentNpm('install'));
}
export function useCIChrome() {
// There's a race condition happening in Chrome. Enabling logging in chrome used by
// protractor actually fixes it. Logging is piped to a file so it doesn't affect our setup.
// --no-sandbox is needed for Circle CI.
// Travis can use headless chrome, but not appveyor.
return Promise.resolve()
.then(() => replaceInFile('protractor.conf.js', `'browserName': 'chrome'`,
`'browserName': 'chrome',
chromeOptions: {
args: [
"--enable-logging",
"--no-sandbox",
${process.env['TRAVIS'] ? '"--headless", "--disable-gpu"' : ''}
]
}
`))
// Not a problem if the file can't be found.
.catch(() => null)
.then(() => replaceInFile('karma.conf.js', `browsers: ['Chrome'],`,
`browsers: ['ChromeCI'],
customLaunchers: {
ChromeCI: {
base: '${process.env['TRAVIS'] ? 'ChromeHeadless' : 'Chrome'}',
flags: ['--no-sandbox']
}
},
`))
.catch(() => null);
}
// Convert a Angular 4 project to Angular 2.
export function useNg2() {
const ng2Deps: any = {
'dependencies': {
'@angular/common': '^2.4.0',
'@angular/compiler': '^2.4.0',
'@angular/core': '^2.4.0',
'@angular/forms': '^2.4.0',
'@angular/http': '^2.4.0',
'@angular/platform-browser': '^2.4.0',
'@angular/platform-browser-dynamic': '^2.4.0',
'@angular/router': '^3.4.0',
'zone.js': '^0.7.4'
},
'devDependencies': {
'@angular/compiler-cli': '^2.4.0',
'@types/jasmine': '~2.2.0',
'@types/jasminewd2': undefined,
'typescript': '~2.0.0'
}
};
const tsconfigAppJson: any = {
'compilerOptions': {
'sourceMap': true,
'declaration': false,
'moduleResolution': 'node',
'emitDecoratorMetadata': true,
'experimentalDecorators': true,
'target': 'es5',
'lib': [
'es2017',
'dom'
],
'outDir': '../out-tsc/app',
'module': 'es2015',
'baseUrl': '',
'types': []
},
'exclude': [
'test.ts',
'**/*.spec.ts'
]
};
const tsconfigSpecJson: any = {
'compilerOptions': {
'sourceMap': true,
'declaration': false,
'moduleResolution': 'node',
'emitDecoratorMetadata': true,
'experimentalDecorators': true,
'lib': [
'es2017',
'dom'
],
'outDir': '../out-tsc/spec',
'module': 'commonjs',
'target': 'es5',
'baseUrl': '',
'types': [
'jasmine',
'node'
]
},
'files': [
'test.ts'
],
'include': [
'**/*.spec.ts',
'**/*.d.ts'
]
};
const tsconfigE2eJson: any = {
'compilerOptions': {
'sourceMap': true,
'declaration': false,
'moduleResolution': 'node',
'emitDecoratorMetadata': true,
'experimentalDecorators': true,
'lib': [
'es2017'
],
'outDir': '../out-tsc/e2e',
'module': 'commonjs',
'target': 'es5',
'types': [
'jasmine',
'node'
]
}
};
return Promise.resolve()
.then(() => updateJsonFile('package.json', json => {
Object.keys(ng2Deps['dependencies']).forEach(pkgName => {
json['dependencies'][pkgName] = ng2Deps['dependencies'][pkgName];
});
Object.keys(ng2Deps['devDependencies']).forEach(pkgName => {
json['devDependencies'][pkgName] = ng2Deps['devDependencies'][pkgName];
});
console.log(JSON.stringify(json))
}))
.then(() => updateJsonFile('src/tsconfig.app.json', json =>
Object.assign(json, tsconfigAppJson)))
.then(() => updateJsonFile('src/tsconfig.spec.json', json =>
Object.assign(json, tsconfigSpecJson)))
.then(() => updateJsonFile('e2e/tsconfig.e2e.json', json =>
Object.assign(json, tsconfigE2eJson)));
}