Skip to content

Commit 8a38dfb

Browse files
feat(publish_yalc): Support fetching from local directories (not github) and wire publish_yalc logic into test downstream script
1 parent 030e30c commit 8a38dfb

File tree

2 files changed

+105
-38
lines changed

2 files changed

+105
-38
lines changed

publish_yalc_package.js

+74-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,88 @@
11
#!/usr/bin/env node
2+
3+
// This script is intended to fetch a package from a git url and publish it locally using the npm package `yalc`.
4+
// That package can then be installed from the local yalc registry an tested against by some downstream project.
5+
// This script can optionally fetch from a local directory instead of a git repository.
6+
7+
// The script leaves the files in place after publishing.
8+
// This can be handy in Travis which can cache the directory contents.
9+
// After caching, subsequent re-runs of this script won't have to fetch the git repository (or do much for `yarn install`).
210
const fs = require('fs');
311
const path = require('path');
12+
const shelljs = require('shelljs');
413
const util = require('./util');
514

615
const ORIG_DIR = process.cwd();
716

8-
function publishYalcPackage(installDir, githubUrl) {
9-
if (!installDir || !githubUrl) {
10-
throw new Error('Usage: publish_yalc_package [INSTALL_DIR] [GITHUB_URL]');
17+
function publishYalcPackage(installTargetDir, installSource, flags) {
18+
flags = flags || {};
19+
if (!installTargetDir || !installSource) {
20+
throw new Error('Usage: publish_yalc_package [INSTALL_DIR] [GITHUB_URL|LOCAL_DIR]');
21+
}
22+
23+
installTargetDir = path.resolve(installTargetDir);
24+
const isRemoteSource = !/^\./.exec(installSource);
25+
const installTargetDirTmp = path.join(installTargetDir, ".tmp");
26+
const installSourceDir = isRemoteSource ? null : path.resolve(installSource);
27+
28+
// Create directory and clone git repo
29+
if (!fs.existsSync(path.join(installTargetDir, '.git'))) {
30+
if (isRemoteSource) {
31+
util._exec(`git clone --depth 3 ${installSource} ${installTargetDir}`);
32+
} else {
33+
shelljs.rm('-rf', installTargetDir);
34+
shelljs.cp('-r', installSourceDir, installTargetDir);
35+
process.chdir(installTargetDir);
36+
util._exec(`git init && git add . && git commit -m "initial commit"`);
37+
process.chdir(ORIG_DIR);
38+
}
39+
}
40+
41+
// Update git repo from source and make working copy match exactly
42+
if (isRemoteSource) {
43+
process.chdir(installTargetDir);
44+
util._exec('git fetch origin');
45+
util._exec('git reset --hard origin/master');
46+
util._exec('git clean --force -d');
47+
} else {
48+
// Create a tmp dir with a copy of the current package contents
49+
shelljs.rm('-rf', installTargetDirTmp);
50+
shelljs.cp('-r', installSourceDir, installTargetDirTmp);
51+
52+
// Copy the current .git metadata from the cache dir into the tmp dir
53+
shelljs.cp('-r', path.join(installTargetDir, ".git"), installTargetDirTmp);
54+
process.chdir(installTargetDirTmp);
55+
56+
// Commit the changes from the current package (if any)
57+
util._exec('git add . && git diff --staged --quiet || git commit -m "update from source directory"');
58+
59+
process.chdir(installTargetDir);
60+
61+
// Move the new .git metadata back into the cache dir
62+
shelljs.rm('-rf', '.git');
63+
shelljs.mv(path.join('.tmp', '.git'), ".");
64+
shelljs.rm('-rf', installTargetDirTmp);
65+
66+
// Update the cache to match the current package contents
67+
util._exec('git reset --hard master');
68+
util._exec('git clean --force -d');
1169
}
1270

13-
if (!fs.existsSync(path.join(installDir, '.git'))) {
14-
util._exec('git clone '+ githubUrl + ' ' + installDir);
71+
// Update dependencies
72+
util._exec('yarn install --check-files');
73+
74+
if (!flags.noBuild) {
75+
// Build package
76+
const pkgJson = JSON.parse(fs.readFileSync('package.json'));
77+
if (pkgJson.scripts && pkgJson.scripts.build) {
78+
util._exec('npm run build');
79+
}
1580
}
1681

17-
process.chdir(installDir);
18-
util._exec('git fetch origin');
19-
util._exec('git reset --hard origin/master');
20-
util._exec('git clean --force -d');
21-
util._exec('yarn --check-files');
22-
util._exec('npm run build');
23-
util._exec('yalc publish');
82+
if (!flags.noPublish) {
83+
// Publish to local yalc registry
84+
util._exec('yalc publish');
85+
}
2486

2587
process.chdir(ORIG_DIR);
2688
}

test_downstream_projects.js

+31-26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const fs = require('fs');
33
const path = require('path');
44

5+
const publishYalcPackage = require('./publish_yalc_package');
56
const util = require('./util');
67
util.packageDir();
78
const PKG_DIR = process.cwd();
@@ -16,20 +17,12 @@ const DOWNSTREAM_PKGS = (process.env.DOWNSTREAM_PKGS || '').split(',').filter(x
1617
function forEachDownstream(callback) {
1718
Object.keys(config).forEach(key => {
1819
if (DOWNSTREAM_PKGS.length && DOWNSTREAM_PKGS.indexOf(key) === -1) {
19-
console.log(key + ' not in DOWNSTREAM_PKGS, skipping...');
20+
console.log(callback.constructor.name + ": " + key + ' not in DOWNSTREAM_PKGS, skipping...');
2021
return;
2122
}
2223

23-
const projectPath = path.resolve(DOWNSTREAMS_PATH, key);
24-
if (!fs.existsSync(projectPath)) {
25-
process.chdir(DOWNSTREAMS_PATH);
26-
const giturl = config[key];
27-
console.log('cloning ' + giturl);
28-
util._exec('git clone '+ giturl + ' ' + key);
29-
}
30-
process.chdir(projectPath);
31-
32-
callback();
24+
process.chdir(path.resolve(DOWNSTREAMS_PATH, key));
25+
callback(key, config[key]);
3326
});
3427
}
3528

@@ -47,20 +40,16 @@ function localPublish() {
4740
util._exec('yalc publish');
4841
}
4942

50-
function getCleanMaster() {
51-
console.log('cleaning ' + process.cwd());
52-
util._exec('git fetch origin');
53-
util._exec('git reset --hard origin/master');
54-
util._exec('git clean --force -d');
55-
}
56-
57-
function revertLocalChanges() {
58-
util._exec('git reset --hard origin/master');
59-
util._exec('git clean --force -d');
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+
});
6050
}
6151

62-
function installDeps() {
63-
util._exec('yarn install --check-files');
52+
function installUpstreamDeps() {
6453
UPSTREAM_PKGS.forEach(upstream => util._exec('yalc add ' + upstream));
6554
}
6655

@@ -75,11 +64,27 @@ function runTests() {
7564
}
7665
}
7766

67+
function revertLocalChanges(key, source) {
68+
const isRemoteSource = source[0] !== '.';
69+
const ref = isRemoteSource ? 'origin/master' : 'master';
70+
util._exec(`git reset --hard ${ref}`);
71+
util._exec('git clean --force -d');
72+
}
73+
74+
console.log(` ===> Making working copy <===`);
7875
makeWorkingCopy();
76+
77+
console.log(` ===> Publishing ${pkgjson.name} to yalc registry <===`);
7978
localPublish();
8079

81-
forEachDownstream(getCleanMaster);
82-
forEachDownstream(installDeps);
80+
console.log(` ===> Fetching downstream projects and their dependencies <===`);
81+
initializeDownstreams();
82+
83+
console.log(` ===> Installing freshly built upstream packages <===`);
84+
forEachDownstream(installUpstreamDeps);
85+
86+
console.log(` ===> Running downstream tests <===`);
8387
forEachDownstream(runTests);
84-
forEachDownstream(revertLocalChanges);
8588

89+
console.log(` ===> Cleaning downstream projects <===`);
90+
forEachDownstream(revertLocalChanges);

0 commit comments

Comments
 (0)