Skip to content

Instantly share code, notes, and snippets.

@Rikezenho
Last active August 21, 2021 04:17
Show Gist options
  • Save Rikezenho/7649b793a1a29d9709a9cdf40a26db31 to your computer and use it in GitHub Desktop.
Save Rikezenho/7649b793a1a29d9709a9cdf40a26db31 to your computer and use it in GitHub Desktop.
Move file components to folder components (node.js)
const fs = require("fs");
const path = require("path");
const baseDirPath = path.join(__dirname, "src", "components");
const baseExtension = ".tsx";
fs.readdirSync(baseDirPath).forEach((fileName) => {
if (!fileName.includes(baseExtension)) return;
const [componentName] = fileName.split(baseExtension);
const newFolderPath = path.join(baseDirPath, componentName);
if (!fs.existsSync(newFolderPath)) {
fs.mkdirSync(newFolderPath);
}
fs.renameSync(
path.join(baseDirPath, fileName),
path.join(newFolderPath, fileName)
);
fs.writeFileSync(
path.join(newFolderPath, "index.ts"),
`export * from './${componentName}'`,
{ encoding: "utf-8" }
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment