|
1 | 1 | #!/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`). |
2 | 10 | const fs = require('fs');
|
3 | 11 | const path = require('path');
|
| 12 | +const shelljs = require('shelljs'); |
4 | 13 | const util = require('./util');
|
5 | 14 |
|
6 | 15 | const ORIG_DIR = process.cwd();
|
7 | 16 |
|
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'); |
11 | 69 | }
|
12 | 70 |
|
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 | + } |
15 | 80 | }
|
16 | 81 |
|
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 | + } |
24 | 86 |
|
25 | 87 | process.chdir(ORIG_DIR);
|
26 | 88 | }
|
|
0 commit comments