UNPKG

mern-scaffold

Version:

mern-scaffold is a CLI tool that helps you to quickly develop basic boilerplate code for your MERN app. You can generate boilerplate code for your Frontend or Backend. You can also generate them as a full stack project.

90 lines (75 loc) 3.07 kB
// built in core node module for working with the file system import fs from 'node:fs'; // built in core node module for working with the file path import path from 'node:path'; // an external package to help me the directory where my npm package will be downloaded into import globalModules from 'global-modules'; // my package name const packageName = 'mern-scaffold'; // Replace with the name of your global package // my package directory const packageDirectory = path.join(globalModules, packageName); const appDirectoryToReadFrom = path.resolve( `${packageDirectory}/applications/frontend` ); fs.readdir(appDirectoryToReadFrom, (err, file) => { if (err) { throw new Error(err.message); } // loop through each file to read its content file.forEach((item) => { // check if a file is a directory if (fs.statSync(`${appDirectoryToReadFrom}/${item}`).isDirectory()) { // create a new directory in the application were trying to build that was created // more like creating a nested folder inside a folder fs.mkdirSync(`${appDirectoryToWriteTo}/${item}`); // read the content of the directory into the new directory you created fs.readdir(`${appDirectoryToReadFrom}/${item}`, (err, file) => { // check if there an error if (err) { throw new Error(err.message); } file.forEach((subFolderFile) => { // assign the content inside each of the file to a variable const subFolderContent = fs.readFileSync( path.resolve(`${appDirectoryToReadFrom}/${item}/${subFolderFile}`), 'utf-8' ); // write the content as well to the new destination fs.writeFileSync( path.resolve(`${appDirectoryToWriteTo}/${item}/${subFolderFile}`), subFolderContent ); }); }); return; } // **** ******************** // read the content of each of the file let data = fs.readFileSync(`${appDirectoryToReadFrom}/${item}`, 'utf-8'); // lets modify the package.json before creating it if (item === 'package.json') { const updated_package = JSON.parse(data); updated_package.name = folder_name; // check which of the component library the user has chosen // for ant design if (component_library_chosen === 'Ant design') { updated_package.dependencies = { ...updated_package.dependencies, antd: 'latest', }; } // for Material UI if (component_library_chosen === 'Material UI') { updated_package.dependencies = { ...updated_package.dependencies, '@emotion/react': 'latest', '@emotion/styled': 'latest', '@mui/material': 'latest', }; } data = JSON.stringify(updated_package); } // write to the folder the user has choosen; fs.writeFileSync(`${appDirectoryToWriteTo}/${item}`, data); }); });