sftp-zip-deploy 0.0.2 → 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.
package/dist/config.d.ts CHANGED
@@ -23,23 +23,35 @@ export interface ISftpZipDeployConfig {
23
23
  */
24
24
  cn: ConnectOptions;
25
25
  /**
26
- * Source directory.
26
+ * Source directory (on the local machine).
27
27
  */
28
28
  srcDir: string;
29
29
  /**
30
- * Destination directory.
30
+ * Destination directory (on the remote server).
31
31
  */
32
32
  destDir: string;
33
33
  /**
34
- * Optional path for the zip file.
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.
35
38
  */
36
- zipPath?: string;
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
+ };
37
49
  /**
38
- * Optional remote commands to run before or after zip file has been copied over.
50
+ * Optional command to execute before and/or after deploying zip file.
39
51
  */
40
- commands?: {
41
- before?: string | string[];
42
- after?: string | string[];
52
+ cmd?: {
53
+ before?: string;
54
+ after?: string;
43
55
  };
44
56
  /**
45
57
  * Optional filter on file enumeration, returning:
package/dist/config.js CHANGED
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- import { ISftpZipDeployConfig } from './config';
2
- export { default as ConnectOptions } from 'ssh2-sftp-client';
1
+ import { ISftpZipDeployConfig } from './config.js';
2
+ export { IZipDetails, ISftpZipDeployConfig } from './config.js';
3
+ export { ConnectOptions } from 'ssh2-sftp-client';
3
4
  export declare function zipAndDeploy(cfg: ISftpZipDeployConfig): Promise<boolean>;
package/dist/index.js CHANGED
@@ -1,63 +1,33 @@
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
- }
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));
33
12
  }
34
- let allFiles = (0, glob_1.globSync)('**/{*.*,.*}', { cwd: cfg.srcDir })
35
- .map(f => f.replace(/\\/g, '/'));
13
+ let files = globSync('**/{*.*,.*}', { cwd: cfg.srcDir })
14
+ .map(f => fixWindowsPath(f));
36
15
  if (typeof cfg.onFilter === 'function') {
37
- allFiles = allFiles.filter(cfg.onFilter);
16
+ files = files.filter(cfg.onFilter);
38
17
  }
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();
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();
50
25
  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
- });
26
+ await sftp.put(zipLocalPath, zipRemotePath);
27
+ if (after) {
28
+ await execSHH2Command(ssh2Client, after, (err, std) => cfg.onCmdOutput?.(after, err, std));
60
29
  }
30
+ await sftp.end();
61
31
  ssh2Client.end();
62
- return true;
32
+ return files.length > 0;
63
33
  }
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,11 @@
1
1
  import { Client as SSH2Client, ConnectConfig } from 'ssh2';
2
2
  export declare function connectSSH2Client(client: SSH2Client, cn: ConnectConfig): Promise<SSH2Client>;
3
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 CHANGED
@@ -1,20 +1,20 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.connectSSH2Client = connectSSH2Client;
4
- exports.execSHH2Command = execSHH2Command;
5
- function connectSSH2Client(client, cn) {
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) {
6
7
  return new Promise((resolve, reject) => {
7
8
  client.once('ready', () => resolve(client));
8
9
  client.once('error', err => reject(err));
9
10
  client.connect(cn);
10
11
  });
11
12
  }
12
- function execSHH2Command(client, cmd, output) {
13
+ export function execSHH2Command(client, cmd, output) {
13
14
  return new Promise((resolve, reject) => {
14
15
  client.exec(cmd, (err, stream) => {
15
16
  if (err) {
16
- reject(err);
17
- return;
17
+ return reject(err);
18
18
  }
19
19
  stream.once('close', () => resolve());
20
20
  stream.on('data', (chunk) => output(undefined, chunk));
@@ -22,3 +22,23 @@ function execSHH2Command(client, cmd, output) {
22
22
  });
23
23
  });
24
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,12 +1,13 @@
1
1
  {
2
2
  "name": "sftp-zip-deploy",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
+ "type": "module",
7
8
  "scripts": {
8
9
  "build": "tsc -p src",
9
- "start": "tsc && node ./src/index.js"
10
+ "start": "tsc -p src && node --no-warnings=ExperimentalWarning=9820 ./dist/index.js"
10
11
  },
11
12
  "files": [
12
13
  "dist"
@@ -30,6 +31,6 @@
30
31
  "devDependencies": {
31
32
  "@types/node": "26.6.6",
32
33
  "@types/ssh2-sftp-client": "9.0.6",
33
- "typescript": "6.0.3"
34
+ "typescript": "7.0.2"
34
35
  }
35
36
  }