Skip to content

Commit d57e3e9

Browse files
committed
ci: properly log execution from subprocess
1 parent 2259d3d commit d57e3e9

1 file changed

Lines changed: 20 additions & 29 deletions

File tree

scripts/snapshots.ts

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { logging } from '@angular-devkit/core';
9-
import { execSync } from 'child_process';
9+
import { execSync, spawnSync } from 'child_process';
1010
import * as fs from 'fs';
1111
import * as os from 'os';
1212
import * as path from 'path';
13-
import { Writable } from 'stream';
1413
import { packages } from '../lib/packages';
1514
import build from './build';
1615

@@ -32,25 +31,18 @@ function _copy(from: string, to: string) {
3231
}
3332

3433

35-
function _exec(command: string, opts: { cwd?: string }, logger: logging.Logger) {
36-
const infoLoggerStream = new Writable({
37-
write(chunk, _encoding, callback) {
38-
logger.info(chunk.toString());
39-
callback();
40-
},
41-
});
42-
const errorLoggerStream = new Writable({
43-
write(chunk, _encoding, callback) {
44-
logger.error(chunk.toString());
45-
callback();
46-
},
47-
});
48-
34+
function _exec(command: string, args: string[], opts: { cwd?: string }, logger: logging.Logger) {
4935
logger.debug(`Running command "${JSON.stringify(command)}"...`);
50-
execSync(command, {
36+
const { stdout, stderr, status, error } = spawnSync(command, args, {
5137
...opts,
52-
stdio: [0, infoLoggerStream, errorLoggerStream],
5338
});
39+
40+
logger.error(stderr.toString());
41+
if (status != 0) {
42+
logger.fatal(error.message);
43+
throw error;
44+
}
45+
logger.info(stdout.toString());
5446
}
5547

5648

@@ -70,10 +62,9 @@ export default function(opts: SnapshotsOptions, logger: logging.Logger) {
7062
const message = execSync(`git log --format="%h %s" -n1`).toString().trim();
7163

7264
const githubToken = fs.readFileSync(opts.githubTokenFile, 'utf-8');
73-
7465
logger.info('Setting up global git name.');
75-
_exec(`git config --global user.email "[email protected]"`, {}, logger);
76-
_exec(`git config --global user.name "Angular Builds"`, {}, logger);
66+
_exec('git', ['config', '--global', 'user.email', '[email protected]'], {}, logger);
67+
_exec('git', ['config', '--global', 'user.name', 'Angular Builds'], {}, logger);
7768

7869

7970
// Run build.
@@ -94,23 +85,23 @@ export default function(opts: SnapshotsOptions, logger: logging.Logger) {
9485
publishLogger.debug('Temporary directory: ' + root);
9586

9687
const url = `https://github.com/${pkg.snapshotRepo}.git`;
97-
_exec(`git clone ${JSON.stringify(url)}`, { cwd: root }, publishLogger);
88+
_exec('git', ['clone', url], { cwd: root }, publishLogger);
9889

9990
const destPath = path.join(root, path.basename(pkg.snapshotRepo));
10091
_copy(pkg.dist, destPath);
10192

102-
_exec(`git config credential.helper "store --file=.git/credentials"`, { cwd: destPath },
103-
publishLogger);
93+
_exec('git', ['config', 'credential.helper', 'store --file=.git/credentials'],
94+
{ cwd: root }, publishLogger);
10495
fs.writeFileSync(path.join(destPath, '.git/credentials'), `https://${githubToken}@github.com`);
10596

10697
// Make sure that every snapshots is unique.
10798
fs.writeFileSync(path.join(destPath, 'uniqueId'), '' + new Date());
10899

109100
// Commit and push.
110-
_exec(`git add -A`, { cwd: destPath }, publishLogger);
111-
_exec(`git commit -am ${JSON.stringify(message)}`, { cwd: destPath }, publishLogger);
112-
_exec(`git tag ${pkg.snapshotHash}`, { cwd: destPath }, publishLogger);
113-
_exec(`git push origin`, { cwd: destPath }, publishLogger);
114-
_exec(`git push --tags origin`, { cwd: destPath }, publishLogger);
101+
_exec('git', ['add', '.'], { cwd: destPath }, publishLogger);
102+
_exec('git', ['commit', '-am', message], { cwd: destPath }, publishLogger);
103+
_exec('git', ['tag', pkg.snapshotHash], { cwd: destPath }, publishLogger);
104+
_exec('git', ['push', 'origin'], { cwd: destPath }, publishLogger);
105+
_exec('git', ['push', '--tags', 'origin'], { cwd: destPath }, publishLogger);
115106
}
116107
}

0 commit comments

Comments
 (0)