@arc-fusion/cli
Version:
CLI for running Arc Fusion on your local machine
105 lines (92 loc) • 2.91 kB
JavaScript
const semver = require('semver')
const fs = require('fs')
const path = require('path')
const ADMIN_RELEASE = require('./admin.json').version
function fileExists (filePath) {
try {
fs.accessSync(filePath)
return true
} catch (e) {
return false
}
}
const COMMAND_ROOT = path.resolve('.')
const FUSION_ROOT = path.dirname(__dirname)
const BOOTSTRAP_ROOT = path.join(FUSION_ROOT, 'bootstrap')
let LOCAL_SCRIPT_PATH
try {
// don't assume the command is being called from the project root
// we should support commands being run from subdirectories
// require.resolve automatically resolves symlinks, so isn't useful with `npm link @arc-fusion/cli`
// (which is really helpful in testing)
let possibleProjectRoot = COMMAND_ROOT
while (possibleProjectRoot.length) {
const localScriptPath = path.join(
possibleProjectRoot,
'node_modules',
'@arc-fusion',
'cli',
'bin',
'fusion.js'
)
if (fileExists(localScriptPath)) {
LOCAL_SCRIPT_PATH = localScriptPath
break
}
possibleProjectRoot = possibleProjectRoot.substr(
0,
possibleProjectRoot.lastIndexOf(path.sep)
)
}
} catch (e) {}
const IS_GLOBAL = !LOCAL_SCRIPT_PATH
const PROJECT_ROOT = IS_GLOBAL
? COMMAND_ROOT
// remove node_modules/@arc-fusion/cli/bin/fusion.js from the local script path
: LOCAL_SCRIPT_PATH.split(path.sep).slice(0, -5).join(path.sep)
/**
* sanitizeDBName takes an input string and returns a valid MongoDB database name
* @param {String} name
* @returns {String} a valid DB name for use with MongoDB
*/
const sanitizeDBName = (name) => {
/**
* Restrictions on Database Name
* - Cannot be empty or contain a null character (\0).
* - Cannot contain special characters in Windows, including /, \, ., ", $, *, <, >, |, :, or ?.
* - Cannot contain special characters in Unix/Linux systems, including /, \, ., or (space).
*
* Reserved names:
* - admin: used for administrative purposes.
* - local: used for storing data specific to a single server instance.
* - config: used for sharded cluster metadata.
*/
if (['admin', 'local', 'config'].includes(name)) {
return 'localhost'
}
return name.replace(/[\s.,"$*<>|?:]/g, '')
}
const REPO_NAME = sanitizeDBName(path.basename(PROJECT_ROOT))
const CACHE_VOLUME_NAME = `fusion_cache_${REPO_NAME}`
require('dotenv').config({ path: path.join(PROJECT_ROOT, '.env') })
const MONGO_VERSION =
process.env.FUSION_RELEASE &&
(process.env.FUSION_RELEASE === 'latest' ||
semver.gte(process.env.FUSION_RELEASE.replace(/-.*$/, ''), '7.0.0'))
? '6'
: '3.6'
module.exports = {
ADMIN_RELEASE,
BOOTSTRAP_ROOT,
COMMAND_ROOT,
FUSION_RELEASE: process.env.FUSION_RELEASE || 'latest',
FUSION_ROOT,
IS_GLOBAL,
LOCAL_SCRIPT_PATH,
MONGO_VERSION,
PROJECT_ROOT,
REPO_NAME,
CACHE_VOLUME_NAME,
sanitizeDBName
}