Skip to content

Commit 3dd1d43

Browse files
author
Daniele Sabre
committed
Added support for new env DSDEPLOY_FTP_PREDEPLOY
1 parent 3759d13 commit 3dd1d43

File tree

5 files changed

+54
-404
lines changed

5 files changed

+54
-404
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ typings/
7171
# dotenv environment variables file
7272
.env
7373
.env.test
74+
.env.local
7475

7576
# parcel-bundler cache (https://parceljs.org/)
7677
.cache
@@ -102,3 +103,5 @@ dist
102103

103104
# TernJS port file
104105
.tern-port
106+
107+
.idea

README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,32 @@ Build and deploy a nodejs application (such as React, Angular, Vue ecc...).
1313

1414
Create a **.env.local** file in your project directory with following variables:
1515

16-
| Environment variable | Type | Description |
17-
| :-------------------------------- | :--------- | :--------------------------------------------------------------------------------------------------- |
18-
| `DSDEPLOY_FTP_USER` | `string` | **Required**. FTP username |
19-
| `DSDEPLOY_FTP_HOST` | `string` | **Required**. FTP host |
20-
| `DSDEPLOY_FTP_PASSWORD` | `string` | FTP password (prompted if none given) |
21-
| `DSDEPLOY_FTP_PORT` | `integer` | FTP port (default 21) |
22-
| `DSDEPLOY_FTP_REMOTE_DIR` | `string` | Remote directory name where file will be placed, will use the project directory name if not provided |
23-
| `DSDEPLOY_FTP_LOCAL_DIR` | `string` | Local directory to upload, will look for the dist or build directory if not provided |
24-
| `DSDEPLOY_FTP_DELETE_REMOTE` | `integer` | Set to `0` or `1` to delete remote files before upload (default `0`) |
25-
| `DSDEPLOY_FTP_FORCE_PASSIVE_MODE` | `integer` | Set to `0` or `1` to use passive mode (default `1`) |
26-
| `DSDEPLOY_FTP_USE_SFTP` | `integer` | Set to `0` or `1` to use SFTP (default `0`) |
16+
| Environment variable | Type | Description |
17+
| :-------------------------------- | :--------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
18+
| `DSDEPLOY_FTP_USER` | `string` | **Required**. FTP username |
19+
| `DSDEPLOY_FTP_HOST` | `string` | **Required**. FTP host |
20+
| `DSDEPLOY_FTP_PASSWORD` | `string` | FTP password (prompted if none given) |
21+
| `DSDEPLOY_FTP_PORT` | `integer` | FTP port (default 21) |
22+
| `DSDEPLOY_FTP_REMOTE_DIR` | `string` | Remote directory name where file will be placed, will use the project directory name if not provided |
23+
| `DSDEPLOY_FTP_LOCAL_DIR` | `string` | Local directory to upload, will look for the dist or build directory if not provided |
24+
| `DSDEPLOY_FTP_DELETE_REMOTE` | `integer` | Set to `0` or `1` to delete remote files before upload (default `0`) |
25+
| `DSDEPLOY_FTP_FORCE_PASSIVE_MODE` | `integer` | Set to `0` or `1` to use passive mode (default `1`) |
26+
| `DSDEPLOY_FTP_USE_SFTP` | `integer` | Set to `0` or `1` to use SFTP (default `0`) |
27+
| `DSDEPLOY_FTP_PREDEPLOY` | `string` | Execute commands before deploy, you can define multiple commands to execute by separating them with a semicolon. If you don't want to execute any commands, set the variable to `none` (default: `npm run build`). |
28+
29+
### Example of a .env.local with default/example values
30+
```dotenv
31+
DSDEPLOY_FTP_USER=yourftpusername
32+
DSDEPLOY_FTP_HOST=host.example.xyz
33+
DSDEPLOY_FTP_PASSWORD=yourpassword
34+
DSDEPLOY_FTP_PORT=21
35+
DSDEPLOY_FTP_REMOTE_DIR=remote-directory
36+
DSDEPLOY_FTP_LOCAL_DIR=dist
37+
DSDEPLOY_FTP_DELETE_REMOTE=0
38+
DSDEPLOY_FTP_FORCE_PASSIVE_MODE=1
39+
DSDEPLOY_FTP_USE_SFTP=0
40+
DSDEPLOY_FTP_PREDEPLOY="npm run build"
41+
```
2742

2843

2944
## Usage

bin/deploy-ftp.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,24 @@ if (!process.env.DSDEPLOY_FTP_USER || !process.env.DSDEPLOY_FTP_HOST) {
2222
process.exit(1);
2323
}
2424

25-
// build project
26-
console.log(chalk.cyan('\nBuilding project...'));
27-
execSync('npm run build');
25+
// execute pre-deploy commands
26+
if ((process.env.DSDEPLOY_FTP_PREDEPLOY + '').trim() !== 'none') {
27+
console.log(chalk.bgCyan(chalk.white('\nPre-deploy commands...')));
28+
29+
const preDeployCommands = (process.env.DSDEPLOY_FTP_PREDEPLOY || 'npm run build').split(';').map(c => c.trim());
30+
for (let i = 0; i < preDeployCommands.length; i++) {
31+
console.log(chalk.cyan(`\nExecute command: ${preDeployCommands[i]}`));
32+
try {
33+
console.log(execSync(preDeployCommands[i]).toString().trim());
34+
} catch (err) {
35+
console.log(chalk.red(err.toString()));
36+
process.exit(1);
37+
}
38+
}
39+
}
40+
else{
41+
console.log(chalk.cyan('\nNo pre-deploy commands found'));
42+
}
2843

2944
// get local dir to upload if not passed from env variables
3045
let localDir = process.env.DSDEPLOY_FTP_LOCAL_DIR || null;
@@ -59,7 +74,7 @@ const config = {
5974
host: process.env.DSDEPLOY_FTP_HOST,
6075
port: process.env.DSDEPLOY_FTP_PORT || 21,
6176
localRoot: localDir,
62-
remoteRoot: '/' + (process.env.DSDEPLOY_FTP_REMOTE_DIR || cwd.split('/').reverse()[0]).trim().trimChar('/') + '/',
77+
remoteRoot: '/' + ((process.env.DSDEPLOY_FTP_REMOTE_DIR || cwd.split('/').reverse()[0])).trim().trimChar('/') + '/',
6378
include: ["*", "**/*"], // this would upload everything except dot files
6479
deleteRemote: parseInt(process.env.DSDEPLOY_FTP_DELETE_REMOTE) === 1, // delete ALL existing files at destination before uploading, if true
6580
forcePasv: typeof process.env.DSDEPLOY_FTP_FORCE_PASSIVE_MODE !== 'undefined' ? parseInt(process.env.DSDEPLOY_FTP_FORCE_PASSIVE_MODE) === 1 : true, // Passive mode is forced (EPSV command is not sent)

0 commit comments

Comments
 (0)