UNPKG

@knapsack/app

Version:

Build Design Systems with Knapsack

148 lines (146 loc) 5.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processConfig = processConfig; /** * Copyright (C) 2018 Basalt This file is part of Knapsack. Knapsack is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Knapsack is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Knapsack; if not, see <https://www.gnu.org/licenses>. */ const path_1 = require("path"); const utils_1 = require("@knapsack/utils"); const file_utils_1 = require("@knapsack/file-utils"); const zod_1 = require("zod"); const renderer_base_1 = require("../domains/patterns/renderers/renderer-base"); const log_1 = require("../cli/log"); const events_1 = require("../server/events"); const knapsack_config_1 = require("../types/knapsack-config"); /** * Handle backwards compatibility of config */ function convertOldConfig(config) { // handle backward compatibility here, may need to make new Interface return config; } function makeConfigPathsAbsolute({ config, base, }) { function prepPath(path) { if (!path) return undefined; const absPath = (0, file_utils_1.isAbsolute)(path) ? path : (0, path_1.join)(base, path); (0, file_utils_1.assertsIsAbsolutePath)(absPath); return absPath; } return { ...config, public: prepPath(config.public), data: prepPath(config.data), dist: prepPath(config.dist), designTokens: !config.designTokens ? null : { ...config.designTokens, distDir: prepPath(config.designTokens.distDir), srcFilePath: prepPath(config.designTokens.srcFilePath), }, cloud: !config.cloud ? null : { ...config.cloud, repoRoot: prepPath(config.cloud.repoRoot), }, devServer: !config.devServer ? null : { ...config.devServer, ssl: !config.devServer.ssl ? null : { ...config.devServer.ssl, key: prepPath(config.devServer.ssl.key), cert: prepPath(config.devServer.ssl.cert), }, }, }; } function validateConfig({ config, base, }) { if (config.cloud?.repoRoot) { if (!(0, file_utils_1.isAbsolute)(config.cloud.repoRoot)) { throw new Error(`cloud.repoRoot must be an absolute path: ${config.cloud.repoRoot}`); } const repoRoot = (0, file_utils_1.findGitRoot)(base); if (repoRoot && (0, path_1.parse)(repoRoot).dir !== (0, path_1.parse)(config.cloud.repoRoot).dir) { log_1.log.warn(`In "cloud.repoRoot" you specified "${config.cloud.repoRoot}" but that directory is not the correct git root: "${repoRoot}".`); } } (0, utils_1.assertUniqueIdInArray)({ items: config.templateRenderers, key: 'id', errorMsgPrefix: 'Each templateRenderer must have a unique id.', }); // Don't warn when the `@knapsack/update` cli is running. if (process.env.KS_UPDATE_CLI_IS_RUNNING !== 'yes') { config.templateRenderers.forEach((templateRenderer, i) => { const id = templateRenderer.id || `number ${i + 1}`; if (templateRenderer instanceof renderer_base_1.RendererBase === false) { log_1.log.error(`Each templateRenderer must be an instance of "KnapsackRenderer" and ${id} is not`); process.exit(1); } }); } (0, file_utils_1.ensureDirSync)(config.data); (0, file_utils_1.ensureDirSync)(config.public); (0, utils_1.assertUniqueIdInArray)({ items: config.plugins, key: 'id', errorMsgPrefix: 'Each plugin must have a unique id.', }); if (config.devServer?.ssl) { if (!config.devServer.ssl.key || !config.devServer.ssl.cert) { throw new Error(`devServer.ssl.key and devServer.ssl.cert must be provided together`); } if (!(0, file_utils_1.existsSync)(config.devServer.ssl.key)) { throw new Error(`devServer.ssl.key points to file that does not exist: "${(0, file_utils_1.relative)(base, config.devServer.ssl.key)}"`); } if (!(0, file_utils_1.existsSync)(config.devServer.ssl.cert)) { throw new Error(`devServer.ssl.cert points to file that does not exist: "${(0, file_utils_1.relative)(base, config.devServer.ssl.cert)}"`); } } } /** * Prepare user config: validate, convert all paths to absolute, assign defaults */ function processConfig({ userConfig, configPathBase, configFilePath, }) { let config; try { const { error } = knapsack_config_1.KnapsackConfigSchema.safeParse(userConfig); if (error) { throw new Error(zod_1.z.prettifyError(error)); } config = makeConfigPathsAbsolute({ config: { plugins: [], ...convertOldConfig(userConfig), }, base: configPathBase, }); validateConfig({ config, base: configPathBase, }); } catch (e) { log_1.log.error(new Error(`Invalid Config in "${(0, file_utils_1.relative)(process.cwd(), configFilePath)}"`, { cause: e })); process.exit(1); } events_1.knapsackEvents.emitConfigReady(config); return config; } //# sourceMappingURL=config.js.map