|
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | 4 | /* eslint-disable no-console */ |
5 | | -const spawn = require( 'child_process').spawnSync; |
| 5 | +const spawnSync = require( 'child_process').spawnSync; |
6 | 6 | const fs = require('fs'); |
7 | 7 | const temp = require('temp'); |
8 | | -const chalk = require('chalk'); |
| 8 | +const { blue, green, red, gray, yellow } = require('chalk'); |
9 | 9 |
|
10 | | -const outputPath = temp.mkdirSync('angular-cli-builds'); |
| 10 | +const path = require('path'); |
| 11 | +const glob = require('glob'); |
11 | 12 | const cli = 'https://github.com/angular/angular-cli.git'; |
12 | 13 | const cliBuilds = 'https://github.com/angular/cli-builds.git'; |
13 | 14 | const ngToolsWebpackBuilds = 'https://github.com/angular/ngtools-webpack-builds.git'; |
14 | 15 |
|
15 | | -function execute(command, cwd, ...args) { |
16 | | - return new Promise((resolve, reject) => { |
17 | | - const runCommand = spawn(command, args, { cwd }); |
| 16 | + |
| 17 | +class Executor { |
| 18 | + constructor(cwd) { this._cwd = cwd; } |
| 19 | + |
| 20 | + execute(command, ...args) { |
| 21 | + args = args.filter(x => x !== undefined); |
| 22 | + console.log(blue(`Running \`${command} ${args.map(x => `"${x}"`).join(' ')}\`...`)); |
| 23 | + console.log(blue(`CWD: ${this._cwd}`)); |
| 24 | + |
| 25 | + const runCommand = spawnSync(command, args, { cwd: this._cwd }); |
18 | 26 | if (runCommand.status === 0) { |
19 | | - console.log(chalk.gray(runCommand.output.toString())); |
20 | | - resolve(runCommand.output.toString()); |
| 27 | + console.log(gray(runCommand.stdout.toString())); |
| 28 | + return runCommand.stdout.toString(); |
21 | 29 | } else { |
22 | | - reject({ message: runCommand.error || runCommand.stdout.toString() || runCommand.stderr.toString() }); |
| 30 | + throw new Error( |
| 31 | + `Command returned status ${runCommand.status}. Details:\n${runCommand.stderr}`); |
23 | 32 | } |
24 | | - }); |
25 | | -} |
| 33 | + } |
26 | 34 |
|
27 | | -function printMessage(message) { |
28 | | - console.log(chalk.green(`${message}\r\n`)); |
29 | | -} |
| 35 | + git(...args) { return this.execute('git', ...args); } |
| 36 | + npm(...args) { return this.execute('npm', ...args); } |
| 37 | + rm(...args) { return this.execute('rm', ...args); } |
30 | 38 |
|
31 | | -function updateDependencyPath(path, commitMessage) { |
32 | | - return new Promise((resolve, reject) => { |
33 | | - fs.readFile(path, 'utf-8', (readError, data) => { |
34 | | - if (readError) { |
35 | | - reject(readError); |
36 | | - } else { |
37 | | - let packageJSON = JSON.parse(data); |
38 | | - packageJSON.dependencies['@ngtools/webpack'] = `${ngToolsWebpackBuilds}#${commitMessage.substr(1, 7)}`; |
39 | | - fs.writeFile(path, JSON.stringify(packageJSON, null, 2), (writeError, updatedFile) => { |
40 | | - if (writeError) { |
41 | | - reject(writeError); |
42 | | - } else { |
43 | | - resolve(updatedFile); |
44 | | - } |
45 | | - }); |
46 | | - } |
47 | | - }); |
48 | | - }); |
49 | | -} |
| 39 | + glob(pattern, options) { |
| 40 | + return glob.sync(pattern, Object.assign({}, options || {}, { cwd: this._cwd })); |
| 41 | + } |
50 | 42 |
|
51 | | -function updateVersion(path, commitMessage) { |
52 | | - return new Promise((resolve, reject) => { |
53 | | - fs.readFile(path, 'utf-8', (readError, data) => { |
54 | | - if (readError) { |
55 | | - reject(readError); |
56 | | - } else { |
57 | | - let packageJSON = JSON.parse(data); |
58 | | - packageJSON.version = packageJSON.version + '-' + commitMessage.substr(1, 7); |
59 | | - fs.writeFile(path, JSON.stringify(packageJSON, null, 2), (writeError, updatedFile) => { |
60 | | - if (writeError) { |
61 | | - reject(writeError); |
62 | | - } else { |
63 | | - resolve(updatedFile); |
64 | | - } |
65 | | - }); |
| 43 | + cp(root, destRoot) { |
| 44 | + function mkdirp(p) { |
| 45 | + if (fs.existsSync(p)) { |
| 46 | + return; |
66 | 47 | } |
67 | | - }); |
68 | | - }); |
| 48 | + mkdirp(path.dirname(p)); |
| 49 | + fs.mkdirSync(p); |
| 50 | + } |
| 51 | + |
| 52 | + this.glob(path.join(root, '**/*'), { nodir: true }) |
| 53 | + .forEach(name => { |
| 54 | + const src = name; |
| 55 | + const dest = path.join(destRoot, src.substr(root.length)); |
| 56 | + |
| 57 | + mkdirp(path.dirname(dest)); |
| 58 | + fs.writeFileSync(dest, fs.readFileSync(src)); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + read(p) { |
| 63 | + return fs.readFileSync(path.join(this._cwd, p), 'utf-8'); |
| 64 | + } |
| 65 | + write(p, content) { |
| 66 | + fs.writeFileSync(path.join(this._cwd, p), content); |
| 67 | + } |
| 68 | + |
| 69 | + updateVersion(hash) { |
| 70 | + const packageJson = JSON.parse(this.read('package.json')); |
| 71 | + packageJson.version = `${packageJson.version}-${hash.substr(1, 7)}`; |
| 72 | + this.write('package.json', JSON.stringify(packageJson, null, 2)); |
| 73 | + } |
| 74 | + |
| 75 | + updateDependencies(hash) { |
| 76 | + const packageJson = JSON.parse(this.read('package.json')); |
| 77 | + packageJson.dependencies['@ngtools/webpack'] = ngToolsWebpackBuilds + '#' + hash; |
| 78 | + this.write('package.json', JSON.stringify(packageJson, null, 2)); |
| 79 | + } |
69 | 80 | } |
70 | 81 |
|
71 | | -function getCommitMessage(path) { |
72 | | - return execute('git', path, 'log', '--format=%h %s', '-n', 1) |
73 | | - .then(data => { |
74 | | - return data; |
75 | | - }); |
| 82 | + |
| 83 | +async function main() { |
| 84 | + const cliPath = process.cwd(); |
| 85 | + const cliExec = new Executor(cliPath); |
| 86 | + const tempRoot = temp.mkdirSync('angular-cli-builds'); |
| 87 | + const tempExec = new Executor(tempRoot); |
| 88 | + |
| 89 | + console.log(green(`Cloning builds repos...\n`)); |
| 90 | + tempExec.git('clone', cliBuilds); |
| 91 | + tempExec.git('clone', ngToolsWebpackBuilds); |
| 92 | + |
| 93 | + console.log(green('Building...')); |
| 94 | + |
| 95 | + const cliBuildsRoot = path.join(tempRoot, 'cli-builds'); |
| 96 | + const cliBuildsExec = new Executor(cliBuildsRoot); |
| 97 | + const ngToolsWebpackBuildsRoot = path.join(tempRoot, 'ngtools-webpack-builds'); |
| 98 | + const ngToolsWebpackBuildsExec = new Executor(ngToolsWebpackBuildsRoot); |
| 99 | + |
| 100 | + cliExec.npm('run', 'build'); |
| 101 | + |
| 102 | + const message = cliExec.git('log', '--format=%h %s', '-n', '1'); |
| 103 | + const hash = message.split(' ')[0]; |
| 104 | + |
| 105 | + console.log(green('Copying ng-tools-webpack-builds dist')); |
| 106 | + ngToolsWebpackBuildsExec.git('checkout', '-B', process.env['TRAVIS_BRANCH']); |
| 107 | + ngToolsWebpackBuildsExec.rm('-rf', ...ngToolsWebpackBuildsExec.glob('*')); |
| 108 | + cliExec.cp('dist/@ngtools/webpack', ngToolsWebpackBuildsRoot); |
| 109 | + console.log(green('Updating package.json version')); |
| 110 | + ngToolsWebpackBuildsExec.updateVersion(hash); |
| 111 | + ngToolsWebpackBuildsExec.git('add', '-A'); |
| 112 | + ngToolsWebpackBuildsExec.git('commit', '-m', message); |
| 113 | + ngToolsWebpackBuildsExec.git('tag', hash); |
| 114 | + |
| 115 | + ngToolsWebpackBuildsExec.git('config', 'credential.helper', 'store --file=.git/credentials'); |
| 116 | + ngToolsWebpackBuildsExec.write('.git/credentials', |
| 117 | + `https://${process.env['GITHUB_ACCESS_TOKEN']}@github.com`); |
| 118 | + |
| 119 | + |
| 120 | + console.log(green('Copying cli-builds dist')); |
| 121 | + cliBuildsExec.git('checkout', '-B', process.env['TRAVIS_BRANCH']); |
| 122 | + cliBuildsExec.rm('-rf', ...cliBuildsExec.glob('*')); |
| 123 | + cliExec.cp('dist/@angular/cli', cliBuildsRoot); |
| 124 | + |
| 125 | + console.log(green('Updating package.json version')); |
| 126 | + cliBuildsExec.updateVersion(hash); |
| 127 | + cliBuildsExec.updateDependencies(hash); |
| 128 | + |
| 129 | + cliBuildsExec.git('add', '-A'); |
| 130 | + cliBuildsExec.git('commit', '-m', message); |
| 131 | + cliBuildsExec.git('tag', hash); |
| 132 | + |
| 133 | + cliBuildsExec.git('config', 'credential.helper', 'store --file=.git/credentials'); |
| 134 | + cliBuildsExec.write('.git/credentials', |
| 135 | + `https://${process.env['GITHUB_ACCESS_TOKEN']}@github.com`); |
| 136 | + |
| 137 | + console.log(green('Done. Pushing...')); |
| 138 | + ngToolsWebpackBuildsExec.git('push', '-f'); |
| 139 | + ngToolsWebpackBuildsExec.git('push', '--tags'); |
| 140 | + cliBuildsExec.git('push', '-f'); |
| 141 | + cliBuildsExec.git('push', '--tags'); |
76 | 142 | } |
77 | 143 |
|
78 | | -Promise.resolve() |
79 | | - .then(() => process.chdir(outputPath)) |
80 | | - .then(() => console.log(process.cwd())) |
81 | | - .then(() => printMessage('Cloning...')) |
82 | | - .then(() => execute('git', process.cwd(), 'clone', cli)) |
83 | | - .then(() => execute('git', process.cwd(), 'clone', cliBuilds)) |
84 | | - .then(() => execute('git', process.cwd(), 'clone', ngToolsWebpackBuilds)) |
85 | | - .then(() => printMessage('Installing packages...')) |
86 | | - .then(() => execute('npm', './angular-cli', 'install')) |
87 | | - .then(() => printMessage('Creating build...')) |
88 | | - .then(() => execute('npm', './angular-cli', 'run', 'build')) |
89 | | - //---------------------------- ngtools-webpack-builds ----------------------// |
90 | | - .then(() => printMessage('Copying ngtools-webpack-builds dist....')) |
91 | | - .then(() => execute('cp', './ngtools-webpack-builds', '-a', './../angular-cli/dist/@ngtools/webpack/.', '.')) |
92 | | - .then(() => printMessage('Updating package.json')) |
93 | | - .then(() => getCommitMessage('./angular-cli')) |
94 | | - .then((message) => updateVersion('./ngtools-webpack-builds/package.json', message)) |
95 | | - .then(() => execute('git', './ngtools-webpack-builds', 'add', '-A')) |
96 | | - .then(() => getCommitMessage('./angular-cli')) |
97 | | - .then((message) => execute('git', './ngtools-webpack-builds', 'commit', '-am', message.substr(1))) |
98 | | - // Update the credentials using the GITHUB TOKEN. |
99 | | - .then(() => execute('git', './ngtools-webpack-builds', 'config', 'credential.helper', |
100 | | - 'store --file=.git/credentials')) |
101 | | - .then(() => fs.writeFileSync('./ngtools-webpack-builds/.git/credentials', |
102 | | - `https://${process.env['GITHUB_ACCESS_TOKEN']}@github.com`)) |
103 | | - .then(() => console.log(process.env['DEPLOY_SCRIPT'])) |
104 | | - .then(() => execute('git', './ngtools-webpack-builds', 'push')) |
105 | | - //---------------------------- cli-builds ----------------------------------// |
106 | | - .then(() => printMessage('Copying cli-builds dist....')) |
107 | | - .then(() => execute('cp', './cli-builds', '-a', './../angular-cli/dist/@angular/cli/.', '.')) |
108 | | - .then(() => printMessage('Updating package.json')) |
109 | | - .then(() => getCommitMessage('./angular-cli')) |
110 | | - .then((message) => updateVersion('./cli-builds/package.json', message)) |
111 | | - .then(() => getCommitMessage('./ngtools-webpack-builds')) |
112 | | - .then((message) => updateDependencyPath('./cli-builds/package.json', message)) |
113 | | - .then(() => execute('git', './cli-builds', 'add', '-A')) |
114 | | - .then(() => getCommitMessage('./angular-cli')) |
115 | | - .then((message) => execute('git', './cli-builds', 'commit', '-am', message.substr(1))) |
116 | | - // Update the credentials using the GITHUB TOKEN. |
117 | | - .then(() => execute('git', './cli-builds', 'config', 'credential.helper', |
118 | | - 'store --file=.git/credentials')) |
119 | | - .then(() => fs.writeFileSync('./cli-builds/.git/credentials', |
120 | | - `https://${process.env['GITHUB_ACCESS_TOKEN']}@github.com`)) |
121 | | - .then(() => execute('git', './cli-builds', 'push')) |
122 | | - //---------------------------- done ----------------------------------------// |
123 | | - .then(() => console.log('Done...')) |
124 | | - .catch(err => console.error(`Error:\n${err.message}`)); |
| 144 | +main() |
| 145 | + .then(() => console.log(green('All good. Thank you.'))) |
| 146 | + .catch(err => console.log(red('Error happened. Details:\n' + err))); |
0 commit comments