@chakra-icons/cli
Version:
a tooling for manage everything in @chakra-icons
210 lines (206 loc) • 7.94 kB
JavaScript
;
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/index.ts
var src_exports = {};
__export(src_exports, {
build: () => build,
buildFlow: () => buildFlow,
init: () => init
});
module.exports = __toCommonJS(src_exports);
// src/build.ts
var import_generator = __toESM(require("@babel/generator"));
var t = __toESM(require("@babel/types"));
var import_change_case = require("change-case");
var import_create_chakra_icons = require("create-chakra-icons");
var import_degit = __toESM(require("degit"));
var A = __toESM(require("fp-ts/Array"));
var E = __toESM(require("fp-ts/Either"));
var import_function = require("fp-ts/function");
var O = __toESM(require("fp-ts/Option"));
var S = __toESM(require("fp-ts/string"));
var TE = __toESM(require("fp-ts/TaskEither"));
var fs = __toESM(require("fs/promises"));
var _glob = __toESM(require("glob"));
var path = __toESM(require("path"));
var metaIconDefault = (name, repository, iconPath, clonePath, sourcePath) => ({
name,
repository,
iconPath,
clonePath,
sourcePath,
sources: []
});
var prefixWhenNumeric = (prefix) => (n) => /^\d.*$/.test(n) ? `${prefix}${n}` : n;
var moduleName = (0, import_function.flow)(prefixWhenNumeric("I"), import_change_case.pascalCase, S.replace(/[^A-Z0-9]/gi, ""));
var hotfixPkgIconNameReplace = [
[
/tabler-icons/i,
{
"device-game-pad": "device-dpad"
// prevent conflict with `device-gamepad`
}
]
];
var hotfixName = (metaIcon) => (name) => (0, import_function.pipe)(
hotfixPkgIconNameReplace,
A.findFirst(([pattern]) => pattern.test(metaIcon.repository)),
O.fold(
() => name,
([, maps]) => (0, import_function.pipe)(
maps[name],
O.fromNullable,
O.getOrElse(() => name)
)
)
);
var setSources = (metaIcon) => (beSources) => {
const createSource = (sources, svg) => {
const { dir, name: _name } = path.parse(svg);
const name = (0, import_function.pipe)(_name, hotfixName(metaIcon), moduleName);
const entryPoint = path.join(
dir.replace(path.join(metaIcon.clonePath, metaIcon.iconPath), metaIcon.sourcePath),
"index.ts"
);
const entry = { name, svg, code: path.join(path.dirname(entryPoint), `${name}.tsx`) };
const maybeIcon = sources.find((icon) => S.Eq.equals(icon.entryPoint, entryPoint));
if (maybeIcon) {
sources[sources.indexOf(maybeIcon)]?.entries.push(entry);
return sources;
}
return [...sources, { entryPoint, entries: [entry] }];
};
return {
...metaIcon,
sources: beSources.reduce((a, b) => createSource(a, b), metaIcon.sources)
};
};
var writeFile2 = (file, data) => TE.tryCatch(() => fs.mkdir(path.dirname(file), { recursive: true }).then(() => fs.writeFile(file, data)), E.toError);
var glob2 = (target) => TE.tryCatch(() => _glob.glob(target), E.toError);
var cloneRepository = (metaIcon) => TE.tryCatch(
() => (0, import_degit.default)(metaIcon.repository, { force: true }).clone(metaIcon.clonePath).then(() => metaIcon),
E.toError
);
var findSvgs = (metaIcon) => (0, import_function.pipe)(path.join(metaIcon.clonePath, metaIcon.iconPath, "**", "*.svg"), glob2, TE.map(setSources(metaIcon)));
var createIcons = (options) => TE.tryCatch(() => Promise.resolve(import_create_chakra_icons.cli.main(options)), E.toError);
var generateIcons = (metaIcon) => (0, import_function.pipe)(
metaIcon.sources,
A.map(
({ entries }) => (0, import_function.pipe)(
entries,
A.map(({ name: n, svg: i, code: o }) => createIcons({ n, i, o, ts: true, T: "C", useFilename: true }))
)
),
A.flatten,
TE.sequenceArray
);
var generateExportAllCode = (name) => (0, import_function.pipe)(`./${name}`, t.stringLiteral, t.exportAllDeclaration, import_generator.default, (b) => b.code);
var genrateExportAllNamespace = (alias, source) => (0, import_generator.default)(
t.exportNamedDeclaration(null, [(0, import_function.pipe)(alias, t.identifier, t.exportNamespaceSpecifier)], t.stringLiteral(source))
).code;
var generateEntrypointCode = (source) => (0, import_function.pipe)(
source.entries,
A.map((s) => generateExportAllCode(s.name)),
(arr) => arr.join("\r\n")
);
var generateEntrypoint = (metaIcon) => (0, import_function.pipe)(
metaIcon.sources,
A.map((a) => writeFile2(a.entryPoint, generateEntrypointCode(a))),
TE.sequenceArray
);
var generateSnapshot = (options) => (metaIcon) => (0, import_function.pipe)(
options.snapshot,
O.fromNullable,
O.fold(
() => TE.of(metaIcon),
(file) => (0, import_function.pipe)(
TE.of(metaIcon),
TE.chainFirst((_metaIcon) => writeFile2(file, JSON.stringify(_metaIcon, null, 2)))
)
)
);
var generateRootEntryPoint = (metaIcon) => (0, import_function.pipe)(
metaIcon.sources,
A.findFirst((source) => S.Eq.equals(source.entryPoint, path.join(metaIcon.sourcePath, "index.ts"))),
O.fold(
() => [
writeFile2(
path.join(metaIcon.sourcePath, "index.ts"),
(0, import_function.pipe)(
metaIcon.sources,
// eslint-disable-next-line @typescript-eslint/unbound-method
A.map((source) => (0, import_function.pipe)(source.entryPoint, path.dirname, path.basename)),
A.map((source) => genrateExportAllNamespace(source, "./".concat(source))),
(arrs) => arrs.join("\r\n")
)
)
],
() => []
),
TE.sequenceArray
);
var buildFlow = (0, import_function.flow)(
// make MetaIcon and cloneRepository
TE.chain(cloneRepository),
// scan svg in target directory
TE.chain(findSvgs),
// generated icons (src/*.tsx) in file system
TE.chainFirst(generateIcons),
// generated index.ts
TE.chainFirst(generateEntrypoint),
// generated src/index.ts when iconPath is nested
TE.chainFirst(generateRootEntryPoint)
);
var build = (options) => {
const { name, repository, iconPath, clonePath, sourcePath } = options;
const metaIcon = metaIconDefault(name, repository, iconPath, clonePath, sourcePath);
return (0, import_function.pipe)(
// make `metaIcon` object to TaskEither
TE.of(metaIcon),
// main flow
buildFlow,
// when pass `snapshot` in options
TE.chainFirst(generateSnapshot(options))
// when pass `entryPoints` in options
// it will be make entrypoints.ts for use in tsup.config
// TE.chainFirst(generateExportEntrypoints(options)),
// it will be update package.json
// TE.chainFirst(updatePkgJson(options)),
);
};
// src/init.ts
var init = (options) => {
console.log({ options });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
build,
buildFlow,
init
});