Introduction to 👽

Once scaffolding is developed, of course it has to be posted to NPM (don’t ask me why). Today we are going to focus on how to publish packages to NPM (before this section we will add two small features to scaffolding: remote repository creation + dependency download, if you are not interested you can skip to the section on publishing packages).

👽 Create a remote warehouse

After the project template is generated, many times we create a remote repository for the project synchronously, then upload the project template and share the repository address with the team partners. This process is tedious and untechnical, so let the code do it automatically.

🚩 Configure interaction items

First check whether the user needs to automatically generate the remote repository:

 Prompts prompts. Js * @description: Prompts */
const chalk = require('chalk');

const PROMPTS = (isFile, appName) = > {
  return{...needCreateRepo: {
      name: 'needCreateRepo'.type: 'confirm'.message: 'Whether to create a remote repository:',}}}; };module.exports = PROMPTS;
Copy the code

🚩 defines the remote repository generation tool

Git’s repo creation command is used in the shellJS library to call git’s repo creation command in node:

 /* * @filename: createrepo. js * @description: create a remote repository */

const shell = require('shelljs');

async function createRepo(appName, repoUrl) {
  // create a warehouse address
  const projectUrl = `${repoUrl}/${appName}.git`;
  // Run git
  const info = shell.exec(`git push --set-upstream ${projectUrl} master`);

  // Output the result
  if (info.stdout) {
    await clearConsole('green'.'** Remote repository created successfully:${projectUrl}* * `);
  } else {
    await clearConsole('red'.'** Remote repository creation failed **'); }}module.exports = createRepo;
Copy the code

🚩 introduces this function in the main logic

/* * @filename: create.js * @description :cli-create command */...const generateFile = require('.. /utils/generateFile');

async function create(appName) {...// Switch to the project directory
 shell.cd(appName);

 const { needCreateRepo } = await inquirer.prompt(newPrompts['needCreateRepo']);
 // Define the warehouse address
 const repoUrl = `http://xxx/front`;
 // Execute code when 'yes' is selectedneedCreateRepo && createRepo(appName, repoUrl); }...Copy the code

👽 Dependent Download

Same idea, let’s do dependency download.

Configuring Interaction Items

 Prompts prompts. Js * @description: Prompts */
const chalk = require('chalk');

const PROMPTS = (isFile, appName) = > {
  return{...needInstallPackages: {
      name: 'needInstallPackages'.type: 'confirm'.message: 'Whether to download the project depends on:',}}; };module.exports = PROMPTS;
Copy the code

🚩 defines dependency download tools

 /* * @filename: installpackages.js * @description: dependency download */
const shell = require('shelljs');
const chalk = require('chalk');

async function installPackages() {
  console.log(chalk.yellow('About to start downloading project dependencies'));
  // Yarn is used by default. It can also be made to let the user decide which package manager to use
  const info = shell.exec(`yarn`);

  // Determine the result
  if (info.stdout) {
    await clearConsole('green'.'** dependent download success **');
  } else {
    await clearConsole('red'.'** dependent download failed **'); }}module.exports = installPackages;
Copy the code

🚩 introduces this function in the main logic

/* * @filename: create.js * @description :cli-create command */...const generateFile = require('.. /utils/generateFile');

async function create(appName) {...// Currently only Web projects need to download dependencies
 if (appType === 'web') {
   const { needInstallPackages } = await inquirer.prompt(newPrompts['needInstallPackages']); needInstallPackages && installPackages(); }}...Copy the code

👽 NPM package release

This part is more simple and NPM source must bear the release is the official source (https://registry.npmjs.org/) :

🚩 First of all, please register an NPM account on the official website of NPM, and remember your username and password.

🚩 Run NPM adduser and enter the user name, password, and email address in sequence.

🚩 Run NPM publish to publish the NPM package.

🚩 After installing your NPM package globally, you can use the scaffolding command normally (remember to execute NPM unlink first)!

👽 note

Welcome to view the scaffolding source code, or download the NPM package experience: NPM I V9 -CLI -g.