Let’s start with the renderings

To summarize the effects of the previous article: it’s the command line approach; Can execute system commands; Inputable parameter

Then the requirements are implemented step by step

Environment: macOS 10.15.2 node 12.16.1

Command-line mode

Prepare the script directory first
Mkdir xlfdcli CD xlfdcli NPM init #Copy the code
Edit the X-ray cli
#! /usr/bin/env node console.log('success')Copy the code

At this point, modify the permissions and you can actually run it:How to get rid of. /Directly tox-cliPerform?

Add it in package.json

Add “bin” : {” X-ray cli “:” X-ray cli} “, of course also can change the name of the other “bin” : {” xlfdcli “:” X-ray cli}”

{" name ":" xlfdcli ", "version" : "1.0.0", "description" : ""," main ":" index. Js ", "scripts" : {" test ", "echo \" Error: no test specified\" && exit 1" }, "bin": { "x-cli": "x-cli" }, "author": "", "license": "ISC" }Copy the code
npm link

NPM link: sudo: NPM link: sudo:

Sudo NPM link # NPM WARN [email protected] No description # NPM WARN [email protected] No repository field. #up to date in 2.007s #found 0 vulnerabilities #/usr/local/bin/x-cli -> /usr/local/lib/node_modules/xlfdcli/x-cli #/usr/local/lib/node_modules/xlfdcli -> /Users/johndiamond/Documents/workspace/xlfdcli x-cli #successCopy the code

Executing system commands

There are three ways to install NPM I — Save Shelljs:

#! /usr/bin/env node // ### child_process ### const {exec} = require('child_process') ${process.argv[2]}`, (error, stdout, ### const shelljs = require('shelljs') shelljs.exec(' echo ': ${process.argv[2]}`) // ### shelljs/global ### require('shelljs/global') const dirName = 'global' mkdir(dirName) cd(dirName) touch('creatbyglobal.txt') echo('done')Copy the code

The input parameters

Install NPM I — Save yargs

#! /usr/bin/env node const { argv } = require('yargs') console.log(argv) console.log(argv.type)Copy the code

Combined with a peak teacher command line tutorial already know how to achieve the above three requirements

Command line content

The basic idea is to manually define two types of identifiers in ngin. Conf corresponding to the above three functions. Replace identifiers according to script parameters; Restart the nginx configuration to take effect

Identify nginx. Conf

Combined with the above knowledge, we will be able to achieve the last requirements of the previous chapter (a small part of the incomplete).

Because the code is simpler, I won’t comment it out

#! /usr/bin/env node const Fs = require('fs') const { argv } = require('yargs') .usage('x-cli [options]') .example('x-cli -t location -u /privacy') .help('h') .alias('h', 'help') .epilog('creat by xlfd') .option('t', { alias: 'type', demand: True, type: 'string', describe: 'server and location'}). Option ('u', {alias: 'uri', demand: true, type: 'string'). Option ('a', {alias: 'alias', type: 'string', describe: Option ('r', {alias: 'root', type: 'string', describe: 'server root directory root'}). Option (' I ', {alias: Option ('p', {alias: 'port', type: 'number', describe: }) const nginxPath = '/usr/local/nginx/sbin' const _type = argv.t const _uri = argv.u const _alias = argv.a const _index = argv.i const _port = argv.p if (_type === 'location') { addLocation({ path: './nginx.conf', port: _port, index: _index, uri: _uri, alias: _alias } ) } else { addServer({ path: './nginx.conf', port: _port, index: _index, uri: _uri, alias: _alias } ) } function addLocation ({path, port = 80, index, uri, alias} = {}) { const markStr = `#jenkins-CI-newLocation${port}` const locationStr = `location ${uri} { ${alias ? `alias ${alias}`: '#'}; index ${index ? index : 'index.html'} index.html; } ${markStr}` readFile(path, markStr, locationStr, 'location') } function addServer ({path, port = 80, index, uri, alias} = {}) { const serverMarkStr = '#jenkins-CI-newServer' const locationMarkStr = `#jenkins-CI-newLocation${port}` const locationStr = `location ${uri} { ${alias ? `alias ${alias}`: '#'}; index ${index ? index : 'index.html'} index.html; } ${locationMarkStr}` const serverStr = `server { listen ${port}; server_name localhost; ${locationStr} } ${serverMarkStr}` readFile(path, serverMarkStr, serverStr, 'server') } function readFile (path, source, target, type = 'location') { Fs.readFile(path, (error, Data) => {if (error) throw error let confStr = data.toString() // replace confStr = confstr.replace (source, target) Fs.writeFile(path, confStr, (err) => { if(err) throw err console.log(`${type === 'location' ? 'location' : } function reload(path) {const shelljs = require('shelljs')  shelljs.exec(`cd ${path}`) shelljs.exec('./nginx -s reload') console.log('nginx reloaded') }Copy the code

This command line can be embedded into the Jenkins-ci script for practical use, but be careful about the path when using it