Skip to content

Commit 368bc6b

Browse files
hansljelbourn
authored andcommitted
chore: fix publish gulp task and output more information. (#1099)
1 parent cdad90b commit 368bc6b

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@types/hammerjs": "^2.0.30",
4545
"@types/jasmine": "^2.2.31",
4646
"@types/merge2": "0.0.28",
47+
"@types/minimist": "^1.1.28",
4748
"@types/node": "^6.0.34",
4849
"@types/run-sequence": "0.0.27",
4950
"browserstacktunnel-wrapper": "^1.4.2",
@@ -68,6 +69,7 @@
6869
"karma-sauce-launcher": "^1.0.0",
6970
"madge": "^0.6.0",
7071
"merge2": "^1.0.2",
72+
"minimist": "^1.2.0",
7173
"node-sass": "^3.4.2",
7274
"protractor": "^3.3.0",
7375
"protractor-accessibility-plugin": "0.1.1",

tools/gulp/tasks/release.ts

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import {execSync} from 'child_process';
2-
import {readdirSync, statSync} from 'fs';
1+
import {spawn} from 'child_process';
2+
import {existsSync, readdirSync, statSync} from 'fs';
33
import {task} from 'gulp';
44
import gulpRunSequence = require('run-sequence');
55
import path = require('path');
6+
import minimist = require('minimist');
67

78
import {execTask} from '../task_helpers';
89
import {DIST_COMPONENTS_ROOT} from '../constants';
910

11+
const argv = minimist(process.argv.slice(3));
12+
1013

1114
task('build:release', function(done: () => void) {
1215
// Synchronously run those tasks.
@@ -27,24 +30,64 @@ task(':publish:whoami', execTask('npm', ['whoami'], {
2730

2831
task(':publish:logout', execTask('npm', ['logout']));
2932

30-
task(':publish', function() {
31-
const label = process.argv.slice(2)[1]; // [0] would be ':publish'
32-
const labelArg = label ? `--tag ${label}` : '';
33-
const currentDir = process.cwd();
3433

35-
readdirSync(DIST_COMPONENTS_ROOT)
36-
.forEach(dirName => {
37-
const componentPath = path.join(DIST_COMPONENTS_ROOT, dirName);
38-
const stat = statSync(componentPath);
34+
function _execNpmPublish(componentName: string, label: string): Promise<void> {
35+
const componentPath = path.join(DIST_COMPONENTS_ROOT, componentName);
36+
const stat = statSync(componentPath);
3937

40-
if (!stat.isDirectory()) {
41-
return;
42-
}
38+
if (!stat.isDirectory()) {
39+
return;
40+
}
41+
42+
if (!existsSync(path.join(componentPath, 'package.json'))) {
43+
console.log(`Skipping ${componentPath} as it does not have a package.json.`);
44+
return;
45+
}
46+
47+
process.chdir(componentPath);
48+
console.log(`Publishing ${componentName}...`);
49+
50+
const command = 'npm';
51+
const args = ['publish', '--access', 'public', label ? `--tag` : undefined, label || undefined];
52+
return new Promise((resolve, reject) => {
53+
console.log(`Executing "${command} ${args.join(' ')}"...`);
54+
55+
const childProcess = spawn(command, args);
56+
childProcess.stdout.on('data', (data: Buffer) => {
57+
console.log(`stdout: ${data.toString().split(/[\n\r]/g).join('\n ')}`);
58+
});
59+
childProcess.stderr.on('data', (data: Buffer) => {
60+
console.error(`stderr: ${data.toString().split(/[\n\r]/g).join('\n ')}`);
61+
});
4362

44-
process.chdir(componentPath);
45-
execSync(`npm publish --access public ${labelArg}`);
63+
childProcess.on('close', (code: number) => {
64+
if (code == 0) {
65+
resolve();
66+
} else {
67+
reject(new Error(`Component ${componentName} did not publish, status: ${code}.`));
68+
}
4669
});
47-
process.chdir(currentDir);
70+
});
71+
}
72+
73+
task(':publish', function(done: (err?: any) => void) {
74+
const label = argv['tag'];
75+
const currentDir = process.cwd();
76+
77+
if (!label) {
78+
console.log('You can use a label with --tag=labelName.');
79+
console.log('Publishing using the latest tag.');
80+
} else {
81+
console.log(`Publishing using the ${label} tag.`);
82+
}
83+
console.log('\n\n');
84+
85+
// Build a promise chain that publish each component.
86+
readdirSync(DIST_COMPONENTS_ROOT)
87+
.reduce((prev, dirName) => prev.then(() => _execNpmPublish(dirName, label)), Promise.resolve())
88+
.then(() => done())
89+
.catch((err: Error) => done(err))
90+
.then(() => process.chdir(currentDir));
4891
});
4992

5093
task('publish', function(done: () => void) {

0 commit comments

Comments
 (0)