@nimbleai/generator-mern
Version:
86 lines (80 loc) • 2.56 kB
JavaScript
const Generator = require('yeoman-generator');
const path = require('path');
const glob = require('glob');
module.exports = class extends Generator {
async prompting() {
this.answers = await this.prompt([
{
type: 'input',
name: 'projectName',
message: 'Your MERN project name',
default: this.appname
}
]);
}
writing() {
// Copy top-level dotfolders (e.g., .vscode, .husky)
['.vscode', '.husky'].forEach((folder) => {
this.fs.copy(
this.templatePath(folder),
this.destinationPath(folder)
);
});
// Copy top-level config files and dotfiles
const configFiles = [
'_gitignore',
'README.md'
];
configFiles.forEach((file) => {
let destinationName = file;
if (file === '_gitignore') {
destinationName = '.gitignore';
} else if (file === '_prettierrc') {
destinationName = '.prettierrc';
} else if (file.startsWith('_')) {
destinationName = file.substring(1);
}
this.fs.copy(
this.templatePath(file),
this.destinationPath(destinationName)
);
});
// Copy client and server folders, including dotfiles and underscore files
['client', 'server'].forEach((dir) => {
// Copy all regular files and folders
this.fs.copy(
this.templatePath(`${dir}/**`),
this.destinationPath(`${dir}/`),
{
globOptions: {
dot: true,
ignore: [this.templatePath(`${dir}/_*`)]
}
},
);
// Copy dotfiles in client/server
const dotfiles = glob.sync(this.templatePath(`${dir}/.*`), { dot: true });
dotfiles.forEach((filePath) => {
const fileName = path.basename(filePath);
this.fs.copy(
filePath,
this.destinationPath(`${dir}/${fileName}`)
);
});
// Rename files with leading underscore in client/server
const underscoreFiles = glob.sync(this.templatePath(`${dir}/_*`));
underscoreFiles.forEach((filePath) => {
const fileName = path.basename(filePath);
const destName = fileName.substring(1); // Remove leading '_'
this.fs.copy(
filePath,
this.destinationPath(`${dir}/${destName}`)
);
});
});
}
end() {
this.log(`MERN stack project '${this.answers.projectName}' created successfully.`);
this.log(`To get started:\n\n cd ${this.answers.projectName}\n cd client && npm install\n cd ../server && npm install`);
}
};