sftp-zip-deploy 0.0.0-stage → 0.0.1
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/README.md +2 -3
- package/package.json +26 -6
- package/src/config.ts +71 -0
- package/src/index.ts +86 -0
- package/src/utils.ts +23 -0
- package/tsconfig.json +18 -0
package/README.md
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
|
|
1
|
+
# sftp-zip-deploy
|
|
2
|
+
Fast zip-deployment over SFTP
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "sftp-zip-deploy",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "sftp-zip-deploy",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"start": "tsc && node ./src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/vitaly-t/sftp-zip-deploy.git"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=26"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"glob": "14.0.6",
|
|
19
|
+
"ssh2-sftp-client": "12.1.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "26.6.6",
|
|
23
|
+
"@types/ssh2-sftp-client": "9.0.6",
|
|
24
|
+
"typescript": "6.0.3"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {ConnectOptions} from 'ssh2-sftp-client';
|
|
2
|
+
|
|
3
|
+
export interface IZipDetails {
|
|
4
|
+
/**
|
|
5
|
+
* All files placed into zip.
|
|
6
|
+
*/
|
|
7
|
+
files: string[];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Local path to the zip file.
|
|
11
|
+
*/
|
|
12
|
+
zipLocalPath: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Remote path to the zip file.
|
|
16
|
+
*/
|
|
17
|
+
zipRemotePath: string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Zip file size, in bytes.
|
|
21
|
+
*/
|
|
22
|
+
zipSize: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ISftpZipDeployConfig {
|
|
26
|
+
/**
|
|
27
|
+
* Connection parameters.
|
|
28
|
+
*/
|
|
29
|
+
cn: ConnectOptions;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Source directory.
|
|
33
|
+
*/
|
|
34
|
+
srcDir: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Destination directory.
|
|
38
|
+
*/
|
|
39
|
+
destDir: string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Optional path for the zip file.
|
|
43
|
+
*/
|
|
44
|
+
zipPath?: string;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Optional remote commands to run before or after zip file has been copied over.
|
|
48
|
+
*/
|
|
49
|
+
commands?: {
|
|
50
|
+
before?: string | string[],
|
|
51
|
+
after?: string | string[]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Optional filter on file enumeration, returning:
|
|
56
|
+
* - true for files to be added;
|
|
57
|
+
* - false for files to be skipped.
|
|
58
|
+
*/
|
|
59
|
+
onFilter?: (file: string) => boolean;
|
|
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
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
}
|