@dasa-health/icons
Version:
Livia DS React Icons
89 lines (76 loc) • 2.33 kB
JavaScript
const fs = require("fs");
const path = require("path");
function flatten(lists) {
return lists.reduce((a, b) => a.concat(b), []);
}
function getDirectories(srcPath) {
return fs
.readdirSync(srcPath)
.map((file) => path.join(srcPath, file))
.filter((path) => fs.statSync(path).isDirectory());
}
function getDirectoriesRecursive(srcPath) {
return [
srcPath,
...flatten(getDirectories(srcPath).map(getDirectoriesRecursive)),
];
}
function getDirectoriesWithIndex(srcPath) {
return fs
.readdirSync(srcPath)
.map((file) => path.join(srcPath, file))
.filter((path) => fs.statSync(path).isFile());
}
function mapDirectoriesWithIndex(srcPath) {
return flatten(
getDirectoriesRecursive(srcPath).map(getDirectoriesWithIndex)
)
.filter(Boolean)
.filter((name) => name.indexOf("index.js") === -1);
}
const rootPath = path.join(__dirname, "../");
const srcPath = path.join(rootPath, "./src");
function buildSrcIndex(mainPath = "") {
const pathArray = mapDirectoriesWithIndex(mainPath);
const indexStream = fs.createWriteStream(`${mainPath}/index.js`);
const typingsStream = fs.createWriteStream(`${srcPath}/index.d.ts`);
const archivesToUse = pathArray.reduce((acc, filePath) => {
const relativePath = `.${filePath
.replace(srcPath, "")
.replace(".js", "")
.replace(/\\/g, "/")}`;
const archiveName = relativePath
.split(/\W/)
.filter(Boolean)
.map(
(word) =>
`${word.slice(0, 1).toUpperCase()}${word
.slice(1, word.length)
.toLowerCase()}`
)
.join("")
.replace("Icons", "");
return acc.concat({
archiveName: archiveName,
archivePath: relativePath,
});
}, []);
indexStream.once("open", function () {
archivesToUse.forEach(({ archiveName, archivePath }) => {
indexStream.write(
`export { default as ${archiveName} } from "${archivePath}"; \n`
);
});
indexStream.end();
});
typingsStream.once("open", function () {
typingsStream.write(`import React from 'react';\n`);
archivesToUse.forEach(({ archiveName }) => {
typingsStream.write(
`declare const ${archiveName}: React.FC<React.SVGAttributes<SVGElement>>;\n`
);
});
typingsStream.end();
});
}
buildSrcIndex(srcPath);