UNPKG

clr-dir

Version:

A npm package to organize the directory quickly

45 lines (38 loc) 1.16 kB
const path = require("path"); const { mkDirByPathSync, moveFile } = require("./utils"); const default_file_structure = require("./file_structure.json"); const getNewLocation = ({ fileExtension, PATH, default_file_structure }) => { for (let file in default_file_structure) { const isArray = Array.isArray(default_file_structure[file]); if (isArray) { const isExtInArray = default_file_structure[file].find( (f) => f === fileExtension ); if (isExtInArray) return PATH + `/${file}`; } else { new_path = getNewLocation({ fileExtension, PATH: path.join(PATH, file), default_file_structure: default_file_structure[file], }); if (new_path) return new_path; } } return null; }; const organize = async (file, PATH) => { const fileExtension = file.split(".").slice(-1)[0]; const newLocation = getNewLocation({ fileExtension, PATH, default_file_structure, }); if (!newLocation) return null; try { const dir = mkDirByPathSync(newLocation); return await moveFile(PATH, dir, file); } catch (e) { console.log(e); } }; module.exports = organize;