Skip to content

Commit

Permalink
fix: cli --platform flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Dec 17, 2024
1 parent 737a02b commit 8439dd5
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-bananas-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Add and check types to CLI file, fix typo for --platform flag.
38 changes: 33 additions & 5 deletions bin/style-dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,39 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import node_fs from 'node:fs';
import JSON5 from 'json5';
import program from 'commander';
import { Command } from 'commander';
// usually also node:fs in this context, but can be customized by user
import { fs } from 'style-dictionary/fs';
import StyleDictionary from 'style-dictionary';
import { logWarningLevels, logVerbosityLevels } from '../lib/enums/index.js';

/**
* @typedef {{
* config?: string;
* platform?: string[];
* verbose?: boolean;
* warn?: boolean;
* silent?: boolean;
* }} BuildOptions
*/

const program = new Command();
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const { silent, verbose } = logVerbosityLevels;
const pkg = JSON5.parse(node_fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8'));

/**
* @param {string} val
* @param {string[]} arr
*/
function collect(val, arr) {
arr.push(val);
return arr;
}

/**
* @param {BuildOptions} options
*/
function getConfigPath(options) {
let configPath = options.config;

Expand Down Expand Up @@ -108,6 +126,10 @@ program.on('command:*', function () {
process.exit(1);
});

/**
* @param {string} configPath
* @param {BuildOptions} options
*/
function getSD(configPath, options) {
let verbosity;
let warnings;
Expand All @@ -120,27 +142,33 @@ function getSD(configPath, options) {
return new StyleDictionary(configPath, { verbosity, warnings });
}

/**
* @param {BuildOptions} [options]
*/
async function styleDictionaryBuild(options) {
options = options || {};
const configPath = getConfigPath(options);
const sd = getSD(configPath, options);

if (options.platform && options.platform.length > 0) {
return Promise.all(options.platforms.map((platform) => sd.buildPlatform(platform)));
Promise.all(options.platform.map((platform) => sd.buildPlatform(platform)));
} else {
return sd.buildAllPlatforms();
await sd.buildAllPlatforms();
}
}

/**
* @param {BuildOptions} [options]
*/
async function styleDictionaryClean(options) {
options = options || {};
const configPath = getConfigPath(options);
const sd = getSD(configPath, options);

if (options.platform && options.platform.length > 0) {
return Promise.all(options.platforms.map((platform) => sd.cleanPlatform(platform)));
Promise.all(options.platform.map((platform) => sd.cleanPlatform(platform)));
} else {
return sd.cleanAllPlatforms();
await sd.cleanAllPlatforms();
}
}

Expand Down
29 changes: 29 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"source": ["studio.json"],
"log": {
"verbosity": "default"
},
"platforms": {
"css": {
"transformGroup": "css",
"transforms": ["name/kebab"],
"buildPath": "build/css/",
"files": [
{
"destination": "_variables.css",
"format": "css/variables"
}
]
},
"js": {
"transformGroup": "js",
"buildPath": "build/js/",
"files": [
{
"destination": "variables.js",
"format": "javascript/es6"
}
]
}
}
}
21 changes: 16 additions & 5 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"@zip.js/zip.js": "^2.7.44",
"chalk": "^5.3.0",
"change-case": "^5.3.0",
"commander": "^8.3.0",
"commander": "^12.1.0",
"is-plain-obj": "^4.1.0",
"json5": "^2.2.2",
"patch-package": "^8.0.0",
Expand Down
10 changes: 10 additions & 0 deletions studio.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"colors": {
"$type": "color",
"red": {
"500": {
"$value": "#f00"
}
}
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"skipLibCheck": true,
"declaration": true
},
"include": ["lib/**/*.js", "**/*.ts"],
"include": ["lib/**/*.js", "**/*.ts", "bin/**/*.js"],
"exclude": ["node_modules", "**/coverage/*"]
}

0 comments on commit 8439dd5

Please sign in to comment.