Allow YAML config load, and escape route if it doesn't exist

The "escape route" is the setup command (yet to be implemented)
This commit is contained in:
Hippo 2020-04-26 18:23:52 +05:30
parent 53b0832a8c
commit a57add4809
4 changed files with 74 additions and 6 deletions

View file

@ -1,6 +1,12 @@
// load configuraton from .env file (if exists) // load configuraton from .env file (if exists)
require('dotenv').config() require('dotenv').config()
const convict = require('convict') 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({ let config = convict({
webdav: { webdav: {
@ -70,7 +76,30 @@ let config = convict({
} }
}) })
// 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'
)
}
var validated
// Validate config
try {
config.validate() config.validate()
validated = true
} catch(e) {
console.error(e)
validated = false
}
module.exports = config.getProperties() allConf = config.getProperties()
allConf.validated = validated
module.exports = allConf

View file

@ -5,6 +5,23 @@ const path = require('path')
const config = require('./config') 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 { const {
fetchFromMedium, fetchFromMedium,
pushToGhost, pushToGhost,
@ -14,10 +31,6 @@ const {
uploadDav, uploadDav,
} = require ('./functions') } = require ('./functions')
program
.version('1.0.0-dev')
.description('pull posts from Medium and add them to a Ghost blog');
program.command('fetch-medium <post_url>') program.command('fetch-medium <post_url>')
.alias('fetch') .alias('fetch')
.description('fetch a Medium post') .description('fetch a Medium post')

View file

@ -16,6 +16,7 @@
"commander": "^3.0.2", "commander": "^3.0.2",
"convict": "^5.2.0", "convict": "^5.2.0",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"js-yaml": "^3.13.1",
"markdown": "^0.5.0", "markdown": "^0.5.0",
"mediumexporter": "github:badrihippo/mediumexporter#seance-latest", "mediumexporter": "github:badrihippo/mediumexporter#seance-latest",
"r2": "^2.0.1", "r2": "^2.0.1",

View file

@ -39,6 +39,13 @@ are-we-there-yet@~1.1.2:
delegates "^1.0.0" delegates "^1.0.0"
readable-stream "^2.0.6" 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: asynckit@^0.4.0:
version "0.4.0" version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" 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-date-object "^1.0.1"
is-symbol "^1.0.2" 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: follow-redirects@1.5.10:
version "1.5.10" version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a" 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" resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE= 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: json5@2.1.0:
version "2.1.0" version "2.1.0"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.0.tgz#e7a0c62c48285c628d20a10b85c89bb807c32850" 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" resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.1.2.tgz#da1765262bf8c0f571749f2ad6c26300207ae673"
integrity sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug== 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: string-width@^1.0.1:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"