-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
68 lines (63 loc) · 2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const fse = require("fs-extra");
const node_ssh = require("node-ssh");
const consola = require("consola");
const chalk = require("chalk");
const path = require("path");
const ssh = new node_ssh();
async function asyncModule() {
const { BUILD_ENV, IS_BUILD_DEV } = this.options.env;
const { outputDir, dirName } = this.options.globalModuleOptions;
if (!IS_BUILD_DEV) {
return;
}
this.nuxt.hook("generate:done", async nuxt => {
const ftp_config = nuxt.options.project.ftp[BUILD_ENV];
const ssh_config = {
host: ftp_config.host,
username: ftp_config.username,
password: ftp_config.password,
port: ftp_config.port,
readyTimeout: 60000
};
const distDir = path.join(nuxt.options.rootDir, "dist", outputDir, dirName);
const isExist = await fse.pathExists(distDir);
if (isExist) {
try {
await ssh.connect(ssh_config);
} catch (error) {
consola.error(chalk.red("测试服务器连接失败,上传代码没有成功"));
return Promise.resolve();
}
//清空目标目录
// await ssh.execCommand(
// `rm -rf ${ftp_config.dir}/${nuxt.options.project.dirName}/`
// );
consola.info("正在将代码上传至测试服务器。。。");
//上传代码
const result = await ssh.putDirectory(
distDir,
`${ftp_config.dir}/${nuxt.options.project.dirName}`,
{
recursive: true,
concurrency: 1,
tick(local, remote, error) {
if (error) {
consola.error(local);
} else {
consola.success(local, "->", remote);
}
}
}
);
if (result) {
consola.success(`上传代码成功!`);
consola.info("最新测试版本", `${nuxt.options.project.version.nextDev}`);
} else {
consola.error("上传失败,请重新上传");
}
ssh.dispose();
}
});
}
module.exports = asyncModule;
module.exports.meta = require("./package.json");