Make writing a habit together! This is the fourth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Scaffolding command registration and execution process development

What will be gained

  • How to design a high performance scaffold
  • NodeMultiprocess development
  • JavaScriptThe actual skills of the opponent
  • NodeMulti-process principle

The main content

  • Illustrate high performance scaffolding architecture design methods

  • Encapsulate common Command and Package classes

  • Dynamic command loading and execution based on cache + Node multi-process

  • Completely decouple the business logic from the scaffolding logic

  • Node multi-process child_process source code analysis

keywords

  • High-performance/extensible scaffolding – Leveraging caching to improve scaffolding performance and decouple business logic

  • Object-oriented – Use Class to complete JavaScript object-oriented programming

  • Node Multi-process – Dig into the principle of Node multi-process

Register the command

Install dependencies

lerna add commander core/cli/
Copy the code
// core\cli\lib\index.js
// Introduce the encapsulated init command
const init = require('@hzw-cli-dev/init');
// Create a commander instance
const program = new commander.Command();

/ * * *@description: Register command *@param {*}
 * @return {*}* /
function registerCommand() {
  // Register debug mode
  program
    .name(Object.keys(pkg.bin)[0])
    .usage('<command> [options]')
    .version(pkg.version)
    .option('-D, --debug'.'Debug mode enabled'.false);

  // Register the command
  program
    .command('init [projectName]')
    .option('-f, --force'.'Force initialization of the project')
    .action(init);

  // Get parameters
  const params = program.opts();

  // Register the debug command
  program.on('option:debug'.() = > {
    if (params.debug) {
      process.env.LOG_LEVEL = 'verbose';
    } else {
      process.env.LOG_LEVEL = 'info';
    }
    // Set the log level
    log.level = process.env.LOG_LEVEL;
    log.verbose('test debug');
  });

  // Listen for all unregistered commands
  program.on('command:*'.(obj) = > {
    const commands = program.commands.map((cmd) = > cmd.name());
    log.info(colors.red('Unknown command' + obj[0]));
    if (commands.length > 0) {
      log.info(colors.blue('Supported commands' + commands.join(', '))); }});// Parse the parameters
  program.parse(process.argv);

  // Determine whether to enter the command to display the help document
  if (program.args && program.args.length < 1) {
    program.outputHelp();
    console.log(); }}Copy the code

Create a package and remove the service code of the init command for subsequent maintenance.

lerna create @hzw-cli-dev/init
Copy the code
// command\init\lib\index.js

function init(projectName, cmdObj) {
  console.log('init', projectName, cmdObj);
}
module.exports = init;
Copy the code

Current scaffold construction pain point analysis

The current scaffold architecture is shown below

This architecture has been designed to meet the general needs of scaffolding, but there are the following problems

  • Slow installation of scaffolding:allpackageAre integrated in thecli, so when there are too many commands, it will slow down the scaffolding installation.
  • Poor flexibility: initThe command can only be used@hzw-cli-dev/initPackages, for large companies, for each teaminitThe commands may vary and may be requiredinitDynamic command, as in:
    • The A team uses @hzw-cli-dev/init-a as the initialization module

    • The B team uses @hzw-cli-dev/init-b as the initialization module

    • The C team uses @hzw-cli-dev/init-c as the initialization module

This presented a challenge to our architectural design, requiring us to be able to dynamically load the init module, which would increase the complexity of the architecture, but greatly improve the scalability of the scaffolding, decoupling the scaffolding framework from the business logic.

Scaffolding architecture optimization

The current scaffold execution flow is shown below

The optimized flow chart is as follows

The scaffold command dynamically loads the functional architecture design