@ikona/cli
Version:
165 lines (156 loc) • 4.71 kB
JavaScript
// src/illustrations/types.ts
import fsExtra2 from "fs-extra";
import { glob } from "glob";
import * as path from "node:path";
// src/utils/validations.ts
import fsExtra from "fs-extra";
import { join, extname } from "node:path";
// src/utils/hash.ts
function addHashToSpritePath(path2, hash) {
return path2.replace(/\.svg$/, `.${hash}.svg`);
}
// src/utils/validations.ts
function clear(folderPath) {
fsExtra.readdir(folderPath, (err, files) => {
if (err) {
console.error("Error reading folder:", err);
return;
}
const svgFiles = files.filter(
(file) => extname(file).toLowerCase() === ".svg" && file.startsWith("sprite")
);
svgFiles.forEach((svgFile) => {
const filePath = join(folderPath, svgFile);
fsExtra.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 fsExtra.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 fsExtra.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 fsExtra2.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([
fsExtra2.ensureDir(inputDirRelative),
fsExtra2.ensureDir(outputDirRelative),
fsExtra2.ensureDir(typeDirRelative)
]);
const files = 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
});
}
}
export {
generateIllustrationTypes
};