add-dist-header
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -38,3 +38,4 @@ #!/usr/bin/env node | ||
const flagMap = Object.fromEntries(flags.map(flag => flag.replace(/^[-]*/, '').split('='))); | ||
const setVersion = flagMap.version !== 'false'; | ||
const replace = flagMap.replace !== 'false'; | ||
const version = flagMap.version !== 'false'; | ||
const isFolder = existsSync(param.filename) && statSync(param.filename).isDirectory(); | ||
@@ -47,3 +48,8 @@ const pattern = isFolder ? param.filename + '/*' : param.filename; | ||
exit('File not found: ' + param.filename); | ||
filenames.forEach(file => | ||
logResult(addDistHeader.prepend({ filename: file, dist: param.dist, setVersion: setVersion }))); | ||
const prepend = (file) => addDistHeader.prepend({ | ||
filename: file, | ||
dist: param.dist, | ||
replaceComment: replace, | ||
setVersion: version, | ||
}); | ||
filenames.forEach(file => logResult(prepend(file))); |
@@ -1,2 +0,2 @@ | ||
//! add-dist-header v0.1.0 ~ https://github.com/center-key/add-dist-header ~ MIT License | ||
//! add-dist-header v0.1.1 ~ https://github.com/center-key/add-dist-header ~ MIT License | ||
@@ -7,2 +7,3 @@ export declare type Options = { | ||
extension?: string; | ||
replaceComment?: boolean; | ||
setVersion?: boolean; | ||
@@ -9,0 +10,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
//! add-dist-header v0.1.0 ~ https://github.com/center-key/add-dist-header ~ MIT License | ||
//! add-dist-header v0.1.1 ~ https://github.com/center-key/add-dist-header ~ MIT License | ||
@@ -10,2 +10,3 @@ import { format, parse } from 'path'; | ||
dist: 'dist', | ||
replaceComment: true, | ||
setVersion: true, | ||
@@ -21,11 +22,17 @@ }; | ||
}; | ||
const firstLine = { | ||
js: /^(\/\/[^!].*|\/[*][^!].*[*]\/)\n/, | ||
ml: /^<!--.*-->\n/, | ||
other: /^\/[*][^!].*[*]\/\n/, | ||
}; | ||
const pkg = JSON.parse(readFileSync('package.json', 'utf8')); | ||
const inputFile = parse(settings.filename); | ||
const outputFileExt = settings.extension ?? inputFile.ext; | ||
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 fileExt = settings.extension ?? inputFile.ext; | ||
const jsStyle = /\.(js|ts|cjs|mjs)$/.test(fileExt); | ||
const mlStyle = /\.(html|sgml|xml|php)$/.test(fileExt); | ||
const type = jsStyle ? 'js' : mlStyle ? 'ml' : 'other'; | ||
const input = readFileSync(settings.filename, 'utf8'); | ||
const pkg = JSON.parse(readFileSync('package.json', 'utf8')); | ||
const out1 = settings.replaceComment ? input.replace(firstLine[type], '') : input; | ||
const versionPattern = /~~~version~~~/g; | ||
const dist = settings.setVersion ? input.replace(versionPattern, pkg.version) : input; | ||
const out2 = settings.setVersion ? out1.replace(versionPattern, pkg.version) : out1; | ||
const info = pkg.homepage ?? pkg.docs ?? pkg.repository; | ||
@@ -35,18 +42,15 @@ const unlicensed = !pkg.license || pkg.license === 'UNLICENSED'; | ||
const banner = `${pkg.name} v${pkg.version} ~ ${info} ~ ${license}`; | ||
const header = comment.start + banner + comment.end; | ||
const output = header + '\n\n' + dist; | ||
const header = commentStyle[type].start + banner + commentStyle[type].end; | ||
const fixedDigits = { minimumFractionDigits: 2, maximumFractionDigits: 2 }; | ||
const spacerLines = (path) => path.includes('.min.') || mlStyle ? '\n' : '\n\n'; | ||
const distFolder = makeDir.sync(settings.dist); | ||
const outputFilename = format({ | ||
dir: settings.dist, | ||
name: inputFile.name, | ||
ext: outputFileExt, | ||
}); | ||
writeFileSync(outputFilename, output); | ||
const outputPath = format({ dir: settings.dist, name: inputFile.name, ext: fileExt }); | ||
const out3 = header + spacerLines(outputPath) + out2.replace(/^\s*\n/, ''); | ||
writeFileSync(outputPath, out3); | ||
return { | ||
dist: distFolder, | ||
header: header, | ||
file: outputFilename, | ||
length: output.length, | ||
size: (output.length / 1024).toLocaleString([], fixedDigits) + ' kB', | ||
file: outputPath, | ||
length: out3.length, | ||
size: (out3.length / 1024).toLocaleString([], fixedDigits) + ' kB', | ||
}; | ||
@@ -53,0 +57,0 @@ }, |
{ | ||
"name": "add-dist-header", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Adds a header comment to a file and saves it to your distribution folder (written in TypeScript)", | ||
@@ -76,5 +76,4 @@ "license": "MIT", | ||
"step:08": "node bin/cli.js --version=false build dist", | ||
"step:09": "w3c-html-validator", | ||
"pretest": "npm-run-all step:*", | ||
"test": "mocha spec/*.spec.js" | ||
"test": "mocha spec/*.spec.js && w3c-html-validator" | ||
}, | ||
@@ -81,0 +80,0 @@ "dependencies": { |
@@ -79,2 +79,8 @@ # Add Dist Header | ||
## 4) CLI Flags | ||
| Flag | Description | Values | Default | | ||
| --------- | ----------------------------------------------------------------------------------- | --------------- | ------- | | ||
| `replace` | Delete the original first line comment (if not marked `!` important). | `true`, `false` | `true` | | ||
| `version` | Substitute occurrences of `~~~version~~~` with the **package.json** version number. | `true`, `false` | `true` | | ||
<br> | ||
@@ -81,0 +87,0 @@ |
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
16351
195
89