Skip to content

Commit

Permalink
JavaScript config files
Browse files Browse the repository at this point in the history
This change enables the user to use a JavaScript module for their
config file. The goal of this change is to enable more flexible
configuration similar to Webpack or Babel. For example filter
functions, which previously could only be provided using the Node
API, can now be included directly in a JS config file.

JavaScript config files could also eventually remove the need for
the register APIs because you could simply pass formatters,
transformers, etc. as functions directly in the config instead of
registering them with an alias and then referencing that alias in
the config. This could potentially provide a simpler API very
similar to the way webpack handles plugins:
https://webpack.js.org/configuration/plugins/. Just a thought
though. For now the default config file `./config.json` and the
ability to continue to use JSON config files has not changed.
  • Loading branch information
Elliot Dickison committed Sep 2, 2018
1 parent b7b36f7 commit 633cf15
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
10 changes: 2 additions & 8 deletions bin/style-dictionary
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ function styleDictionaryBuild(options) {
options = options || {};
var configPath = options.config ? options.config : './config.json';

console.log("Reading config file from " + configPath);
var config = JSON.parse( fs.readFileSync(configPath) );

// Create a style dictionary object with the config
var styleDictionary = StyleDictionary.extend( config );
var styleDictionary = StyleDictionary.extend( configPath );

if (options.platform && options.platform.length > 0) {
options.platform.forEach(function(platform) {
Expand All @@ -74,11 +71,8 @@ function styleDictionaryClean(options) {
options = options || {};
var configPath = options.config ? options.config : './config.json';

console.log("Reading config file from " + configPath);
var config = JSON.parse( fs.readFileSync(configPath) );

// Create a style dictionary object with the config
var styleDictionary = StyleDictionary.extend( config );
var styleDictionary = StyleDictionary.extend( configPath );

if (options.platform && options.platform.length > 0) {
options.platform.forEach(function(platform) {
Expand Down
2 changes: 1 addition & 1 deletion docs/build_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Here is what the build system is doing under the hood.

If you use this as a node module, the steps are slightly different, but the overall.

1. When you call the [`extend`](api.md#extend) method, you can either pass it a path to a JSON config file, or give it a plain object that has the configuration. This will perform steps 1-3 above.
1. When you call the [`extend`](api.md#extend) method, you can either pass it a path to a JSON or JS config file, or give it a plain object that has the configuration. This will perform steps 1-3 above.
1. Then you can now call `buildAllPlatforms` or other methods like `buildPlatform('scss')` or `exportPlatform('javascript')`. This is equivalent to step 4 above.

```javascript
Expand Down
10 changes: 5 additions & 5 deletions lib/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

var combineJSON = require('./utils/combineJSON'),
deepExtend = require('./utils/deepExtend'),
fs = require('fs-extra'),
resolveCwd = require('resolve-cwd'),
_ = require('lodash'),
chalk = require('chalk');

Expand Down Expand Up @@ -81,13 +81,13 @@ var combineJSON = require('./utils/combineJSON'),
function extend(opts) {
var options, to_ret;

// Overloaded method, can accept a string as a path that points to a JSON file
// or a plain object. Potentially refactor.
// Overloaded method, can accept a string as a path that points to a JS or
// JSON file or a plain object. Potentially refactor.
if (_.isString(opts)) {
options = fs.readJsonSync(opts);
options = require(resolveCwd(opts));
} else {
options = opts;
}
}``

// Creating a new object and copying over the options
// Also keeping an options object just in case
Expand Down
14 changes: 9 additions & 5 deletions test/buildAllPlatforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ var assert = require('chai').assert,
helpers = require('./helpers'),
StyleDictionary = require('../index');

// Test configs
var config = helpers.fileToJSON(__dirname + '/configs/test.json');
var test = StyleDictionary.extend(config);

describe('buildAllPlatforms', function() {
beforeEach(function() {
helpers.clearOutput();
});

it('should work', function() {
it('should work with json config', function() {
var test = StyleDictionary.extend(__dirname + '/configs/test.json');
test.buildAllPlatforms();
assert(helpers.fileExists('./test/output/web/_icons.css'));
assert(helpers.fileExists('./test/output/android/colors.xml'));
});

it('should work with js config', function() {
var test = StyleDictionary.extend(__dirname + '/configs/test.js');
test.buildAllPlatforms();
assert(helpers.fileExists('./test/output/web/_icons.css'));
assert(helpers.fileExists('./test/output/android/colors.xml'));
Expand Down
34 changes: 34 additions & 0 deletions test/cliBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

var assert = require('chai').assert,
childProcess = require("child_process"),
helpers = require('./helpers');

describe('cliBuildWithJsConfig', function() {
beforeEach(function() {
helpers.clearOutput();
});

it('should work with json config', function() {
childProcess.execSync("node ./bin/style-dictionary build --config ./test/configs/test.json")
assert(helpers.fileExists('./test/output/web/_icons.css'));
assert(helpers.fileExists('./test/output/android/colors.xml'));
});

it('should work with javascript config', function() {
childProcess.execSync("node ./bin/style-dictionary build --config ./test/configs/test.js")
assert(helpers.fileExists('./test/output/web/_icons.css'));
assert(helpers.fileExists('./test/output/android/colors.xml'));
});
});
1 change: 1 addition & 0 deletions test/configs/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./test.json")

0 comments on commit 633cf15

Please sign in to comment.