sftp-zip-deploy 0.2.2 → 0.2.4

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/dist/config.d.ts CHANGED
@@ -23,8 +23,7 @@ export interface ISftpZipDeployConfig {
23
23
  */
24
24
  zipFile?: {
25
25
  /**
26
- * Local directory where zip file is to be created.
27
- * Default is './'
26
+ * Local directory where zip file is to be created. Default is './'
28
27
  */
29
28
  dir?: string;
30
29
  /**
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ISftpZipDeployConfig } from './config.js';
2
2
  export { IZipDetails, ISftpZipDeployConfig } from './config.js';
3
- export { default as ConnectOptions } from 'ssh2-sftp-client';
3
+ export type { ConnectOptions } from 'ssh2-sftp-client';
4
4
  /**
5
5
  * From the configuration object, it implements the following steps:
6
6
  *
package/dist/index.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import SftpClient from 'ssh2-sftp-client';
2
2
  import { Client as SSH2Client } from 'ssh2';
3
3
  import { connectSSH2Client, execSSH2Command, fixWindowsPath, zipFiles } from './utils.js';
4
- import { globSync } from 'glob';
4
+ import { glob } from 'glob';
5
5
  import path from 'node:path';
6
- export { default as ConnectOptions } from 'ssh2-sftp-client';
7
6
  /**
8
7
  * From the configuration object, it implements the following steps:
9
8
  *
@@ -16,34 +15,40 @@ export { default as ConnectOptions } from 'ssh2-sftp-client';
16
15
  * or `false` otherwise (i.e. it does not create an empty zip)
17
16
  */
18
17
  export async function zipAndDeploy(cfg) {
19
- const ssh2Client = new SSH2Client();
18
+ const sftp = new SftpClient();
19
+ const ssh2 = new SSH2Client();
20
20
  const { before = '', after = '' } = cfg.cmd ?? {};
21
- if (before || after) {
22
- await connectSSH2Client(ssh2Client, cfg.cn);
23
- }
24
- if (before) {
25
- await execSSH2Command(ssh2Client, before, (err, std) => cfg.onCmdOutput?.(before, err, std));
26
- }
27
- let files = globSync('**/{*.*,*}', { cwd: cfg.srcDir, nodir: true, dot: true })
28
- .map(f => fixWindowsPath(f));
29
- if (typeof cfg.onFilter === 'function') {
30
- files = files.filter(cfg.onFilter);
21
+ let hasFiles = false;
22
+ try {
23
+ if (before || after) {
24
+ await connectSSH2Client(ssh2, cfg.cn);
25
+ }
26
+ if (before) {
27
+ await execSSH2Command(ssh2, before, (err, std) => cfg.onCmdOutput?.(before, err, std));
28
+ }
29
+ let files = await glob('**/{*.*,*}', { cwd: cfg.srcDir, nodir: true, dot: true })
30
+ .then(a => a.map(f => fixWindowsPath(f)));
31
+ if (typeof cfg.onFilter === 'function') {
32
+ files = files.filter(cfg.onFilter);
33
+ }
34
+ hasFiles = files.length > 0;
35
+ if (hasFiles) {
36
+ const zipFileDir = cfg.zipFile?.dir ?? './';
37
+ const zipFileName = cfg.zipFile?.name ?? 'release.zip';
38
+ const zipLocalPath = fixWindowsPath(path.join(zipFileDir, zipFileName));
39
+ const zipRemotePath = fixWindowsPath(path.join(cfg.destDir, zipFileName));
40
+ const zipSize = await zipFiles(cfg.srcDir, files, { dir: zipFileDir, name: zipFileName });
41
+ cfg.onZipReady?.({ files, zipLocalPath, zipRemotePath, zipSize });
42
+ await sftp.connect(cfg.cn);
43
+ await sftp.put(zipLocalPath, zipRemotePath);
44
+ if (after) {
45
+ await execSSH2Command(ssh2, after, (err, std) => cfg.onCmdOutput?.(after, err, std));
46
+ }
47
+ }
31
48
  }
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);
49
+ finally {
42
50
  await sftp.end();
43
- if (after) {
44
- await execSSH2Command(ssh2Client, after, (err, std) => cfg.onCmdOutput?.(after, err, std));
45
- }
51
+ ssh2.end();
46
52
  }
47
- ssh2Client.end();
48
- return files.length > 0;
53
+ return hasFiles;
49
54
  }
package/dist/utils.d.ts CHANGED
@@ -5,7 +5,10 @@ export declare function execSSH2Command(client: SSH2Client, cmd: string, output:
5
5
  /**
6
6
  * Zips list of files, and resolves with the size of the resulting archive (in bytes).
7
7
  */
8
- export declare function zipFiles(dir: string, files: string[], zipPath: string): Promise<number>;
8
+ export declare function zipFiles(dir: string, files: string[], zip: {
9
+ dir: string;
10
+ name: string;
11
+ }): Promise<number>;
9
12
  /**
10
13
  * Fixes Windows paths, by replacing every '\' with '/'
11
14
  */
package/dist/utils.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createZipArchive, ZipEntry } from 'node:zlib';
2
- import { readFile } from 'node:fs/promises';
2
+ import { readFile, mkdir, access } from 'node:fs/promises';
3
3
  import { createWriteStream } from 'node:fs';
4
4
  import { pipeline } from 'node:stream/promises';
5
5
  import path from 'node:path';
@@ -17,13 +17,19 @@ export function execSSH2Command(client, cmd, output) {
17
17
  return reject(err);
18
18
  }
19
19
  stream.once('close', (code) => {
20
- if (code) {
21
- reject(new Error(`Command "${cmd}" exited with code ${code}`));
22
- }
23
- else {
24
- resolve();
20
+ switch (code) {
21
+ case 0:
22
+ resolve();
23
+ break;
24
+ case null:
25
+ case undefined:
26
+ reject(new Error(`Command "${cmd}" terminated abnormally`));
27
+ break;
28
+ default:
29
+ reject(new Error(`Command "${cmd}" exited with code ${code}`));
25
30
  }
26
31
  });
32
+ stream.once('error', (err) => reject(err));
27
33
  stream.on('data', (chunk) => output(undefined, chunk));
28
34
  stream.stderr.on('data', (chunk) => output(chunk, undefined));
29
35
  });
@@ -32,14 +38,15 @@ export function execSSH2Command(client, cmd, output) {
32
38
  /**
33
39
  * Zips list of files, and resolves with the size of the resulting archive (in bytes).
34
40
  */
35
- export async function zipFiles(dir, files, zipPath) {
41
+ export async function zipFiles(dir, files, zip) {
36
42
  const entries = [];
37
43
  for (const f of files) {
38
44
  const p = path.join(dir, f);
39
45
  entries.push(await ZipEntry.create(f, await readFile(p)));
40
46
  }
41
47
  const archive = createZipArchive(entries);
42
- const ws = createWriteStream(zipPath);
48
+ await access(zip.dir).catch(() => mkdir(zip.dir, { recursive: true }));
49
+ const ws = createWriteStream(path.join(zip.dir, zip.name));
43
50
  await pipeline(archive, ws);
44
51
  return ws.bytesWritten;
45
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sftp-zip-deploy",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Zip and deploy folder over SFTP",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",