From d038a6f6840a457397b78978659d8593c4752030 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Tue, 10 Sep 2019 12:52:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4deploy.js=EF=BC=8C=E6=8D=A2?= =?UTF-8?q?=E7=94=A8=20npm/deployer=20=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 -- deploy.js | 120 --------------------------------------------------- package.json | 4 +- 3 files changed, 2 insertions(+), 126 deletions(-) delete mode 100644 deploy.js diff --git a/README.md b/README.md index e0ecfa3..fdf8935 100644 --- a/README.md +++ b/README.md @@ -77,10 +77,6 @@ npm install ``` npm run deploy -- -p '远程登录密码' -k "用户私钥文件" -u '远程登录用户' -r '远程路径' -H '远程主机' -P '远程端口' ``` - 或者不借助 npm, 直接调用 deploy.js: - ``` - node deploy.js -p 远程登录密码' -k "用户私钥文件" -u '远程登录用户' -r '远程路径' -H '远程主机' -P '远程端口' - ``` 5. `daemon` to launch web server on a TIC node host diff --git a/deploy.js b/deploy.js deleted file mode 100644 index 6009cd8..0000000 --- a/deploy.js +++ /dev/null @@ -1,120 +0,0 @@ -const fs = require('fs') -const path = require('path') -const ssh = new (require('node-ssh'))() - -/** ******************* 读取命令行以及配置文件里的参数 ******************** **/ -const commander = require('commander') -const deepmerge = require('deepmerge') - -var Config = {} - -// 读取配置文件 -try { - if (fs.existsSync('./ConfigBasic.js')) { - Config = require('./ConfigBasic.js') - console.info('ConfigBasic loaded') - } - if (fs.existsSync('./ConfigCustom.js')) { // 如果存在,覆盖掉 ConfigBasic 里的默认参数 - Config = deepmerge(Config, require('./ConfigCustom.js')) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加 - console.info('ConfigCustom loaded') - } - if (fs.existsSync('./ConfigSecret.js')) { // 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数 - Config = deepmerge(Config, require('./ConfigSecret.js')) - console.info('ConfigSecret loaded') - } -} catch (err) { - console.error('Loading config files failed: ' + err.message) -} - -commander - .version('1.0', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version' - .option('-H, --host ', `Host IP or domain name of the target server. Default to ${Config.deploy.host}`) - .option('-P, --port ', `Ssh port number of the target server. Default to ${Config.deploy.port}`) - .option('-r, --root ', `Path to deploy on the target server. Default to ${Config.deploy.root}`) - .option('-d, --dist ', `Folder to deploy on the target server. Default to ${Config.deploy.dist}`) - .option('-u, --user ', `User id to login the target server. Default to ${Config.deploy.user}`) - .option('-k, --key ', `User private key file to login the target server. Default to ${Config.deploy.key}`) - .option('-p, --password ', `User password to login the target server. You may have to enclose it in "". Default to ${Config.deploy.password}`) - .parse(process.argv) - -const root = commander.root || Config.deploy.root // 本地的项目目录。似乎该目录必须已经存在于服务器上 -console.log(` root = ${root} `) -const dist = commander.dist || Config.deploy.dist || 'dist' // 新系统将发布在这个目录里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。 -console.log(` dist = ${dist} `) -const privateKeyFile = commander.key || Config.deploy.key || `${process.env.HOME}/.ssh/id_rsa` -console.log(` privateKeyFile = ${privateKeyFile}`) - -const connection = { - host: commander.host || Config.deploy.host, - port: commander.port || Config.deploy.port || 22, - username: commander.user || Config.deploy.user, - privateKey: fs.existsSync(privateKeyFile) ? privateKeyFile : undefined, - password: commander.password || Config.deploy.password, - tryKeyboard: true, - onKeyboardInteractive: (name, instructions, instructionsLang, prompts, finish) => { // 不起作用 - if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) { - finish([password]) - } - }, -} -console.log(` connection = ${JSON.stringify(connection)}`) - -/** ********************** 连接到待部署的主机,拷贝文件到指定路径 ************* **/ -function subDirs (path) { - const dirs = [path] - if (fs.statSync(path).isFile()) { - return dirs - } - fs.readdirSync(path).forEach(item => { - const stat = fs.statSync(`${path}/${item}`) - if (stat.isDirectory()) { - dirs.push(...subDirs(`${path}/${item}`)) - } - }) - return dirs -} - -const necessaryPath = (path) => { - return subDirs(path) - .map(it => it.replace(path, '')) - .filter(it => it) - .map(it => it.split('/').filter(it => it)) -} - -ssh.connect(connection).then(async () => { - console.log(`[ mv ${dist} ${dist}-backup-${new Date().toISOString()} ... ]`) - await ssh.execCommand(`mv ${dist} ${dist}-backup-${new Date().toISOString()}`, { cwd: root }) - console.log(`[ mkdir ${dist} ... ]`) - await ssh.execCommand(`mkdir ${dist}`, { cwd: root }) - const toCreate = necessaryPath('./dist') - for (const name of toCreate) { - console.log(`[ mkdir ${dist}/${name.join('/')} ... ]`) - await ssh.execCommand(`mkdir ${dist}/${name.join('/')}`, { cwd: root }) - } - - let err - console.log(`[ Upload to ${root}/${dist} ... ]`) - await ssh.putDirectory('./dist', `${root}/${dist}`, { - concurrency: 10, - recursive: true, - validate: itemPath => { - const baseName = path.basename(itemPath) - return !baseName.endsWith('.map') - }, - tick: (localPath, remotePath, error) => { - console.log(`Uploading "${localPath}" ===> "${remotePath}" ${error || 'succeeded!'}`) - err = error - }, - }) - ssh.dispose() - if (err) { - console.log('[ Uploaded with error! ]') - process.exit(1) - } else { - console.log('[ Uploaded successfully! ]') - } -}).catch(err => { - console.error(err) - ssh.dispose() - process.exit(1) -}) diff --git a/package.json b/package.json index 547462f..0bd0391 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "scripts": { "dev": "vue-cli-service serve", "dist": "vue-cli-service build", - "deploy": "node ./deploy.js", + "deploy": "node ./node_modules/deployer/deploy.js", "daemon": "cross-env NODE_ENV=production node server.js", "daemon.sup": "cross-env NODE_ENV=production supervisor -i data.log,node_modules,dist -- server.js", "daemon.pm2": "cross-env NODE_ENV=production pm2 start server.js -name node.console.web", @@ -49,8 +49,8 @@ "babel-core": "7.0.0-bridge.0", "babel-jest": "^23.0.1", "babel-plugin-transform-imports": "^1.5.1", + "deployer": "git+https://git.faronear.org/npm/deployer", "node-sass": "^4.9.0", - "node-ssh": "^5.1.2", "sass-loader": "^7.0.1", "stylus": "^0.54.5", "stylus-loader": "^3.0.2",