/
index.ts
45 lines (44 loc) · 1.4 KB
/
index.ts
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
#! /usr/bin/env node
import path from 'path';
import fs from 'fs-extra';
import upload from './upload';
import exec from './exec';
import config from './getConfig';
(async function () {
const cwd = process.cwd();
const sshOption = config.server;
if (!sshOption) {
console.log('not valid config');
return;
}
const rootPath = path.join(cwd);
console.log(config);
const targetPath = path.join(rootPath, config.upload.localPath);
let zipPath;
fs.readdirSync(targetPath).forEach((each: string) => {
if (config.upload.fileRex.test(each)) {
zipPath = path.join(targetPath, each);
}
});
if (!zipPath) {
throw new Error('no zip file');
}
const zipFileName = path.basename(zipPath);
const remotePath = config.upload.remotePath;
const uploadRes = await upload(sshOption, zipPath, remotePath);
if (uploadRes) {
if (!zipFileName) {
throw new Error('You have to provide zip file name.');
}
if (/[\/\\]/.test(zipFileName)) {
throw new Error('For security reason, you can not use "/" or "" in zip file name.');
}
const remoteZipPath = path.join(remotePath, zipFileName);
exec(sshOption, [
`cd ${remotePath}`,
'pwd',
`unzip -o ${remoteZipPath} -d ${remotePath}`,
`rm ${remoteZipPath}`,
]);
}
}());