Skip to content

Commit

Permalink
Merge pull request #11 from simlu/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
simlu authored Mar 21, 2018
2 parents 9e926f3 + dfc92b6 commit e6aca8f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ yaml.load("config.yaml");

Works identical to how this is defined for serverless [here](https://serverless.com/framework/docs/providers/aws/guide/variables/).

#### Extensions

##### Bake variables when loading files

```yaml
${file(./path/to/file.yml), key1=value1&key2=value2}
```

##### Reference Js files

Reference js instead of yaml files.

```yaml
${file(./path/to/file.js)}
```

The reference file needs to export simple object containing configuration

```js
module.exports = {};
```

### Deep Merge

Analogue to the `<<` yaml syntax we can use `<<<` to deep merge into the current nesting level.
Expand Down
21 changes: 17 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,26 @@ const loadRecursive = (dir, 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, (_, k, v) => get(vars, k, v));
result = result.replace(
/\${opt:([a-zA-Z0-9]+?)(?:, ["']([a-zA-Z0-9-]+?)["'])?}/g,
(match, k, v) => get(vars, k, v || match)
);
// load referenced yaml file
const match = /^\${file\((~?[a-zA-Z0-9._\-/]+?)\)(?::([a-zA-Z0-9.]+?))?}$/g.exec(result);
const match = (
/^\${file\((~?[a-zA-Z0-9._/-]+?)\)(?::([a-zA-Z0-9.]+?))?(?:, ([a-zA-Z0-9=&-]+?))?}$/g.exec(result)
);
if (match) {
const filePath = path.join(dir, match[1]);
const loaded = yaml.safeLoad(fs.readFileSync(filePath, 'utf8'));
result = loadRecursive(dir, match[2] ? get(loaded, match[2]) : loaded, vars);
const loaded = filePath.endsWith(".yml")
? yaml.safeLoad(fs.readFileSync(filePath, 'utf8'))
// eslint-disable-next-line global-require, import/no-dynamic-require
: require(filePath);
result = loadRecursive(
dir,
match[2] ? get(loaded, match[2]) : loaded,
Object.assign({}, vars, match[3] ? JSON
.parse(`{"${match[3].replace(/&/g, "\",\"").replace(/=/g, "\":\"")}"}`) : {})
);
}
}
if (result instanceof Object) {
Expand Down
3 changes: 3 additions & 0 deletions test/resources/child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
key: "value"
};
1 change: 1 addition & 0 deletions test/resources/childVariable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
key: ${opt:variable}
7 changes: 7 additions & 0 deletions test/resources/parent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ child:

childValue:
${file(./child.yml):key}


childBaked:
${file(./childVariable.yml), variable=value}

childJs:
${file(./child.js)}
8 changes: 6 additions & 2 deletions test/test_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const parentFile = path.join(__dirname, "resources", 'parent.yml');
describe("Testing Yaml", () => {
it("Testing Variable Undefined", () => {
expect(yaml.load(variablesFile)).to.deep.equal({
plain: 'undefined', default: 'default'
// eslint-disable-next-line no-template-curly-in-string
plain: '${opt:test}', default: 'default'
});
});

Expand All @@ -27,7 +28,10 @@ describe("Testing Yaml", () => {

it("Testing File Resolution", () => {
expect(yaml.load(parentFile)).to.deep.equal({
child: { key: "value" }, childValue: "value"
child: { key: "value" },
childValue: "value",
childBaked: { key: "value" },
childJs: { key: "value" }
});
});
});

0 comments on commit e6aca8f

Please sign in to comment.