Skip to content

Commit d417fa9

Browse files
committed
build: add create script to create a new project using local build
1 parent 4f9dcd3 commit d417fa9

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

bin/devkit-admin

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const args = minimist(process.argv.slice(2), {
2525
const scriptName = args._.shift();
2626
const scriptPath = path.join('../scripts', scriptName);
2727

28+
const cwd = process.cwd();
2829
process.chdir(path.join(__dirname, '..'));
2930

3031

@@ -64,7 +65,7 @@ try {
6465

6566
try {
6667
Promise.resolve()
67-
.then(() => require(scriptPath).default(args, logger))
68+
.then(() => require(scriptPath).default(args, logger, cwd))
6869
.then(exitCode => process.exit(exitCode || 0))
6970
.catch(err => {
7071
logger.fatal(err.stack);
@@ -74,4 +75,3 @@ try {
7475
logger.fatal(err.stack);
7576
process.exit(99);
7677
}
77-

scripts/create.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
// tslint:disable:no-implicit-dependencies
9+
import { logging } from '@angular-devkit/core';
10+
import cli from '@angular/cli/lib/cli';
11+
import * as child_process from 'child_process';
12+
import * as fs from 'fs';
13+
import * as path from 'path';
14+
import { packages } from '../lib/packages';
15+
import build from './build';
16+
17+
export interface CreateOptions {
18+
_: string[];
19+
}
20+
21+
async function _ng(command: string, ...args: string[]) {
22+
const exitCode = await cli({
23+
cliArgs: [command, ...args],
24+
});
25+
26+
if (exitCode !== 0) {
27+
throw new Error('Could not call ng. See above for more details. Error code: ' + exitCode);
28+
}
29+
}
30+
31+
async function _exec(
32+
command: string,
33+
args: string[],
34+
opts: { cwd?: string },
35+
logger: logging.Logger,
36+
) {
37+
const { status, error, stderr, stdout } = child_process.spawnSync(command, args, { ...opts });
38+
39+
if (status != 0) {
40+
logger.error(`Command failed: ${command} ${args.map(x => JSON.stringify(x)).join(', ')}`);
41+
if (error) {
42+
logger.error('Error: ' + (error ? error.message : 'undefined'));
43+
} else {
44+
logger.error(`STDERR:\n${stderr}`);
45+
}
46+
throw error;
47+
}
48+
49+
return { stdout };
50+
}
51+
52+
53+
export default async function(
54+
args: CreateOptions,
55+
logger: logging.Logger,
56+
cwd: string,
57+
): Promise<number> {
58+
const projectName = args._[0];
59+
60+
const oldCwd = process.cwd();
61+
logger.info('Building...');
62+
await build({ local: true }, logger.createChild('build'));
63+
64+
process.chdir(cwd);
65+
logger.info('Creating project...');
66+
await _ng('new', projectName, '--skip-install', '--skip-git', '--no-interactive');
67+
68+
logger.info('Updating package.json...');
69+
const packageJsonPath = path.join(projectName, 'package.json');
70+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
71+
72+
if (!packageJson['dependencies']) {
73+
packageJson['dependencies'] = {};
74+
}
75+
if (!packageJson['devDependencies']) {
76+
packageJson['devDependencies'] = {};
77+
}
78+
79+
// Set the dependencies to the new build we just used.
80+
for (const packageName of Object.keys(packages)) {
81+
if (packageJson['dependencies'].hasOwnProperty(packageName)) {
82+
packageJson['dependencies'][packageName] = packages[packageName].tar;
83+
} else if (packageJson['devDependencies'].hasOwnProperty(packageName)) {
84+
packageJson['devDependencies'][packageName] = packages[packageName].tar;
85+
}
86+
}
87+
88+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf-8');
89+
90+
logger.info('Installing npm packages...');
91+
await _exec('npm', ['install'], { cwd: path.join(cwd, projectName) }, logger);
92+
93+
process.chdir(oldCwd);
94+
95+
return 0;
96+
}

0 commit comments

Comments
 (0)