Skip to content

Commit d9201a1

Browse files
feat(test_downstrem_projects): add support for grouping downstream projects in the config file
1 parent 37cdcb0 commit d9201a1

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

test_downstream_projects.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ const fs = require('fs');
33
const path = require('path');
44
const tmp = require('tmp');
55
const shelljs = require('shelljs');
6+
const _ = require('lodash');
67
const IS_TRAVIS = !!process.env.TRAVIS;
78

89
const yargs = require('yargs')
10+
.option('group', {
11+
alias: 'g',
12+
default: 'all',
13+
description: 'the group of projects to test (from downstream_projects.json "group" key)',
14+
})
915
.option('workspace', {
1016
alias: 'ws',
1117
description: 'use yarn workspace to save space',
@@ -24,19 +30,39 @@ const util = require('./util');
2430
util.packageDir();
2531
const PKG_DIR = process.cwd();
2632

27-
const config = JSON.parse(fs.readFileSync('downstream_projects.json'));
2833
const pkgjson = JSON.parse(fs.readFileSync('package.json'));
29-
const projects = config.projects || config;
30-
const nohoist = (config.projects && config.nohoist) || [];
31-
3234
const DOWNSTREAM_PKGS = (process.env.DOWNSTREAM_PKGS || '').split(',').filter(x => x);
33-
const UPSTREAM_PKGS = (process.env.UPSTREAM_PKGS || '').split(',').filter(x => x);
3435

3536
const TEMP = tmp.dirSync();
3637
const TEMP_DIR = TEMP.name;
3738
const TEMP_DOWNSTREAM_CACHE = path.resolve(TEMP_DIR, '.downstream_cache');
3839
const DOWNSTREAM_CACHE = path.resolve(PKG_DIR, '.downstream_cache');
3940

41+
function parseConfig() {
42+
const config = JSON.parse(fs.readFileSync('downstream_projects.json'));
43+
const configBlock = _.toPairs(config.projects || config);
44+
45+
// Object values are groups (nested config). string values are github url or local file path
46+
const isGroup = ([key, value]) => typeof value === 'object';
47+
const groupsAsPairs = configBlock.filter(pair => isGroup(pair));
48+
const ungroupedProjectsAsPairs = configBlock.filter(pair => !isGroup(pair));
49+
50+
const allGroupedProjectPairs = _.flatten(groupsAsPairs.map(([name, groupedProjects]) => _.toPairs(groupedProjects)));
51+
52+
const groups = _.fromPairs(groupsAsPairs);
53+
groups.all = _.fromPairs(allGroupedProjectPairs.concat(ungroupedProjectsAsPairs));
54+
55+
const projects = groups[yargs.argv.group];
56+
if (!projects) {
57+
throw new Error(`Attempting to run tests for a group named ${yargs.argv.group}, but no matching group was found in downstream_projects.json`);
58+
}
59+
60+
const nohoist = (config.projects && config.nohoist) || [];
61+
return { projects, nohoist };
62+
}
63+
64+
const { projects, nohoist } = parseConfig();
65+
4066
function makeDownstreamCache() {
4167
if (!fs.existsSync(DOWNSTREAM_CACHE)) {
4268
console.log(' ===> making .downstream_cache working directory <===');

0 commit comments

Comments
 (0)