Skip to content

Commit

Permalink
fix: drop conventional-changelog-cli and use its node library instead (
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunjae Lee authored Dec 11, 2019
1 parent c736c92 commit 430c303
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 90 deletions.
8 changes: 5 additions & 3 deletions packages/shipjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
"@babel/runtime": "^7.6.3",
"@octokit/rest": "^16.35.0",
"@slack/webhook": "^5.0.1",
"add-stream": "^1.0.0",
"arg": "4.1.2",
"chalk": "3.0.0",
"change-case": "4.1.0",
"conventional-changelog-cli": "2.0.28",
"conventional-changelog": "^3.1.15",
"dotenv": "^8.2.0",
"ejs": "^3.0.0",
"esm": "3.2.25",
Expand All @@ -52,7 +53,8 @@
"serialize-javascript": "^2.1.0",
"shell-quote": "^1.7.2",
"shipjs-lib": "0.11.2",
"temp-write": "4.0.0"
"temp-write": "4.0.0",
"tempfile": "^3.0.0"
},
"devDependencies": {
"@babel/core": "7.7.5",
Expand All @@ -70,4 +72,4 @@
"jest": "24.9.0",
"jest-watch-typeahead": "0.4.2"
}
}
}
16 changes: 2 additions & 14 deletions packages/shipjs/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import dotenv from 'dotenv';
import setup from './flow/setup';
import prepare from './flow/prepare';
import release from './flow/release';
import parseArgs from 'arg';
import { camelCase } from 'change-case';
import { bold } from './color';
import { print } from './util';
import { print, parseArgs } from './util';
import version from './version';

