preface

I am currently working on a scaffolding project that involves some manipulation of the nodeJS side. Many resources found on the Internet, as well as some ideas, are recorded here as a summary.

The body of the

Construction of scaffolding instructions

The following packages are used for command line operations:

@oclif/command
@oclif/config
@oclif/plugin-help
@oclif/dev-cli
Copy the code

First, create a simple CLI script file run as follows:

#! /usr/bin/env node

require('@oclif/command').run()
    .then(require('@oclif/command/flush'))
    .catch(require('@oclif/errors/handle'));
Copy the code

Then add the configuration to package.json as follows:

{
	"bin": {// Address of the script to which the command points"clitest": "./bin/run"}}Copy the code

Then create a link as follows:

NPM link // Clitest appears on the command line after executionCopy the code

Then, build the scaffolding commands one by one. For example, the create command is as follows:

const Command = require('@oclif/command');

class CreateCli extends Command.Command {
    constructor() { super(... arguments); } asyncrun() {
        try {
            console.log('create');
        } catch(err) {
            console.log(err);
        }
    }
}

CreateCli.description = 'create test';
exports.default = CreateCli;
Copy the code

Finally, specify the command address in package.json as follows:

"oclif": {
	"commands": "./commands"."bin": "clitest"."plugins": [
		"@oclif/plugin-help"]},"files": [
	"/bin"."/commands"].Copy the code

The scaffolding command section is almost complete. The following is a picture of successful execution:

The nodeJS path is faulty

Path problems are often error-prone when writing scaffolding. Here is a summary of some of the path variables commonly used in nodeJS:

__dirname: indicates the complete directory name of the directory where the current execution file is located. __filename: indicates the filename with the complete absolute path of the current execution file. Process.cwd (): indicates the directory name of the current node command. Os.homedir (): indicates the home directory of the systemCopy the code

Here are some of the results:

const path = require('path');
const os = require('os');

console.log(path.resolve(__dirname));
console.log(path.resolve(__filename));
console.log(process.cwd());
console.log(os.homedir());
console.log(path.basename(__dirname));
console.log(path.basename(__filename));
Copy the code

Execution Result:

Monitor file

The NPM used here is Watch.

npm install watch
Copy the code

The watch.watchtree (root) function is usually used. In scaffolding, we often need to listen for changes to files as follows:

Watch.watchtree (SRC, {filter: (filePath) => {// Filter files and folders that do not need to be listened on //... } }, (f, curr, prev) => {if (typeof f == "object" && prev === null && curr === null) {
      // Finished walking the tree
	} else if (prev === null) {
      // f is a new file
	} else if (curr.nlink === 0) {
      // f was removed
	} else {
      // f was changed
	}
});
Copy the code

After that, we need to add files, delete files, and change files.

Login and upload on the Node

The NPM used here is Request.

npm install request
Copy the code

In the login request and upload file process, we need to use formData to upload, but nodeJS does not have formData object, so we need to use Request to upload.

Request. Post ({url, // request interface form: {userName: userName, password: password}}, (err, response: Response, body) => { // ... });Copy the code

In the same way, you can upload a file using a form. However, the general file upload interface requires login, so you need to bring cookies.

const j = request.jar();
j.setCookie(cookie);

const req = request.post({
	url,
	jar: j
}, (err, res: Responese, body) => {
	// ...
});

const form = req.form();
form.append(fileName, file);
Copy the code

If you are interested in my article, please follow my wechat official account.