@knapsack/app
Version:
Build Design Systems with Knapsack
64 lines • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DesignTokens = void 0;
const file_utils_1 = require("@knapsack/file-utils");
const path_1 = require("path");
const file_db_1 = require("../../server/dbs/file-db");
class DesignTokens extends file_db_1.FileDb {
constructor({ dataDir, designTokensConfig, distDir, publicDir, siteId, }) {
if (designTokensConfig === null || designTokensConfig === void 0 ? void 0 : designTokensConfig.srcFilePath) {
(0, file_utils_1.assertFileExists)(designTokensConfig.srcFilePath, `The configured 'designTokens.srcFilePath' in knapsack.config.js file does not exist: ${designTokensConfig.srcFilePath}`);
}
const configFilePath = (designTokensConfig === null || designTokensConfig === void 0 ? void 0 : designTokensConfig.srcFilePath) ||
(0, path_1.join)(dataDir, 'knapsack.design-tokens.json');
super({
filePath: configFilePath,
defaults: {},
type: 'json',
writeFileIfAbsent: true,
});
this.hydrate = async ({ appClientData: { tokensSrc } }) => {
this.data = tokensSrc;
};
this.build = async () => {
const { convertTokenDataToFiles } = await import('@knapsack/design-token-utils');
const files = await this.getData()
.then((tokensSrc) => {
var _a;
try {
return convertTokenDataToFiles({
tokensSrc,
targets: (_a = this.designTokensConfig) === null || _a === void 0 ? void 0 : _a.targets,
});
}
catch (error) {
throw new Error(`Failed to convert tokens: ${error.message}`);
}
})
.then((tokenFiles) => tokenFiles.map(({ path, contents }) => {
const { base } = (0, path_1.parse)(path);
return {
path: (0, path_1.join)(this.distDir, base),
contents,
encoding: 'utf8',
};
}));
try {
await Promise.all(files.map(file_utils_1.writeFormattedFile));
}
catch (error) {
throw new Error(`Failed to save files locally: ${error.message}`);
}
};
this.watch = () => {
super.watchFiles({
handleChange: () => this.build(),
});
};
this.distDir = distDir;
this.site = siteId;
this.designTokensConfig = designTokensConfig;
}
}
exports.DesignTokens = DesignTokens;
//# sourceMappingURL=design-tokens.js.map