Skip to content

Commit

Permalink
Release v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Oct 1, 2021
1 parent 9f46cb8 commit e082823
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 25 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ _Adds a header comment to a file and saves it to your distribution folder_
[![Vulnerabilities](https://snyk.io/test/github/center-key/add-dist-header/badge.svg)](https://snyk.io/test/github/center-key/add-dist-header)
[![Build](https://github.com/center-key/add-dist-header/workflows/build/badge.svg)](https://github.com/center-key/add-dist-header/actions?query=workflow%3Abuild)

**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.

Example header comment:
```javascript
//! my-app v0.3.7 ~ https://github.com/my-organization/my-app ~ MIT License
```

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)

## 1) Setup
Expand Down
3 changes: 2 additions & 1 deletion dist/add-dist-header.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! 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

export declare type Options = {
filename: string;
Expand All @@ -11,6 +11,7 @@ export declare type Result = {
header: string;
file: string;
length: number;
size: string;
};
declare const addDistHeader: {
prepend(options: Options): Result;
Expand Down
24 changes: 19 additions & 5 deletions dist/add-dist-header.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! 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

import { format, parse } from 'path';
import { readFileSync, writeFileSync } from 'fs';
Expand All @@ -12,27 +12,41 @@ const addDistHeader = {
const settings = { ...defaults, ...options };
if (!settings.filename)
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');
const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
const versionPattern = /~~~version~~~/g;
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);
const outputFilename = format({
dir: settings.dist,
name: inputFile.name,
ext: outputFileExt,
});
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 };
24 changes: 19 additions & 5 deletions dist/add-dist-header.umd.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! 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

var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
Expand Down Expand Up @@ -27,27 +27,41 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
const settings = { ...defaults, ...options };
if (!settings.filename)
throw Error('Must specify the "filename" option.');
const commentStyle = {
js: { start: '//! ', end: '' },
ml: { start: '<!-- ', end: ' -->' },
other: { start: '/*! ', end: ' */' },
};
const inputFile = (0, path_1.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 = (0, fs_1.readFileSync)(settings.filename, 'utf8');
const pkg = JSON.parse((0, fs_1.readFileSync)('package.json', 'utf8'));
const versionPattern = /~~~version~~~/g;
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 = make_dir_1.default.sync(settings.dist);
const outputFilename = (0, path_1.format)({
dir: settings.dist,
name: inputFile.name,
ext: outputFileExt,
});
(0, fs_1.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',
};
},
};
exports.addDistHeader = addDistHeader;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"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)",
"license": "MIT",
"type": "module",
Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/dist/kebab.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- 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 -->

<!doctype html>
<html lang=en>
Expand All @@ -7,6 +7,6 @@
<title>Kebab &bull; 🍢🍢🍢</title>
</head>
<body>
<h1>Kebab v0.0.3</h1>
<h1>Kebab v0.1.0</h1>
</body>
</html>
4 changes: 2 additions & 2 deletions spec/fixtures/dist/kebab.min.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! 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 */

.kebab { /* v0.0.3 */
.kebab { /* v0.1.0 */
border: 2px solid firebrick;
border-radius: 3px;
}
6 changes: 3 additions & 3 deletions spec/fixtures/dist/to-kebab.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! 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

const toKebab = (camelStr) => { //v0.0.3
const toKebab = (camelStr) => { //v0.1.0
const dash = (word) => '-' + word.toLowerCase();
return ('' + camelStr).replace(/([A-Z]+)/g, dash).replace(/\s|^-/g, '');
};

module.exports = toKebab; //version: 0.0.3
module.exports = toKebab; //version: 0.1.0
2 changes: 1 addition & 1 deletion spec/fixtures/dist/to-kebab.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! 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

const toKebab = (camelStr: string): string => { //v~~~version~~~
const dash = (word: string) => '-' + word.toLowerCase();
Expand Down

0 comments on commit e082823

Please sign in to comment.