As a front-end developer if you only write business code, from the programmer's point of view already. But from an architectural point of view that is not enough;
In this recording growing experience:
The purpose of trying to achieve: run a script to achieve packaged code, uploaded to the server and deployed to the server;
Server: the need to install pm2, nodejs;
Create a script file name in the root directory of the local self-directed it;
1. Download packages need to rely on
Compressing SSH2 I NPM - S // Compressing role is used to compress a file // SSH2 effect is used to connect the server and performing an operation
2. Create a file in the root directory operations
2.1 introduction of the core module child_process node
const {exec} = require('child_process')
2.2
const compressing = require('compressing')
2.3
const Client = require('ssh2').Client;
2.4 creates an object which is a property of a server connection
const Server = { Host: 'ip address of the remote server', Prot: 22, // default do this if not modified, then username: 'landing remote server's user name', password: "login username password" }
2.5 to create an object of a ssh2
const connect = new Client()
2.6 in the implementation of the entire file so that node to create a child thread
/ * The first parameter is the command to run, and the second parameter is optional and if interested can view child_process.exec understand node official website, the third parameter is a callback method has three in the callback method parameter error stdout stderr if successful will be the error will be null else error error instance stdout and stderr parameter will contain subprocess stdout and stderr output * / const BAT = Exec ( 'Build NPM RUN', (ERR, stdout, stderr) = > { IF (ERR) return the console.log ( `Exec error: $ {}` ERR); the console.log ( "packaging success" ); // start compression method the compress (); })
2.7 compress the packaged files after executing the package child thread
function the compress () { the console.log ( 'compression ******* *******' ); // use the plug compressing the introduced compressed file we need to // the first parameter to compress folder, the second parameter is the name of the archive after compression compressing.zip.compressDir ( 'dist /', 'dist.zip'). the then (() => { the console.log ( '***** ***** compression success' ); // after a successful connection to the server is called method Conn (); }) }
2.8 create a connection to the server method is invoked after successful compression
function Conn () { the console.log ( 'Connection Server ***** ******' ); // use the previous objects defined ssh2 @ READY indicates successful authentication // error indicates an error // represents disconnected end // Close to close the connection expressed, if this is due to errors from, hadError is set to true connect.on ( 'READY', () => { // the file upload operation after the verification is successful upload () }). ON ( 'error', (ERR) => { console.error (ERR) the console.log ( 'error connecting ***** *****' ) }). ON ( 'End' , () => { the console.log ( 'closed connection ***** *****' ) }). ON ('Close', (ERR) => { IF (ERR) return the console.log ( 'Error connecting ***** *****' ) }). Connect (server) // Connect method parameters used inside the server parameter before connecting to the server can be viewed ssh2-npm official website }
2.9 Once verification is complete ssh2 calls the method to upload files
function Upload () { the console.log ( 'start uploading ****** ******' ); // open sftp session parameter is a callback method has two parameters a callback method err example, one example sftp connect.sftp ((ERR, sftp) => { IF (ERR) the throw ERR; // first parameter sftp upload operation is a local file path to be uploaded, the second parameter is want to upload a file server that directory sftp.fastPut ( 'good compressed file name', 'upload to the directory server', (ERR, RES) => { IF (ERR) { console.log ( '**** upload failed ** *** ' ); console.error (ERR); // if an error occurs to end method call disconnect connect.end (); return ; } // If the file uploaded successfully invokes the decompression method unzipShell () }) }) }
2.10 When the file is uploaded successfully invoked decompression method
function unzipShell () { // Start a first interactive shell session parameter is optional on the server, the second parameter is the error correction method is the first example of a second stream is a shell session connect.shell ((err , Stream) => { the console.log ( 'unpacked ****** ******' ); iF (ERR) the throw ERR; the let buf = "" ; // when the session is detected when the output whine close method stream.on ( 'close', ERR => { // close the connection connect.end (); // if it fails to agree printing fails if successful successful IF (ERR) return console.error (ERR); console.info ('SUCCESS !! ******* ******' ); .}) ON ( 'Data', Data => { // Data is received from the event string block stream.data buf + = Data; the console.log (buf) }) // when the extraction is completed in a terminal command // (hereinafter merely exemplary specific operation command to see you project) // get under 1. to upload directory and extract the uploaded files // copy everything to unzip the file after 2.cd folder inside the folder inside to the floor // 3. to the parent directory of the deleted file is compressed and decompressed after the clip // 4.cd to and using pm2 uppermost node hosting service stream.write ( 'cd upload folder path && unzip the compressed file name \ nnext \ n-' ); stream.write ( 'cd extracting the folder below && / bin / cp - * -f ../ R & lt \ nnext \ n-' ); Stream.write('cd ../ && rm -r -f dist && rm -r -f compressed file name \ nnext \ n-' ); stream.write ( '&& ../ CD PM2 Start nodemon server.js \ Nexit \ n-' ); //server.js is what you need to start a file }) }
3. Increase server.js file in the project root directory on the server side I am using a file hosting service node
// import node module Express const Express = The require ( "Express" ); // create a service const = App Express (); // will host the static files to the local service file name is not necessarily dist app.use (Express. static ( "./ dist" )); // start a service port 3000 app.listen (3000, (ERR) => { IF (ERR) return ERR; the console.log ( 'aT Server running HTTP: // 127.0.0.1:3000 ' ); })
Complete code
server.js need to manually create a separate and unrelated code below
const { exec } = require("child_process"); const compressing = require("compressing"); const Client = require("ssh2").Client; const server = { host: '服务器ip', port: 22, username: '用户名', password: '密码' } const connect = new Client(); function conn() { console.log('*******连接服务器***********'); connect.on('ready', () => { Upload (); }) ON (. ERR; 'error', (ERR) => { console.error (ERR); the console.log ( 'Error connecting ***** ******' ) .}) ON ( 'End' , () => { the console.log ( 'closed connection ******* ********' ) }). ON ( 'Close', (ERR) => { IF (ERR) the throw ERR; .}) Connect (Server); } function Upload () { the console.log ( 'start uploading ****** ********' ); connect.sftp ((ERR, SFTP) = > { IF (ERR) the throw sftp.fastPut ('./dist.zip', '/home/yunwo/dist/dist.zip', (err, res) => { if (err) { console.error(err); console.log("*******上传失败******"); connect.end(); return; } unzipShell() }) }) } function unzipShell() { connect.shell((err, stream) => { console.log('*******解压中*******'); if (err) throw err; let buf = ""; stream.on('close', err => { connect.end(); if (err) return console.error(err); console.info('******** success!! *********'); }).on('data', data => { buf += data; console.log(buf); }) stream.write('cd /home/yunwo/dist && unzip dist.zip \nnext\n'); stream.write('cd dist && /bin/cp -r -f * ../ \nnext\n'); stream.write('cd .. && rm -r -f dist && rm -r -f dist.zip \nnext\n'); stream.write('cd .. && pm2 start nodemon yunwoserver.js \nexit\n'); }) } function compress() { console.log('******压缩中********'); compressing.zip.compressDir('dist/', 'dist.zip').then(() => { console.log('******压缩成功******'); conn(); }) } console.log('*******打包中*******'); const bat = exec('npm run build', (err, stdout, stderr) => { if (err) return console.error(`exec error : ${err}`); console.log('******** ********** Packaging Success' ); the compress (); })