|
| 1 | +const { promisify } = require('util'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const Client = require('ssh2').Client; |
| 5 | + |
| 6 | + |
| 7 | +class Publish { |
| 8 | + constructor(config) { |
| 9 | + /* |
| 10 | + * ip |
| 11 | + * port |
| 12 | + * username |
| 13 | + * privateKey |
| 14 | + * passphrase |
| 15 | + * |
| 16 | + * local: '' |
| 17 | + * remote: '' |
| 18 | + * ignore: [] |
| 19 | + * clear: true/false |
| 20 | + * cmds: [] |
| 21 | + * */ |
| 22 | + this.config = Object.assign({}, config); |
| 23 | + this.connection = undefined; |
| 24 | + this.sftp = undefined; |
| 25 | + this.config.ignore = (this.config.ignore || []).map(p => { |
| 26 | + if (path.isAbsolute(p)) { |
| 27 | + return path.normalize(p) |
| 28 | + } |
| 29 | + return path.resolve(this.config.local, p); |
| 30 | + }); |
| 31 | + this.config.privateKey = fs.readFileSync(this.config.privateKey, 'utf-8'); |
| 32 | + } |
| 33 | + |
| 34 | + async getConnection() { |
| 35 | + if (this.connection) return this.connection; |
| 36 | + const con = new Client(); |
| 37 | + return new Promise((resolve, reject) => { |
| 38 | + con.on('ready', ()=> { |
| 39 | + this.connection = con; |
| 40 | + return resolve(con); |
| 41 | + }); |
| 42 | + con.on('error', (error) => { |
| 43 | + console.log(`getConnection error`); |
| 44 | + console.log(error); |
| 45 | + return reject(error); |
| 46 | + }); |
| 47 | + con.connect({ |
| 48 | + host: '39.105.35.151', |
| 49 | + port: 22, |
| 50 | + username: 'root', |
| 51 | + privateKey: fs.readFileSync('/Users/wangjun/Desktop/important/ssh/wj_server'), |
| 52 | + passphrase: 'xinshang2011' |
| 53 | + }); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + async getSftp() { |
| 58 | + |
| 59 | + if (!this.connection) await this.getConnection(); |
| 60 | + if (this.sftp) return this.sftp; |
| 61 | + this.sftp = await promisify(this.connection.sftp).call(this.connection); |
| 62 | + } |
| 63 | + |
| 64 | + async ensureRemoteDir(dirPath) { |
| 65 | + await this.getConnection(); |
| 66 | + await this.getSftp(); |
| 67 | + return new Promise((resolve, reject) => { |
| 68 | + this.sftp.exists(dirPath, (exists) => { |
| 69 | + if (exists) return resolve(); |
| 70 | + this.execCmd(`mkdir -p ${dirPath}`).then(resolve).catch(reject); |
| 71 | + }); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + async uploadFile(from, dest) { |
| 76 | + if (this.ignorePath(from)) return ; |
| 77 | + await this.getSftp(); |
| 78 | + console.log(`upload ${from} to ${dest}`); |
| 79 | + const data = await promisify(fs.readFile).call(fs, from); |
| 80 | + await promisify(this.sftp.writeFile).call(this.sftp, dest, data); |
| 81 | + } |
| 82 | + |
| 83 | + async uploadDir(from, dest) { |
| 84 | + if (this.ignorePath(from)) return; |
| 85 | + await this.ensureRemoteDir(dest); |
| 86 | + const files = await promisify(fs.readdir).call(fs, from); |
| 87 | + |
| 88 | + for (let i=0; i<files.length; i++) { |
| 89 | + const file = files[i]; |
| 90 | + const stat = await promisify(fs.stat).call(fs, path.resolve(from, file)); |
| 91 | + if (stat.isDirectory()) { |
| 92 | + await this.uploadDir(path.resolve(from, file), path.resolve(dest, file)) |
| 93 | + } else if (stat.isFile()) { |
| 94 | + await this.uploadFile(path.resolve(from, file), path.resolve(dest, file)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // await Promise.all(files.map(async file => { |
| 99 | + // const stat = await promisify(fs.stat).call(fs, path.resolve(from, file)); |
| 100 | + // if (stat.isDirectory()) { |
| 101 | + // await this.uploadDir(path.resolve(from, file), path.resolve(dest, file)) |
| 102 | + // } else if (stat.isFile()) { |
| 103 | + // await this.uploadFile(path.resolve(from, file), path.resolve(dest, file)); |
| 104 | + // } |
| 105 | + // })); |
| 106 | + } |
| 107 | + |
| 108 | + async execCmd(cmd) { |
| 109 | + await this.getConnection(); |
| 110 | + const stream = await promisify(this.connection.exec).call(this.connection, cmd); |
| 111 | + return new Promise((resolve, reject) => { |
| 112 | + let data = ''; |
| 113 | + let err = ''; |
| 114 | + stream.on('data', chunk => { |
| 115 | + data += chunk; |
| 116 | + }).stderr.on('data', chunk => { |
| 117 | + err += chunk; |
| 118 | + }).on('close', () => { |
| 119 | + if (err) console.error(`execCmd error ${cmd}`); |
| 120 | + if (err) return reject(err); |
| 121 | + return resolve(data); |
| 122 | + }); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + ignorePath(checkPath) { |
| 127 | + return this.config.ignore.includes(path.normalize(checkPath)) |
| 128 | + } |
| 129 | + |
| 130 | + async execCmds() { |
| 131 | + const { cmds } = this.config; |
| 132 | + if (!cmds || cmds.length <= 0) return ; |
| 133 | + let ret = []; |
| 134 | + for (let i = 0; i < cmds.length; i++) { |
| 135 | + ret.push(await this.execCmd(cmds[i])); |
| 136 | + } |
| 137 | + return ret; |
| 138 | + } |
| 139 | + |
| 140 | + async publish() { |
| 141 | + try { |
| 142 | + if (this.config.clear) await this.execCmd(`rm -rf ${this.config.remote} && echo clear success`); |
| 143 | + const stat = await promisify(fs.stat).call(fs, this.config.local); |
| 144 | + if (stat.isDirectory()) { |
| 145 | + await this.uploadDir(this.config.local, this.config.remote); |
| 146 | + } else if (stat.isFile()) { |
| 147 | + await this.uploadFile(this.config.local, this.config.remote); |
| 148 | + } else { |
| 149 | + throw new Error('unsupported upload type') |
| 150 | + } |
| 151 | + const res = await this.execCmds(); |
| 152 | + res.forEach(m => console.log(m)); |
| 153 | + this.destroy(); |
| 154 | + } catch (e) { |
| 155 | + console.log(e); |
| 156 | + this.destroy(); |
| 157 | + throw e; |
| 158 | + } |
| 159 | + |
| 160 | + }; |
| 161 | + |
| 162 | + destroy() { |
| 163 | + if (this.connection && typeof this.connection.end === 'function') this.connection.end(); |
| 164 | + this.connection = undefined; |
| 165 | + if (this.sftp) this.sftp = undefined; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | +module.exports = Publish; |
0 commit comments