sftp-zip-deploy 0.0.1 → 0.0.3

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 CHANGED
@@ -1,2 +1,50 @@
1
1
  # sftp-zip-deploy
2
- Fast zip-deployment over SFTP
2
+
3
+ [![Node Version](https://img.shields.io/badge/nodejs-%2026-green.svg?logo=node.js&style=flat)](https://nodejs.org)
4
+
5
+ Simple and efficient way to auto-deploy your project to a server:
6
+
7
+ - It zips your distribution folder and sends the archive to your server via SFTP
8
+ - It optionally runs remote commands, before / after zip deployment, via SHH2
9
+ - It provides detailed data about what is going on all the way
10
+
11
+ **IMPORTANT:**
12
+ This library relies on Node-native support for SHH2 and ZIP, and therefore requires Node v26 as minimum.
13
+
14
+ ## Install
15
+
16
+ ```
17
+ npm i sftp-zip-deploy
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ In the example below we deploy a NodeJS project into `/root/web` on our remote server.
23
+
24
+ Once zip file is deployed, we run commands to set that folder as active, unpack
25
+ the archive locally and install project's dependencies.
26
+
27
+ ```ts
28
+ import {zipAndDeploy} from 'sftp-zip-deploy';
29
+
30
+ await zipAndDeploy({
31
+ srcDir: './dist', // folder with content to deploy
32
+ destDir: '/root/web/', // remote folder to copy our zip file
33
+ zipFile: {
34
+ name: 'release.zip' // set custom name for the archive
35
+ },
36
+ cn: {
37
+ host: '1.2.3.4',
38
+ port: 123,
39
+ username: 'root',
40
+ password: '#########'
41
+ },
42
+ cmd: {
43
+ after: 'cd /root/web && unzip -o release.zip && npm i'
44
+ }
45
+ });
46
+ ```
47
+
48
+ Note that we use option `-o` (overwrite) with `unzip`, because otherwise the command may prompt for input,
49
+ if files are already there, and this library does not deal with remote prompts, it will just get stuck.
50
+ So avoid running commands that may trigger an input prompt.
@@ -1,71 +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
- }
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 (on the local machine).
27
+ */
28
+ srcDir: string;
29
+ /**
30
+ * Destination directory (on the remote server).
31
+ */
32
+ destDir: string;
33
+ /**
34
+ * Optional name + location of the local zip file.
35
+ *
36
+ * It defaults to {dir: './', name: 'release.zip'},
37
+ * to create it in the root of your project.
38
+ */
39
+ zipFile?: {
40
+ /**
41
+ * Local directory where zip file is to be created.
42
+ */
43
+ dir?: string;
44
+ /**
45
+ * Name for the local zip file.
46
+ */
47
+ name?: string;
48
+ };
49
+ /**
50
+ * Optional command to execute before and/or after deploying zip file.
51
+ */
52
+ cmd?: {
53
+ before?: string;
54
+ after?: string;
55
+ };
56
+ /**
57
+ * Optional filter on file enumeration, returning:
58
+ * - true for files to be added;
59
+ * - false for files to be skipped.
60
+ */
61
+ onFilter?: (file: string) => boolean;
62
+ /**
63
+ * Notification when zip file has been built,
64
+ * and we are ready to start its transfer.
65
+ */
66
+ onZipReady?: (zd: IZipDetails) => void;
67
+ /**
68
+ * Notification of every output produced by a remote command.
69
+ */
70
+ onCmdOutput?: (cmd: string, err?: Buffer, std?: Buffer) => void;
71
+ }
package/dist/config.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import { ISftpZipDeployConfig } from './config.js';
2
+ export { IZipDetails, ISftpZipDeployConfig } from './config.js';
3
+ export { ConnectOptions } from 'ssh2-sftp-client';
4
+ export declare function zipAndDeploy(cfg: ISftpZipDeployConfig): Promise<boolean>;
package/dist/index.js ADDED
@@ -0,0 +1,33 @@
1
+ import SftpClient from 'ssh2-sftp-client';
2
+ import { Client as SSH2Client } from 'ssh2';
3
+ import { connectSSH2Client, execSHH2Command, fixWindowsPath, zipFiles } from './utils.js';
4
+ import { globSync } from 'glob';
5
+ import path from 'node:path';
6
+ export async function zipAndDeploy(cfg) {
7
+ const ssh2Client = new SSH2Client();
8
+ const { before = '', after = '' } = cfg.cmd ?? {};
9
+ if (before || after) {
10
+ await connectSSH2Client(ssh2Client, cfg.cn);
11
+ await execSHH2Command(ssh2Client, before, (err, std) => cfg.onCmdOutput?.(before, err, std));
12
+ }
13
+ let files = globSync('**/{*.*,.*}', { cwd: cfg.srcDir })
14
+ .map(f => fixWindowsPath(f));
15
+ if (typeof cfg.onFilter === 'function') {
16
+ files = files.filter(cfg.onFilter);
17
+ }
18
+ const zipFileDir = cfg.zipFile?.dir ?? './';
19
+ const zipFileName = cfg.zipFile?.name ?? 'release.zip';
20
+ const zipLocalPath = fixWindowsPath(path.join(zipFileDir, zipFileName));
21
+ const zipRemotePath = fixWindowsPath(path.join(cfg.destDir, zipFileName));
22
+ const zipSize = await zipFiles(cfg.srcDir, files, zipLocalPath);
23
+ cfg.onZipReady?.({ files, zipLocalPath, zipRemotePath, zipSize });
24
+ const sftp = new SftpClient();
25
+ await sftp.connect(cfg.cn);
26
+ await sftp.put(zipLocalPath, zipRemotePath);
27
+ if (after) {
28
+ await execSHH2Command(ssh2Client, after, (err, std) => cfg.onCmdOutput?.(after, err, std));
29
+ }
30
+ await sftp.end();
31
+ ssh2Client.end();
32
+ return files.length > 0;
33
+ }
@@ -0,0 +1,11 @@
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>;
4
+ /**
5
+ * Zips list of files, and resolves with the size of the resulting archive (in bytes).
6
+ */
7
+ export declare function zipFiles(dir: string, files: string[], zipPath: string): Promise<number>;
8
+ /**
9
+ * Fixes Windows path-s, by replacing every '\' with '/'
10
+ */
11
+ export declare function fixWindowsPath(path: string): string;
package/dist/utils.js ADDED
@@ -0,0 +1,44 @@
1
+ import { createZipArchive, ZipEntry } from 'node:zlib';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { createWriteStream } from 'node:fs';
4
+ import { pipeline } from 'node:stream/promises';
5
+ import path from 'node:path';
6
+ export function connectSSH2Client(client, cn) {
7
+ return new Promise((resolve, reject) => {
8
+ client.once('ready', () => resolve(client));
9
+ client.once('error', err => reject(err));
10
+ client.connect(cn);
11
+ });
12
+ }
13
+ export function execSHH2Command(client, cmd, output) {
14
+ return new Promise((resolve, reject) => {
15
+ client.exec(cmd, (err, stream) => {
16
+ if (err) {
17
+ return reject(err);
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
+ }
25
+ /**
26
+ * Zips list of files, and resolves with the size of the resulting archive (in bytes).
27
+ */
28
+ export async function zipFiles(dir, files, zipPath) {
29
+ const entries = [];
30
+ for (const f of files) {
31
+ const p = path.join(dir, f);
32
+ entries.push(await ZipEntry.create(f, await readFile(p)));
33
+ }
34
+ const archive = createZipArchive(entries);
35
+ const ws = createWriteStream(zipPath);
36
+ await pipeline(archive, ws);
37
+ return ws.bytesWritten;
38
+ }
39
+ /**
40
+ * Fixes Windows path-s, by replacing every '\' with '/'
41
+ */
42
+ export function fixWindowsPath(path) {
43
+ return path.replace(/\\/g, '/');
44
+ }
package/package.json CHANGED
@@ -1,16 +1,26 @@
1
1
  {
2
2
  "name": "sftp-zip-deploy",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "type": "module",
6
8
  "scripts": {
7
- "build": "tsc",
8
- "start": "tsc && node ./src/index.js"
9
+ "build": "tsc -p src",
10
+ "start": "tsc -p src && node --no-warnings=ExperimentalWarning=9820 ./dist/index.js"
9
11
  },
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "homepage": "https://github.com/vitaly-t/sftp-zip-deploy",
16
+ "license": "MIT",
10
17
  "repository": {
11
18
  "type": "git",
12
19
  "url": "https://github.com/vitaly-t/sftp-zip-deploy.git"
13
20
  },
21
+ "author": {
22
+ "name": "Vitaly Tomilov"
23
+ },
14
24
  "engines": {
15
25
  "node": ">=26"
16
26
  },
@@ -21,6 +31,6 @@
21
31
  "devDependencies": {
22
32
  "@types/node": "26.6.6",
23
33
  "@types/ssh2-sftp-client": "9.0.6",
24
- "typescript": "6.0.3"
34
+ "typescript": "7.0.2"
25
35
  }
26
36
  }
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
- }