Skip to content

Commit a6ce2ce

Browse files
feat(test_downstream_projects): Add command line toggle for yarn workspaces
1 parent dc56ebb commit a6ce2ce

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

test_downstream_projects.js

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ const path = require('path');
44
const tmp = require('tmp');
55
const shelljs = require('shelljs');
66

7+
const yargs = require('yargs')
8+
.option('workspace', {
9+
alias: 'ws',
10+
description: 'use yarn workspace to save space',
11+
});
12+
713
const nodeCleanup = require('node-cleanup');
814
const publishYalcPackage = require('./publish_yalc_package');
915

@@ -31,8 +37,8 @@ function makeDownstreamCache() {
3137
function localPublish(packageDir) {
3238
packageDir = packageDir || PKG_DIR;
3339
process.chdir(packageDir);
34-
console.log('Publishing using yalc...');
35-
util._exec('yalc publish');
40+
console.log(`Building ${packageDir} and publishing using yalc...`);
41+
util._exec('yarn build && yalc publish');
3642
}
3743

3844
function installUpstreamDeps(upstreamPackages) {
@@ -84,7 +90,7 @@ function getDownstreamInstallDirs(downstreamTreeNode) {
8490
.filter(x => !!x);
8591
}
8692

87-
function installDependencies(downstreamInstallDirs) {
93+
function installWorkspaceDependencies(downstreamInstallDirs) {
8894
const yarnWorkspacePackageJsonPath = path.resolve(DOWNSTREAM_CACHE, "package.json");
8995
const yarnWorkspacePackageJson = {
9096
private: true,
@@ -102,20 +108,47 @@ function installDependencies(downstreamInstallDirs) {
102108
util._exec('yarn && yarn upgrade');
103109
}
104110

105-
function runDownstreamTests(key, upstreamPackages, downstreamTreeNode) {
111+
function runDownstreamTests(key, upstreamPackages, downstreamTreeNode, successLog) {
106112
if (DOWNSTREAM_PKGS.length && DOWNSTREAM_PKGS.indexOf(key) === -1) {
107113
console.log(`${key} not in DOWNSTREAM_PKGS, skipping...`);
108114
return;
109115
}
110116

111117
process.chdir(TEMP_DOWNSTREAM_CACHE);
112118

113-
console.log(` ===> Running '${key}' tests <===`);
114-
console.log(` ===> Installing freshly built upstream packages <===`);
119+
const name = downstreamTreeNode.installDir;
120+
121+
console.log(` ===> '${name}': prepping tests <===`);
115122
process.chdir(downstreamTreeNode.installDir);
123+
124+
if (!yargs.argv.workspace) {
125+
console.log(` ===> '${name}': Installing dependencies <===`);
126+
util._exec('yarn && yarn upgrade');
127+
}
128+
129+
console.log(` ===> '${name}': Installing freshly built upstream packages <===`);
116130
installUpstreamDeps(upstreamPackages);
131+
132+
console.log(` ===> '${name}': Running tests <===`);
117133
runTests();
134+
135+
successLog.push(key);
136+
137+
console.log(` ===> '${name}': Reverting working copy <===`);
118138
revertLocalChanges(downstreamTreeNode.installSource);
139+
140+
141+
const downstreamChildren = Object.keys(downstreamTreeNode.children || {});
142+
if (downstreamChildren.length) {
143+
const thisPkg = JSON.parse(fs.readFileSync('package.json')).name;
144+
const upstreams = upstreamPackages.concat(thisPkg);
145+
146+
localPublish(process.cwd());
147+
148+
downstreamChildren.forEach(child => {
149+
runDownstreamTests(child, upstreams, downstreamTreeNode.children[child], successLog);
150+
});
151+
}
119152
}
120153

121154
console.log(` ===> Creating .downstream_cache working directory <===`);
@@ -128,19 +161,23 @@ console.log(` ===> Fetching downstream projects <===`);
128161
const tree = { children: {} };
129162
fetchDownstreamProjects(config, "", tree.children);
130163

131-
console.log(` ===> Installing downstream dependencies <===`);
132-
const downstreamDirs = getDownstreamInstallDirs(tree);
133-
installDependencies(downstreamDirs);
164+
if (yargs.argv.workspace) {
165+
console.log(` ===> Installing downstream dependencies <===`);
166+
const downstreamDirs = getDownstreamInstallDirs(tree);
167+
installWorkspaceDependencies(downstreamDirs);
168+
}
134169

135170
console.log(` ===> Moving working directory to temp dir ${TEMP_DIR} <===`);
136171
shelljs.mv(DOWNSTREAM_CACHE, TEMP_DIR);
137172

173+
const successLog = [];
138174
nodeCleanup(() => {
139175
shelljs.mv(TEMP_DOWNSTREAM_CACHE, PKG_DIR);
176+
console.log("Successfully ran downstream tests for: " + successLog.join(', '));
140177
});
141178

142179
console.log(` ===> Running downstream tests <===`);
143180
Object.keys(tree.children).forEach(key => {
144-
console.log(` ===> Running tests for '${key}' <===`);
145-
runDownstreamTests(key, [pkgjson.name], tree.children[key]);
181+
runDownstreamTests(key, [pkgjson.name], tree.children[key], successLog);
146182
});
183+

0 commit comments

Comments
 (0)