sftp-zip-deploy 0.1.0 → 0.2.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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Node Version](https://img.shields.io/badge/nodejs-%2026-green.svg?logo=node.js&style=flat)](https://nodejs.org)
4
4
 
5
- Simple and efficient way to auto-deploy your project to a server:
5
+ Fast and efficient way to auto-deploy your project to a server:
6
6
 
7
7
  - It zips your distribution folder and sends the archive to your server via SFTP
8
8
  - It optionally runs remote commands, before / after zip deployment, via SSH2
@@ -10,26 +10,26 @@ 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 use `cmd.after` 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';
30
30
 
31
31
  await zipAndDeploy({
32
- srcDir: './dist', // folder with content to deploy
32
+ srcDir: './dist', // local folder with content to deploy
33
33
  destDir: '/root/web/', // remote folder to copy our zip file
34
34
  zipFile: {
35
35
  name: 'release.zip', // set custom name for the archive
@@ -53,9 +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. If you want to use that type explicitly, you will need to add `@types/ssh2-sftp-client`
58
- as a DEV dependency.
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.
59
58
 
60
59
  The main interface [ISftpZipDeployConfig] has a number of options, including events for providing progress details:
61
60
 
@@ -63,8 +62,16 @@ The main interface [ISftpZipDeployConfig] has a number of options, including eve
63
62
  * [onFilter] - to let you filter files inside distribution folder
64
63
  * [onCmdOutput] - to get all output from remote commands
65
64
 
66
- [onZipReady]:https://github.com/vitaly-t/sftp-zip-deploy/blob/main/src/config.ts#L67
67
- [onFilter]:https://github.com/vitaly-t/sftp-zip-deploy/blob/main/src/config.ts#L61
68
- [onCmdOutput]:https://github.com/vitaly-t/sftp-zip-deploy/blob/main/src/config.ts#L81
65
+ [onZipReady]:https://github.com/vitaly-t/sftp-zip-deploy/blob/main/src/config.ts#L69
66
+ [onFilter]:https://github.com/vitaly-t/sftp-zip-deploy/blob/main/src/config.ts#L63
67
+ [onCmdOutput]:https://github.com/vitaly-t/sftp-zip-deploy/blob/main/src/config.ts#L86
69
68
 
70
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
@@ -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
  });
@@ -37,7 +44,7 @@ export async function zipFiles(dir, files, zipPath) {
37
44
  return ws.bytesWritten;
38
45
  }
39
46
  /**
40
- * Fixes Windows path-s, by replacing every '\' with '/'
47
+ * Fixes Windows paths, by replacing every '\' with '/'
41
48
  */
42
49
  export function fixWindowsPath(path) {
43
50
  return path.replace(/\\/g, '/');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "sftp-zip-deploy",
3
- "version": "0.1.0",
4
- "description": "",
3
+ "version": "0.2.1",
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",