Skip to content

Commit

Permalink
remove extra files on server
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyucode committed Oct 3, 2022
1 parent dac3063 commit a3c1024
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
id_rsa
id
node_modules/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ this is a github action script for upload files to server via SFTP protocol.
dryRun: true # Optional. Default to true.
exclude: 'node_modules/,**/*.spec.ts' # Optional. exclude patterns (glob), use ',' to split, Default to ''.
forceUpload: false # Optional, Force uploading all files, Default to false(upload only newer files).
removeExtraFilesOnServer: false # Optional, Remove extra files on server. Default to false.
```

## Example usage
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ inputs:
forceUpload:
required: false
default: false
removeExtraFilesOnServer:
required: false
default: false
runs:
using: 'node16'
main: 'dist/index.js'
21 changes: 17 additions & 4 deletions lib/deployer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { lstat, opendir } = require('fs/promises');
const { access, lstat, opendir } = require('fs/promises');
const Client = require('ssh2-sftp-client');
const path = require('path');
const M = require('minimatch');
Expand All @@ -17,7 +17,7 @@ class Deployer {
removeRedundant: false,
...options
};
if(this.options.dryRun) console.warn('dryRun option is enabled! no files will be changed.');
if (this.options.dryRun) console.warn('dryRun option is enabled! no files will be changed.');
this.sftp = new Client();
this.promises = [];
}
Expand All @@ -36,7 +36,7 @@ class Deployer {
if (remoteStats === '-' || remoteStats === 'l') {
if (this.options.forceUpload) {
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''}removing directory: ${remotePath} on remote.`);
if (!this.options.dryRun) await this.sftp.rmdir(remoteFile, true);
if (!this.options.dryRun) await this.sftp.rmdir(remotePath, true);
} else {
throw new Error(`remote has same name file as the directory: ${path.basename(remotePath)}`);
}
Expand All @@ -48,6 +48,20 @@ class Deployer {
if (localStats.isDirectory()) {
console.log(`checking dir: ${localPath}`);
const dir = await opendir(localPath);
if (this.options.removeExtraFilesOnServer) {
const filesOnServer = await this.sftp.list(remotePath);
for (const file of filesOnServer) {
const localFile = path.join(localPath, file.name);
try {
await access(localFile);
} catch {
const isDirectory = file.type === 'd';
console.log(`${this.options.dryRun ? 'Dry-run: ' : ''}removing extra ${isDirectory ? 'folder' : 'file'} '${file.name}' on remote.`);
const remoteFile = path.posix.join(remotePath, file.name);
if (!this.options.dryRun) isDirectory ? await this.sftp.rmdir(remoteFile, true) : await this.sftp.delete(remoteFile);
}
}
}
for await (const dirent of dir) {
const localFile = path.join(localPath, dirent.name);
const remoteFile = path.posix.join(remotePath, dirent.name);
Expand All @@ -72,7 +86,6 @@ class Deployer {
} else {
await this.uploadFile(localFile, remoteFile);
}

}
} else {
const remoteFile = path.posix.join(remotePath, path.basename(localPath));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sftp-upload-action",
"version": "1.4.5",
"version": "1.4.6",
"description": "sftp upload using sftp-sync-deploy",
"private": true,
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion sftp.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const config = {
const options = {
dryRun: JSON.parse(core.getInput('dryRun')), // Enable dry-run mode. Default to true
exclude: core.getInput('exclude').split(','), // exclude patterns (glob)
forceUpload: JSON.parse(core.getInput('forceUpload')) // Force uploading all files, Default to false(upload only newer files).
forceUpload: JSON.parse(core.getInput('forceUpload')), // Force uploading all files, Default to false(upload only newer files).
removeExtraFilesOnServer: JSON.parse(core.getInput('dryRun')) // Remove extra files on server, default to false.
};

new Deployer(config, options)
Expand Down
9 changes: 6 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ const fs = require('fs');

new Deployer({
host: '192.168.0.99',
privateKey: fs.readFileSync('./id_rsa'),
privateKey: fs.readFileSync('./id'),
localDir: './test',
remoteDir: '/root/test/'
}, { exclude: ['node_modules'], dryRun: false })
.sync();
}, {
exclude: ['node_modules'],
dryRun: false,
removeExtraFilesOnServer: true,
}).sync();

0 comments on commit a3c1024

Please sign in to comment.