add-dist-header
Advanced tools
Comparing version 0.0.3 to 0.1.0
@@ -16,9 +16,11 @@ #!/usr/bin/env node | ||
// $ cd add-dist-header | ||
// $ node bin/cli.js "spec/fixtures/to-kebab.js" "spec/fixtures/dist" #run on sample file | ||
// $ node bin/cli.js #update distribution files for this project | ||
// $ node bin/cli.js "spec/fixtures" "spec/fixtures/dist" #run on sample files | ||
// $ node bin/cli.js --version=false #update the distribution files for this project | ||
// Imports | ||
import glob from 'glob'; | ||
import { existsSync, statSync } from 'fs'; | ||
import { addDistHeader } from '../dist/add-dist-header.js'; | ||
import { addDistHeader } from '../dist/add-dist-header.js'; | ||
import { existsSync, statSync } from 'fs'; | ||
import chalk from 'chalk'; | ||
import glob from 'glob'; | ||
import log from 'fancy-log'; | ||
@@ -35,2 +37,3 @@ // Parameters | ||
}; | ||
const exit = (message) => (console.error('[add-dist-header] ' + message), process.exit(1)); | ||
const flagMap = Object.fromEntries(flags.map(flag => flag.replace(/^[-]*/, '').split('='))); | ||
@@ -41,6 +44,7 @@ const setVersion = flagMap.version !== 'false'; | ||
const filenames = glob.sync(pattern, { nodir: true }).sort(); | ||
const log = (result) => console.log('[add-dist-header]', result.file, '~ length:', result.length); | ||
const name = chalk.gray('add-dist-header'); | ||
const logResult = (result) => log(name, chalk.blue.bold(result.file), chalk.magenta(result.size)); | ||
if (!filenames.length) | ||
console.error('[add-dist-header] File not found:', param.filename); | ||
exit('File not found: ' + param.filename); | ||
filenames.forEach(file => | ||
log(addDistHeader.prepend({ filename: file, dist: param.dist, setVersion: setVersion }))); | ||
logResult(addDistHeader.prepend({ filename: file, dist: param.dist, setVersion: setVersion }))); |
@@ -1,2 +0,2 @@ | ||
//! add-dist-header v0.0.3 ~ https://github.com/center-key/add-dist-header ~ MIT License | ||
//! add-dist-header v0.1.0 ~ https://github.com/center-key/add-dist-header ~ MIT License | ||
@@ -14,2 +14,3 @@ export declare type Options = { | ||
length: number; | ||
size: string; | ||
}; | ||
@@ -16,0 +17,0 @@ declare const addDistHeader: { |
@@ -1,2 +0,2 @@ | ||
//! add-dist-header v0.0.3 ~ https://github.com/center-key/add-dist-header ~ MIT License | ||
//! add-dist-header v0.1.0 ~ https://github.com/center-key/add-dist-header ~ MIT License | ||
@@ -15,5 +15,12 @@ import { format, parse } from 'path'; | ||
throw Error('Must specify the "filename" option.'); | ||
const commentStyle = { | ||
js: { start: '//! ', end: '' }, | ||
ml: { start: '<!-- ', end: ' -->' }, | ||
other: { start: '/*! ', end: ' */' }, | ||
}; | ||
const inputFile = parse(settings.filename); | ||
const outputFileExt = settings.extension ?? inputFile.ext; | ||
const jsStyle = /\.(js|ts|cjs|mjs)/.test(outputFileExt); | ||
const jsStyle = /\.(js|ts|cjs|mjs)$/.test(outputFileExt); | ||
const mlStyle = /\.(html|sgml|xml|php)$/.test(outputFileExt); | ||
const comment = commentStyle[jsStyle ? 'js' : mlStyle ? 'ml' : 'other']; | ||
const input = readFileSync(settings.filename, 'utf8'); | ||
@@ -23,8 +30,9 @@ const pkg = JSON.parse(readFileSync('package.json', 'utf8')); | ||
const dist = settings.setVersion ? input.replace(versionPattern, pkg.version) : input; | ||
const info = pkg.homepage ?? pkg.repository; | ||
const info = pkg.homepage ?? pkg.docs ?? pkg.repository; | ||
const unlicensed = !pkg.license || pkg.license === 'UNLICENSED'; | ||
const license = unlicensed ? 'All Rights Reserved' : pkg.license + ' License'; | ||
const banner = `${pkg.name} v${pkg.version} ~ ${info} ~ ${license}`; | ||
const header = (jsStyle ? '//! ' : '/*! ') + banner + (jsStyle ? '' : ' */'); | ||
const header = comment.start + banner + comment.end; | ||
const output = header + '\n\n' + dist; | ||
const fixedDigits = { minimumFractionDigits: 2, maximumFractionDigits: 2 }; | ||
const distFolder = makeDir.sync(settings.dist); | ||
@@ -37,5 +45,11 @@ const outputFilename = format({ | ||
writeFileSync(outputFilename, output); | ||
return { dist: distFolder, header: header, file: outputFilename, length: output.length }; | ||
return { | ||
dist: distFolder, | ||
header: header, | ||
file: outputFilename, | ||
length: output.length, | ||
size: (output.length / 1024).toLocaleString([], fixedDigits) + ' kB', | ||
}; | ||
}, | ||
}; | ||
export { addDistHeader }; |
{ | ||
"name": "add-dist-header", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Adds a header comment to a file and saves it to your distribution folder (written in TypeScript)", | ||
@@ -73,5 +73,6 @@ "license": "MIT", | ||
"step:05": "tsc --module UMD --outDir build/umd", | ||
"step:06": "cpy 'build/umd/*.js' build --rename=add-dist-header.umd.cjs", | ||
"step:06": "cpy build/umd/add-dist-header.js build --rename=add-dist-header.umd.cjs", | ||
"step:07": "cpy 'build/*' dist", | ||
"step:08": "node bin/cli.js --version=false build dist", | ||
"step:09": "w3c-html-validator", | ||
"pretest": "npm-run-all step:*", | ||
@@ -88,4 +89,6 @@ "test": "mocha spec/*.spec.js" | ||
"assert-deep-strict-equal": "~0.0", | ||
"chalk": "~4.1", | ||
"cpy-cli": "~3.1", | ||
"eslint": "~7.32", | ||
"fancy-log": "~1.3", | ||
"jshint": "~2.13", | ||
@@ -96,4 +99,5 @@ "make-dir": "~3.1", | ||
"rimraf": "~3.0", | ||
"typescript": "~4.4" | ||
"typescript": "~4.4", | ||
"w3c-html-validator": "~0.8" | ||
} | ||
} |
@@ -11,4 +11,4 @@ # Add Dist Header | ||
**add-dist-header** uses the `name`, `homepage`, and `license` from your | ||
project's **package.json** file to create a header comment in a build target file. | ||
**add-dist-header** uses the `name`, `homepage`, and `license` from your project's **package.json** | ||
file to create a header comment and prepend it to a build file. | ||
@@ -20,5 +20,6 @@ Example header comment: | ||
This is particularly handy when your build tools are configured to remove comments, such as | ||
setting `"removeComments": true` in **tsconfig.json**. | ||
For a real-world example, check the files in the **dist** folder at | ||
Automatically prepending headers to distribution files is particularly handy when your build | ||
tools are configured to remove comments (such as if `"removeComments": true` in set | ||
in **tsconfig.json**). | ||
For a real-world example, see the files in the **dist** folder at | ||
[w3c-html-validator](https://github.com/center-key/w3c-html-validator/tree/main/dist) | ||
@@ -37,3 +38,6 @@ | ||
For example: | ||
The **first** parameter is the *source* file (defaults to `"build/*"`). | ||
The **second** parameter is the *output* folder (defaults to `"dist"`). | ||
Example **package.json** script: | ||
```json | ||
@@ -45,3 +49,6 @@ "scripts": { | ||
Or, run from the terminal in your project home folder, such as: | ||
Alternatively, you can run **add-dist-header** directly from the terminal in your project home | ||
folder. | ||
Example terminal command: | ||
```shell | ||
@@ -51,4 +58,4 @@ $ ls package.json | ||
$ npx add-dist-header "build" "dist" | ||
[add-dist-header] dist/my-app.d.ts ~ length: 413 | ||
[add-dist-header] dist/my-app.js ~ length: 1569 | ||
[17:13:50] add-dist-header dist/my-app.d.ts 413.11 kB | ||
[17:13:51] add-dist-header dist/my-app.js 1,569.70 kB | ||
``` | ||
@@ -58,7 +65,7 @@ | ||
```shell | ||
$ npx add-dist-header #same as above since "build/*" "dist" are the default parameters | ||
[add-dist-header] dist/my-app.d.ts ~ length: 413 | ||
[add-dist-header] dist/my-app.js ~ length: 1569 | ||
$ npx add-dist-header "target/my-app-cli.js" #creates "dist/my-app.js" prepended with a comment header | ||
[add-dist-header] dist/my-app-cli.js ~ length: 413 | ||
$ npx add-dist-header #same as above since "build/*" "dist" are the default parameter values | ||
[17:13:50] add-dist-header dist/my-app.d.ts 413.11 kB | ||
[17:13:51] add-dist-header dist/my-app.js 1,569.70 kB | ||
$ npx add-dist-header "meta/config.js" #creates "dist/config.js" prepended with a comment header | ||
[17:15:03] add-dist-header dist/config.js 3.91 kB | ||
``` | ||
@@ -71,3 +78,3 @@ | ||
The substitution feature can be disabled with the `--version` flag: | ||
The substitution feature is disabled by setting `--version` flag to `false`: | ||
```json | ||
@@ -78,1 +85,6 @@ "scripts": { | ||
``` | ||
<br> | ||
--- | ||
[MIT License](LICENSE.txt) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14985
180
83
15