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' ;
3
3
import { task } from 'gulp' ;
4
4
import gulpRunSequence = require( 'run-sequence' ) ;
5
5
import path = require( 'path' ) ;
6
+ import minimist = require( 'minimist' ) ;
6
7
7
8
import { execTask } from '../task_helpers' ;
8
9
import { DIST_COMPONENTS_ROOT } from '../constants' ;
9
10
11
+ const argv = minimist ( process . argv . slice ( 3 ) ) ;
12
+
10
13
11
14
task ( 'build:release' , function ( done : ( ) => void ) {
12
15
// Synchronously run those tasks.
@@ -27,24 +30,64 @@ task(':publish:whoami', execTask('npm', ['whoami'], {
27
30
28
31
task ( ':publish:logout' , execTask ( 'npm' , [ 'logout' ] ) ) ;
29
32
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 ( ) ;
34
33
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 ) ;
39
37
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
+ } ) ;
43
62
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
+ }
46
69
} ) ;
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 ) ) ;
48
91
} ) ;
49
92
50
93
task ( 'publish' , function ( done : ( ) => void ) {
0 commit comments