const flowMap = {
Expand All @@ -15,14 +13,6 @@ const flowMap = {
trigger: release,
};

function removeDoubleDash(opts) {
return Object.entries(opts).reduce((acc, [key, value]) => {
// eslint-disable-next-line no-param-reassign
acc[camelCase(key)] = value;
return acc;
}, {});
}

function printVersion() {
print(version);
print('');
Expand Down Expand Up @@ -53,9 +43,7 @@ export async function cli(argv) {
return;
}
try {
const opts = removeDoubleDash(
parseArgs(argSpec, { permissive: false, argv })
);
const opts = parseArgs(argSpec, argv);
dotenv.config({ path: path.resolve(opts.dir || '.', '.env') });
await fn(opts);
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/shipjs/src/flow/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async function prepare({
: updateVersion;
await updateVersionFn({ config, nextVersion, releaseType, dir, dryRun });
installDependencies({ config, dir, dryRun });
updateChangelog({
await updateChangelog({
config,
revisionRange,
firstRelease,
Expand Down
40 changes: 0 additions & 40 deletions packages/shipjs/src/step/prepare/__tests__/updateChangelog.spec.js

This file was deleted.

179 changes: 158 additions & 21 deletions packages/shipjs/src/step/prepare/updateChangelog.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import tempWrite from 'temp-write';
import runStep from '../runStep';
import path from 'path';
import { run } from '../../util';
import fs from 'fs';
import conventionalChangelog from 'conventional-changelog';
import tempfile from 'tempfile';
import addStream from 'add-stream';
import runStep from '../runStep';
import { parseArgs } from '../../util';

export default ({
config,
Expand All @@ -17,23 +20,157 @@ export default ({
skipIf: () => config.updateChangelog !== true,
},
() => {
const { conventionalChangelogArgs } = config;
const [from, to] = revisionRange.split('..');
const tempConfigPath = tempWrite.sync(
`module.exports = { gitRawCommitsOpts: { from: '${from}', to: '${to}' } }`
);
const args = [
conventionalChangelogArgs,
releaseCount ? `-r ${releaseCount}` : undefined,
firstRelease ? '-r 0' : undefined,
`-n ${tempConfigPath}`,
]
.filter(Boolean)
.join(' ');
const execPath = path.resolve(
require.main.filename,
'../../../../node_modules/.bin/conventional-changelog'
);
run({ command: `${execPath} ${args}`, dir, dryRun });
if (dryRun) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
const { conventionalChangelogArgs } = config;
const { args, gitRawCommitsOpts, templateContext } = prepareParams({
dir,
conventionalChangelogArgs,
releaseCount,
firstRelease,
revisionRange,
reject,
});
runConventionalChangelog({
args,
templateContext,
gitRawCommitsOpts,
resolve,
reject,
});
});
}
);

// The following implementation is mostly based on this file:
// https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-cli/cli.js
const argSpec = {
'--infile': String,
'--outfile': String,
'--same-file': Boolean,
'--preset': String,
'--pkg': String,
'--append': Boolean,
'--release-count': Number,
'--output-unreleased': Boolean,
'--verbose': Boolean,
'--config': String,
'--context': String,
'--lerna-package': String,
'--tag-prefix': String,
'--commit-path': String,

// Aliases
'-i': '--infile',
'-o': '--outfile',
'-s': '--same-file',
'-p': '--preset',
'-k': '--pkg',
'-a': '--append',
'-r': '--release-count',
'-u': '--output-unreleased',
'-v': '--verbose',
'-n': '--config',
'-c': '--context',
'-l': '--lerna-package',
'-t': '--tag-prefix',
};

function prepareParams({
dir,
conventionalChangelogArgs,
releaseCount,
firstRelease,
revisionRange,
reject,
}) {
const args = parseArgs(argSpec, (conventionalChangelogArgs || '').split(' '));
if (args.infile && args.infile === args.outfile) {
args.sameFile = true;
} else if (args.sameFile) {
if (args.infile) {
args.outfile = args.infile;
} else {
reject(new Error('infile must be provided if same-file flag presents.'));
return undefined;
}
}
args.infile = path.resolve(dir, args.infile);
args.outfile = path.resolve(dir, args.outfile);
if (releaseCount !== undefined) {
args.releaseCount = releaseCount;
}
if (firstRelease !== undefined) {
args.firstRelease = firstRelease;
}
args.pkg = {
path: args.pkg || dir,
};
const [from, to] = revisionRange.split('..');
const gitRawCommitsOpts = {
from,
to,
};
if (args.commitPath) {
gitRawCommitsOpts.path = args.commitPath;
}
const templateContext =
args.context && require(path.resolve(dir, args.context));
return { args, gitRawCommitsOpts, templateContext };
}

function runConventionalChangelog({
args,
templateContext,
gitRawCommitsOpts,
resolve,
reject,
}) {
const changelogStream = conventionalChangelog(
args,
templateContext,
gitRawCommitsOpts
).on('error', reject);

const readStream = fs.createReadStream(args.infile).on('error', reject);
if (args.sameFile) {
if (args.append) {
changelogStream
.pipe(
fs.createWriteStream(args.outfile, {
flags: 'a',
})
)
.on('finish', resolve);
} else {
const tmp = tempfile();

changelogStream
.pipe(addStream(readStream))
.pipe(fs.createWriteStream(tmp))
.on('finish', () => {
fs.createReadStream(tmp)
.pipe(fs.createWriteStream(args.outfile))
.on('finish', resolve);
});
}
} else {
let outStream;
if (args.outfile) {
outStream = fs.createWriteStream(args.outfile);
} else {
outStream = process.stdout;
}

let stream;
if (args.append) {
stream = readStream.pipe(addStream(changelogStream));
} else {
stream = changelogStream.pipe(addStream(readStream));
}

stream.pipe(outStream).on('finish', resolve);
}
}
1 change: 1 addition & 0 deletions packages/shipjs/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export {
} from './slack';
export { default as wrapExecWithDir } from './wrapExecWithDir';
export { default as wrapRun } from './wrapRun';
export { default as parseArgs } from './parseArgs';
14 changes: 14 additions & 0 deletions packages/shipjs/src/util/parseArgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import arg from 'arg';
import { camelCase } from 'change-case';

export default function parseArgs(argSpec, argv) {
return camelizeKeys(arg(argSpec, { permissive: false, argv }));
}

function camelizeKeys(opts) {
return Object.entries(opts).reduce((acc, [key, value]) => {
// eslint-disable-next-line no-param-reassign
acc[camelCase(key)] = value;
return acc;
}, {});
}
11 changes: 0 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3820,17 +3820,6 @@ conventional-changelog-atom@^2.0.3:
dependencies:
q "^1.5.1"

[email protected]:
version "2.0.28"
resolved "https://registry.yarnpkg.com/conventional-changelog-cli/-/conventional-changelog-cli-2.0.28.tgz#76c362cb238b356114310ad7242cce9b765fe100"
integrity sha512-ioagYxYI4GGs3p5APfEgSqjK21Fwl0mTa05MkIH85R64MqtUvNygtjHZWoXtdpM29OONJzxrebWDSyNHX6Fssg==
dependencies:
add-stream "^1.0.0"
conventional-changelog "^3.1.15"
lodash "^4.17.15"
meow "^5.0.0"
tempfile "^3.0.0"

conventional-changelog-codemirror@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/conventional-changelog-codemirror/-/conventional-changelog-codemirror-2.0.3.tgz#ebc088154684f8f5171446b8d546ba6b460d46f2"
Expand Down

0 comments on commit 430c303

Please sign in to comment.