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' ;
33import { task } from 'gulp' ;
44import gulpRunSequence = require( 'run-sequence' ) ;
55import path = require( 'path' ) ;
6+ import minimist = require( 'minimist' ) ;
67
78import { execTask } from '../task_helpers' ;
89import { DIST_COMPONENTS_ROOT } from '../constants' ;
910
11+ const argv = minimist ( process . argv . slice ( 3 ) ) ;
12+
1013
1114task ( 'build:release' , function ( done : ( ) => void ) {
1215 // Synchronously run those tasks.
@@ -27,24 +30,64 @@ task(':publish:whoami', execTask('npm', ['whoami'], {
2730
2831task ( ':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
5093task ( 'publish' , function ( done : ( ) => void ) {
0 commit comments