Skip to content

Commit

Permalink
fix: Fix comment display.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 29, 2021
1 parent 76321a7 commit 9df28e4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 37 deletions.
54 changes: 29 additions & 25 deletions src/cli.ts
Expand Up @@ -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
Expand All @@ -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 });
}
29 changes: 18 additions & 11 deletions src/index.ts
Expand Up @@ -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(' ');
}
23 changes: 23 additions & 0 deletions 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();
// });
2 changes: 1 addition & 1 deletion test/index.test.ts
Expand Up @@ -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();
});

0 comments on commit 9df28e4

Please sign in to comment.