sftp-zip-deploy 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{src/config.ts → dist/config.d.ts} +59 -71
- package/dist/config.js +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +63 -0
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +24 -0
- package/package.json +12 -3
- package/src/index.ts +0 -86
- package/src/utils.ts +0 -23
- package/tsconfig.json +0 -18
|
@@ -1,71 +1,59 @@
|
|
|
1
|
-
import {ConnectOptions} from 'ssh2-sftp-client';
|
|
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
|
-
* Notification when zip file has been built,
|
|
63
|
-
* and we are ready to start its transfer.
|
|
64
|
-
*/
|
|
65
|
-
onZipReady?: (zd: IZipDetails) => void;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Notification of every output produced by a remote command.
|
|
69
|
-
*/
|
|
70
|
-
onCmdOutput?: (cmd: string, err?: Buffer, std?: Buffer) => void;
|
|
71
|
-
}
|
|
1
|
+
import { ConnectOptions } from 'ssh2-sftp-client';
|
|
2
|
+
export interface IZipDetails {
|
|
3
|
+
/**
|
|
4
|
+
* All files placed into zip.
|
|
5
|
+
*/
|
|
6
|
+
files: string[];
|
|
7
|
+
/**
|
|
8
|
+
* Local path to the zip file.
|
|
9
|
+
*/
|
|
10
|
+
zipLocalPath: string;
|
|
11
|
+
/**
|
|
12
|
+
* Remote path to the zip file.
|
|
13
|
+
*/
|
|
14
|
+
zipRemotePath: string;
|
|
15
|
+
/**
|
|
16
|
+
* Zip file size, in bytes.
|
|
17
|
+
*/
|
|
18
|
+
zipSize: number;
|
|
19
|
+
}
|
|
20
|
+
export interface ISftpZipDeployConfig {
|
|
21
|
+
/**
|
|
22
|
+
* Connection parameters.
|
|
23
|
+
*/
|
|
24
|
+
cn: ConnectOptions;
|
|
25
|
+
/**
|
|
26
|
+
* Source directory.
|
|
27
|
+
*/
|
|
28
|
+
srcDir: string;
|
|
29
|
+
/**
|
|
30
|
+
* Destination directory.
|
|
31
|
+
*/
|
|
32
|
+
destDir: string;
|
|
33
|
+
/**
|
|
34
|
+
* Optional path for the zip file.
|
|
35
|
+
*/
|
|
36
|
+
zipPath?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Optional remote commands to run before or after zip file has been copied over.
|
|
39
|
+
*/
|
|
40
|
+
commands?: {
|
|
41
|
+
before?: string | string[];
|
|
42
|
+
after?: string | string[];
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Optional filter on file enumeration, returning:
|
|
46
|
+
* - true for files to be added;
|
|
47
|
+
* - false for files to be skipped.
|
|
48
|
+
*/
|
|
49
|
+
onFilter?: (file: string) => boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Notification when zip file has been built,
|
|
52
|
+
* and we are ready to start its transfer.
|
|
53
|
+
*/
|
|
54
|
+
onZipReady?: (zd: IZipDetails) => void;
|
|
55
|
+
/**
|
|
56
|
+
* Notification of every output produced by a remote command.
|
|
57
|
+
*/
|
|
58
|
+
onCmdOutput?: (cmd: string, err?: Buffer, std?: Buffer) => void;
|
|
59
|
+
}
|
package/dist/config.js
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ConnectOptions = void 0;
|
|
7
|
+
exports.zipAndDeploy = zipAndDeploy;
|
|
8
|
+
const ssh2_sftp_client_1 = __importDefault(require("ssh2-sftp-client"));
|
|
9
|
+
const ssh2_1 = require("ssh2");
|
|
10
|
+
const utils_1 = require("./utils");
|
|
11
|
+
const node_zlib_1 = require("node:zlib");
|
|
12
|
+
const node_fs_1 = require("node:fs");
|
|
13
|
+
const glob_1 = require("glob");
|
|
14
|
+
const promises_1 = require("node:fs/promises");
|
|
15
|
+
const promises_2 = require("node:stream/promises");
|
|
16
|
+
var ssh2_sftp_client_2 = require("ssh2-sftp-client");
|
|
17
|
+
Object.defineProperty(exports, "ConnectOptions", { enumerable: true, get: function () { return __importDefault(ssh2_sftp_client_2).default; } });
|
|
18
|
+
async function zipAndDeploy(cfg) {
|
|
19
|
+
const ac = cfg.commands?.after ?? [];
|
|
20
|
+
const afterCommands = (Array.isArray(ac) ? ac : [ac]);
|
|
21
|
+
const bc = cfg.commands?.before ?? [];
|
|
22
|
+
const beforeCommands = (Array.isArray(bc) ? bc : [bc]);
|
|
23
|
+
const ssh2Client = new ssh2_1.Client();
|
|
24
|
+
if (ac.length > 0 || bc.length > 0) {
|
|
25
|
+
await (0, utils_1.connectSSH2Client)(ssh2Client, cfg.cn);
|
|
26
|
+
for (const c of beforeCommands) {
|
|
27
|
+
await (0, utils_1.execSHH2Command)(ssh2Client, c, (err, std) => {
|
|
28
|
+
if (typeof cfg.onCmdOutput === 'function') {
|
|
29
|
+
cfg.onCmdOutput(c, err, std);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
let allFiles = (0, glob_1.globSync)('**/{*.*,.*}', { cwd: cfg.srcDir })
|
|
35
|
+
.map(f => f.replace(/\\/g, '/'));
|
|
36
|
+
if (typeof cfg.onFilter === 'function') {
|
|
37
|
+
allFiles = allFiles.filter(cfg.onFilter);
|
|
38
|
+
}
|
|
39
|
+
if (allFiles.length === 0) {
|
|
40
|
+
return false; // nothing to deploy
|
|
41
|
+
}
|
|
42
|
+
const entries = [];
|
|
43
|
+
for (const src of allFiles) {
|
|
44
|
+
entries.push(await node_zlib_1.ZipEntry.create(src, await (0, promises_1.readFile)(src)));
|
|
45
|
+
}
|
|
46
|
+
const archive = (0, node_zlib_1.createZipArchive)(entries);
|
|
47
|
+
const ws = (0, node_fs_1.createWriteStream)('release.zip');
|
|
48
|
+
await (0, promises_2.pipeline)(archive, ws);
|
|
49
|
+
const sftp = new ssh2_sftp_client_1.default();
|
|
50
|
+
await sftp.connect(cfg.cn);
|
|
51
|
+
const p = `${cfg.destDir}/release.zip`;
|
|
52
|
+
await sftp.put('release.zip', p);
|
|
53
|
+
await sftp.end();
|
|
54
|
+
for (const c of afterCommands) {
|
|
55
|
+
await (0, utils_1.execSHH2Command)(ssh2Client, c, (err, std) => {
|
|
56
|
+
if (typeof cfg.onCmdOutput === 'function') {
|
|
57
|
+
cfg.onCmdOutput(c, err, std);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
ssh2Client.end();
|
|
62
|
+
return true;
|
|
63
|
+
}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { Client as SSH2Client, ConnectConfig } from 'ssh2';
|
|
2
|
+
export declare function connectSSH2Client(client: SSH2Client, cn: ConnectConfig): Promise<SSH2Client>;
|
|
3
|
+
export declare function execSHH2Command(client: SSH2Client, cmd: string, output: (err?: Buffer, std?: Buffer) => void): Promise<void>;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.connectSSH2Client = connectSSH2Client;
|
|
4
|
+
exports.execSHH2Command = execSHH2Command;
|
|
5
|
+
function connectSSH2Client(client, cn) {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
client.once('ready', () => resolve(client));
|
|
8
|
+
client.once('error', err => reject(err));
|
|
9
|
+
client.connect(cn);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
function execSHH2Command(client, cmd, output) {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
client.exec(cmd, (err, stream) => {
|
|
15
|
+
if (err) {
|
|
16
|
+
reject(err);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
stream.once('close', () => resolve());
|
|
20
|
+
stream.on('data', (chunk) => output(undefined, chunk));
|
|
21
|
+
stream.stderr.on('data', (chunk) => output(chunk, undefined));
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
package/package.json
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sftp-zip-deploy",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"build": "tsc",
|
|
8
|
+
"build": "tsc -p src",
|
|
8
9
|
"start": "tsc && node ./src/index.js"
|
|
9
10
|
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/vitaly-t/sftp-zip-deploy",
|
|
15
|
+
"license": "MIT",
|
|
10
16
|
"repository": {
|
|
11
17
|
"type": "git",
|
|
12
18
|
"url": "https://github.com/vitaly-t/sftp-zip-deploy.git"
|
|
13
19
|
},
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "Vitaly Tomilov"
|
|
22
|
+
},
|
|
14
23
|
"engines": {
|
|
15
24
|
"node": ">=26"
|
|
16
25
|
},
|
package/src/index.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import {ISftpZipDeployConfig} from './config';
|
|
2
|
-
import SftpClient from 'ssh2-sftp-client';
|
|
3
|
-
import {Client as SSH2Client} from 'ssh2';
|
|
4
|
-
import {connectSSH2Client, execSHH2Command} from './utils';
|
|
5
|
-
import {createZipArchive, ZipEntry} from 'node:zlib';
|
|
6
|
-
import {createWriteStream} from 'node:fs';
|
|
7
|
-
import {globSync} from 'glob';
|
|
8
|
-
import {readFile} from 'node:fs/promises';
|
|
9
|
-
import {pipeline} from 'node:stream/promises';
|
|
10
|
-
|
|
11
|
-
export {default as ConnectOptions} from 'ssh2-sftp-client';
|
|
12
|
-
|
|
13
|
-
export async function zipAndDeploy(cfg: ISftpZipDeployConfig): Promise<boolean> {
|
|
14
|
-
|
|
15
|
-
const ac = cfg.commands?.after ?? [];
|
|
16
|
-
const afterCommands = (Array.isArray(ac) ? ac : [ac]);
|
|
17
|
-
const bc = cfg.commands?.before ?? [];
|
|
18
|
-
const beforeCommands = (Array.isArray(bc) ? bc : [bc]);
|
|
19
|
-
|
|
20
|
-
const ssh2Client = new SSH2Client();
|
|
21
|
-
|
|
22
|
-
if (ac.length > 0 || bc.length > 0) {
|
|
23
|
-
await connectSSH2Client(ssh2Client, cfg.cn);
|
|
24
|
-
for (const c of beforeCommands) {
|
|
25
|
-
await execSHH2Command(ssh2Client, c, (err, std) => {
|
|
26
|
-
if (typeof cfg.onCmdOutput === 'function') {
|
|
27
|
-
cfg.onCmdOutput(c, err, std);
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let allFiles = globSync('**/{*.*,.*}', {cwd: cfg.srcDir})
|
|
34
|
-
.map(f => f.replace(/\\/g, '/'));
|
|
35
|
-
|
|
36
|
-
if (typeof cfg.onFilter === 'function') {
|
|
37
|
-
allFiles = allFiles.filter(cfg.onFilter);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (allFiles.length === 0) {
|
|
41
|
-
return false; // nothing to deploy
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const entries: ZipEntry[] = [];
|
|
45
|
-
for (const src of allFiles) {
|
|
46
|
-
entries.push(await ZipEntry.create(src, await readFile(src)));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const archive = createZipArchive(entries);
|
|
50
|
-
const ws = createWriteStream('release.zip');
|
|
51
|
-
await pipeline(archive, ws);
|
|
52
|
-
|
|
53
|
-
const sftp = new SftpClient();
|
|
54
|
-
await sftp.connect(cfg.cn);
|
|
55
|
-
|
|
56
|
-
const p = `${cfg.destDir}/release.zip`;
|
|
57
|
-
await sftp.put('release.zip', p);
|
|
58
|
-
|
|
59
|
-
await sftp.end();
|
|
60
|
-
|
|
61
|
-
for (const c of afterCommands) {
|
|
62
|
-
await execSHH2Command(ssh2Client, c, (err, std) => {
|
|
63
|
-
if (typeof cfg.onCmdOutput === 'function') {
|
|
64
|
-
cfg.onCmdOutput(c, err, std);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
ssh2Client.end();
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
(async function main() {
|
|
74
|
-
const start = Date.now();
|
|
75
|
-
await zipAndDeploy({
|
|
76
|
-
srcDir: './src',
|
|
77
|
-
destDir: '/root/web/',
|
|
78
|
-
cn: {
|
|
79
|
-
host: '69.48.191.37',
|
|
80
|
-
port: 7822,
|
|
81
|
-
username: 'root',
|
|
82
|
-
password: '694b54a95c724624'
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
console.log(`Finished in: ${Date.now() - start} ms`);
|
|
86
|
-
})();
|
package/src/utils.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import {Client as SSH2Client, ConnectConfig} from 'ssh2';
|
|
2
|
-
|
|
3
|
-
export function connectSSH2Client(client: SSH2Client, cn: ConnectConfig): Promise<SSH2Client> {
|
|
4
|
-
return new Promise((resolve, reject) => {
|
|
5
|
-
client.once('ready', () => resolve(client));
|
|
6
|
-
client.once('error', err => reject(err));
|
|
7
|
-
client.connect(cn);
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function execSHH2Command(client: SSH2Client, cmd: string, output: (err?: Buffer, std?: Buffer) => void): Promise<void> {
|
|
12
|
-
return new Promise((resolve, reject) => {
|
|
13
|
-
client.exec(cmd, (err, stream) => {
|
|
14
|
-
if (err) {
|
|
15
|
-
reject(err);
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
stream.once('close', () => resolve());
|
|
19
|
-
stream.on('data', (chunk: Buffer) => output(undefined, chunk));
|
|
20
|
-
stream.stderr.on('data', (chunk: Buffer) => output(chunk, undefined));
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2025",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"noUnusedLocals": false,
|
|
8
|
-
"removeComments": true,
|
|
9
|
-
"ignoreDeprecations": "6.0",
|
|
10
|
-
"types": ["*"]
|
|
11
|
-
},
|
|
12
|
-
"include": [
|
|
13
|
-
"./src/*.ts"
|
|
14
|
-
],
|
|
15
|
-
"exclude": [
|
|
16
|
-
"node_modules"
|
|
17
|
-
]
|
|
18
|
-
}
|