Skip to content

Commit

Permalink
fix(cli): move process exit to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinzZH committed Oct 10, 2018
1 parent 8b21c03 commit 40465a2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
36 changes: 26 additions & 10 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
const program = require('commander');
const path = require('path');
const signale = require('signale');
const main = require('./src/main.js');
const mainFn = require('./src/main.js');
const cwd = process.cwd();
const {
version
} = require('./package');

program.version(version);

program.option('-s, --source <source>', 'Path for source code');
program.option('-o, --only', 'Print only');
program.option('-r, --repository <repository>', 'Path for repository');
program.option('-h, --head', 'Enable changelog from HEAD');
program.option('-p, --path <path>', 'Path for changelog.md(default to root of source code), e.g. xxx.md');
program.option('-p, --path <path>', 'Path for changelog.md(default to root of repository), e.g. xxx.md');
program.option('-l, --list', 'List the changelog between tags');

program.parse(process.argv);

let source = cwd;
let repository = cwd;
let logFile = cwd;

if (program.source) {
source = path.resolve(cwd, program.source);
if (program.repository) {
repository = path.resolve(cwd, program.repository);
}

if (program.path) {
Expand All @@ -37,14 +38,29 @@ if (program.path) {
logFile = path.join(logFile, './changeLog.md');
}
} else {
logFile = path.resolve(source, './changeLog.md');
logFile = path.resolve(repository, './changeLog.md');
}

signale.note(`Generate change @Folder: ${source}`);
signale.note(`Generate change @Folder: ${repository}`);

main({
path: source,
mainFn({
save: !program.only,
path: repository,
LogFile: logFile,
appendHEAD: program.head,
genEachLog: program.list
}).then(result => {
if (program.only) {
console.log('========================================================');
console.log(result);
console.log('========================================================');
}
process.exit(0);
}).catch(err => {
if (err.message) {
signale.error(err.message);
} else {
signale.error(err);
}
process.exit(1);
});
30 changes: 14 additions & 16 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ const changelog = require('./changelog.js');
module.exports = async (config = {}) => {

if (!config.path) {
signale.error('source path is required, pls update conf/config.js.');
process.exit(1);
throw new Error('source path is required, pls update conf/config.js.');
} else {
const source = path.resolve(config.path);
if (!fs.existsSync(source)) {
signale.error(`source path (${config.path}) doesnot exist, please check again.`);
process.exit(1);
throw new Error(`source path (${config.path}) doesnot exist, please check again.`);
} else {
config.path = source;
}
Expand All @@ -22,17 +20,15 @@ module.exports = async (config = {}) => {
if (config.LogFile) {
const folder = path.resolve(config.LogFile, '../');
if (!fs.existsSync(folder)) {
signale.error(`changelog folder (${folder}) doesnot exist, please check again.`);
process.exit(1);
throw new Error(`changelog folder (${folder}) doesnot exist, please check again.`);
}
}

if (config.commitHref && config.tagHref) {
global.commitHref = config.commitHref;
} else {
const url = await gitCmd(config.path, ['ls-remote', '--get-url']).catch(err => {
signale.error(`fetch git info fail by code : ${err}`);
process.exit(1);
throw new Error(`fetch git info fail by code : ${err}`);
});
global.commitHref = config.commitHref = url.replace('.git', '/commit/');
if (url.indexOf('github') >= 0) {
Expand All @@ -47,15 +43,13 @@ module.exports = async (config = {}) => {
const splitStr = '_**_';

const trimStr = await gitCmd(config.path, ['log', '--tags', '--no-walk', `--pretty="%ai${splitStr}%h${splitStr}%D"`]).catch(err => {
signale.error(`fetch git tag fail by code : ${err}`);
process.exit(1);
throw new Error(`fetch git tag fail by code : ${err}`);
});

const tagSplitStr = 'tag: ';

if (!trimStr) {
signale.error('empty tags info.');
process.exit(1);
throw new Error('empty tags info.');
}

const list = trimStr.split('\n');
Expand Down Expand Up @@ -108,9 +102,7 @@ module.exports = async (config = {}) => {
const tagsLen = tags.length;

if (tagsLen === 0 || config.appendHEAD && tagsLen === 1) {
signale.success('project does not have any tag');
process.exit(0);
return;
throw new Error('project does not have any tag');
}

signale.star(`fetch the changelog for tags(${tagsLen})`);
Expand All @@ -137,9 +129,15 @@ module.exports = async (config = {}) => {
}
}
}
fs.writeFileSync(config.LogFile, `${mdContent}`);

if (config.save) {
signale.star('write file');
fs.writeFileSync(config.LogFile, `${mdContent}`);
}

signale.success('finish');

return mdContent;
};

const dateFormat = d => {
Expand Down

0 comments on commit 40465a2

Please sign in to comment.