From 76321a74d05e357b281afd767d66dc30d981fd1b Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Fri, 29 Oct 2021 14:45:51 +0800 Subject: [PATCH] fix: fix homepage display. --- src/index.ts | 33 ++++++++++++++------------------- test/index.test.ts | 14 ++++++++++---- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/index.ts b/src/index.ts index b85ba53..3ee8d55 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 = ''; } @@ -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; } \ No newline at end of file diff --git a/test/index.test.ts b/test/index.test.ts index 4437bf4..191a19c 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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(''); @@ -21,7 +27,7 @@ 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(); }); @@ -29,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('/**!\n')).toEqual(0); expect(desName.includes('pkgname v1.0.0')).toBeTruthy(); });