Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript config files #114

Merged
merged 1 commit into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")