From 47c24bf6aa3a886d001341ed3e8a697914ff4655 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Sat, 4 Sep 2021 22:49:47 +0800 Subject: [PATCH] feat: rebuild code. --- .gitignore | 3 ++ README.md | 29 +++++++++++------- bin/cli.js | 60 ++----------------------------------- index.js | 75 ---------------------------------------------- package.json | 12 ++++++-- src/cli.ts | 54 +++++++++++++++++++++++++++++++++ src/index.ts | 59 ++++++++++++++++++++++++++++++++++++ test/index.test.ts | 34 +++++++++++++++++++++ test/package.json | 1 + tsconfig.json | 21 +++++++++++++ 10 files changed, 203 insertions(+), 145 deletions(-) delete mode 100644 index.js create mode 100644 src/cli.ts create mode 100644 src/index.ts create mode 100644 test/index.test.ts create mode 100644 test/package.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index fc1b3dd..1ac6f03 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +lib +coverage +__snapshots__ node_modules npm-debug.log* yarn-debug.log* diff --git a/README.md b/README.md index dbb509e..398d7b9 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Add a banner to a string. Get one-line/multi-line comment banner based on package.json. -# Install +## Install Install with npm: @@ -30,7 +30,7 @@ Multi-line results in: */ ``` -# Structure +## Structure The following keys should be defined in package.json: @@ -53,12 +53,12 @@ The following keys should be defined in package.json: `author` value can be defined like object or simply string too. -# Use +## Use -# option +### option -- `bannerjs.multibanner(option)` Multi-line results -- `bannerjs.onebanner(option)` One-line results +- `multibanner(option)` Multi-line results +- `onebanner(option)` One-line results ```js var banner = require('bannerjs'); @@ -72,6 +72,15 @@ bannerjs.multibanner({ }) ``` +### API + +```ts +import { PackageJson } from 'types-package-json'; + +export declare function getPackage(rootPath?: string): PackageJson; +export declare function onebanner(option?: PackageJson): string; +export declare function multibanner(option?: PackageJson): string; +``` ## Use in gulp @@ -84,9 +93,9 @@ var banner = require('gulp-banner'); var bannerjs = require('bannerjs'); gulp.task('default', function() { - gulp.src('./test.js') - .pipe(banner(bannerjs.multibanner())) - .pipe(gulp.dest('dist/')); + gulp.src('./test.js') + .pipe(banner(bannerjs.multibanner())) + .pipe(gulp.dest('dist/')); }); ``` @@ -104,6 +113,7 @@ var minified = banner.onebanner() + '\n' + uglify.minify(code, { ascii_only: true } }).code; + fs.writeFileSync('src/test.js', minified); ``` @@ -138,7 +148,6 @@ cat my-js.js | bannerjs -o | uglify-js > my-js.min.js } ``` - # License MIT license \ No newline at end of file diff --git a/bin/cli.js b/bin/cli.js index 13ca53b..e964b5b 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,60 +1,4 @@ #!/usr/bin/env node - -var read = require('fs').createReadStream -var write = require('fs').createWriteStream - -var banner = require('../') - -var args = process.argv.slice(2) -var help = false -var multi = false -var one = false - -args = args.filter(function (arg) { - if (arg === '-h' || arg === '--help') { - help = true - return false - } else if (arg === '-m' || arg === '--multi') { - multi = true - return false - } else if (arg === '-o' || arg === '--one') { - one = true - return false - } - return true -}) - - -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 { - - var banner_str = ''; - - if(multi){ - banner_str = banner.multibanner() - }else{ - banner_str = banner.onebanner() - } - - - var dest = args[2] ? write(args[2]) : process.stdout - var source = args[1] ? read(args[1]) : process.stdin - - dest.write(banner_str+'\n') - source.on('end', function () { - dest.write('\n') - }).pipe(dest, {end: false}) -} \ No newline at end of file +const bannerjs = require('../lib'); +bannerjs.run(); \ No newline at end of file diff --git a/index.js b/index.js deleted file mode 100644 index c2c8ac2..0000000 --- a/index.js +++ /dev/null @@ -1,75 +0,0 @@ -var fs = require('fs'); -var assign = require('object-assign'); -var pkg = require(process.cwd() + '/package.json') - -// var banner_str = ( -// '/*!\n' + -// ' * JSLite JavaScript Library v' + pkg.version + '\n' + -// ' * http://JSLite.io\n *\n' + '' + -// String(fs.readFileSync('./MIT-LICENSE')).trim().split('\n') -// .map( (l,u) => u==0?` * ${l}`:``).join('') + -// '\n * Date:' + new Date() + -// '\n */\n' -// ); - -function bannersource() { - - var author = pkg.author || ''; - if (author && author['name']) { - author = author.name; - } - - var homepage = pkg.homepage; - if (!homepage && pkg.repository && pkg.repository.url) { - homepage = pkg.repository.url - } - if (!homepage) { - homepage = '' - } - - var description = pkg.description || ''; - if (!description) { - description = '' - } - - var license = pkg.license || '' - if (license && license.type) { - license = license.type - } - - return { - author: author, - homepage: homepage, - name: pkg.name, - license: license, - version: pkg.version, - description: description - } -} - -exports.onebanner = function (option) { - var bn = bannersource(); - if (option) bn = assign(bn, option); - return ['/*! ', bn.name, ' v', bn.version, - ' | ', bn.license, ' (c) ', - new Date().getFullYear(), ' ', bn.author, - ' | ', bn.homepage, - ' */', - ].join('') -}; - -exports.multibanner = function (option) { - var bn = bannersource(); - if (option) bn = assign(bn, option); - return ( - '/*!' + - '\n * ' + bn.name + ' v' + bn.version + - '\n * ' + bn.description + - '\n * ' + - '\n * Copyright' + ' (c) ' + new Date().getFullYear() + ' ' + bn.author + - '\n * ' + bn.homepage + - '\n * ' + - '\n * Licensed under the ' + bn.license + ' license.' + - '\n */\n' - ); -}; \ No newline at end of file diff --git a/package.json b/package.json index 7298922..aa46c0c 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,13 @@ }, "author": "kenny wang ", "license": "MIT", + "scripts": { + "start": "node lib/index.js", + "watch": "tsbb watch --no-esm --disable-babel", + "build": "tsbb build --no-esm --disable-babel", + "test": "tsbb test", + "coverage": "tsbb test --coverage" + }, "repository": { "type": "git", "url": "https://github.com/jaywcjlove/bannerjs.git" @@ -27,7 +34,8 @@ "generator", "package.json" ], - "dependencies": { - "object-assign": "4.1.1" + "devDependencies": { + "types-package-json": "2.0.39", + "tsbb": "^3.0.4" } } diff --git a/src/cli.ts b/src/cli.ts new file mode 100644 index 0000000..6176b42 --- /dev/null +++ b/src/cli.ts @@ -0,0 +1,54 @@ +#!/usr/bin/env node + +import { createReadStream, createWriteStream } from 'fs'; +import { onebanner, multibanner } from './'; + +export function run() { + let args = process.argv.slice(2) + let help = false; + let multi = false; + let one = false; + + args = args.filter(function (arg) { + if (arg === '-h' || arg === '--help') { + help = true + return false + } else if (arg === '-m' || arg === '--multi') { + multi = true + return false + } else if (arg === '-o' || arg === '--one') { + one = true + return false + } + return true + }); + + 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(); + } + + 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'); + }) + .pipe(dest, { end: true }); + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..b85ba53 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,59 @@ +import path from 'path'; +import { PackageJson } from 'types-package-json'; + +export * from './cli'; + +/** + * Get package.json Object + */ +export function getPackage(rootPath: string = process.cwd()): PackageJson { + const pkgPath = path.resolve(rootPath, 'package.json'); + const pkg: PackageJson = require(pkgPath); + pkg.author = pkg.author; + if (pkg.author && typeof pkg.author === 'object' && pkg.author.name) { + pkg.author = pkg.author.name; + } + + if (!pkg.homepage && pkg.repository && pkg.repository.url) { + pkg.homepage = pkg.repository.url; + } + if (!pkg.homepage) { + pkg.homepage = ''; + } + + pkg.description = pkg.description; + if (!pkg.description) { + pkg.description = ''; + } + + return pkg; +} + +export function onebanner(option?: PackageJson) { + let bn = getPackage(); + 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(''); +} + +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(''); +} \ No newline at end of file diff --git a/test/index.test.ts b/test/index.test.ts new file mode 100644 index 0000000..4437bf4 --- /dev/null +++ b/test/index.test.ts @@ -0,0 +1,34 @@ +import path from 'path'; +import { getPackage, onebanner, multibanner } from '../src'; + +it('getPackage test case', async () => { + const pkg = await getPackage(); + expect(pkg.name).toEqual('bannerjs'); + expect(pkg.license).toEqual('MIT'); + expect(pkg.repository).toEqual({ + 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 testpkg = getPackage(path.resolve(process.cwd(), 'test')); + expect(testpkg.author).toBeUndefined(); + expect(testpkg.homepage).toEqual(''); + expect(testpkg.description).toEqual(''); +}); + +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.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.includes('pkgname v1.0.0')).toBeTruthy(); +}); diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..1968f11 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "esModuleInterop": true, + "declaration": true, + "target": "es2017", + "noImplicitAny": true, + "resolveJsonModule": true, + "moduleResolution": "node", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "sourceMap": true, + "strict": false, + "skipLibCheck": true, + "outDir": "lib", + "baseUrl": "." + }, + "include": [ + "src/**/*" + ] +}