|
1 | 1 | #!/usr/bin/env node |
2 | 2 | "use strict"; |
3 | | -let conventionalChangelog = require('conventional-changelog'); |
| 3 | + |
| 4 | +const shelljs = require('shelljs'); |
| 5 | +const path = require('path'); |
| 6 | +const fs = require('fs'); |
| 7 | +const conventionalChangelog = require('conventional-changelog'); |
| 8 | +const _exec = require('./util')._exec; |
| 9 | +const yargs = require('yargs') |
| 10 | + .option('from', { |
| 11 | + description: 'The starting tag' |
| 12 | + }) |
| 13 | + .option('to', { |
| 14 | + description: 'The ending tag' |
| 15 | + }) |
| 16 | + .option('deps', { |
| 17 | + description: 'Include changelogs of dependencies', |
| 18 | + array: true, |
| 19 | + }) |
| 20 | + .check(argv => { |
| 21 | + if (argv.to && !argv.from) |
| 22 | + throw new Error(`If you specify a 'to', you should also specify a 'from'.`); |
| 23 | + return true; |
| 24 | + }); |
4 | 25 |
|
5 | 26 | let options = { |
6 | 27 | preset: 'ui-router-core' |
7 | 28 | }; |
8 | 29 |
|
9 | | -if(require.main === module) { |
10 | | - let context, gitOpts; |
| 30 | +const context = {}, gitOpts = {}; |
| 31 | +const to = yargs.argv.to; |
| 32 | +const from = yargs.argv.from; |
| 33 | +const deps = yargs.argv.deps || []; |
| 34 | +const scriptPath = path.resolve(__dirname, __filename); |
| 35 | + |
| 36 | +if (to && from) { |
| 37 | + gitOpts.to = context.currentTag = to; |
| 38 | + gitOpts.from = context.previousTag = from; |
| 39 | +} else if (from) { |
| 40 | + gitOpts.from = context.previousTag = from; |
| 41 | +} else if (!to && !from) { |
| 42 | + gitOpts.from = context.previousTag = getVersion('current'); |
| 43 | +} else { |
| 44 | + throw new Error('How did this happen?'); |
| 45 | +} |
| 46 | + |
| 47 | +const cwd = shelljs.pwd().stdout; |
| 48 | + |
| 49 | +if (deps.length) { |
| 50 | + // If --deps was used, shell out and re-run the show_changelog command without the --deps argument |
| 51 | + // This is an awful hack to flush the changelog to stdout before getting the dependency changelog(s) |
| 52 | + // because conventional-changelog-core doesn't seem to have a callback to tap into |
| 53 | + const fromArg = (from ? ` --from ${from}` : ''); |
| 54 | + const toArg = (to ? ` --to ${to}` : ''); |
| 55 | + let stdout = _exec(`${scriptPath} ${fromArg} ${toArg}`, true).stdout; |
| 56 | + console.log(stdout.trim()); |
11 | 57 |
|
12 | | - if (process.argv[2]) { |
13 | | - context = {}; |
14 | | - gitOpts = {}; |
15 | | - gitOpts.from = context.previousTag = process.argv[2]; |
| 58 | + shelljs.mkdir('.show_changelog.tmp'); |
| 59 | + try { |
| 60 | + deps.forEach(showDepChangelog); |
| 61 | + } finally { |
| 62 | + shelljs.cd(cwd); |
| 63 | + shelljs.rm('-rf', '.show_changelog.tmp'); |
16 | 64 | } |
| 65 | +} else { |
| 66 | + showChangelog(context, gitOpts); |
| 67 | +} |
| 68 | + |
| 69 | +function showDepChangelog(dependency) { |
| 70 | + if (typeof dependency !== 'string') throw new Error('Expected dep to be a string: ' + dependency); |
| 71 | + |
| 72 | + const tmpdir = path.resolve(cwd, '.show_changelog.tmp', dependency.replace(/[^a-zA-Z]/g, "_")); |
17 | 73 |
|
18 | | - if (process.argv[3]) { |
19 | | - gitOpts.to = context.currentTag = process.argv[3]; |
| 74 | + const pkgPath = `${cwd}/node_modules/${dependency}/package.json`; |
| 75 | + const pkg = JSON.parse(fs.readFileSync(pkgPath)); |
| 76 | + const repotype = pkg.repository && pkg.repository.type; |
| 77 | + let giturl = pkg.repository && pkg.repository.url; |
| 78 | + |
| 79 | + if (repotype !== 'git') { |
| 80 | + throw new Error(`Expected repository.type to be 'git' in ${pkgPath} but it was '${repotype}'`); |
20 | 81 | } |
21 | 82 |
|
22 | | - showChangelog(context, gitOpts); |
| 83 | + if (!giturl) { |
| 84 | + throw new Error(`Expected repository.url to be defined in ${pkgPath} `); |
| 85 | + } |
| 86 | + |
| 87 | + giturl = giturl.replace(/^git\+/, ''); |
| 88 | + |
| 89 | + const from = getDepVersion(dependency, 'tag', gitOpts.from); |
| 90 | + const to = (function() { |
| 91 | + if (gitOpts.to) return getDepVersion(dependency, 'tag', gitOpts.to); |
| 92 | + return getDepVersion(dependency, 'workingcopy'); |
| 93 | + }()); |
| 94 | + |
| 95 | + if (from === to) return; |
| 96 | + |
| 97 | + try { |
| 98 | + _exec(`git clone ${giturl} ${tmpdir}`, true); |
| 99 | + shelljs.config.silent = true; |
| 100 | + shelljs.pushd(tmpdir); |
| 101 | + shelljs.config.silent = false; |
| 102 | + console.log(`\n`); |
| 103 | + console.log(`### Updated \`${dependency}\` from ${from} to ${to}`); |
| 104 | + let depChangelog = _exec(`node ${scriptPath} --from ${from} --to ${to}`, true).stdout.trim(); |
| 105 | + console.log(depChangelog.split(/[\r\n]/).slice(1).join('\n')); |
| 106 | + } finally { |
| 107 | + shelljs.popd(); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// mode: 'workingcopy', 'current', 'previous', 'tag' |
| 112 | +// tag: (optional) the tag to fetch from |
| 113 | +function getVersion(mode, tag) { |
| 114 | + const showversion = path.resolve(__dirname, 'show_version.js'); |
| 115 | + return _exec(`node ${showversion} --${mode} ${tag || ''}`, true).stdout.replace(/[\r\n]/g, ''); |
| 116 | +} |
| 117 | + |
| 118 | +// dep: the dependency |
| 119 | +// mode: 'workingcopy', 'current', 'previous', 'tag' |
| 120 | +// tag: (optional) the tag to fetch from |
| 121 | +function getDepVersion(dep, mode, tag) { |
| 122 | + const showversion = path.resolve(__dirname, 'show_version.js'); |
| 123 | + return _exec(`node ${showversion} --dep ${dep} --${mode} ${tag || ''}`, true).stdout.replace(/[\r\n]/g, ''); |
23 | 124 | } |
24 | 125 |
|
25 | 126 | function showChangelog(context, gitOpts) { |
26 | | - var writerOpts = { doFlush: true, generateOn: function() { return false; } }; |
27 | | - conventionalChangelog(options, context, gitOpts, undefined, writerOpts).pipe(process.stdout); |
| 127 | + var writerOpts = { |
| 128 | + doFlush: true, |
| 129 | + generateOn: function () { |
| 130 | + return false; |
| 131 | + } |
| 132 | + }; |
| 133 | + const readable = conventionalChangelog(options, context, gitOpts, undefined, writerOpts).pipe(process.stdout); |
| 134 | + return new Promise(resolve => readable.on('end', resolve)); |
28 | 135 | } |
0 commit comments