@ikona/cli
Version:
200 lines (190 loc) • 6.58 kB
JavaScript
"use strict";
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/illustrations/types.ts
var types_exports = {};
__export(types_exports, {
generateIllustrationTypes: () => generateIllustrationTypes
});
module.exports = __toCommonJS(types_exports);
var import_fs_extra2 = __toESM(require("fs-extra"));
var import_glob = require("glob");
var path = __toESM(require("path"));
// src/utils/validations.ts
var import_fs_extra = __toESM(require("fs-extra"));
var import_node_path = require("path");
// src/utils/hash.ts
function addHashToSpritePath(path2, hash) {
return path2.replace(/\.svg$/, `.${hash}.svg`);
}
// src/utils/validations.ts
function clear(folderPath) {
import_fs_extra.default.readdir(folderPath, (err, files) => {
if (err) {
console.error("Error reading folder:", err);
return;
}
const svgFiles = files.filter(
(file) => (0, import_node_path.extname)(file).toLowerCase() === ".svg" && file.startsWith("sprite")
);
svgFiles.forEach((svgFile) => {
const filePath = (0, import_node_path.join)(folderPath, svgFile);
import_fs_extra.default.unlink(filePath, (err2) => {
if (err2) {
console.error(`Error removing file ${filePath}:`, err2);
} else {
console.log(`Removed file: ${filePath}`);
}
});
});
});
}
async function writeIfChanged({
filepath,
newContent,
hash,
force
}) {
let _filepath = filepath;
if (hash) {
_filepath = addHashToSpritePath(filepath, hash);
}
const currentContent = await import_fs_extra.default.readFile(_filepath, "utf8").catch(() => "");
const shouldSkip = currentContent === newContent && force !== true;
if (shouldSkip)
return false;
if (hash) {
const folder = filepath.replace(/sprite\.svg$/, ``);
clear(folder);
}
await import_fs_extra.default.writeFile(_filepath, newContent, "utf8");
return true;
}
// src/illustrations/templates/illustrations.ts
var illustrationsTemplate = (illustrationNames) => `import { IllustrationPath } from './types/illustration-path';
export const illustrations = [
${illustrationNames.join(",\n ")},
] satisfies Array<IllustrationPath>;
`;
// src/illustrations/templates/paths.ts
var pathsTemplate = (illustrationNames) => `export type IllustrationPath =
| ${illustrationNames.join("\n | ").replace(/"/g, "'")};
`;
// src/utils/glob.ts
function getIllustrationsExtensionsGlobPattern(extensions) {
return `**/*.{${extensions.join(",")}}`;
}
// src/illustrations/types.ts
async function generateTypes({
files,
typeDir,
outputDir,
force
}) {
const typeOutputFilepath = path.join(typeDir, "illustration-path.d.ts");
const currentTypes = await import_fs_extra2.default.readFile(typeOutputFilepath, "utf8").catch(() => "");
const typesUpToDate = files.every(
(path2) => currentTypes.includes(`"${path2}"`)
);
if (typesUpToDate) {
console.log("Illustrations are up to date");
return;
}
const stringifiedIllustrationNames = files.map(
(path2) => JSON.stringify(`/illustrations/${path2}`)
);
const typeOutputContent = pathsTemplate(stringifiedIllustrationNames);
const typesChanged = await writeIfChanged({
filepath: typeOutputFilepath,
newContent: typeOutputContent,
force
});
if (typesChanged) {
for (const file of files) {
console.log("\u2705", file);
}
console.log(
`Types saved to ${path.relative(process.cwd(), typeOutputFilepath)}`
);
}
const illustrationsOutputFilepath = path.join(outputDir, "illustrations.ts");
const illustrationsOutputContent = illustrationsTemplate(
stringifiedIllustrationNames
);
const illustrationsChanged = await writeIfChanged({
filepath: illustrationsOutputFilepath,
newContent: illustrationsOutputContent,
force
});
if (illustrationsChanged) {
console.log(
`Illustrations saved to ${path.relative(
process.cwd(),
illustrationsOutputFilepath
)}`
);
}
if (typesChanged || illustrationsChanged) {
console.log(`Generated ${files.length} icons`);
} else {
console.log(`Illustrations are up to date`);
}
}
async function generateIllustrationTypes(config) {
const outputDir = config.outputDir;
const { inputDir } = config.illustrations;
const cwd = process.cwd();
const inputDirRelative = path.relative(cwd, inputDir);
const outputDirRelative = path.join(cwd, outputDir);
const typeDirRelative = path.join(cwd, outputDir, "types");
await Promise.all([
import_fs_extra2.default.ensureDir(inputDirRelative),
import_fs_extra2.default.ensureDir(outputDirRelative),
import_fs_extra2.default.ensureDir(typeDirRelative)
]);
const files = import_glob.glob.sync(
getIllustrationsExtensionsGlobPattern(config.illustrations.extensions),
{
cwd: inputDir
}
).sort((a, b) => a.localeCompare(b));
if (files.length === 0) {
console.log(`No illustration files found in ${inputDirRelative}`);
} else {
await generateTypes({
files,
typeDir: typeDirRelative,
outputDir: outputDirRelative,
force: config.force
});
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
generateIllustrationTypes
});