Skip to content

Commit

Permalink
feat(parser): adding custom parser support (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanksdesign authored Sep 21, 2020
1 parent de11808 commit 887a837
Show file tree
Hide file tree
Showing 19 changed files with 1,719 additions and 1,638 deletions.
2 changes: 2 additions & 0 deletions __tests__/__json_files/yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo: "bar"
bar: "{foo}"
33 changes: 32 additions & 1 deletion __tests__/utils/combineJSON.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

var combineJSON = require('../../lib/utils/combineJSON');
var path = require('path');
var yaml = require('yaml');

describe('utils', () => {
describe('combineJSON', () => {
Expand Down Expand Up @@ -64,7 +65,7 @@ describe('utils', () => {
expect(opts.target[opts.key]).toBe(1);
expect(opts.copy[opts.key]).toBe(2);
throw new Error('test');
})
}, true)
).toThrow(/test/);
});

Expand All @@ -73,5 +74,35 @@ describe('utils', () => {
expect(test).toHaveProperty('json5A', 5);
expect(test.d).toHaveProperty('json5e', 1);
});

describe('custom parsers', () => {
it('should support yaml.parse', () => {
const parsers = [{
pattern: /\.yaml$/,
// yaml.parse function matches the intended function signature
parse: ({contents}) => yaml.parse(contents)
}];
const output = combineJSON([`__tests__/__json_files/yaml.yaml`], false, null, false, parsers);
expect(output).toHaveProperty('foo', 'bar');
expect(output).toHaveProperty('bar', '{foo}');
});

it('should multiple parsers on the same file', () => {
const testOutput = { test: 'test' };
const parsers = [{
pattern: /.json$/,
parse: (content) => {
return { test: 'foo' }
}
},{
pattern: /.json$/,
parse: (content) => {
return testOutput
}
}];
const output = combineJSON([`__tests__/__json_files/simple.json`], false, null, false, parsers);
expect(output).toHaveProperty('test', 'test');
});
});
});
});
3 changes: 3 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ You can find out more about creating configurations in JS in our documentation a

| Attribute | Type | Description |
| :--- | :--- | :--- |
| parsers | Array[Object] (optional) | Custom token parsers to run on token files |
| parsers[].pattern | Regex | A file regular expression to match files the parser should run on. |
| parsers[].parser | Function | Parser function that takes the string content of the file and returns a plain Javascript object. |
| include | Array[String] (optional) | An array of path [globs](https://github.com/isaacs/node-glob) to Style Dictionary property files that contain default styles. The Style Dictionary uses this as a base collection of properties. The properties found using the "source" attribute will overwrite properties found using include. |
| source | Array[String] | An array of path [globs](https://github.com/isaacs/node-glob) to JSON files that contain style properties. The Style Dictionary will do a deep merge of all of the JSON files, allowing you to separate your properties into multiple files. |
| platforms | Object | An object containing platform config objects that describe how the Style Dictionary should build for that platform. You can add any arbitrary attributes on this object that will get passed to formats and actions (more on these in a bit). This is useful for things like build paths, name prefixes, variable names, etc. |
Expand Down
Empty file.
248 changes: 248 additions & 0 deletions examples/advanced/custom-parser/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions examples/advanced/custom-parser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "style-dictionary-example-custom-parser",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "style-dictionary build --config ./sd.config.js"
},
"author": "",
"license": "Apache-2.0",
"devDependencies": {
"style-dictionary": "^2.10.0"
}
}
Loading

0 comments on commit 887a837

Please sign in to comment.