sftp-zip-deploy 0.1.0 → 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 +14 -7
- package/dist/config.d.ts +6 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +18 -14
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +9 -2
- package/package.json +2 -2
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
|
|
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,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
|
|
57
|
-
|
|
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
|
|
|
@@ -68,3 +67,11 @@ The main interface [ISftpZipDeployConfig] has a number of options, including eve
|
|
|
68
67
|
[onCmdOutput]:https://github.com/vitaly-t/sftp-zip-deploy/blob/main/src/config.ts#L81
|
|
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('**/{
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
await
|
|
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
|
|
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', () =>
|
|
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
|
|
47
|
+
* Fixes Windows paths, by replacing every '\' with '/'
|
|
41
48
|
*/
|
|
42
49
|
export function fixWindowsPath(path) {
|
|
43
50
|
return path.replace(/\\/g, '/');
|