UNPKG

@chakra-ui/cli

Version:

Generate theme typings for autocomplete

60 lines (57 loc) 1.88 kB
"use strict"; import { log } from '@clack/prompts'; import { bundleNRequire } from 'bundle-n-require'; import chokidar from 'chokidar'; import { existsSync, mkdirSync, rm } from 'node:fs'; import { writeFile } from 'node:fs/promises'; import { dirname, resolve, join } from 'node:path'; const isValidSystem = (mod) => { return Object.hasOwnProperty.call(mod, "$$chakra"); }; const read = async (file) => { const filePath = resolve(file); const { mod, dependencies } = await bundleNRequire(filePath); const resolvedMod = mod.default || mod.preset || mod.system || mod; if (!isValidSystem(resolvedMod)) { throw new Error( `No default export found in ${file}. Did you forget to provide an export default?` ); } return { mod: resolvedMod, dependencies }; }; const outPath = (path, file) => { const ext = process.env.LOCAL ? "ts" : "d.ts"; return join(path, `${file}.${ext}`); }; function ensureDir(dirPath) { if (existsSync(dirPath)) return; ensureDir(dirname(dirPath)); mkdirSync(dirPath); } const write = async (path, file, content) => { try { await writeFile(outPath(path, file), await content); } catch (error) { throw new Error( `Failed to write file ${outPath(path, file)}: ${error instanceof Error ? error.message : String(error)}` ); } }; function watch(paths, cb) { const watcher = chokidar.watch(paths, { ignoreInitial: true }); watcher.on("ready", cb).on("change", async (filePath) => { log.info(`\u{1F4E6} File changed: ${filePath}`); return cb(); }); process.once("SIGINT", () => watcher.close()); process.once("SIGTERM", () => watcher.close()); } async function clean(basePath) { log.info("\u{1F9F9} Cleaning output directory"); rm(basePath, { recursive: true }, (err) => { if (err) { log.error(err.message); } }); } export { clean, ensureDir, read, watch, write };