Skip to content

Commit

Permalink
fix: fix homepage display.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 29, 2021
1 parent 54e1516 commit 76321a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
33 changes: 14 additions & 19 deletions src/index.ts
Expand Up @@ -14,9 +14,10 @@ export function getPackage(rootPath: string = process.cwd()): PackageJson {
pkg.author = pkg.author.name;
}

if (!pkg.homepage && pkg.repository && pkg.repository.url) {
pkg.homepage = pkg.repository.url;
if (!pkg.homepage && pkg.repository) {
pkg.homepage = typeof pkg.repository === 'string' ? pkg.repository : pkg.repository.url;
}

if (!pkg.homepage) {
pkg.homepage = '';
}
Expand All @@ -34,26 +35,20 @@ export function onebanner(option?: PackageJson) {
if (option) {
bn = Object.assign(bn, option);
}
return ['/*! ', bn.name, ' v', bn.version,
' | ', bn.license, ' (c) ',
new Date().getFullYear(), ' ', bn.author,
' | ', bn.homepage,
' */',
].filter(Boolean).join('');
return `/*! ${bn.name} v${bn.version} | ${bn.license} © ${new Date().getFullYear()} ${bn.author} ${bn.homepage} */`
}

export function multibanner(option?: PackageJson) {
let bn = getPackage();
if (option) bn = Object.assign(bn, option);
const result: string[] = [];
result.push('/**!');
result.push(`\n * ${bn.name} v${bn.version}`);
result.push(`\n * ${bn.description}`);
result.push(`\n * `);
result.push(`\n * Copyright (c) ${new Date().getFullYear()} ${bn.author}`);
result.push(`\n * ${bn.homepage}`);
result.push(`\n * `);
result.push(`\n * Licensed under the ${bn.license} license.`);
result.push(`\n */\n`);
return result.filter(Boolean).join('');
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;
}
14 changes: 10 additions & 4 deletions test/index.test.ts
Expand Up @@ -9,8 +9,14 @@ it('getPackage test case', async () => {
type: "git",
url: "https://github.com/jaywcjlove/bannerjs.git"
});
const babel = getPackage(path.resolve(process.cwd(), 'node_modules/@babel/core'));
expect(babel.author).toEqual('The Babel Team');

const ansiEscapes = getPackage(path.resolve(process.cwd(), 'node_modules/ansi-escapes'));
expect(ansiEscapes.author).toEqual('Sindre Sorhus');
expect(ansiEscapes.homepage).toEqual('sindresorhus/ansi-escapes');

const compatData = getPackage(path.resolve(process.cwd(), 'node_modules/@babel/compat-data'));
expect(compatData.homepage).toEqual('https://github.com/babel/babel.git');

const testpkg = getPackage(path.resolve(process.cwd(), 'test'));
expect(testpkg.author).toBeUndefined();
expect(testpkg.homepage).toEqual('');
Expand All @@ -21,14 +27,14 @@ it('onebanner test case', async () => {
const des = onebanner();
expect(typeof des).toEqual('string');
const desName = onebanner({ name: 'pkgname', version: '1.0.0' });
expect(desName.indexOf('/*! pkgname v1.0.0')).toEqual(0)
expect(desName.indexOf('/*! pkgname v1.0.0')).toEqual(0);
expect(desName.includes('pkgname v1.0.0')).toBeTruthy();
});

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('/**!\n')).toEqual(0);
expect(desName.includes('pkgname v1.0.0')).toBeTruthy();
});

0 comments on commit 76321a7

Please sign in to comment.