From 4fb9910221f903e815bd0a29f94ce220666fc4ac Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 21 Dec 2017 12:03:34 -0700 Subject: [PATCH] updates to cli - removed -c,--config flag in favor of env only or .env files - loads .env always if found - scripts re-written to call cli-serve directly --- bin/cli | 2 +- bin/cli-assets | 2 +- bin/cli-jobs | 2 +- bin/cli-migration | 2 +- bin/cli-plugins | 2 +- bin/cli-serve | 2 +- bin/cli-settings | 2 +- bin/cli-setup | 2 +- bin/cli-token | 2 +- bin/cli-users | 2 +- bin/cli-verify | 2 +- bin/commander.js | 51 ----------------------------------------------- bin/util.js | 38 ----------------------------------- config.js | 4 ++++ package.json | 4 ++-- 15 files changed, 17 insertions(+), 102 deletions(-) delete mode 100644 bin/commander.js diff --git a/bin/cli b/bin/cli index f618b24be..e01c9cd64 100755 --- a/bin/cli +++ b/bin/cli @@ -4,7 +4,7 @@ * Module dependencies. */ -const program = require('./commander'); +const program = require('commander'); const {head, map} = require('lodash'); const Matcher = require('did-you-mean'); diff --git a/bin/cli-assets b/bin/cli-assets index 4805bead0..456f16877 100755 --- a/bin/cli-assets +++ b/bin/cli-assets @@ -4,7 +4,7 @@ * Module dependencies. */ -const program = require('./commander'); +const program = require('commander'); const parseDuration = require('ms'); const Table = require('cli-table'); const AssetModel = require('../models/asset'); diff --git a/bin/cli-jobs b/bin/cli-jobs index 6048a69db..345b22b56 100755 --- a/bin/cli-jobs +++ b/bin/cli-jobs @@ -4,7 +4,7 @@ * Module dependencies. */ -const program = require('./commander'); +const program = require('commander'); const scraper = require('../services/scraper'); const mailer = require('../services/mailer'); const util = require('./util'); diff --git a/bin/cli-migration b/bin/cli-migration index 036ac8b02..f730b8fbb 100755 --- a/bin/cli-migration +++ b/bin/cli-migration @@ -4,7 +4,7 @@ * Module dependencies. */ -const program = require('./commander'); +const program = require('commander'); const util = require('./util'); const inquirer = require('inquirer'); const mongoose = require('../services/mongoose'); diff --git a/bin/cli-plugins b/bin/cli-plugins index b7e301fce..8217b023f 100755 --- a/bin/cli-plugins +++ b/bin/cli-plugins @@ -7,7 +7,7 @@ // Interface heavily inspired by the yarn package manager: // https://yarnpkg.com/ -const program = require('./commander'); +const program = require('commander'); const inquirer = require('inquirer'); // Make things colorful! diff --git a/bin/cli-serve b/bin/cli-serve index 723558651..cc00f84ed 100755 --- a/bin/cli-serve +++ b/bin/cli-serve @@ -1,6 +1,6 @@ #!/usr/bin/env node -const program = require('./commander'); +const program = require('commander'); const util = require('./util'); const serve = require('../serve'); diff --git a/bin/cli-settings b/bin/cli-settings index f8768046c..481e5b2bf 100755 --- a/bin/cli-settings +++ b/bin/cli-settings @@ -1,6 +1,6 @@ #!/usr/bin/env node -const program = require('./commander'); +const program = require('commander'); const inquirer = require('inquirer'); const mongoose = require('../services/mongoose'); const SettingsService = require('../services/settings'); diff --git a/bin/cli-setup b/bin/cli-setup index c7a44c453..35521e05a 100755 --- a/bin/cli-setup +++ b/bin/cli-setup @@ -4,7 +4,7 @@ * Module dependencies. */ -const program = require('./commander'); +const program = require('commander'); const inquirer = require('inquirer'); const mongoose = require('../services/mongoose'); const SettingModel = require('../models/setting'); diff --git a/bin/cli-token b/bin/cli-token index fa5d2e350..87d955df9 100755 --- a/bin/cli-token +++ b/bin/cli-token @@ -4,7 +4,7 @@ * Module dependencies. */ -const program = require('./commander'); +const program = require('commander'); const mongoose = require('../services/mongoose'); const TokensService = require('../services/tokens'); const util = require('./util'); diff --git a/bin/cli-users b/bin/cli-users index 4cb2ac496..c15572fe4 100755 --- a/bin/cli-users +++ b/bin/cli-users @@ -4,7 +4,7 @@ * Module dependencies. */ -const program = require('./commander'); +const program = require('commander'); const inquirer = require('inquirer'); const UsersService = require('../services/users'); const UserModel = require('../models/user'); diff --git a/bin/cli-verify b/bin/cli-verify index 6e67e0016..9a4c30ab1 100755 --- a/bin/cli-verify +++ b/bin/cli-verify @@ -4,7 +4,7 @@ * Module dependencies. */ -const program = require('./commander'); +const program = require('commander'); const mongoose = require('../services/mongoose'); const util = require('./util'); const databaseVerifications = require('./verifications/database'); diff --git a/bin/commander.js b/bin/commander.js deleted file mode 100644 index 328a25b29..000000000 --- a/bin/commander.js +++ /dev/null @@ -1,51 +0,0 @@ -const pkg = require('../package.json'); -const dotenv = require('dotenv'); -const fs = require('fs'); -const program = require('commander'); - -//============================================================================== -// Setting up the program command line arguments. -//============================================================================== - -const parseArgs = require('minimist')(process.argv.slice(2), { - alias: { - 'c': 'config' - }, - string: [ - 'config', - 'pid' - ], - default: { - 'config': null, - 'pid': null - } -}); - -/** - * If the config flag is present, then we have to load the configuration from - * the file specified. We will then load those values into the environment. - */ -if (parseArgs.config) { - let envConfig = dotenv.parse(fs.readFileSync(parseArgs.config, {encoding: 'utf8'})); - - Object.keys(envConfig).forEach((k) => { - process.env[k] = envConfig[k]; - }); -} - -/** - * If the pid flag is present, then we have to create a pid file at the location - * specified. - */ -if (parseArgs.pid) { - const util = require('./util'); - - console.log('Wrote PID'); - - util.pid(parseArgs.pid); -} - -module.exports = program - .version(pkg.version) - .option('-c, --config [path]', 'Specify the configuration file to load') - .option('--pid [path]', 'Specify a path to output the current PID to'); diff --git a/bin/util.js b/bin/util.js index ab9035fe5..572d07a2e 100644 --- a/bin/util.js +++ b/bin/util.js @@ -1,5 +1,4 @@ const debug = require('debug')('talk:util'); -const fs = require('fs'); const util = module.exports = {}; @@ -49,43 +48,6 @@ util.onshutdown = (jobs) => { util.toshutdown = util.toshutdown.concat(jobs); }; -/** - * Register a PID file to be maintained for the lifespan of the process. - * @param {String} path path to the PID file to create - */ -util.pid = (path) => { - if (!/\//.test(path)) { - if (!/\.pid/.test(path)) { - path += '.pid'; - } - path = `/tmp/${path}`; - } - - const pid = `${process.pid.toString()}\n`; - - fs.writeFile(path, pid, (err) => { - if (err) { - console.error(`Can't write PID file: ${err}`); - throw err; - } - - // Add the cleanup for the fs onto the shutdown. - util.onshutdown([ - () => new Promise((resolve, reject) => { - - // Remove the pid file. - fs.unlink(path, (err) => { - if (err) { - return reject(err); - } - - return resolve(); - }); - }) - ]); - }); -}; - // Attach to the SIGTERM + SIGINT handles to ensure a clean shutdown in the // event that we have an external event. SIGUSR2 is called when the app is asked // to be 'killed', same procedure here. diff --git a/config.js b/config.js index 5eefeba61..21f62d696 100644 --- a/config.js +++ b/config.js @@ -7,6 +7,10 @@ // entrypoint for the entire applications configuration. require('env-rewrite').rewrite(); +// Apply all the configuration provided in the .env file if it isn't already +// in the environment. +require('dotenv').config(); + const uniq = require('lodash/uniq'); const ms = require('ms'); const debug = require('debug')('talk:config'); diff --git a/package.json b/package.json index a9337b274..115d4d915 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,8 @@ "watch": "npm-run-all clean generate-introspection --parallel watch:*", "watch:client": "NODE_ENV=development webpack --progress --watch", "watch:server": "nodemon --config .nodemon.json", - "start:development": "NODE_ENV=development ./bin/cli -c .env serve -j -w", - "start": "NODE_ENV=production ./bin/cli serve -j -w", + "start:development": "NODE_ENV=development ./bin/cli-serve -j -w", + "start": "NODE_ENV=production ./bin/cli-serve -j -w", "prebuild": "npm-run-all clean generate-introspection", "build": "NODE_ENV=production webpack -p --bail", "lint:yaml": "yamllint locales/*.yml",