From a57add480982b64429ad1f78ea14a6d6f8d7c7ea Mon Sep 17 00:00:00 2001 From: Hippo Date: Sun, 26 Apr 2020 18:23:52 +0530 Subject: [PATCH] Allow YAML config load, and escape route if it doesn't exist The "escape route" is the setup command (yet to be implemented) --- config.js | 33 +++++++++++++++++++++++++++++++-- index.js | 21 +++++++++++++++++---- package.json | 1 + yarn.lock | 25 +++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 6 deletions(-) diff --git a/config.js b/config.js index 25c7f7f..5f47dbd 100644 --- a/config.js +++ b/config.js @@ -1,6 +1,12 @@ // load configuraton from .env file (if exists) require('dotenv').config() const convict = require('convict') +const os = require('os') +const path = require('path') +const yaml = require('js-yaml') +const fs = require('fs') + +convict.addParser({ extension: ['yml', 'yaml'], parse: yaml.safeLoad }) let config = convict({ webdav: { @@ -70,7 +76,30 @@ let config = convict({ } }) -config.validate() +// Load configs from home directory, if present +userConfig = path.join(os.homedir(), '.config/seance.yaml') +if (fs.existsSync(userConfig)) { + config.loadFile(userConfig) +} else if (!runSetup) { + console.warn( + 'Warning: no config file detected! ' + + 'Please run `seance setup` to configure your system' + ) +} -module.exports = config.getProperties() +var validated + +// Validate config +try { + config.validate() + validated = true +} catch(e) { + console.error(e) + validated = false +} + +allConf = config.getProperties() +allConf.validated = validated + +module.exports = allConf diff --git a/index.js b/index.js index 99e7115..d97bbba 100755 --- a/index.js +++ b/index.js @@ -5,6 +5,23 @@ const path = require('path') const config = require('./config') +program + .version('1.0.0-dev') + .description('pull posts from Medium and add them to a Ghost blog'); + +program.command('setup') + .description('Initial setup and configuration') + .action(() => { + console.log('Bye!') + }) + +if (!config.validated && process.argv[2] == 'setup') { + // short-circuit and just run the setup command + // because nothing else will work anyway + program.parse(process.argv) + process.exit(1) +} + const { fetchFromMedium, pushToGhost, @@ -14,10 +31,6 @@ const { uploadDav, } = require ('./functions') -program - .version('1.0.0-dev') - .description('pull posts from Medium and add them to a Ghost blog'); - program.command('fetch-medium ') .alias('fetch') .description('fetch a Medium post') diff --git a/package.json b/package.json index 57117f3..0a31b9e 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "commander": "^3.0.2", "convict": "^5.2.0", "dotenv": "^8.2.0", + "js-yaml": "^3.13.1", "markdown": "^0.5.0", "mediumexporter": "github:badrihippo/mediumexporter#seance-latest", "r2": "^2.0.1", diff --git a/yarn.lock b/yarn.lock index 19c500e..c0e1a2d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -39,6 +39,13 @@ are-we-there-yet@~1.1.2: delegates "^1.0.0" readable-stream "^2.0.6" +argparse@^1.0.7: + version "1.0.10" + resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" + integrity sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg== + dependencies: + sprintf-js "~1.0.2" + asynckit@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" @@ -242,6 +249,11 @@ es-to-primitive@^1.2.0: is-date-object "^1.0.1" is-symbol "^1.0.2" +esprima@^4.0.0: + version "4.0.1" + resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71" + integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A== + follow-redirects@1.5.10: version "1.5.10" resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a" @@ -406,6 +418,14 @@ isarray@~1.0.0: resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE= +js-yaml@^3.13.1: + version "3.13.1" + resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.13.1.tgz#aff151b30bfdfa8e49e05da22e7415e9dfa37847" + integrity sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw== + dependencies: + argparse "^1.0.7" + esprima "^4.0.0" + json5@2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.0.tgz#e7a0c62c48285c628d20a10b85c89bb807c32850" @@ -852,6 +872,11 @@ sprintf-js@^1.0.3: resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.1.2.tgz#da1765262bf8c0f571749f2ad6c26300207ae673" integrity sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug== +sprintf-js@~1.0.2: + version "1.0.3" + resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" + integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw= + string-width@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"