UNPKG

@knapsack/app

Version:

Build Design Systems with Knapsack

126 lines (124 loc) 4.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.saveFilesLocally = exports.getIsSavingLocally = exports.babelCodeForBrowser = exports.isRemoteUrl = void 0; exports.fileExistsOrExit = fileExistsOrExit; /** * 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 fs_extra_1 = __importDefault(require("fs-extra")); const core_1 = require("@babel/core"); const babel_config_starter_1 = require("@knapsack/babel-config-starter"); const utils_1 = require("@knapsack/utils"); const file_utils_1 = require("@knapsack/file-utils"); const log_1 = require("../cli/log"); var utils_2 = require("@knapsack/utils"); Object.defineProperty(exports, "isRemoteUrl", { enumerable: true, get: function () { return utils_2.isRemoteUrl; } }); function fileExistsOrExit(filePath, msg) { if ((0, file_utils_1.isPathFile)(filePath)) return; log_1.log.error(msg || `This file does not exist! ${filePath}`); process.exit(1); } /** * Useful for taking code generated on the server and transpiling it to the client * @example * function run(name: string) { * console.log(name); * } * const html = ` * <script> * ${run.toString()} * ${run.name}('hi!'); * </script> * `; * const transpiled = babelCodeForBrowser({ code: html }); */ exports.babelCodeForBrowser = (0, utils_1.memoize)(({ code, minified }) => { return new Promise((resolve, reject) => { (0, core_1.transform)(code, { ...babel_config_starter_1.basicBrowserBabelConfig, configFile: false, highlightCode: false, minified, comments: true, }, (err, results) => { if (err) { reject(err); } else { resolve(results.code); } }); }); }); /** * Transpile/compile modern JS (including TS) using TypeScript * @deprecated use `babelCodeForBrowser` instead * @see {babelCodeForBrowser} * just leaving this in since it's a useful example */ // function transpileCodeString({ // code, // module, // }: { // code: string; // module: 'cjs' | 'es'; // }): string { // const { outputText } = transpileModule(code, { // compilerOptions: { // module: module === 'es' ? ts.ModuleKind.ES2020 : ts.ModuleKind.CommonJS, // jsx: ts.JsxEmit.React, // allowSyntheticDefaultImports: true, // esModuleInterop: true, // moduleResolution: ts.ModuleResolutionKind.Node16, // resolveJsonModule: true, // }, // }); // return outputText; // } let isSavingLocally = false; let timeoutId; const getIsSavingLocally = () => isSavingLocally; exports.getIsSavingLocally = getIsSavingLocally; const saveFilesLocally = async ({ files }) => { clearTimeout(timeoutId); isSavingLocally = true; await Promise.all(files.map(({ contents, path, encoding, isDeleted }) => { if (isDeleted) { return (0, file_utils_1.remove)(path); } switch (encoding) { case 'utf8': return (0, file_utils_1.writeFile)({ path, contents }); case 'base64': { const data = Buffer.from(contents, 'base64'); return fs_extra_1.default.writeFile(path, data); } default: throw new Error(`Incorrect encoding used when saving files locally: "${encoding}" for file ${path}.`); } })); timeoutId = setTimeout(() => { isSavingLocally = false; }, 2_000); return { ok: true, message: 'All config files saved locally.', }; }; exports.saveFilesLocally = saveFilesLocally; //# sourceMappingURL=server-utils.js.map