UNPKG

@chakra-icons/cli

Version:

a tooling for manage everything in @chakra-icons

272 lines (263 loc) 8.62 kB
#!/usr/bin/env node import { build, init } from "./chunk-OMRMG4XN.mjs"; // index.bin.ts import arg from "arg"; import * as ruins from "ruins-ts"; // package.json var package_default = { name: "@chakra-icons/cli", description: "a tooling for manage everything in @chakra-icons", version: "2.0.0", author: "ri7nz <ri7nz@koding.ninja>", homepage: "https://github.com/kodingdotninja/chakra-icons", repository: "https://github.com/kodingdotninja/chakra-icons.git", bugs: "https://github.com/kodingdotninja/chakra-icons/issues", bin: { "chakra-icons": "dist/index.bin.js" }, main: "./dist/index.js", module: "./dist/index.mjs", types: "./dist/index.d.ts", scripts: { "# general commands": "--------------------------------------------------", build: "tsup", format: 'prettier --write "**/*.{js,json,md,svg}"', lint: 'eslint --fix "**/*.ts"', "#": "-------------------------------------------------------------------" }, dependencies: { "@babel/generator": "^7.22.10", "@babel/types": "^7.22.11", arg: "^5.0.2", "change-case": "^4.1.2", "create-chakra-icons": "*", degit: "npm:tiged", "fp-ts": "^2.16.1", glob: "^10.3.3", "ruins-ts": "^0.0.4" }, devDependencies: { "@types/degit": "^2.8.3", eslint: "^8.47.0", "eslint-plugin-fp-ts": "^0.3.2", tsup: "^7.2.0", typescript: "^5.2.2" }, files: [ "dist/" ], keywords: [ "kdnj", "kodingdotninja", "chakra-icons", "create-chakra-icons" ], license: "MIT" }; // src/clean.ts import * as A from "fp-ts/Array"; import * as E from "fp-ts/Either"; import { pipe } from "fp-ts/function"; import * as TE from "fp-ts/TaskEither"; import * as fs from "fs/promises"; import _glob from "glob"; import path from "path"; var clean = (options) => pipe( TE.tryCatch(() => _glob.glob(path.join(options.sourcePath, "**", "*.{ts,tsx}")), E.toError), TE.chainFirst(() => TE.tryCatch(() => fs.rmdir(options.clonePath, { recursive: true, maxRetries: 2 }), E.toError)), TE.chainFirst(() => TE.tryCatch(() => fs.rmdir(path.join("dist"), { recursive: true, maxRetries: 2 }), E.toError)), TE.chainFirst( (files) => pipe( files, A.map((file) => TE.tryCatch(() => fs.rm(file), E.toError)), TE.sequenceSeqArray ) ) ); // src/prepack.ts import * as fs2 from "fs/promises"; import path2 from "path"; var noOp = (a) => a; var ifThen = (condition, whenTrue, whenFalse) => (a) => (condition ? whenTrue : whenFalse)(a); var removeDevDependencies_ = ({ devDependencies: _, ...pkgJson }) => ({ ...pkgJson }); var addPeerDependencies_ = (peerDependencies) => (pkgJson) => ({ ...pkgJson, peerDependencies }); var addPkgScripts = (newScripts) => ({ scripts, ...pkgJson }) => ({ ...pkgJson, scripts: { ...scripts, ...newScripts } }); var mapDeps = (arr) => arr.reduce((acc, cur) => { const pkgVerRegex = ( // eslint-disable-next-line prefer-named-capture-group /(^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*)@([~^]?([\dvx*]+(?:[-.](?:[\dx*]+|alpha|beta))*))?$/ ); const [, name, , version2] = pkgVerRegex.exec(cur) ?? []; if (!name) throw Error("Package Name invalid"); if (!version2) throw Error("Package Version invalid"); return { ...acc, [name]: version2.replace("@", "") }; }, {}); var mapScripts = (arr) => arr.reduce((acc, cur) => { const [name, version2] = cur.split("="); if (!name) throw Error("Package Name invalid"); if (!version2) throw Error("Package Version invalid"); return { ...acc, [name]: version2 }; }, {}); var prepack = ({ removeDevDeps = false, addPeerDeps = [], addScripts: addScripts_ = [] }) => { if (!removeDevDeps && addPeerDeps.length === 0 && addScripts_.length === 0) return; const pkgJsonPath = path2.resolve("./package.json"); const removeDevDependencies = ifThen(removeDevDeps, removeDevDependencies_, noOp); const addPeerDependencies = ifThen(addPeerDeps.length > 0, addPeerDependencies_(mapDeps(addPeerDeps)), noOp); const addScripts = ifThen(addScripts_.length > 0, addPkgScripts(mapScripts(addScripts_)), noOp); const writePkgJson = (pkgJson) => fs2.writeFile(pkgJsonPath, pkgJson); return fs2.readFile(pkgJsonPath, { encoding: "utf8" }).then(JSON.parse).then(removeDevDependencies).then(addPeerDependencies).then(addScripts).then((pkgJson) => JSON.stringify(pkgJson, null, 2)).then(writePkgJson); }; // index.bin.ts var parseArgs = () => { try { const argv = process.argv.slice(2); const args = arg( { // COMMON OPTIONS "--help": Boolean, "-h": "--help", "--version": Boolean, "-v": "--version", // REQUIRED "--name": String, "-n": "--name", "--repo": String, "--icon-path": String, "-r": "--repo", "-i": "--icon-path", "--output": String, "-o": "--output", // OPTIONS "--clone-path": String, "-C": "--clone-path", "--snapshot": String, "-S": "--snapshot", // FLAGS "--with-entrypoints": Boolean, "--with-clean": Boolean, "-E": "--with-entrypoints", // Prepack Related "--remove-dev-deps": Boolean, "--add-peer-deps": String, // example --add-peer-deps "pkg@v1,pkg2@>=2" "--add-scripts": String, "--rd": "--remove-dev-deps", "--ap": "--add-peer-deps", "--ax": "--add-scripts" }, { argv } ); const [first] = args._; const { "--name": name, "--repo": repository, "--icon-path": iconPath, "--clone-path": _clonePath, "--output": _sourcePath, "--snapshot": snapshot, "--with-entrypoints": entryPoints, "--version": version2, "--help": help2, "--remove-dev-deps": removeDevDeps, "--add-peer-deps": addPeerDeps, "--add-scripts": addScripts } = args; const clonePath = _clonePath ?? ".chakraIcons"; const sourcePath = _sourcePath ?? "src"; if (first === "prepack") return { type: "prepack", options: { removeDevDeps, addPeerDeps: addPeerDeps?.split(",") ?? [], addScripts: addScripts?.split(",") ?? [] } }; if (first === "init" && name && repository && iconPath) return { type: "init", options: { name, repository, iconPath } }; if (name && repository && iconPath && clonePath && sourcePath) { const options = { name, repository, iconPath, clonePath, sourcePath, snapshot, entryPoints: Boolean(entryPoints) }; if (first === "clean") return { type: "clean", options }; return { type: "build", options }; } if (version2) return { type: "version" }; if (help2) return { type: "help" }; return { type: "fail", error: Error("cannot parse any arguments :(") }; } catch (e) { return { type: "fail", error: e }; } }; var help = () => process.stdout.write(` USAGE: ${package_default.name} <SUBCOMMAND> SUBCOMMAND: init: init -n <NAME> -r <ORGS/REPO-NAME> -i <PATH/SVG> build: build -n <NAME> -r <ORGS/REPO-NAME> -i <PATH/SVG> OPTIONS: --snapshot <NAME>, -S <NAME> create snapshot information (.json). FLAGS: --with-entrypoints, -E generate entrypoints when generating new icon. prepack: tools for manage package.json fields OPTIONS: --add-peer-deps, --ap add peer dependencies (e.g --ap "react@^17,react-dom@^17") --add-scripts, --ax add new script in scripts field of package.json (e.g --ax "prcommit=commitlint") FLAGS: --remove-dev-deps, --rd remove field devDependencies in current work directory (package.json) `); var version = () => console.log(package_default.version); var fail = (e) => { console.log("something wrong :( "); console.error(String(e)); process.exit(1); }; var run = async (command) => { switch (command.type) { case "prepack": return prepack(command.options); case "build": return ruins.fromTaskEither(build(command.options)).catch((e) => fail(e)); case "init": return init(command.options); case "clean": return ruins.fromTaskEither(clean(command.options)).then((files) => { console.log(`Clean ${files.length} files`); }).catch((e) => fail(e)); case "fail": return fail(command.error); case "version": return version(); default: return help(); } }; run(parseArgs()).then(() => process.exit(0)).catch((e) => console.error(e));