1
1
#!/usr/bin/env node
2
2
const fs = require ( 'fs' ) ;
3
3
const path = require ( 'path' ) ;
4
+ const tmp = require ( 'tmp' ) ;
5
+ const shelljs = require ( 'shelljs' ) ;
4
6
5
7
const publishYalcPackage = require ( './publish_yalc_package' ) ;
6
8
const util = require ( './util' ) ;
@@ -10,7 +12,10 @@ const PKG_DIR = process.cwd();
10
12
const config = JSON . parse ( fs . readFileSync ( 'downstream_projects.json' ) ) ;
11
13
const pkgjson = JSON . parse ( fs . readFileSync ( 'package.json' ) ) ;
12
14
13
- const DOWNSTREAMS_PATH = path . resolve ( PKG_DIR , '.downstream_cache' ) ;
15
+ const DOWNSTREAM_CACHE = path . resolve ( PKG_DIR , '.downstream_cache' ) ;
16
+ const TEMP = tmp . dirSync ( ) ;
17
+ const TEMP_DIR = TEMP . name ;
18
+
14
19
const UPSTREAM_PKGS = ( process . env . UPSTREAM_PKGS || '' ) . split ( ',' ) . filter ( x => x ) . concat ( pkgjson . name ) ;
15
20
const DOWNSTREAM_PKGS = ( process . env . DOWNSTREAM_PKGS || '' ) . split ( ',' ) . filter ( x => x ) ;
16
21
@@ -21,16 +26,15 @@ function forEachDownstream(callback) {
21
26
return ;
22
27
}
23
28
24
- process . chdir ( path . resolve ( DOWNSTREAMS_PATH , key ) ) ;
29
+ process . chdir ( path . resolve ( DOWNSTREAM_CACHE , key ) ) ;
25
30
callback ( key , config [ key ] ) ;
26
31
} ) ;
27
32
}
28
33
29
- function makeWorkingCopy ( ) {
30
- process . chdir ( PKG_DIR ) ;
31
- if ( ! fs . existsSync ( DOWNSTREAMS_PATH ) ) {
34
+ function makeDownstreamCache ( ) {
35
+ if ( ! fs . existsSync ( DOWNSTREAM_CACHE ) ) {
32
36
console . log ( 'making .downstream_cache working directory' ) ;
33
- fs . mkdirSync ( DOWNSTREAMS_PATH ) ;
37
+ fs . mkdirSync ( DOWNSTREAM_CACHE ) ;
34
38
}
35
39
}
36
40
@@ -40,15 +44,6 @@ function localPublish() {
40
44
util . _exec ( 'yalc publish' ) ;
41
45
}
42
46
43
- function initializeDownstreams ( ) {
44
- Object . keys ( config ) . forEach ( key => {
45
- const installTargetDir = path . resolve ( DOWNSTREAMS_PATH , key ) ;
46
- const installSource = config [ key ] ;
47
- const flags = { noBuild : true , noPublish : true } ;
48
- publishYalcPackage ( installTargetDir , installSource , flags ) ;
49
- } ) ;
50
- }
51
-
52
47
function installUpstreamDeps ( ) {
53
48
UPSTREAM_PKGS . forEach ( upstream => util . _exec ( 'yalc add ' + upstream ) ) ;
54
49
}
@@ -64,27 +59,56 @@ function runTests() {
64
59
}
65
60
}
66
61
67
- function revertLocalChanges ( key , source ) {
62
+ function revertLocalChanges ( source ) {
68
63
const isRemoteSource = source [ 0 ] !== '.' ;
69
64
const ref = isRemoteSource ? 'origin/master' : 'master' ;
70
65
util . _exec ( `git reset --hard ${ ref } ` ) ;
71
66
util . _exec ( 'git clean --force -d' ) ;
72
67
}
73
68
74
- console . log ( ` ===> Making working copy <===` ) ;
75
- makeWorkingCopy ( ) ;
69
+ try {
70
+ console . log ( ` ===> Making .downstream_cache working directory <===` ) ;
71
+ makeDownstreamCache ( ) ;
76
72
77
- console . log ( ` ===> Publishing ${ pkgjson . name } to yalc registry <===` ) ;
78
- localPublish ( ) ;
73
+ console . log ( ` ===> Publishing ${ pkgjson . name } to yalc registry <===` ) ;
74
+ localPublish ( ) ;
79
75
80
- console . log ( ` ===> Fetching downstream projects and their dependencies <===` ) ;
81
- initializeDownstreams ( ) ;
76
+ Object . keys ( config ) . forEach ( key => {
77
+ if ( DOWNSTREAM_PKGS . length && DOWNSTREAM_PKGS . indexOf ( key ) === - 1 ) {
78
+ console . log ( callback . constructor . name + ": " + key + ' not in DOWNSTREAM_PKGS, skipping...' ) ;
79
+ return ;
80
+ }
82
81
83
- console . log ( ` ===> Installing freshly built upstream packages <===` ) ;
84
- forEachDownstream ( installUpstreamDeps ) ;
82
+ const DOWNSTREAM_PACKAGE_DIR = path . resolve ( DOWNSTREAM_CACHE , key ) ;
83
+ process . chdir ( PKG_DIR ) ;
84
+
85
+ console . log ( ` ===> Fetching downstream project '${ key } ' and its dependencies <===` ) ;
86
+ const installTargetDir = DOWNSTREAM_PACKAGE_DIR ;
87
+ const installSource = config [ key ] ;
88
+ const flags = { noBuild : true , noPublish : true } ;
89
+ publishYalcPackage ( installTargetDir , installSource , flags ) ;
85
90
86
- console . log ( ` ===> Running downstream tests <===` ) ;
87
- forEachDownstream ( runTests ) ;
91
+ console . log ( ` ===> Installing freshly built upstream packages <===` ) ;
92
+ process . chdir ( DOWNSTREAM_PACKAGE_DIR ) ;
93
+ installUpstreamDeps ( ) ;
94
+
95
+ const DOWNSTREAM_PACKAGE_TEMP_DIR = path . resolve ( TEMP_DIR , path . basename ( DOWNSTREAM_PACKAGE_DIR ) ) ;
96
+ try {
97
+ console . log ( ` ===> Moving downstream project '${ key } ' to temp dir '${ TEMP_DIR } ' <===` ) ;
98
+ shelljs . mv ( DOWNSTREAM_PACKAGE_DIR , TEMP_DIR ) ;
99
+
100
+ console . log ( ` ===> Running downstream tests <===` ) ;
101
+ process . chdir ( DOWNSTREAM_PACKAGE_TEMP_DIR ) ;
102
+ runTests ( ) ;
103
+ } finally {
104
+ console . log ( ` ===> Moving downstream project '${ key } ' back from temp dir <===` ) ;
105
+ shelljs . mv ( DOWNSTREAM_PACKAGE_TEMP_DIR , DOWNSTREAM_CACHE ) ;
106
+ }
88
107
89
- console . log ( ` ===> Cleaning downstream projects <===` ) ;
90
- forEachDownstream ( revertLocalChanges ) ;
108
+ console . log ( ` ===> Cleaning downstream project '${ key } ' <===` ) ;
109
+ process . chdir ( DOWNSTREAM_PACKAGE_DIR ) ;
110
+ revertLocalChanges ( installSource ) ;
111
+ } ) ;
112
+ } finally {
113
+ shelljs . rm ( '-rf' , TEMP_DIR )
114
+ }
0 commit comments