“This is the 26th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Import-local Executes the flow

What it does: When there is a scaffolding command in node_modules in the current project, and a scaffolding command in the global Node environment, it takes precedence over the local version in node_modules.

// core\lerna\cli.js

Import / / in the local
const importLocal = require("import-local");

// If importLocal(__filename) is true, a line of log is output
// Check whether scaffolding exists in local 'node_modules'
// __filename is the current file address D:\lerna-main\core\ lerna-cli.js
if (importLocal(__filename)) {
  // Use the local version of the log output
  require("npmlog").info("cli"."using local version of lerna");
} else {
  require(".")(process.argv.slice(2));
}
Copy the code

Look at the source code for import-local. It doesn’t look like there’s a lot of it

// node_modules\import-local\index.js

const path = require('path');
const resolveCwd = require('resolve-cwd');
const pkgDir = require('pkg-dir');

module.exports = filename= > {
  // filename Specifies the global execution file of the scaffold
  // Get the global directory where the scaffold is located, containing package.json
  // If the current module is deeply nested, the directory containing package.json will be searched layer by layer
  const globalDir = pkgDir.sync(path.dirname(filename)); 
  // Compare globalDir and filename relative paths to get the final CLI.js
  const relativePath = path.relative(globalDir, filename);
  // Merge the globalDir and package.json paths and retrieve the package.json file contents
  const pkg = require(path.join(globalDir, 'package.json'));
  // Combine pkg.name with relativePath to get lerna/cli.js
  // Then call resolveCwd to determine if the current project is local to the module return to the local module path
  const localFile = resolveCwd.silent(path.join(pkg.name, relativePath));
  // Return null if the module has loaded modules otherwise
  returnlocalFile && path.relative(localFile, filename) ! = =' ' ? require(localFile) : null;
};
Copy the code

As you can see, the import-local module is a function that executes require(localFile) inside the function when the condition is met and then returns to execute it

require("npmlog").info("cli"."using local version of lerna");
Copy the code

Using local version of Lerna is printed first, because the scaffolding command implementation uses microtask queues, although require(localFile) is executed first. However, the real execution logic is stored in the microtask queue, and using local version of LERna is the last content of the macro task, so the log will be output when the macro task is executed, and then the actual business logic code in the microtask queue will be executed.

Design resume based on Lerna

You can add this chapter to your resume after you have fully mastered it

Note: I did not understand chapter 4-12 ~ 4-18, so I need to review it later, but it will not affect the progress of the main course

  • Be familiar withyargsScaffolding development framework
  • Familiar with morepackageManagement toollernaThe application method and implementation principle of
  • Deep understanding ofnode.jsModule path resolution process

How to develop a scaffold using Yargs

Take vuE-CLI as an example. The most basic command is vue create project –force

  • bin:package.jsonConfigured inbinProperty, which can be interpreted as the main command, i.evue, local development time throughnpm linkInstall locally.
  • Need to be inbinPoint to the file that is added to the scaffold’s executable file#! /usr/bin/env nodeTell the operating system to query in environment variablesnode, and through thenodeTo execute the file.
  • command: command, as in the examplecreate
  • param: parameter, as in the exampleproject
  • option: arguments can also carry options, as in the example--force.

Then talk about the scaffold initialization process

  • Call the constructor to generate a scaffold:Yargs ()
  • callyargsCommonly used methods to enhance the function of scaffolding
    • Yargs.options
    • Yargs.group
    • . .
  • Parse scaffold parameters
    • usingyargs/helpersTo provide thehideBin, the callYargs(hideBin(process.argv)).argvComplete resolution
    • Yargs.parse(argv,options)
  • Register scaffold command
    • Yargs.command(command,describe,builder,handler)
    • Yargs.command({command,describe,builder,handler})

How does LERNA work

Lerna is a multi-package project management tool based on Git + NPM

The use of the Lerna

  • Lerna init
  • Lerna create
  • Lerna add
  • Lerna link
  • . .

Implementation principle of Lerna

  • throughimport-localCall local firstlernaCommand.
  • throughYargsGenerate scaffolding, register global properties first, then register commands, and finally passparseMethod parses the parameters.
  • LernaThe command is passed in for registrationbuilderhandlerTwo parameters,builderMethod used to register command specificoptionshandlerThe business logic used to process commands.
  • LernaBy configuringnpmA locally dependent approach to local development, written in thepackage.jsonWrite to the dependency of:file:your-local-module-pathIn thelerna publish Will automatically replace the changed path with an online path.

Node. js module path resolution process

. . loading