11#!/usr/bin/env node
22const fs = require ( 'fs' ) ;
33const path = require ( 'path' ) ;
4+ const tmp = require ( 'tmp' ) ;
5+ const shelljs = require ( 'shelljs' ) ;
46
57const publishYalcPackage = require ( './publish_yalc_package' ) ;
68const util = require ( './util' ) ;
@@ -10,7 +12,10 @@ const PKG_DIR = process.cwd();
1012const config = JSON . parse ( fs . readFileSync ( 'downstream_projects.json' ) ) ;
1113const pkgjson = JSON . parse ( fs . readFileSync ( 'package.json' ) ) ;
1214
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+
1419const UPSTREAM_PKGS = ( process . env . UPSTREAM_PKGS || '' ) . split ( ',' ) . filter ( x => x ) . concat ( pkgjson . name ) ;
1520const DOWNSTREAM_PKGS = ( process . env . DOWNSTREAM_PKGS || '' ) . split ( ',' ) . filter ( x => x ) ;
1621
@@ -21,16 +26,15 @@ function forEachDownstream(callback) {
2126 return ;
2227 }
2328
24- process . chdir ( path . resolve ( DOWNSTREAMS_PATH , key ) ) ;
29+ process . chdir ( path . resolve ( DOWNSTREAM_CACHE , key ) ) ;
2530 callback ( key , config [ key ] ) ;
2631 } ) ;
2732}
2833
29- function makeWorkingCopy ( ) {
30- process . chdir ( PKG_DIR ) ;
31- if ( ! fs . existsSync ( DOWNSTREAMS_PATH ) ) {
34+ function makeDownstreamCache ( ) {
35+ if ( ! fs . existsSync ( DOWNSTREAM_CACHE ) ) {
3236 console . log ( 'making .downstream_cache working directory' ) ;
33- fs . mkdirSync ( DOWNSTREAMS_PATH ) ;
37+ fs . mkdirSync ( DOWNSTREAM_CACHE ) ;
3438 }
3539}
3640
@@ -40,15 +44,6 @@ function localPublish() {
4044 util . _exec ( 'yalc publish' ) ;
4145}
4246
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-
5247function installUpstreamDeps ( ) {
5348 UPSTREAM_PKGS . forEach ( upstream => util . _exec ( 'yalc add ' + upstream ) ) ;
5449}
@@ -64,27 +59,56 @@ function runTests() {
6459 }
6560}
6661
67- function revertLocalChanges ( key , source ) {
62+ function revertLocalChanges ( source ) {
6863 const isRemoteSource = source [ 0 ] !== '.' ;
6964 const ref = isRemoteSource ? 'origin/master' : 'master' ;
7065 util . _exec ( `git reset --hard ${ ref } ` ) ;
7166 util . _exec ( 'git clean --force -d' ) ;
7267}
7368
74- console . log ( ` ===> Making working copy <===` ) ;
75- makeWorkingCopy ( ) ;
69+ try {
70+ console . log ( ` ===> Making .downstream_cache working directory <===` ) ;
71+ makeDownstreamCache ( ) ;
7672
77- console . log ( ` ===> Publishing ${ pkgjson . name } to yalc registry <===` ) ;
78- localPublish ( ) ;
73+ console . log ( ` ===> Publishing ${ pkgjson . name } to yalc registry <===` ) ;
74+ localPublish ( ) ;
7975
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+ }
8281
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 ) ;
8590
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+ }
88107
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