yaml-boost
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -1,39 +0,40 @@ | ||
"use strict"; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import get from 'lodash.get'; | ||
import set from 'lodash.set'; | ||
import mergeWith from 'lodash.mergewith'; | ||
import yaml from 'js-yaml'; | ||
const fs = require('fs'); | ||
const concatArrays = (objValue, srcValue) => ([objValue, srcValue] | ||
.every(Array.isArray) ? objValue.concat(srcValue) : undefined); | ||
const path = require('path'); | ||
const get = require('lodash.get'); | ||
const set = require('lodash.set'); | ||
const mergeWith = require('lodash.mergewith'); | ||
const yaml = require('js-yaml'); | ||
const concatArrays = (objValue, srcValue) => [objValue, srcValue].every(Array.isArray) ? objValue.concat(srcValue) : undefined; | ||
const loadRecursive = (dir, relDir, data, vars) => { | ||
const loadRecursive = async (dir, relDir, data, vars) => { | ||
let result = data; | ||
if (typeof result === 'string' || result instanceof String) { | ||
// replace yaml variables with defaults | ||
result = result.replace(/\${opt:([a-zA-Z0-9]+?)(?:, ["']([a-zA-Z0-9\-.]+?)["'])?}/g, (match, k, v) => get(vars, k, v || match)); // load requires | ||
const match = /^\${(require|file|fileFn)\(([~^]?[a-zA-Z\d._\-@/]+?)\)(?::([a-zA-Z\d.]+?))?(?:, ([a-zA-Z\d=\-&/.:[\],]+?))?}$/g.exec(result); | ||
result = result.replace( | ||
/\${opt:([a-zA-Z0-9]+?)(?:, ["']([a-zA-Z0-9\-.]+?)["'])?}/g, | ||
(match, k, v) => get(vars, k, v || match) | ||
); | ||
// load requires | ||
const match = ( | ||
/^\${(require|file|fileFn)\(([~^]?[a-zA-Z\d._\-@/]+?)\)(?::([a-zA-Z\d.]+?))?(?:, ([a-zA-Z\d=\-&/.:[\],]+?))?}$/g | ||
).exec(result); | ||
if (match) { | ||
const varsNew = { ...vars, | ||
...(match[4] ? JSON.parse(`{"${match[4].replace(/&/g, '","').replace(/=/g, '":"')}"}`) : {}) | ||
const varsNew = { | ||
...vars, | ||
...(match[4] ? JSON | ||
.parse(`{"${match[4].replace(/&/g, '","').replace(/=/g, '":"')}"}`) : {}) | ||
}; | ||
let loaded; | ||
let newRelDir = relDir; | ||
if (['file', 'fileFn'].includes(match[1])) { | ||
const filePath = match[2].startsWith('^') ? path.join(relDir, match[2].substring(1)) : path.join(dir, match[2]); | ||
const filePath = match[2].startsWith('^') | ||
? path.join(relDir, match[2].substring(1)) | ||
: path.join(dir, match[2]); | ||
newRelDir = path.dirname(filePath); | ||
loaded = filePath.endsWith('.yml') || filePath.endsWith('.yaml') ? yaml.load(fs.readFileSync(filePath, 'utf8')) // eslint-disable-next-line global-require, import/no-dynamic-require | ||
: require(filePath); | ||
loaded = (filePath.endsWith('.yml') || filePath.endsWith('.yaml')) | ||
? yaml.load(fs.readFileSync(filePath, 'utf8')) | ||
: (await import(filePath)).default; | ||
if (match[1] === 'fileFn') { | ||
@@ -43,22 +44,24 @@ loaded = loaded(varsNew); | ||
} else { | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
loaded = require(match[2]); | ||
loaded = (await import(match[2])).default; | ||
} | ||
const target = match[3] ? get(loaded, match[3]) : loaded; | ||
result = loadRecursive(dir, newRelDir, typeof target === 'function' ? target() : target, varsNew); | ||
result = await loadRecursive(dir, newRelDir, typeof target === 'function' ? target() : target, varsNew); | ||
} | ||
} | ||
if (result instanceof Object) { | ||
const toMerge = get(result, '<<<', []).map(e => loadRecursive(dir, relDir, e, vars)); | ||
const toMerge = await Promise.all( | ||
get(result, '<<<', []) | ||
.map((e) => loadRecursive(dir, relDir, e, vars)) | ||
); | ||
delete result['<<<']; | ||
Object.keys(result).forEach(key => set(result, key, loadRecursive(dir, relDir, get(result, key), vars))); | ||
const keys = Object.keys(result); | ||
const values = await Promise.all(keys | ||
.map((key) => loadRecursive(dir, relDir, get(result, key), vars))); | ||
keys.forEach((key, idx) => set(result, key, values[idx])); | ||
result = toMerge.reduce((prev, cur) => mergeWith(prev, cur, concatArrays), result); | ||
} | ||
return result; | ||
}; | ||
const resolve = (refPath, content, vars) => { | ||
export const resolve = async (refPath, content, vars) => { | ||
const dirname = path.dirname(refPath); | ||
@@ -68,6 +71,8 @@ return loadRecursive(dirname, dirname, yaml.load(content), vars); | ||
module.exports.resolve = resolve; | ||
export const load = (filePath, vars = {}) => resolve( | ||
filePath, | ||
fs.readFileSync(filePath, 'utf8'), | ||
vars | ||
); | ||
module.exports.load = (filePath, vars = {}) => resolve(filePath, fs.readFileSync(filePath, 'utf8'), vars); | ||
module.exports.dump = yaml.dump; | ||
export const dump = yaml.dump; |
{ | ||
"name": "yaml-boost", | ||
"version": "2.0.1", | ||
"type": "module", | ||
"version": "3.0.0", | ||
"description": "Yaml Parser with additional functionality.", | ||
@@ -8,9 +9,8 @@ "main": "lib/index.js", | ||
"clean": "rm -rf lib", | ||
"build": "npx babel src --out-dir lib --copy-files --include-dotfiles --config-file ./.babelrc", | ||
"build": "cp -rf ./src ./lib", | ||
"build-clean": "yarn run clean && yarn run build", | ||
"test-simple": "nyc mocha \"./test/**/*.spec.js\"", | ||
"test-simple": "c8 mocha --experimental-loader=./test/hot.js \"./test/**/*.spec.js\"", | ||
"test": "yarn run clean && yarn run gardener && yarn run test-simple", | ||
"coveralls": "node ./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info", | ||
"semantic-release": "yarn run build-clean && npx semantic-release", | ||
"gardener": "node gardener", | ||
"gardener": "node gardener.js", | ||
"docker": "docker run --net host -u`id -u`:`id -g` -v $(pwd):/user/project -v ~/.aws:/user/.aws -v ~/.npmrc:/user/.npmrc -w /user/project -it --entrypoint /bin/bash", | ||
@@ -35,20 +35,17 @@ "t": "yarn test", | ||
"devDependencies": { | ||
"@babel/cli": "7.14.8", | ||
"@babel/core": "7.15.0", | ||
"@babel/register": "7.15.3", | ||
"@blackflux/eslint-plugin-rules": "2.0.3", | ||
"@blackflux/robo-config-plugin": "5.3.0", | ||
"babel-eslint": "10.1.0", | ||
"babel-preset-latest-node": "5.5.1", | ||
"chai": "4.3.4", | ||
"coveralls": "3.1.1", | ||
"eslint": "7.32.0", | ||
"eslint-config-airbnb-base": "14.2.1", | ||
"eslint-plugin-import": "2.24.2", | ||
"@babel/core": "7.22.9", | ||
"@babel/eslint-parser": "7.22.9", | ||
"@babel/register": "7.22.5", | ||
"@blackflux/eslint-plugin-rules": "3.0.1", | ||
"@blackflux/robo-config-plugin": "9.1.10", | ||
"c8": "8.0.1", | ||
"chai": "4.3.7", | ||
"eslint": "8.46.0", | ||
"eslint-config-airbnb-base": "15.0.0", | ||
"eslint-plugin-import": "2.28.0", | ||
"eslint-plugin-json": "3.1.0", | ||
"eslint-plugin-markdown": "2.2.0", | ||
"eslint-plugin-mocha": "9.0.0", | ||
"js-gardener": "3.0.3", | ||
"nyc": "15.1.0", | ||
"semantic-release": "17.4.7" | ||
"eslint-plugin-markdown": "3.0.1", | ||
"eslint-plugin-mocha": "10.1.0", | ||
"js-gardener": "5.0.2", | ||
"smart-fs": "4.0.1" | ||
}, | ||
@@ -70,3 +67,3 @@ "keywords": [ | ||
"engines": { | ||
"node": ">= 12" | ||
"node": ">= 16" | ||
}, | ||
@@ -73,0 +70,0 @@ "files": [ |
@@ -26,6 +26,8 @@ # Yaml-Boost | ||
### load(filePath, vars = {}) | ||
### async load(filePath, vars = {}) | ||
Load filePath with given variables. | ||
Note that this function is asynchronous. | ||
### dump | ||
@@ -39,5 +41,5 @@ | ||
```js | ||
const yaml = require('yaml-boost'); | ||
import { load } from 'yaml-boost'; | ||
yaml.load('config.yaml'); | ||
load('config.yaml'); | ||
``` | ||
@@ -80,3 +82,3 @@ | ||
```js | ||
module.exports = {}; | ||
export default {}; | ||
``` | ||
@@ -95,3 +97,3 @@ | ||
```js | ||
module.exports = (args) => ({ args }); | ||
export default (args) => ({ args }); | ||
``` | ||
@@ -139,9 +141,10 @@ | ||
```js | ||
const path = require('path'); | ||
const minimist = require('minimist'); | ||
const yaml = require('yaml-boost'); | ||
import path from 'path'; | ||
import minimist from 'minimist'; | ||
import { load } from 'yaml-boost'; | ||
module.exports = yaml.load(path.join(__dirname, 'serverless.core.yml'), minimist(process.argv.slice(2))); | ||
const cfg = await load(path.join(__dirname, 'serverless.core.yml'), minimist(process.argv.slice(2))); | ||
export default cfg; | ||
``` | ||
Then instead of defining `serverless.yml`, define your config in `serverless.core.yml`. |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
15
70
146
1
Yes
10270