Skip to content

Instantly share code, notes, and snippets.

@chrisvxd
Last active November 10, 2020 14:53
Show Gist options
  • Save chrisvxd/81137cb29b20a38c438c7918c776af4c to your computer and use it in GitHub Desktop.
Save chrisvxd/81137cb29b20a38c438c7918c776af4c to your computer and use it in GitHub Desktop.
Build style-dictionary from YAML config
const YAML = require('yamljs');
const globby = require('globby');
const fs = require('fs-extra');
const path = require('path');
const styleDictionary = require('style-dictionary');
const convertStringValues = obj =>
Object.keys(obj).reduce((acc, key) => {
const item = obj[key];
const hasValueKey = !!item.value;
let resolvedValue;
if (hasValueKey) {
resolvedValue = item;
} else if (typeof item === 'string') {
resolvedValue = { value: item };
} else {
resolvedValue = convertStringValues(item);
}
return {
...acc,
[key]: resolvedValue
};
}, {});
(async () => {
await fs.remove('build');
await fs.copy('properties', '.properties_json');
const paths = await globby(['.properties_json/**/*.yml']);
await Promise.all(
paths.map(async p => {
const data = YAML.parse(await fs.readFile(p, 'utf8'));
// Map value fields to objects
console.info(`Converting ${p} to JSON...\r`);
const json = JSON.stringify(convertStringValues(data));
// Write output
const newP = path.join(
path.dirname(p),
path.basename(p, '.yml') + '.json'
);
console.info(`Writing ${newP}...`);
await fs.writeFile(newP, json);
})
);
console.info('Processing style-dictionary...');
const sd = styleDictionary.extend('./config.json');
sd.buildAllPlatforms();
console.info('Removing build directory...');
await fs.remove('.properties_json');
})();
@chrisvxd
Copy link
Author

chrisvxd commented Feb 4, 2020

package.json

{
  "name": "style-dictionary-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "node scripts/build.js"
  },
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "fs-extra": "^8.1.0",
    "globby": "^11.0.0",
    "style-dictionary": "^2.8.3",
    "yamljs": "^0.3.0"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment