From 5ad6475aed3b29269efbd1457cef7a323b599be7 Mon Sep 17 00:00:00 2001 From: Cristiano Rastelli Date: Tue, 6 Nov 2018 21:36:30 +0000 Subject: [PATCH] Added 'json/flat' format (#192) --- .../formats/__snapshots__/all.test.js.snap | 6 ++ __tests__/formats/jsonFlat.test.js | 63 +++++++++++++++++++ lib/common/formats.js | 17 +++++ package.json | 1 + 4 files changed, 87 insertions(+) create mode 100644 __tests__/formats/jsonFlat.test.js diff --git a/__tests__/formats/__snapshots__/all.test.js.snap b/__tests__/formats/__snapshots__/all.test.js.snap index 44e2328bf..806483adb 100644 --- a/__tests__/formats/__snapshots__/all.test.js.snap +++ b/__tests__/formats/__snapshots__/all.test.js.snap @@ -478,6 +478,12 @@ exports[`formats all should return json as a string 1`] = ` exports[`formats all should return json/asset as a string 1`] = `"{}"`; +exports[`formats all should return json/flat as a string 1`] = ` +"{ + \\"color_red\\": \\"#FF0000\\" +}" +`; + exports[`formats all should return json/nested as a string 1`] = ` "{ \\"color\\": { diff --git a/__tests__/formats/jsonFlat.test.js b/__tests__/formats/jsonFlat.test.js new file mode 100644 index 000000000..2bf19a0e6 --- /dev/null +++ b/__tests__/formats/jsonFlat.test.js @@ -0,0 +1,63 @@ +/* + * 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 formats = require('../../lib/common/formats'); +var fs = require('fs-extra'); +var helpers = require('../__helpers'); + +var file = { + "destination": "__output/", + "format": "json/flat" +}; + +var dictionary = { + "allProperties": [{ + "name": "color-base-red", + "value": "#EF5350", + "original": { + "value": "#EF5350" + }, + "attributes": { + "category": "color", + "type": "base", + "item": "red" + }, + "path": [ + "color", + "base", + "red" + ] + }] +}; + +var formatter = formats['json/flat'].bind(file); + +describe('formats', () => { + describe('json/flat', () => { + + beforeEach(() => { + helpers.clearOutput(); + }); + + afterEach(() => { + helpers.clearOutput(); + }); + + it('should be a valid JSON file', () => { + fs.writeFileSync('./__tests__/__output/output.flat.json', formatter(dictionary) ); + var test = require('../__output/output.flat.json'); + expect(test['color-base-red']).toEqual(dictionary.allProperties[0].value); + }); + }); + +}); diff --git a/lib/common/formats.js b/lib/common/formats.js index b9a222bc8..f80abbfaf 100644 --- a/lib/common/formats.js +++ b/lib/common/formats.js @@ -585,6 +585,23 @@ module.exports = { return JSON.stringify(minifyDictionary(dictionary.properties), null, 2); }, + /** + * Creates a JSON flat file of the style dictionary. + * + * @memberof Formats + * @kind member + * @example + * ```json + * { + * "color-base-red": "#ff000" + * } + * ``` + */ + 'json/flat': function(dictionary) { + return '{\n' + _.map(dictionary.allProperties, function(prop) { + return ` "${prop.name}": ${JSON.stringify(prop.value)}`; + }).join(',\n') + '\n}'; + }, /** * Creates a sketchpalette file of all the base colors diff --git a/package.json b/package.json index 9bb419c09..da88cad17 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "scripts": { "lint": "eslint index.js lib/**/*.js test/*.js test/**/*.js", "test": "npm run lint && jest --runInBand", + "test-watch": "npm run lint && jest --runInBand --watch", "coverage": "istanbul cover ./node_modules/mocha/bin/_mocha -- test/*.js test/**/*.js", "preversion": "npm test", "version": "node ./scripts/version.js && npm run generate-docs",