diff --git a/src/cli.ts b/src/cli.ts index 6176b42..98a4975 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -9,7 +9,7 @@ export function run() { let multi = false; let one = false; - args = args.filter(function (arg) { + args = args.filter((arg) => { if (arg === '-h' || arg === '--help') { help = true return false @@ -24,31 +24,35 @@ export function run() { }); if (help) { - console.log('Usage: bannerjs'); - console.log(''); - console.log('Pipe Usage: bannerjs'); - console.log(''); - console.log('Options:'); - console.log(''); - console.log(' -m --multi Output multi-line results'); - console.log(' -o --one Output one-line results'); - console.log(''); - if (!help) process.exit(1); - } else { - let bannerStr = ''; - if (multi) { - bannerStr = multibanner(); - } else { - bannerStr = onebanner(); - } + const helpStr = ` + Usage: bannerjs + + Pipe Usage: bannerjs + + -m --multi Output multi-line results + -o --one Output one-line results - let dest = args[2] ? createWriteStream(args[2]) : process.stdout; - let source = args[1] ? createReadStream(args[1]) : process.stdin; + `; + console.log(helpStr); + return; + } - dest.write(`${bannerStr}\n`); - source.on('end', () => { - dest.write('\n'); - }) - .pipe(dest, { end: true }); + let bannerStr = ''; + if (multi) { + bannerStr = multibanner(); + } else { + bannerStr = onebanner(); } + + let dest = args[2] ? createWriteStream(args[2]) : process.stdout; + let source = args[1] ? createReadStream(args[1]) : process.stdin; + + dest.write(`${bannerStr}\n`); + source.on('end', () => { + dest.write('\n'); + }) + .on('error', (err) => { + console.log('ERR:', err) + }) + .pipe(dest, { end: true }); } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 3ee8d55..15ba525 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,20 +35,27 @@ export function onebanner(option?: PackageJson) { if (option) { bn = Object.assign(bn, option); } - return `/*! ${bn.name} v${bn.version} | ${bn.license} © ${new Date().getFullYear()} ${bn.author} ${bn.homepage} */` + return [ + '/*!', bn.name, bn.version && `v${bn.version}`, + bn.license && `| ${bn.license}`, + `© ${new Date().getFullYear()}`, + bn.author, + bn.homepage, + '*/' + ].filter(Boolean).join(' '); } export function multibanner(option?: PackageJson) { let bn = getPackage(); if (option) bn = Object.assign(bn, option); - const str = `/**! - * ${bn.name} v${bn.version} - * ${bn.description} - * - * Copyright (c) ${new Date().getFullYear()} ${bn.author} - * ${bn.homepage} - * Licensed under the ${bn.license} license. - */\n - `; - return str; + return [ + '/**!', + '\n *', bn.name, bn.version && `v${bn.version}`, + '\n *', bn.description, + '\n *', + `\n * Copyright (c) ${new Date().getFullYear()}`, bn.author, + '\n *', bn.homepage, + '\n *', bn.license && `Licensed under the ${bn.license} license`, + '\n */\n' + ].filter(Boolean).join(' '); } \ No newline at end of file diff --git a/test/cli.test.ts b/test/cli.test.ts new file mode 100644 index 0000000..a8b25ad --- /dev/null +++ b/test/cli.test.ts @@ -0,0 +1,23 @@ +import { run } from '../src/cli'; + +const argv = process.argv.slice(0, 2); + +it('-h test case', async () => { + console.log = jest.fn(); + process.argv = [...argv, '-h']; + expect(run()).toBeUndefined(); + // @ts-ignore + const str: string = console.log.mock.calls[0][0]; + expect(str.indexOf('bannerjs') > -1).toBeTruthy(); + process.argv = [...argv]; +}); + +// test('-m test case', async () => { + +// console.log = jest.fn(); +// process.argv = [...argv, '-m']; +// expect(run()).toBeUndefined(); +// // @ts-ignore +// const str: string = console.log.mock.calls[0][0]; +// expect(str.indexOf('bannerjs') > -1).toBeTruthy(); +// }); diff --git a/test/index.test.ts b/test/index.test.ts index 191a19c..43999d2 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -35,6 +35,6 @@ it('multibanner test case', async () => { const des = multibanner(); expect(typeof des).toEqual('string'); const desName = multibanner({ name: 'pkgname', version: '1.0.0' }); - expect(desName.indexOf('/**!\n')).toEqual(0); + expect(desName.indexOf(' * pkgname v1.0.0') > 1).toBeTruthy(); expect(desName.includes('pkgname v1.0.0')).toBeTruthy(); });