Skip to content

Commit

Permalink
feat: rebuild code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 4, 2021
1 parent e535591 commit 47c24bf
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 145 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
lib
coverage
__snapshots__
node_modules
npm-debug.log*
yarn-debug.log*
Expand Down
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -30,7 +30,7 @@ Multi-line results in:
*/
```

# Structure
## Structure

The following keys should be defined in package.json:

Expand All @@ -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');
Expand All @@ -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

Expand All @@ -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/'));
});
```

Expand All @@ -104,6 +113,7 @@ var minified = banner.onebanner() + '\n' + uglify.minify(code, {
ascii_only: true
}
}).code;

fs.writeFileSync('src/test.js', minified);
```

Expand Down Expand Up @@ -138,7 +148,6 @@ cat my-js.js | bannerjs -o | uglify-js > my-js.min.js
}
```


# License

MIT license
60 changes: 2 additions & 58 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -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})
}
const bannerjs = require('../lib');
bannerjs.run();
75 changes: 0 additions & 75 deletions index.js

This file was deleted.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
},
"author": "kenny wang <wowohoo@qq.com>",
"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"
Expand All @@ -27,7 +34,8 @@
"generator",
"package.json"
],
"dependencies": {
"object-assign": "4.1.1"
"devDependencies": {
"types-package-json": "2.0.39",
"tsbb": "^3.0.4"
}
}
54 changes: 54 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
59 changes: 59 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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('');
}
Loading

0 comments on commit 47c24bf

Please sign in to comment.