@knapsack/app
Version:
Build Design Systems with Knapsack
115 lines • 3.92 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const yargs_1 = __importDefault(require("yargs/yargs"));
const helpers_1 = require("yargs/helpers");
const path_1 = require("path");
const file_utils_1 = require("@knapsack/file-utils");
const bootstrap_1 = require("../lib/bootstrap");
const { config } = (0, bootstrap_1.bootstrapForCli)();
const srcFormats = [
'knapsack',
'w3c',
'tokens-studio',
'style-dictionary',
];
const { verbose, src, clean, dist, 'src-format': srcFormatUntyped, } = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
.option('src', {
type: 'string',
description: 'Path to the design tokens JSON file',
demandOption: true,
})
.option('dist', {
type: 'string',
description: 'Path to the directory where the files will be written',
demandOption: true,
})
.option('clean', {
type: 'boolean',
description: 'Delete the dist directory before writing files',
default: false,
})
.option('src-format', {
type: 'string',
choices: srcFormats,
default: 'knapsack',
})
.option('config', {
type: 'string',
description: 'Path to knapsack.config.cjs file',
})
.option('verbose', {
description: 'Show verbose output',
type: 'boolean',
default: false,
})
.strictOptions().argv;
const srcFormat = srcFormatUntyped;
async function run() {
var _a;
try {
console.log(`Building design tokens...`);
const { convertTokenDataToFiles, convertStudioSrcToSpecSrc, convertStyleDictFormat, } = await import('@knapsack/design-token-utils');
const srcPath = (0, path_1.isAbsolute)(src) ? src : (0, path_1.join)(process.cwd(), src);
const distDir = (0, path_1.isAbsolute)(dist) ? dist : (0, path_1.join)(process.cwd(), dist);
if (!(await (0, file_utils_1.exists)(srcPath))) {
throw new Error(`The design tokens JSON file does not exist: ${srcPath}`);
}
if (clean) {
await (0, file_utils_1.emptyDir)(distDir);
}
else {
await (0, file_utils_1.ensureDir)(distDir);
}
let tokensSrc;
switch (srcFormat) {
case 'style-dictionary': {
tokensSrc = convertStyleDictFormat({
tokensSrcGroup: await (0, file_utils_1.readJSON)(srcPath),
});
break;
}
case 'tokens-studio': {
const result = convertStudioSrcToSpecSrc({
src: await (0, file_utils_1.readJSON)(srcPath),
});
tokensSrc = result.data;
break;
}
case 'knapsack':
case 'w3c': {
tokensSrc = await (0, file_utils_1.readJSON)(srcPath);
break;
}
default: {
const _exhaustiveCheck = srcFormat;
}
}
const files = convertTokenDataToFiles({
tokensSrc,
targets: (_a = config === null || config === void 0 ? void 0 : config.designTokens) === null || _a === void 0 ? void 0 : _a.targets,
});
await Promise.all(files.map(async ({ path, contents }) => {
const { base } = (0, path_1.parse)(path);
await (0, file_utils_1.writeFormattedFile)({
path: (0, path_1.join)(distDir, base),
contents,
});
}));
console.log(distDir);
console.log(`Design tokens built successfully to above directory!`);
}
catch (e) {
if (verbose) {
console.log(e);
}
else {
console.log(e.message);
}
process.exit(1);
}
}
run();
//# sourceMappingURL=build-tokens.js.map