Building a Monorepo-Style Node.js CLI Scaffolding Tool for Frontend Projects
This comprehensive tutorial walks you through creating a Node.js command‑line scaffolding tool that uses yargs for argument parsing, inquirer for interactive prompts, copy‑dir for file operations, mustache for dynamic templates, and pnpm with ora for automated dependency installation, all organized in a monorepo structure.
The article begins with a story about a junior developer struggling to present CRUD experience in interviews and introduces the idea of building a CLI scaffolding tool to package best‑practice templates for front‑end projects.
It then explains how to set up a basic CLI project, create an index.js entry file, and declare a command in package.json using the bin field.
Command‑parameter module : Using Node.js process and the yargs library, the tutorial shows how to define options, support both --name=orderPage and --name orderPage formats, and create sub‑commands with yargs.command . Example code: #!/usr/bin/env node const yargs = require('yargs'); yargs.command(['create','c'], 'Create a template', (yargs) => { return yargs.option('name', { alias: 'n', demand: true, describe: 'Template name', type: 'string' }); }, (argv) => { console.log('argv', argv); }).argv;
User‑interaction module : The guide recommends inquirer for questionnaire‑style prompts, demonstrating how to ask for template name, type, framework, and UI library, with validation and conditional questions. Example snippet: const inquirer = require('inquirer'); function inquirerPrompt(argv) { const { name } = argv; return inquirer.prompt([ { type: 'input', name: 'name', message: 'Template name', default: name, validate: val => /^[A-Z][a-zA-Z]+$/.test(val) || 'Name must start with a capital letter and contain only letters' }, { type: 'list', name: 'type', message: 'Template type', choices: ['form','dynamicForm','nestedForm'] }, { type: 'list', name: 'frame', message: 'Framework', choices: ['react','vue'] } ]); }
File‑copy module : Using copy-dir , the tutorial shows how to copy entire template folders, and how to guard directories with fs.mkdirSync (recursive) to avoid missing folder errors. The mkdirGuard function recursively creates parent directories when needed.
Dynamic file generation : By integrating the mustache library, template files with .tpl extensions can embed placeholders like {{name}} . The readTemplate function reads a template, renders it with data, and copyTemplate writes the result, falling back to a simple file copy for non‑template files.
Automatic dependency installation : The guide uses Node's child_process.exec to run pnpm add commands for the selected framework and UI library. A spinner from the ora library provides visual feedback during installation.
Finally, the article covers publishing the CLI to npm, installing it in other projects, and running commands like pnpm mortal create -- --name=OrderPage to generate a new page with all required files and dependencies.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.