forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.ts
96 lines (79 loc) · 2.72 KB
/
create.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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable:no-implicit-dependencies
import { logging } from '@angular-devkit/core';
import cli from '@angular/cli/lib/cli';
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { packages } from '../lib/packages';
import build from './build';
export interface CreateOptions {
_: string[];
}
async function _ng(command: string, ...args: string[]) {
const exitCode = await cli({
cliArgs: [command, ...args],
});
if (exitCode !== 0) {
throw new Error('Could not call ng. See above for more details. Error code: ' + exitCode);
}
}
async function _exec(
command: string,
args: string[],
opts: { cwd?: string },
logger: logging.Logger,
) {
const { status, error, stderr, stdout } = child_process.spawnSync(command, args, { ...opts });
if (status != 0) {
logger.error(`Command failed: ${command} ${args.map(x => JSON.stringify(x)).join(', ')}`);
if (error) {
logger.error('Error: ' + (error ? error.message : 'undefined'));
} else {
logger.error(`STDERR:\n${stderr}`);
}
throw error;
}
return { stdout };
}
export default async function(
args: CreateOptions,
logger: logging.Logger,
cwd: string,
): Promise<number> {
const projectName = args._[0];
const oldCwd = process.cwd();
logger.info('Building...');
await build({ local: true }, logger.createChild('build'));
process.chdir(cwd);
logger.info('Creating project...');
await _ng('new', projectName, '--skip-install', '--skip-git', '--no-interactive');
logger.info('Updating package.json...');
const packageJsonPath = path.join(projectName, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
if (!packageJson['dependencies']) {
packageJson['dependencies'] = {};
}
if (!packageJson['devDependencies']) {
packageJson['devDependencies'] = {};
}
// Set the dependencies to the new build we just used.
for (const packageName of Object.keys(packages)) {
if (packageJson['dependencies'].hasOwnProperty(packageName)) {
packageJson['dependencies'][packageName] = packages[packageName].tar;
} else if (packageJson['devDependencies'].hasOwnProperty(packageName)) {
packageJson['devDependencies'][packageName] = packages[packageName].tar;
}
}
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf-8');
logger.info('Installing npm packages...');
await _exec('npm', ['install'], { cwd: path.join(cwd, projectName) }, logger);
process.chdir(oldCwd);
return 0;
}