sftp-zip-deploy 0.0.8 → 0.2.0

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
@@ -10,20 +10,20 @@ Simple and efficient way to auto-deploy your project to a server:
10
10
 
11
11
  **IMPORTANT:**
12
12
 
13
- This library relies on Node-native support for SSH2 and ZIP, and therefore requires Node v26 as minimum.
13
+ This library relies on Node-native support for SSH2 and ZIP, and therefore requires Node v26 or newer.
14
14
 
15
15
  ## Install
16
16
 
17
17
  ```
18
- npm i sftp-zip-deploy
18
+ npm i sftp-zip-deploy --save-dev
19
19
  ```
20
20
 
21
21
  ## Usage
22
22
 
23
23
  In the example below, we deploy a NodeJS project into `/root/web` on our remote server.
24
24
 
25
- Once zip file is deployed, we run commands to set that folder as active, then unpack
26
- the archive and install project's dependencies.
25
+ Once the zip file is deployed, we run commands to set that folder as active, then unpack
26
+ the archive and install the project's dependencies.
27
27
 
28
28
  ```ts
29
29
  import {zipAndDeploy} from 'sftp-zip-deploy';
@@ -53,8 +53,8 @@ So avoid running commands that may trigger an input prompt.
53
53
 
54
54
  ## API
55
55
 
56
- The library shares connection options for SFTP / SSH2, and reuses the standard `ConnectOptions` / `ConnectConfig`
57
- types from the underlying modules.
56
+ The library shares connection options for SFTP / SSH2, and reuses `ConnectOptions` from `ssh2-sftp-client` module.
57
+ To use that type explicitly, you will need to add `@types/ssh2-sftp-client` as a DEV dependency.
58
58
 
59
59
  The main interface [ISftpZipDeployConfig] has a number of options, including events for providing progress details:
60
60
 
@@ -67,3 +67,11 @@ The main interface [ISftpZipDeployConfig] has a number of options, including eve
67
67
  [onCmdOutput]:https://github.com/vitaly-t/sftp-zip-deploy/blob/main/src/config.ts#L81
68
68
 
69
69
  [ISftpZipDeployConfig]:https://github.com/vitaly-t/sftp-zip-deploy/blob/main/src/config.ts#L6
70
+
71
+ ## Tips
72
+
73
+ Because this library uses the latest ZIP functionality from Node, you may get this warning in the console:
74
+
75
+ > ExperimentalWarning: The zlib ZIP archive API is an experimental feature and might change at any time
76
+
77
+ You can suppress it by adding `--no-warnings=ExperimentalWarning=9820` to your node script command.
package/dist/config.d.ts CHANGED
@@ -33,7 +33,7 @@ export interface ISftpZipDeployConfig {
33
33
  name?: string;
34
34
  };
35
35
  /**
36
- * Optional commands to execute before / after deploying zip file.
36
+ * Optional commands to execute before / after deploying the zip file.
37
37
  */
38
38
  cmd?: {
39
39
  /**
@@ -42,6 +42,8 @@ export interface ISftpZipDeployConfig {
42
42
  before?: string;
43
43
  /**
44
44
  * Command(s) to be executed after zip has been deployed.
45
+ *
46
+ * It is skipped, if no zip was deployed (no files found).
45
47
  */
46
48
  after?: string;
47
49
  };
@@ -67,6 +69,9 @@ export interface ISftpZipDeployConfig {
67
69
  * @param err
68
70
  * Set when the output was sent into StdErr, i.e. an error is reported.
69
71
  *
72
+ * In most cases however, when there is an issue, the command returns
73
+ * an error code, in which case an error is thrown instead.
74
+ *
70
75
  * @param std
71
76
  * Set when normal/standard output is generated (not an error).
72
77
  */
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ export { default as ConnectOptions } from 'ssh2-sftp-client';
8
8
  * 2. Enumerates all files inside `srcDir`, plus filters them via `onFilter` (if used)
9
9
  * 3. Produces zip file with all the resulting files and triggers `onZipReady` (if used)
10
10
  * 4. Uploads zip file to the remote machine
11
- * 5. Executes `cmd.after` commands, if such were specified
11
+ * 5. Executes `cmd.after` commands, if such were specified, and zip was actually deployed.
12
12
  * 6. Resolves with `true`, if at least one file was found and zip was created,
13
13
  * or `false` otherwise (i.e. it does not create an empty zip)
14
14
  */
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ export { default as ConnectOptions } from 'ssh2-sftp-client';
11
11
  * 2. Enumerates all files inside `srcDir`, plus filters them via `onFilter` (if used)
12
12
  * 3. Produces zip file with all the resulting files and triggers `onZipReady` (if used)
13
13
  * 4. Uploads zip file to the remote machine
14
- * 5. Executes `cmd.after` commands, if such were specified
14
+ * 5. Executes `cmd.after` commands, if such were specified, and zip was actually deployed.
15
15
  * 6. Resolves with `true`, if at least one file was found and zip was created,
16
16
  * or `false` otherwise (i.e. it does not create an empty zip)
17
17
  */
@@ -20,26 +20,30 @@ export async function zipAndDeploy(cfg) {
20
20
  const { before = '', after = '' } = cfg.cmd ?? {};
21
21
  if (before || after) {
22
22
  await connectSSH2Client(ssh2Client, cfg.cn);
23
+ }
24
+ if (before) {
23
25
  await execSSH2Command(ssh2Client, before, (err, std) => cfg.onCmdOutput?.(before, err, std));
24
26
  }
25
- let files = globSync('**/{*.*,.*}', { cwd: cfg.srcDir })
27
+ let files = globSync('**/{*.*,*}', { cwd: cfg.srcDir, nodir: true, dot: true })
26
28
  .map(f => fixWindowsPath(f));
27
29
  if (typeof cfg.onFilter === 'function') {
28
30
  files = files.filter(cfg.onFilter);
29
31
  }
30
- const zipFileDir = cfg.zipFile?.dir ?? './';
31
- const zipFileName = cfg.zipFile?.name ?? 'release.zip';
32
- const zipLocalPath = fixWindowsPath(path.join(zipFileDir, zipFileName));
33
- const zipRemotePath = fixWindowsPath(path.join(cfg.destDir, zipFileName));
34
- const zipSize = await zipFiles(cfg.srcDir, files, zipLocalPath);
35
- cfg.onZipReady?.({ files, zipLocalPath, zipRemotePath, zipSize });
36
- const sftp = new SftpClient();
37
- await sftp.connect(cfg.cn);
38
- await sftp.put(zipLocalPath, zipRemotePath);
39
- if (after) {
40
- await execSSH2Command(ssh2Client, after, (err, std) => cfg.onCmdOutput?.(after, err, std));
32
+ if (files.length > 0) {
33
+ const zipFileDir = cfg.zipFile?.dir ?? './';
34
+ const zipFileName = cfg.zipFile?.name ?? 'release.zip';
35
+ const zipLocalPath = fixWindowsPath(path.join(zipFileDir, zipFileName));
36
+ const zipRemotePath = fixWindowsPath(path.join(cfg.destDir, zipFileName));
37
+ const zipSize = await zipFiles(cfg.srcDir, files, zipLocalPath);
38
+ cfg.onZipReady?.({ files, zipLocalPath, zipRemotePath, zipSize });
39
+ const sftp = new SftpClient();
40
+ await sftp.connect(cfg.cn);
41
+ await sftp.put(zipLocalPath, zipRemotePath);
42
+ await sftp.end();
43
+ if (after) {
44
+ await execSSH2Command(ssh2Client, after, (err, std) => cfg.onCmdOutput?.(after, err, std));
45
+ }
41
46
  }
42
- await sftp.end();
43
47
  ssh2Client.end();
44
48
  return files.length > 0;
45
49
  }
package/dist/utils.d.ts CHANGED
@@ -6,6 +6,6 @@ export declare function execSSH2Command(client: SSH2Client, cmd: string, output:
6
6
  */
7
7
  export declare function zipFiles(dir: string, files: string[], zipPath: string): Promise<number>;
8
8
  /**
9
- * Fixes Windows path-s, by replacing every '\' with '/'
9
+ * Fixes Windows paths, by replacing every '\' with '/'
10
10
  */
11
11
  export declare function fixWindowsPath(path: string): string;
package/dist/utils.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createZipArchive, ZipEntry, createGzip, constants } from 'node:zlib';
1
+ import { createZipArchive, ZipEntry } from 'node:zlib';
2
2
  import { readFile } from 'node:fs/promises';
3
3
  import { createWriteStream } from 'node:fs';
4
4
  import { pipeline } from 'node:stream/promises';
@@ -16,7 +16,14 @@ export function execSSH2Command(client, cmd, output) {
16
16
  if (err) {
17
17
  return reject(err);
18
18
  }
19
- stream.once('close', () => resolve());
19
+ stream.once('close', (code) => {
20
+ if (code) {
21
+ reject(new Error(`Command "${cmd}" exited with code ${code}`));
22
+ }
23
+ else {
24
+ resolve();
25
+ }
26
+ });
20
27
  stream.on('data', (chunk) => output(undefined, chunk));
21
28
  stream.stderr.on('data', (chunk) => output(chunk, undefined));
22
29
  });
@@ -33,17 +40,11 @@ export async function zipFiles(dir, files, zipPath) {
33
40
  }
34
41
  const archive = createZipArchive(entries);
35
42
  const ws = createWriteStream(zipPath);
36
- const maxCompressionOptions = {
37
- level: constants.Z_BEST_COMPRESSION, // 9 (default is 6)
38
- windowBits: 15, // Max 32KB history window (default is 15)
39
- memLevel: 9, // Max internal hash table memory (default is 8)
40
- strategy: constants.Z_DEFAULT_STRATEGY,
41
- };
42
- await pipeline(archive, createGzip(maxCompressionOptions), ws);
43
+ await pipeline(archive, ws);
43
44
  return ws.bytesWritten;
44
45
  }
45
46
  /**
46
- * Fixes Windows path-s, by replacing every '\' with '/'
47
+ * Fixes Windows paths, by replacing every '\' with '/'
47
48
  */
48
49
  export function fixWindowsPath(path) {
49
50
  return path.replace(/\\/g, '/');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "sftp-zip-deploy",
3
- "version": "0.0.8",
4
- "description": "",
3
+ "version": "0.2.0",
4
+ "description": "Zip and deploy folder over SFTP",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "type": "module",