UNPKG

@pigeon-posse/pkg-extended-scripts

Version:

Separate scripts from package.json

113 lines (107 loc) 2.97 kB
import color from '@clippium/color'; import { formatter } from '@clippium/preset-colored'; import { defineData } from 'clippium'; const name = "@pigeon-posse/pkg-extended-scripts"; const version = "1.1.2"; const description = "Separate scripts from package.json"; const extra = { id: "pkg-extended-scripts"}; const desc = color.white.dim; const errorStyle = color.redBright; const PES_FILENAME = "pes.config"; const pkgName = name; const data = defineData({ name: extra.id, version, desc: description, positionals: { key: { type: "string", desc: "Script key for execute" } }, flags: { help: { alias: ["h"], type: "boolean", group: "General:", desc: "Show help" }, version: { alias: ["v"], type: "boolean", group: "General:", desc: "Show version" }, input: { alias: ["i"], type: "string", desc: "Custom Input directory for pes.config.{js,cjs,mjs,cjs,ts,mts}" }, debug: { type: "boolean", group: "General:", desc: "Debug mode" } } }); const config = { help: { formatter: formatter({ title: color.cyan.inverse.bold, bin: color.cyan, version: color.cyan.dim.italic, name: color.bold, positionals: color.green.dim, commands: color.green, flags: color.yellow, desc, examples: color.cyan, sectionTitle: color.white.bold.underline, sectionDesc: desc, sectionsProps: desc.italic }) }, error: { on: ({ error }) => console.error(errorStyle(error.message)) } }; const isBrowser = typeof window !== "undefined" && typeof document !== "undefined"; const trimSlashes = (segment, index) => { if (index === 0) { let i = segment.length; while (i > 0 && segment[i - 1] === "/") i--; return segment.slice(0, i); } else { let start = 0, end = segment.length; while (start < end && segment[start] === "/") start++; while (end > start && segment[end - 1] === "/") end--; return segment.slice(start, end); } }; const joinPath = async (...paths) => { if (!isBrowser) { const { join } = await import('path'); return join(...paths); } return paths.filter(Boolean).map(trimSlashes).filter(Boolean).join("/"); }; const findAndImport = async (paths) => { const pathToFileURL = isBrowser ? (path) => new URL(path) : await import('node:url').then((r) => r.pathToFileURL); for (const path of paths) { try { const mod = await import(pathToFileURL(path).href); return mod.default; } catch { } } return void 0; }; const defineConfig = (config) => config; const getConfig = async (input) => { input = input || "./"; const paths = await Promise.all([ "js", "cjs", "mjs", "ts", "cts", "mts" ].map(async (ext) => await joinPath(input, `/${PES_FILENAME}.${ext}`))); return await findAndImport(paths); }; export { defineConfig as a, config as c, data as d, getConfig as g, pkgName as p, version as v };