Move config loading to a new file

This is so that we can extend it further (by loading from more
locations, etc.)
This commit is contained in:
Hippo 2020-04-24 21:20:41 +05:30
parent e1ec23fc3b
commit e41358118a
3 changed files with 39 additions and 21 deletions

18
config.js Normal file
View file

@ -0,0 +1,18 @@
// load configuraton from .env file (if exists)
require('dotenv').config()
let config = {
'webdav:server_url': process.env.WEBDAV_SERVER_URL,
'webdav:username': process.env.WEBDAV_USERNAME,
'webdav:password': process.env.WEBDAV_PASSWORD,
'webdav:path_prefix': process.env.WEBDAV_PATH_PREFIX,
'webdav:uploaded_path_prefix': process.env.WEBDAV_UPlOADED_PATH_PREFIX,
'webdav:use_digest': process.env.WEBDAV_USE_DIGEST,
'ghost:url': process.env.GHOST_URL,
'ghost:version': process.env.GHOST_VERSION,
'ghost:admin_key': process.env.GHOST_ADMIN_KEY,
'scissor:path': process.env.SEPARATOR_IMAGE,
}
module.exports = config

View file

@ -9,12 +9,14 @@ const { markdown } = require('markdown')
const GhostAdminAPI = require('@tryghost/admin-api') const GhostAdminAPI = require('@tryghost/admin-api')
const Rembrandt = require('rembrandt') const Rembrandt = require('rembrandt')
const config = require('./config')
const MEDIUM_IMG_CDN = 'https://miro.medium.com/fit/c/' const MEDIUM_IMG_CDN = 'https://miro.medium.com/fit/c/'
const ghostAdmin = new GhostAdminAPI({ const ghostAdmin = new GhostAdminAPI({
url: process.env.GHOST_URL, url: config['ghost:url'],
version: process.env.GHOST_VERSION, version: config['ghost:version'],
key: process.env.GHOST_ADMIN_KEY key: config['ghost:admin_key'],
}) })
/** /**
@ -111,11 +113,11 @@ const pushToGhost = async (postSlug) => {
) )
// Path where WebDAV files will be placed (eg. https://example.com:2078) // Path where WebDAV files will be placed (eg. https://example.com:2078)
const davPath = path.join(process.env.WEBDAV_PATH_PREFIX, uploadPath) const davPath = path.join(config['webdav:path_prefix'], uploadPath)
// Public path to upload those files (eg. https://media.example.com/uploads) // Public path to upload those files (eg. https://media.example.com/uploads)
// We'll do it directly since path.join mangles the protocol // We'll do it directly since path.join mangles the protocol
const uploadedPath = process.env.WEBDAV_UPLOADED_PATH_PREFIX + '/' + uploadPath const uploadedPath = config['webdav:uploaded_path_prefix'] + '/' + uploadPath
// load metadata file // load metadata file
console.debug('Loading metadata') console.debug('Loading metadata')
@ -255,7 +257,7 @@ const checkScissors = async (imagePath) => {
// Decide "separator" image // Decide "separator" image
// If set, images matching this will be ignored and replaced // If set, images matching this will be ignored and replaced
// with a horizontal-rule ("---" in markdown) instead. // with a horizontal-rule ("---" in markdown) instead.
let scissors = process.env.SEPARATOR_IMAGE let scissors = config['scissor:path']
// if scissors not set, return false // if scissors not set, return false
// (it's never a scissors since it never matches) // (it's never a scissors since it never matches)
@ -315,7 +317,7 @@ const generateUserData = async (mediumUsername, email) => {
console.log("Uploading to server") console.log("Uploading to server")
await uploadDav(path.join(process.env.WEBDAV_PATH_PREFIX,'avatars'), await uploadDav(path.join(config['webdav:path_prefix'],'avatars'),
filePath) filePath)
// Generate Ghost JSON // Generate Ghost JSON
@ -329,7 +331,7 @@ const generateUserData = async (mediumUsername, email) => {
bio: json.payload.user.bio, bio: json.payload.user.bio,
email: email, email: email,
name: json.payload.user.name, name: json.payload.user.name,
profile_image: process.env.WEBDAV_UPLOADED_PATH_PREFIX + '/avatars/' + fileName profile_image: config['webdav:uploaded_path_prefix'] + '/avatars/' + fileName
} }
] ]
}, },
@ -346,8 +348,8 @@ const createDirIfNotExist = async (client, folder) => {
// recursively create subfolders if they don't exist. // recursively create subfolders if they don't exist.
//safety: don't touch directories outside WEBDAV_PATH_PREFIX //safety: don't touch directories outside WEBDAV_PATH_PREFIX
if (!folder.startsWith(process.env.WEBDAV_PATH_PREFIX)) { if (!folder.startsWith(config['webdav:path_prefix'])) {
throw new Error(`Cannot create directories outside ${process.env.WEBDAV_PATH_PREFIX}`) throw new Error(`Cannot create directories outside ${config['webdav:path_prefix']}`)
} }
// check the folder // check the folder
@ -408,11 +410,11 @@ const uploadDav = async (dirPath, filePath) => {
// connect to webdav // connect to webdav
client = createClient( client = createClient(
process.env.WEBDAV_SERVER_URL, config['webdav:server_url'],
{ {
username: process.env.WEBDAV_USERNAME, username: config['webdav:username'],
password: process.env.WEBDAV_PASSWORD, password: config['webdav:password'],
digest: process.env.WEBDAV_USE_DIGEST == 'true' digest: config['webdav:use_digest'] == 'true'
}) })
// create directory if not exists // create directory if not exists

View file

@ -1,12 +1,10 @@
#! /usr/bin/env node #! /usr/bin/env node
// load configuraton from .env file (if exists) const program = require('commander')
const dotenv = require('dotenv')
dotenv.config()
const program = require('commander');
const path = require('path') const path = require('path')
const config = require('./config')
const { const {
fetchFromMedium, fetchFromMedium,
pushToGhost, pushToGhost,
@ -14,7 +12,7 @@ const {
generateUserData, generateUserData,
checkScissors, checkScissors,
uploadDav, uploadDav,
} = require ('./functions'); } = require ('./functions')
program program
.version('1.0.0-dev') .version('1.0.0-dev')
@ -61,7 +59,7 @@ program.command('webdav-test <file>')
// decide path // decide path
current_date = new Date(); current_date = new Date();
var dir_path = path.join( var dir_path = path.join(
process.env.WEBDAV_PATH_PREFIX, config['webdav:path_prefix'],
current_date.getUTCFullYear().toString(), current_date.getUTCFullYear().toString(),
current_date.getUTCMonth().toString(), current_date.getUTCMonth().toString(),
'test' // TODO: replace with article slug 'test' // TODO: replace with article slug