@metalsmith/default-values
Advanced tools
Comparing version 3.3.0 to 4.0.0
@@ -7,4 +7,22 @@ ### Changelog | ||
#### [v4.0.0](https://github.com/metalsmith/default-values/compare/v3.3.0...v4.0.0) | ||
- Provides dual ESM/CJS module and aligns repo setup with other core plugins [`5d23dbd`](https://github.com/metalsmith/default-values/commit/5d23dbd1091e8d09030bdd9b2d6c945e6cd80476) | ||
- test: migrate test coverage from nyc -> c8, tests from CJS to ESM [`e03aacf`](https://github.com/metalsmith/default-values/commit/e03aacf7b4956a962258d791027782fe34953aa2) | ||
- Moves to more permissive LGPL license [`294f77f`](https://github.com/metalsmith/default-values/commit/294f77ffff73f848064dccb2d1817d484e9853c5) | ||
- Provides access to metalsmith.metadata() within default setter functions [`d4a5723`](https://github.com/metalsmith/default-values/commit/d4a57232bb7942ef001ec348a5f44350990f43a2) | ||
- Swaps out lodash.get/set for resp. dlv/dset & fixes security vulnerability [`1c27fd3`](https://github.com/metalsmith/default-values/commit/1c27fd330d854529f7d11b084135fd7ac64d3f36) | ||
- Clarifies default setting at keypaths and buffer conversion [`6c363de`](https://github.com/metalsmith/default-values/commit/6c363de5f777046a03c7432ac425fbf6af089d9f) | ||
- Adds 'strategy' option to allow overwriting existing values [`6e61e96`](https://github.com/metalsmith/default-values/commit/6e61e9653e7bee8d425324805bbc6e88c9fb7a0a) | ||
- Transforms default value into buffer if target type is already a buffer [`7287c08`](https://github.com/metalsmith/default-values/commit/7287c08aa94ef7cd7bdbceb58687acdfdfbc533b) | ||
- Adds generics for file & global metadata to Typescript types [`fb81838`](https://github.com/metalsmith/default-values/commit/fb818381d1246fa023f57f2ee4adaaa53428866f) | ||
- test: adds TS test [`f00280c`](https://github.com/metalsmith/default-values/commit/f00280c89eb01cafd59875e605060a597aae56f0) | ||
- Minor performance enhancement in calling setDefaults [`9dd6a84`](https://github.com/metalsmith/default-values/commit/9dd6a843b8acd413b0d2323f5b056248e74313ae) | ||
- Drops support for Node.js < 14.18.0, updates CI [`fbfa2c7`](https://github.com/metalsmith/default-values/commit/fbfa2c7897368cd272a19d3038d0b98c69a78574) | ||
- Updates dset 3.1.2 -> 3.1.3 [`d9a8d1e`](https://github.com/metalsmith/default-values/commit/d9a8d1e5805f4ccb49c80bec36196d45f87ce7fd) | ||
#### [v3.3.0](https://github.com/metalsmith/default-values/compare/v3.2.1...v3.3.0) | ||
> 26 November 2022 | ||
- Allow setting default contents when buffer is empty [`5d41d17`](https://github.com/metalsmith/default-values/commit/5d41d1790b1d4bc6a2905c08d028ab67c83221f4) | ||
@@ -11,0 +29,0 @@ - Docs: Add elaborate example with other plugins + single defaults set usage [`bb50253`](https://github.com/metalsmith/default-values/commit/bb502537ffb476208cb032570b3d7c4f4db98847) |
@@ -1,14 +0,23 @@ | ||
import { Plugin } from 'metalsmith'; | ||
import defaultValues from '.'; | ||
import { Plugin, File } from 'metalsmith'; | ||
export default defaultValues; | ||
export interface DefaultsSet { | ||
/** 1 or more glob patterns to match files. Defaults to `'**'` (all). */ | ||
pattern?: string; | ||
export type DefaultSetter<FileMeta, GlobalMeta> = (data:FileMeta, metadata: GlobalMeta) => any | ||
export interface DefaultsSet<FileMeta = File, GlobalMeta = {[key:string]:any}> { | ||
/** an object whose keys will be set as file metadata keys */ | ||
defaults: { | ||
[key:string]: string; | ||
[key:string]: DefaultSetter<FileMeta, GlobalMeta>|string|boolean|number|Object; | ||
} | ||
/** | ||
* 1 or more glob patterns to match files. | ||
* @default '**' | ||
**/ | ||
pattern?: string; | ||
/** | ||
* Strategy to handle setting defaults to keys that are aleady defined. | ||
* @default 'keep' | ||
*/ | ||
strategy?: 'keep'|'overwrite' | ||
} | ||
export type Options = DefaultsSet|DefaultsSet[] | ||
export type Options<FileMeta, GlobalMeta> = DefaultsSet<FileMeta, GlobalMeta>|DefaultsSet<FileMeta, GlobalMeta>[] | ||
/** | ||
@@ -29,2 +38,2 @@ * Set `defaults` to file metadata matching `pattern`'s. | ||
**/ | ||
declare function defaultValues(options: Options): Plugin; | ||
declare function defaultValues<FileMeta = File, GlobalMeta = {[key:string]:any}>(options: Options<FileMeta, GlobalMeta>): Plugin; |
@@ -1,9 +0,36 @@ | ||
'use strict' | ||
import { Buffer } from 'buffer'; | ||
import get from 'dlv'; | ||
import { dset } from 'dset'; | ||
const set_defaults = require('./set_defaults') | ||
/** | ||
* Sets defaults for object values | ||
* @param {Array<Array<*>>} defaults | ||
* @param {'keep'|'overwrite'} strategy | ||
* @return {import('.').DefaultSetter} Takes an object and sets defaults | ||
*/ | ||
function set_defaults(defaults, strategy) { | ||
return (item, context) => { | ||
defaults.forEach(([key, defaultValue]) => { | ||
const value = get(item, key); | ||
if (strategy === 'overwrite' || value === void 0 || value === null || Buffer.isBuffer(value) && value.toString().trim().length === 0) { | ||
if (typeof defaultValue === 'function') defaultValue = defaultValue(item, context); | ||
if (Buffer.isBuffer(value) && !Buffer.isBuffer(defaultValue)) defaultValue = Buffer.from(defaultValue); | ||
dset(item, key, defaultValue); | ||
} | ||
}); | ||
return item; | ||
}; | ||
} | ||
/** | ||
* @callback DefaultSetter | ||
* @param {import('metalsmith').File} file | ||
* @param {Object<string, *>} metadata | ||
*/ | ||
/** | ||
* @typedef {Object} DefaultsSet | ||
* @property {string|string[]} [pattern="**"] 1 or more glob patterns to match files. Defaults to `'**'` (all). | ||
* @property {Object} [defaults={}] an object whose keys will be set as file metadata keys | ||
* @property {Object<string, *>} [defaults={}] an object whose keys will be set as file metadata keys | ||
* @property {'keep'|'overwrite'} [strategy="keep"] Strategy to handle setting defaults to keys that are aleady defined. Defaults to `'keep'` | ||
*/ | ||
@@ -14,4 +41,5 @@ | ||
defaults: {}, | ||
strategy: 'keep', | ||
pattern: '**' | ||
} | ||
}; | ||
@@ -39,34 +67,35 @@ /** | ||
return function defaultValues(files, metalsmith, done) { | ||
const debug = metalsmith.debug('@metalsmith/default-values') | ||
debug('Running with options: %O ', options) | ||
const debug = metalsmith.debug('@metalsmith/default-values'); | ||
debug('Running with options: %O ', options); | ||
if (!Array.isArray(options) && typeof options === 'object' && options !== null) { | ||
options = [options] | ||
options = [options]; | ||
} | ||
const defaultSets = (options || []).map((defaultsSet) => Object.assign({}, defaultDefaultsSet, defaultsSet)) | ||
const defaultSets = (options || []).map(defaultsSet => Object.assign({}, defaultDefaultsSet, defaultsSet)); | ||
// Loop through configurations | ||
defaultSets.forEach(function ({ pattern, defaults }) { | ||
const matches = metalsmith.match(pattern, Object.keys(files)) | ||
debug.info('Matched %s files to pattern "%s": %o', matches.length, pattern, matches) | ||
defaultSets.forEach(function ({ | ||
pattern, | ||
defaults, | ||
strategy | ||
}) { | ||
const matches = metalsmith.match(pattern, Object.keys(files)); | ||
const defaultsEntries = Object.entries(defaults); | ||
debug.info('Matched %s files to pattern "%s": %o', matches.length, pattern, matches); | ||
if (matches.length) { | ||
matches.forEach((file) => { | ||
set_defaults(defaults)(files[file]) | ||
debug.info( | ||
'Defaults set for file "%s", the resulting metadata is: %O', | ||
file, | ||
Object.keys(defaults).reduce((resulting, prop) => { | ||
resulting[prop] = files[file][prop] | ||
return resulting | ||
}, {}) | ||
) | ||
}) | ||
const setDefaults = set_defaults(defaultsEntries, strategy); | ||
matches.forEach(file => { | ||
setDefaults(files[file], metalsmith.metadata()); | ||
debug.info('Defaults set for file "%s", the resulting metadata is: %O', file, Object.keys(defaults).reduce((resulting, prop) => { | ||
resulting[prop] = files[file][prop]; | ||
return resulting; | ||
}, {})); | ||
}); | ||
} else { | ||
debug.warn('No matches for pattern "%s"', pattern) | ||
debug.warn('No matches for pattern "%s"', pattern); | ||
} | ||
}) | ||
done() | ||
} | ||
}); | ||
done(); | ||
}; | ||
} | ||
module.exports = defaultValues | ||
export { defaultValues as default }; |
{ | ||
"name": "@metalsmith/default-values", | ||
"version": "3.3.0", | ||
"version": "4.0.0", | ||
"description": "A metalsmith plugin for setting default values to file metadata.", | ||
@@ -19,3 +19,3 @@ "keywords": [ | ||
}, | ||
"license": "GPL-3.0", | ||
"license": "LGPL-3.0-or-later", | ||
"author": "Woody Goodricke <github@andrewgoodricke.com>", | ||
@@ -25,3 +25,11 @@ "maintainers": [ | ||
], | ||
"main": "lib/index.js", | ||
"source": "src/index.js", | ||
"main": "lib/index.cjs", | ||
"module": "lib/index.js", | ||
"exports": { | ||
"import": "./lib/index.js", | ||
"require": "./lib/index.cjs" | ||
}, | ||
"type": "module", | ||
"types": "lib/index.d.ts", | ||
"directories": { | ||
@@ -36,3 +44,3 @@ "lib": "lib", | ||
"scripts": { | ||
"changelog": "auto-changelog -u --commit-limit false --ignore-commit-pattern '^((dev|chore|ci):|Release)'", | ||
"changelog": "auto-changelog -u --commit-limit false --ignore-commit-pattern '^((dev|chore|test|ci):|Release)'", | ||
"format": "prettier --write \"**/*.{yml,md,js,json}\"", | ||
@@ -42,20 +50,24 @@ "format:check": "prettier --list-different \"**/*.{yml,md,js,json}\"", | ||
"lint:check": "eslint --fix-dry-run .", | ||
"coverage": "nyc report --reporter=text-lcov > ./coverage.info", | ||
"coverage": "npm test && c8 report --reporter=text-lcov > ./coverage.info", | ||
"release": "release-it .", | ||
"test": "nyc mocha" | ||
"build": "microbundle --target node --no-sourcemap -f cjs,esm --generateTypes=false", | ||
"test": "c8 mocha", | ||
"pretest": "npm run build" | ||
}, | ||
"dependencies": { | ||
"lodash.get": "^4.4.2", | ||
"lodash.set": "^4.3.2" | ||
"dlv": "^1.1.3", | ||
"dset": "^3.1.3" | ||
}, | ||
"devDependencies": { | ||
"auto-changelog": "^2.4.0", | ||
"eslint": "^8.28.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"metalsmith": "^2.5.1", | ||
"mocha": "^9.2.2", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.8.0", | ||
"release-it": "^15.5.0" | ||
"c8": "^9.1.0", | ||
"eslint": "^8.57.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-import": "^2.29.1", | ||
"eslint-plugin-n": "^16.6.2", | ||
"metalsmith": "^2.6.3", | ||
"microbundle": "^0.15.1", | ||
"mocha": "^10.3.0", | ||
"prettier": "^3.2.5", | ||
"release-it": "^17.1.1" | ||
}, | ||
@@ -66,3 +78,3 @@ "peerDependencies": { | ||
"engines": { | ||
"node": ">=12" | ||
"node": ">=14.18.0" | ||
}, | ||
@@ -69,0 +81,0 @@ "publishConfig": { |
@@ -9,3 +9,3 @@ # @metalsmith/default-values | ||
[![code coverage][codecov-badge]][codecov-url] | ||
[![license: GPL-3.0][license-badge]][license-url] | ||
[![license: LGPL-3.0][license-badge]][license-url] | ||
@@ -15,4 +15,4 @@ ## Features | ||
- sets default values for metadata keys and file contents on files matched by pattern | ||
- does not overwrite or transform key values that are already defined | ||
- can set computed defaults based on other metadata | ||
- does not overwrite or transform key values that are already defined, unless `strategy: 'overwrite'`. | ||
- can set computed defaults based on other file keys or metalsmith metadata | ||
@@ -87,9 +87,24 @@ ## Installation | ||
- `pattern` (`string|string[]`): One or more glob patterns to match file paths. Defaults to `'**'` (all). | ||
- `defaults` (`Object<string, any>`): An object whose key-value pairs will be added to file metadata. You can also specify a function `callback(file)` to set dynamic defaults based on other, existing file metadata. | ||
- `defaults` (`Object<string, any>`): An object whose key-value pairs will be added to file metadata. You can also specify a function `callback(file, metadata)` to set dynamic defaults based on existing file or global metadata. | ||
- `strategy` (`'keep'|'overwrite'`): Strategy to handle setting defaults to keys that are aleady defined. | ||
### Examples | ||
#### Setting defaults at a keypath | ||
You can set a default at a file's nested keypath: | ||
```js | ||
metalsmith.use( | ||
defaultValues({ | ||
pattern: '**/*.md', | ||
pubdate(file) { return new Date() } | ||
'config.scripts.app': '/app.js', | ||
}) | ||
) | ||
``` | ||
#### Setting default contents | ||
Since version 3.3.0 the Metalsmith File's contents (which are a Node buffer) default can also be set (only if the buffer is empty): | ||
You can set a file's default contents (which is a Node buffer) and any other Buffer properties: | ||
@@ -100,2 +115,3 @@ ```js | ||
pattern: '**/*.md', | ||
strategy: 'overwrite', | ||
contents: Buffer.from('TO DO') | ||
@@ -106,2 +122,30 @@ }) | ||
When using a JSON config, a string can be used as default and it will automatically be transformed into a buffer. | ||
#### Setting dynamic defaults | ||
You can set dynamic defaults based on current file metadata or metalsmith metadata: | ||
```js | ||
metalsmith | ||
.metadata({ | ||
build: { timestamp: Date.now() } | ||
}) | ||
.use( | ||
defaultValues([ | ||
{ | ||
strategy: 'overwrite', | ||
defaults: { | ||
buildInfo(file, metadata) { | ||
return metadata.build | ||
}, | ||
excerpt(file) { | ||
return file.contents.toString().slice(0, 200) | ||
} | ||
} | ||
} | ||
]) | ||
) | ||
``` | ||
#### Combining with other plugins | ||
@@ -176,3 +220,3 @@ | ||
[GPL-3.0](LICENSE) | ||
[LGPL-3.0 or later](LICENSE) | ||
@@ -179,0 +223,0 @@ [npm-badge]: https://img.shields.io/npm/v/@metalsmith/default-values.svg |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
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
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
219
227
Yes
37585
11
3
1
+ Addeddlv@^1.1.3
+ Addeddset@^3.1.3
+ Addeddlv@1.1.3(transitive)
+ Addeddset@3.1.4(transitive)
- Removedlodash.get@^4.4.2
- Removedlodash.set@^4.3.2
- Removedlodash.get@4.4.2(transitive)
- Removedlodash.set@4.3.2(transitive)