add-dist-header
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -28,5 +28,3 @@ #!/usr/bin/env node | ||
import { globSync } from 'glob'; | ||
import chalk from 'chalk'; | ||
import fs from 'fs'; | ||
import log from 'fancy-log'; | ||
import path from 'path'; | ||
@@ -36,3 +34,3 @@ import slash from 'slash'; | ||
// Parameters and flags | ||
const validFlags = ['delimiter', 'ext', 'keep-first', 'keep', 'no-version', 'note', 'quiet', 'recursive']; | ||
const validFlags = ['all-files', 'delimiter', 'ext', 'keep-first', 'keep', 'no-version', 'note', 'quiet', 'recursive']; | ||
const cli = cliArgvUtil.parse(validFlags); | ||
@@ -42,17 +40,2 @@ const source = cli.params[0] ?? 'build/*'; | ||
// Deprecated | ||
if (cli.flagOn.keepFirst) console.log('DEPRECATED: Replace --keep flag with --keep-first'); | ||
cli.flagOn.keep = cli.flagOn.keep || cli.flagOn.keepFirst; | ||
// Reporting | ||
const logResult = (result) => { | ||
const name = chalk.gray('add-dist-header'); | ||
const arrow = chalk.gray.bold('→'); | ||
const source = chalk.blue.bold(result.source); | ||
const target = chalk.magenta(result.file); | ||
const size = chalk.white('(' + result.size + ')'); | ||
if (!cli.flagOn.quiet && result.valid) | ||
log(name, source, arrow, target, size); | ||
}; | ||
// Prepend | ||
@@ -76,3 +59,4 @@ const normalize = (name) => path.normalize(name.endsWith(path.sep) ? name.slice(0, -1) : name); | ||
throw Error('[add-dist-header] ' + error); | ||
const clacOptions = (sourceFilename) => ({ | ||
const calcOptions = (sourceFilename) => ({ | ||
allFiles: cli.flagOn.allFiles, | ||
dist: targetRoot + path.dirname(sourceFilename).substring(origin.length), | ||
@@ -83,3 +67,4 @@ delimiter: cli.flagMap.delimiter ?? '~~', | ||
}); | ||
filenames.forEach(filename => | ||
logResult(addDistHeader.prepend(filename, clacOptions(filename)))); | ||
const getResult = (filename) => addDistHeader.prepend(filename, calcOptions(filename)); | ||
const quiet = cli.flagOn.quiet; | ||
filenames.forEach(filename => addDistHeader.reporter(getResult(filename), { quiet })); |
@@ -1,4 +0,5 @@ | ||
//! add-dist-header v1.2.2 ~~ https://github.com/center-key/add-dist-header ~~ MIT License | ||
//! add-dist-header v1.3.0 ~~ https://github.com/center-key/add-dist-header ~~ MIT License | ||
export type Settings = { | ||
allFiles: boolean; | ||
dist: string; | ||
@@ -10,15 +11,19 @@ extension: string | null; | ||
}; | ||
export type Options = Partial<Settings>; | ||
export type Result = { | ||
valid: boolean; | ||
text: boolean; | ||
dist: string; | ||
header: string; | ||
header: string | null; | ||
source: string; | ||
file: string; | ||
length: number; | ||
size: string; | ||
length: number | null; | ||
size: string | null; | ||
}; | ||
export type ReporterSettings = { | ||
quite: boolean; | ||
}; | ||
declare const addDistHeader: { | ||
prepend(filename: string, options?: Options): Result; | ||
prepend(filename: string, options?: Partial<Settings>): Result; | ||
reporter(result: Result, options?: ReporterSettings): Result; | ||
}; | ||
export { addDistHeader }; |
@@ -1,7 +0,9 @@ | ||
//! add-dist-header v1.2.2 ~~ https://github.com/center-key/add-dist-header ~~ MIT License | ||
//! add-dist-header v1.3.0 ~~ https://github.com/center-key/add-dist-header ~~ MIT License | ||
import { isBinary } from 'istextorbinary'; | ||
import path from 'path'; | ||
import chalk from 'chalk'; | ||
import fs from 'fs'; | ||
import log from 'fancy-log'; | ||
import makeDir from 'make-dir'; | ||
import path from 'path'; | ||
import slash from 'slash'; | ||
@@ -11,2 +13,3 @@ const addDistHeader = { | ||
const defaults = { | ||
allFiles: false, | ||
dist: 'dist', | ||
@@ -38,3 +41,3 @@ extension: null, | ||
const type = jsStyle ? 'js' : mlStyle ? 'ml' : 'other'; | ||
const invalidContent = isBinary(filename); | ||
const isTextFile = !isBinary(filename); | ||
const input = fs.readFileSync(filename, 'utf-8').trimStart(); | ||
@@ -63,15 +66,32 @@ const normalizeEol = /\r/g; | ||
const final = doctype + header + spacerLines + out4.replace(leadingBlanks, ''); | ||
if (!invalidContent) | ||
if (isTextFile) | ||
fs.writeFileSync(outputPath, final); | ||
else if (settings.allFiles) | ||
fs.copyFileSync(filename, outputPath); | ||
return { | ||
valid: !invalidContent, | ||
valid: isTextFile || settings.allFiles, | ||
text: isTextFile, | ||
dist: distFolder, | ||
header: header, | ||
header: isTextFile ? header : null, | ||
source: filename, | ||
file: outputPath, | ||
length: final.length, | ||
size: (final.length / 1024).toLocaleString([], fixedDigits) + ' KB', | ||
length: isTextFile ? final.length : null, | ||
size: isTextFile ? (final.length / 1024).toLocaleString([], fixedDigits) + ' KB' : null, | ||
}; | ||
}, | ||
reporter(result, options) { | ||
const defaults = { | ||
quiet: false, | ||
}; | ||
const settings = { ...defaults, ...options }; | ||
const name = chalk.gray('add-dist-header'); | ||
const arrow = chalk.gray.bold('→'); | ||
const source = chalk.blue.bold(result.source); | ||
const target = chalk.magenta(result.file); | ||
const size = chalk.white('(' + result.size + ')'); | ||
if (!settings.quiet && result.valid) | ||
log(name, source, arrow, target, size); | ||
return result; | ||
}, | ||
}; | ||
export { addDistHeader }; |
{ | ||
"name": "add-dist-header", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Prepend a one-line banner comment (with license notice) to distribution files", | ||
@@ -96,8 +96,8 @@ "license": "MIT", | ||
"@types/node": "~20.5", | ||
"@typescript-eslint/eslint-plugin": "~6.4", | ||
"@typescript-eslint/parser": "~6.4", | ||
"@typescript-eslint/eslint-plugin": "~6.6", | ||
"@typescript-eslint/parser": "~6.6", | ||
"assert-deep-strict-equal": "~1.1", | ||
"copy-file-util": "~1.1", | ||
"copy-folder-util": "~1.1", | ||
"eslint": "~8.47", | ||
"eslint": "~8.48", | ||
"jshint": "~2.13", | ||
@@ -107,5 +107,5 @@ "mocha": "~10.2", | ||
"run-scripts-util": "~1.2", | ||
"typescript": "~5.1", | ||
"typescript": "~5.2", | ||
"w3c-html-validator": "~1.4" | ||
} | ||
} |
@@ -21,6 +21,13 @@ # Add Dist Header | ||
Example header comment for a **.css** file: | ||
```javascript | ||
```css | ||
/*! my-app v3.1.4 ~~ https://github.com/my-org/my-app ~~ MIT License */ | ||
``` | ||
Example header comment for a **.xml** file: | ||
```xml | ||
<!-- my-app v3.1.4 ~~ https://github.com/my-org/my-app ~~ MIT License --> | ||
``` | ||
Header comments are only prepended to text files. | ||
Binary files are ignored (unless the `--all-files` flag is specified). | ||
Automatically prepending headers to distribution files is particularly handy when your build | ||
@@ -75,2 +82,3 @@ tools are configured to remove comments (such as if `"removeComments": true` in set | ||
| -------------- | --------------------------------------------------------- | ---------- | ------- | | ||
| `--all-files` | Add headers to text files and just copy binary<br>files. | N/A | N/A | | ||
| `--delimiter` | Characters separating the parts of the header<br>comment. | **string** | `~~` | | ||
@@ -95,2 +103,5 @@ | `--ext` | Filter files by file extension, such as `.js`.<br>Use a comma to specify multiple extensions. | **string** | N/A | | ||
- `add-dist-header build/minimized dist --all-files`<br> | ||
Same as above command except that binary files, such as .png files, will also be copied over unmodified. | ||
- `add-dist-header build dist --no-version --delimiter=🔥`<br> | ||
@@ -103,3 +114,3 @@ Add comment headers but do not substitute the version number and use "🔥" as the separator in the header comment instead of "~~". | ||
## C) Application Code | ||
Even though **add-dist-header** is primarily intended for build scripts, the package can easily be used programmatically in ESM and TypeScript projects. | ||
Even though **add-dist-header** is primarily intended for build scripts, the package can be used programmatically in ESM and TypeScript projects. | ||
@@ -106,0 +117,0 @@ Example: |
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
18944
181
144