UNPKG

@eljs/utils

Version:
215 lines (213 loc) 7.71 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/file/copy.ts var copy_exports = {}; __export(copy_exports, { copyDirectory: () => copyDirectory, copyDirectorySync: () => copyDirectorySync, copyFile: () => copyFile, copyFileSync: () => copyFileSync, copyTpl: () => copyTpl, copyTplSync: () => copyTplSync }); module.exports = __toCommonJS(copy_exports); var import_chalk = __toESM(require("chalk")); var import_glob = require("glob"); var import_node_fs = __toESM(require("node:fs")); var import_promises = __toESM(require("node:fs/promises")); var import_node_path = __toESM(require("node:path")); var import_dir = require("./dir"); var import_is = require("./is"); var import_read = require("./read"); var import_render = require("./render"); var import_write = require("./write"); async function copyFile(from, to, options = {}) { try { let destFile = convertFilePrefix(to); const { mode, basedir, data, renderOptions } = options; if (destFile.indexOf("{{") > -1 || destFile.indexOf("<%") > -1) { destFile = (0, import_render.renderTemplate)(destFile, data || {}, renderOptions); } await (0, import_dir.mkdir)(import_node_path.default.dirname(destFile)); if (basedir) { console.log( `${import_chalk.default.green("Copy: ")} ${import_node_path.default.relative(basedir, destFile)}` ); } await import_promises.default.copyFile(from, destFile, mode); } catch (error) { const err = error; err.message = `Copy file from ${from} to ${to} failed: ${err.message}`; throw err; } } function copyFileSync(from, to, options = {}) { try { let destFile = convertFilePrefix(to); const { mode, basedir, data, renderOptions } = options; if (destFile.indexOf("{{") > -1 || destFile.indexOf("<%") > -1) { destFile = (0, import_render.renderTemplate)(destFile, data || {}, renderOptions); } (0, import_dir.mkdirSync)(import_node_path.default.dirname(destFile)); if (basedir) { console.log( `${import_chalk.default.green("Copy: ")} ${import_node_path.default.relative(basedir, destFile)}` ); } import_node_fs.default.copyFileSync(from, destFile, mode); } catch (error) { const err = error; err.message = `Copy file from ${from} to ${to} failed: ${err.message}`; throw err; } } async function copyTpl(from, to, data, options) { const { basedir, renderOptions } = options || {}; const tpl = await (0, import_read.readFile)(from); try { const content = (0, import_render.renderTemplate)(tpl, data, renderOptions); let destFile = convertFilePrefix(to.replace(/\.tpl$/, "")); if (destFile.indexOf("{{") > -1 || destFile.indexOf("<%") > -1) { destFile = (0, import_render.renderTemplate)(destFile, data, renderOptions); } await (0, import_dir.mkdir)(import_node_path.default.dirname(destFile)); if (basedir) { console.log( `${import_chalk.default.green("Write:")} ${import_node_path.default.relative(basedir, destFile)}` ); } await (0, import_write.writeFile)(destFile, content); } catch (error) { const err = error; err.message = `Copy template from ${from} to ${to} failed: ${err.message}`; throw err; } } function copyTplSync(from, to, data, options) { const { basedir, renderOptions } = options || {}; const tpl = (0, import_read.readFileSync)(from); try { const content = (0, import_render.renderTemplate)(tpl, data, renderOptions); let destFile = convertFilePrefix(to.replace(/\.tpl$/, "")); if (destFile.indexOf("{{") > -1 || destFile.indexOf("<%") > -1) { destFile = (0, import_render.renderTemplate)(destFile, data, renderOptions); } (0, import_dir.mkdirSync)(import_node_path.default.dirname(destFile)); if (basedir) { console.log( `${import_chalk.default.green("Write:")} ${import_node_path.default.relative(basedir, destFile)}` ); } (0, import_write.writeFileSync)(destFile, content); } catch (error) { const err = error; err.message = `Copy template from ${from} to ${to} failed: ${err.message}`; throw err; } } async function copyDirectory(from, to, data, options) { options = options || {}; try { const files = await (0, import_glob.glob)("**/*", { cwd: from, dot: true, ignore: ["**/node_modules/**"] }); for (const file of files) { const srcFile = import_node_path.default.join(from, file); if (await (0, import_is.isDirectory)(srcFile)) { continue; } const destFile = import_node_path.default.join(to, file); if (file.endsWith(".tpl")) { await copyTpl(srcFile, destFile, data, options); } else { await copyFile(srcFile, destFile, { ...options, data }); } } } catch (error) { const err = error; err.message = `Copy directory from ${from} to ${to} failed: ${err.message}`; throw err; } } function copyDirectorySync(from, to, data, options) { options = options || {}; try { const files = (0, import_glob.globSync)("**/*", { cwd: from, dot: true, ignore: ["**/node_modules/**"] }); for (const file of files) { const srcFile = import_node_path.default.join(from, file); if ((0, import_is.isDirectorySync)(srcFile)) { continue; } const destFile = import_node_path.default.join(to, file); if (file.endsWith(".tpl")) { copyTplSync(srcFile, destFile, options); } else { copyFileSync(srcFile, destFile, { ...options, data }); } } } catch (error) { const err = error; err.message = `Copy directory from ${from} to ${to} failed: ${err.message}`; throw err; } } function convertFilePrefix(file, prefix = "-") { if (file.indexOf(prefix) === -1) { return file; } return file.split("/").map((filename) => { if (filename.charAt(0) === prefix && filename.charAt(1) !== prefix) { return `.${filename.slice(1)}`; } if (filename.charAt(0) === prefix && filename.charAt(1) === prefix) { return `${filename.slice(1)}`; } return filename; }).join("/"); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { copyDirectory, copyDirectorySync, copyFile, copyFileSync, copyTpl, copyTplSync });