From e9329e95f8be3b493e670ef30fee72c11208f59a Mon Sep 17 00:00:00 2001 From: Bart Karalus Date: Tue, 6 Feb 2024 16:25:19 +0100 Subject: [PATCH] feat: option to load some private ssh details from a .env file --- README.md | 11 +++++++++++ lib/deployer.js | 32 ++++++++++++++++++++++++++++++-- package.json | 1 + 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 84725ff..9bb9f99 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ deploy: port: [port] # Default is 22 delete: [true|false] # Default is true progress: [true|false] # Default is true + dotenv: [true|false] # Default is false args: rsh: key: @@ -45,6 +46,16 @@ deploy: - **ignore_errors**: Ignore errors - **create_before_update**: First create non-existing files, then update existing files +# Env based options + +When `deploy.dotenv` config option is set to TRUE, some options can be ommitted and they will be loaded from following ENV variables instead: +- `HEXO_RSYNC_HOST` -> maps to `deploy.host` +- `HEXO_RSYNC_PORT` -> maps to `deploy.port` +- `HEXO_RSYNC_USER` -> maps to `deploy.user` +- `HEXO_RSYNC_ROOT` -> maps to `deploy.root` + +This can be useful in a situation when you do not want to store your SSH details in the repository, but load them from your deployment environment. + ## License MIT diff --git a/lib/deployer.js b/lib/deployer.js index 37ece38..c7be0da 100644 --- a/lib/deployer.js +++ b/lib/deployer.js @@ -3,9 +3,10 @@ const color = require('picocolors'); const { spawn } = require('hexo-util'); const pathFn = require('path'); +require('dotenv/config'); module.exports = function(args) { - if (!args.host || !args.user || !args.root) { + if (!args.dotenv && (!args.host || !args.user || !args.root)) { let help = ''; help += 'You should configure deployment settings in _config.yml first!\n\n'; @@ -18,6 +19,7 @@ module.exports = function(args) { help += ' port: [port] # Default is 22\n'; help += ' delete: [true|false] # Default is true\n'; help += ' progress: [true|false] # Default is true\n'; + help += ' dotenv: [true|false] # Default is false\n'; help += ' args: \n'; help += ' rsh: \n'; help += ' key: \n'; @@ -35,6 +37,32 @@ module.exports = function(args) { if (!Object.prototype.hasOwnProperty.call(args, 'progress')) args.progress = true; if (!Object.prototype.hasOwnProperty.call(args, 'ignore_errors')) args.ignore_errors = false; if (!Object.prototype.hasOwnProperty.call(args, 'create_before_update')) args.create_before_update = false; + if (!Object.prototype.hasOwnProperty.call(args, 'dotenv')) args.dotenv = true; + + if (args.dotenv === true) { + const { + HEXO_RSYNC_HOST, + HEXO_RSYNC_PORT, + HEXO_RSYNC_USER, + HEXO_RSYNC_ROOT, + } = process.env; + + if (HEXO_RSYNC_HOST) { + args.host = HEXO_RSYNC_HOST; + } + + if (HEXO_RSYNC_PORT) { + args.port = HEXO_RSYNC_PORT; + } + + if (HEXO_RSYNC_USER) { + args.user = HEXO_RSYNC_USER; + } + + if (HEXO_RSYNC_ROOT) { + args.root = HEXO_RSYNC_ROOT; + } + } const params = [ '-az', @@ -74,4 +102,4 @@ module.exports = function(args) { }); } return spawn('rsync', params, {verbose: true}); -}; +}; \ No newline at end of file diff --git a/package.json b/package.json index 8707c0a..9df30f4 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "mocha": "^10.2.0" }, "dependencies": { + "dotenv": "^16.4.1", "hexo-util": "^3.0.1", "picocolors": "^1.0.0" },