cli-engine-config
Advanced tools
Comparing version 3.4.0 to 4.0.0-ts.17
@@ -1,283 +0,266 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.defaultConfig = undefined; | ||
exports.buildConfig = buildConfig; | ||
var _path = require('path'); | ||
var _path2 = _interopRequireDefault(_path); | ||
var _os = require('os'); | ||
var _os2 = _interopRequireDefault(_os); | ||
var _fsExtra = require('fs-extra'); | ||
var _fsExtra2 = _interopRequireDefault(_fsExtra); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const path = require("path"); | ||
const os = require("os"); | ||
const fs = require("fs-extra"); | ||
function dir(config, category, d) { | ||
let cacheKey = `dir:${category}`; | ||
let cache = config.__cache[cacheKey]; | ||
if (cache) return cache; | ||
d = d || _path2.default.join(config.home, category === 'data' ? '.local/share' : '.' + category); | ||
if (config.windows) d = process.env.LOCALAPPDATA || d; | ||
d = process.env.XDG_DATA_HOME || d; | ||
d = _path2.default.join(d, config.dirname); | ||
_fsExtra2.default.mkdirpSync(d); | ||
config.__cache[cacheKey] = d; | ||
return d; | ||
let cacheKey = `dir:${category}`; | ||
let cache = config.__cache[cacheKey]; | ||
if (cache) | ||
return cache; | ||
d = d || path.join(config.home, category === 'data' ? '.local/share' : '.' + category); | ||
if (config.windows) | ||
d = process.env.LOCALAPPDATA || d; | ||
d = process.env.XDG_DATA_HOME || d; | ||
d = path.join(d, config.dirname); | ||
fs.mkdirpSync(d); | ||
config.__cache[cacheKey] = d; | ||
return d; | ||
} | ||
function debug(bin) { | ||
const debug = require('debug')(bin).enabled || envVarTrue(envVarKey(bin, 'DEBUG')); | ||
return debug ? 1 : 0; | ||
const debug = require('debug')(bin).enabled || envVarTrue(envVarKey(bin, 'DEBUG')); | ||
return debug ? 1 : 0; | ||
} | ||
function envVarKey(...parts) { | ||
return parts.map(p => p.replace(/-/g, '_')).join('_').toUpperCase(); | ||
return parts | ||
.map(p => p.replace(/-/g, '_')) | ||
.join('_') | ||
.toUpperCase(); | ||
} | ||
function envVarTrue(k) { | ||
let v = process.env[k]; | ||
return v === '1' || v === 'true'; | ||
let v = process.env[k]; | ||
return v === '1' || v === 'true'; | ||
} | ||
function loadUserConfig(config) { | ||
const cache = config.__cache['userConfig']; | ||
if (cache) return cache; | ||
const configPath = _path2.default.join(config.configDir, 'config.json'); | ||
let userConfig; | ||
try { | ||
userConfig = _fsExtra2.default.readJSONSync(configPath); | ||
} catch (e) { | ||
if (e.code === 'ENOENT') { | ||
userConfig = { | ||
skipAnalytics: false, | ||
install: null | ||
}; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
config.__cache['userConfig'] = userConfig; | ||
if (config.skipAnalytics) userConfig.install = null;else if (!userConfig.install) { | ||
const uuid = require('uuid/v4'); | ||
userConfig.install = uuid(); | ||
const cache = config.__cache['userConfig']; | ||
if (cache) | ||
return cache; | ||
const configPath = path.join(config.configDir, 'config.json'); | ||
let userConfig; | ||
try { | ||
_fsExtra2.default.writeJSONSync(configPath, userConfig); | ||
} catch (e) { | ||
userConfig.install = null; | ||
userConfig = fs.readJSONSync(configPath); | ||
} | ||
} | ||
return userConfig; | ||
catch (e) { | ||
if (e.code === 'ENOENT') { | ||
userConfig = { | ||
skipAnalytics: false, | ||
install: null, | ||
}; | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
config.__cache['userConfig'] = userConfig; | ||
if (config.skipAnalytics) | ||
userConfig.install = null; | ||
else if (!userConfig.install) { | ||
const uuid = require('uuid/v4'); | ||
userConfig.install = uuid(); | ||
try { | ||
fs.writeJSONSync(configPath, userConfig); | ||
} | ||
catch (e) { | ||
userConfig.install = null; | ||
} | ||
} | ||
return userConfig; | ||
} | ||
function shell(onWindows = false) { | ||
let shellPath; | ||
if (process.env['SHELL']) { | ||
shellPath = process.env['SHELL'].split(`/`); | ||
} else if (onWindows && process.env['COMSPEC']) { | ||
shellPath = process.env['COMSPEC'].split(/\\|\//); | ||
} else { | ||
shellPath = ['unknown']; | ||
} | ||
return shellPath[shellPath.length - 1]; | ||
let shellPath; | ||
const { SHELL, COMSPEC } = process.env; | ||
if (SHELL) { | ||
shellPath = SHELL.split(`/`); | ||
} | ||
else if (onWindows && COMSPEC) { | ||
shellPath = COMSPEC.split(/\\|\//); | ||
} | ||
else { | ||
shellPath = ['unknown']; | ||
} | ||
return shellPath[shellPath.length - 1]; | ||
} | ||
function userAgent(config) { | ||
const channel = config.channel === 'stable' ? '' : ` ${config.channel}`; | ||
return `${config.name}/${config.version}${channel} (${config.platform}-${config.arch}) node-${process.version}`; | ||
const channel = config.channel === 'stable' ? '' : ` ${config.channel}`; | ||
return `${config.name}/${config.version}${channel} (${config.platform}-${config.arch}) node-${process.version}`; | ||
} | ||
function registry(config) { | ||
const env = process.env[envVarKey(config.bin, 'NPM_REGISTRY')]; | ||
return env || config.pjson['cli-engine'].npmRegistry || 'https://registry.yarnpkg.com'; | ||
const env = process.env[envVarKey(config.bin, 'NPM_REGISTRY')]; | ||
return env || config.pjson['cli-engine'].npmRegistry || 'https://registry.yarnpkg.com'; | ||
} | ||
function commandsDir(config) { | ||
let commandsDir = config.pjson['cli-engine'].commands; | ||
if (!commandsDir) return; | ||
return _path2.default.join(config.root, commandsDir); | ||
let commandsDir = config.pjson['cli-engine'].commands; | ||
if (!commandsDir) | ||
return; | ||
return path.join(config.root, commandsDir); | ||
} | ||
function hooks(config) { | ||
let hooks = {}; | ||
for (let [k, v] of Object.entries(config.pjson['cli-engine'].hooks || {})) { | ||
hooks[k] = Array.isArray(v) ? v : [v]; | ||
} | ||
return hooks; | ||
let hooks = {}; | ||
for (let [k, v] of Object.entries(config.pjson['cli-engine'].hooks || {})) { | ||
hooks[k] = Array.isArray(v) ? v : [v]; | ||
} | ||
return hooks; | ||
} | ||
function envSkipAnalytics(config) { | ||
if (config.userConfig.skipAnalytics) { | ||
return true; | ||
} else if (envVarTrue('TESTING') || envVarTrue(envVarKey(config.bin, 'SKIP_ANALYTICS'))) { | ||
return true; | ||
} | ||
return false; | ||
if (config.userConfig.skipAnalytics) { | ||
return true; | ||
} | ||
else if (envVarTrue('TESTING') || envVarTrue(envVarKey(config.bin, 'SKIP_ANALYTICS'))) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
function topics(config) { | ||
if (!config.__cache['topics']) { | ||
config.__cache['topics'] = config.pjson['cli-engine'].topics || {}; | ||
for (let [k, v] of Object.entries(config.__cache['topics'])) { | ||
if (!v.name) v.name = k; | ||
if (!config.__cache['topics']) { | ||
config.__cache['topics'] = config.pjson['cli-engine'].topics || {}; | ||
for (let [k, v] of Object.entries(config.__cache['topics'])) { | ||
if (!v.name) | ||
v.name = k; | ||
} | ||
} | ||
} | ||
return config.__cache['topics']; | ||
return config.__cache['topics']; | ||
} | ||
function validatePJSON(pjson) { | ||
// const exampleCLI = { | ||
// bin: 'heroku', | ||
// dirname: 'heroku', | ||
// node: '8.0.0', | ||
// defaultCommand: 'dashboard', | ||
// commands: './lib/commands', | ||
// hooks: { | ||
// init: './lib/hooks/init.js', | ||
// update: './lib/hooks/update.js', | ||
// prerun: './lib/hooks/prerun.js', | ||
// 'plugins:preinstall': './lib/hooks/plugins/preinstall.js' | ||
// }, | ||
// s3: {host: 'host'}, | ||
// plugins: ['heroku-pg', 'heroku-redis'] | ||
// } | ||
// TODO: validate | ||
// const cli = pjson['cli-engine'] || {} | ||
// const comment = 'cli-engine-config' | ||
// const title = { | ||
// warning: 'invalid CLI package.json', | ||
// error: 'invalid CLI package.json' } | ||
// validate(cli, {comment, title, exampleConfig: exampleCLI}) | ||
// validate(cli.hooks, { | ||
// comment, | ||
// condition: (option, validOption) => { | ||
// console.dir({option, validOption}) | ||
// }, | ||
// title, | ||
// exampleConfig: exampleCLI.hooks | ||
// }) | ||
// const exampleCLI = { | ||
// bin: 'heroku', | ||
// dirname: 'heroku', | ||
// node: '8.0.0', | ||
// defaultCommand: 'dashboard', | ||
// commands: './lib/commands', | ||
// hooks: { | ||
// init: './lib/hooks/init.js', | ||
// update: './lib/hooks/update.js', | ||
// prerun: './lib/hooks/prerun.js', | ||
// 'plugins:preinstall': './lib/hooks/plugins/preinstall.js' | ||
// }, | ||
// s3: {host: 'host'}, | ||
// plugins: ['heroku-pg', 'heroku-redis'] | ||
// } | ||
// TODO: validate | ||
// const cli = pjson['cli-engine'] || {} | ||
// const comment = 'cli-engine-config' | ||
// const title = { | ||
// warning: 'invalid CLI package.json', | ||
// error: 'invalid CLI package.json' } | ||
// validate(cli, {comment, title, exampleConfig: exampleCLI}) | ||
// validate(cli.hooks, { | ||
// comment, | ||
// condition: (option, validOption) => { | ||
// console.dir({option, validOption}) | ||
// }, | ||
// title, | ||
// exampleConfig: exampleCLI.hooks | ||
// }) | ||
} | ||
function buildConfig(existing = {}) { | ||
if (!existing) existing = {}; | ||
if (existing._version) return existing; | ||
if (existing.root && !existing.pjson) { | ||
let pjsonPath = _path2.default.join(existing.root, 'package.json'); | ||
if (_fsExtra2.default.existsSync(pjsonPath)) { | ||
// parse the package.json at the root | ||
let pjson = _fsExtra2.default.readJSONSync(_path2.default.join(existing.root, 'package.json')); | ||
existing.pjson = { | ||
...defaultConfig.pjson, | ||
if (!existing) | ||
existing = {}; | ||
if (existing._version) | ||
return existing; | ||
if (existing.root && !existing.pjson) { | ||
let pjsonPath = path.join(existing.root, 'package.json'); | ||
if (fs.existsSync(pjsonPath)) { | ||
// parse the package.json at the root | ||
let pjson = fs.readJSONSync(path.join(existing.root, 'package.json')); | ||
existing.pjson = { | ||
...exports.defaultConfig.pjson, | ||
'cli-engine': { | ||
...exports.defaultConfig.pjson['cli-engine'], | ||
...(pjson['cli-engine'] || {}), | ||
}, | ||
...pjson, | ||
}; | ||
validatePJSON(existing.pjson); | ||
} | ||
} | ||
const pjson = { | ||
name: 'cli-engine', | ||
version: '0.0.0', | ||
dependencies: {}, | ||
'cli-engine': { | ||
...defaultConfig.pjson['cli-engine'], | ||
...(pjson['cli-engine'] || {}) | ||
hooks: {}, | ||
defaultCommand: 'help', | ||
userPlugins: false, | ||
s3: { host: null }, | ||
}, | ||
...pjson | ||
}; | ||
validatePJSON(existing.pjson); | ||
} | ||
} | ||
return { | ||
_version: '1', | ||
pjson: { | ||
name: 'cli-engine', | ||
version: '0.0.0', | ||
dependencies: {}, | ||
'cli-engine': { | ||
hooks: {}, | ||
defaultCommand: 'help', | ||
userPlugins: false, | ||
s3: { host: null } | ||
} | ||
}, | ||
channel: 'stable', | ||
home: _os2.default.homedir() || _os2.default.tmpdir(), | ||
root: _path2.default.join(__dirname, '..'), | ||
arch: _os2.default.arch() === 'ia32' ? 'x86' : _os2.default.arch(), | ||
platform: _os2.default.platform() === 'win32' ? 'windows' : _os2.default.platform(), | ||
mock: false, | ||
argv: process.argv.slice(1), | ||
get defaultCommand() { | ||
return this.pjson['cli-engine'].defaultCommand; | ||
}, | ||
get name() { | ||
return this.pjson.name; | ||
}, | ||
get version() { | ||
return this.pjson.version; | ||
}, | ||
get hooks() { | ||
return hooks(this); | ||
}, | ||
get windows() { | ||
return this.platform === 'windows'; | ||
}, | ||
get userAgent() { | ||
return userAgent(this); | ||
}, | ||
get dirname() { | ||
return this.pjson['cli-engine'].dirname || this.bin; | ||
}, | ||
get shell() { | ||
return shell(this.windows); | ||
}, | ||
get bin() { | ||
return this.pjson['cli-engine'].bin || this.name; | ||
}, | ||
get debug() { | ||
return debug(this.bin || 'cli-engine') || 0; | ||
}, | ||
get dataDir() { | ||
return dir(this, 'data'); | ||
}, | ||
get configDir() { | ||
return dir(this, 'config'); | ||
}, | ||
get cacheDir() { | ||
return dir(this, 'cache', this.platform === 'darwin' ? _path2.default.join(this.home, 'Library', 'Caches') : null); | ||
}, | ||
get userConfig() { | ||
return loadUserConfig(this); | ||
}, | ||
get skipAnalytics() { | ||
return envSkipAnalytics(this); | ||
}, | ||
get install() { | ||
return this.userConfig.install; | ||
}, | ||
get s3() { | ||
return this.pjson['cli-engine'].s3; | ||
}, | ||
get commandsDir() { | ||
return commandsDir(this); | ||
}, | ||
get legacyConverter() { | ||
return this.pjson['cli-engine'].legacyConverter; | ||
}, | ||
get userPlugins() { | ||
return this.pjson['cli-engine'].userPlugins; | ||
}, | ||
get topics() { | ||
return topics(this); | ||
}, | ||
get errlog() { | ||
return _path2.default.join(this.cacheDir, 'error.log'); | ||
}, | ||
get npmRegistry() { | ||
return registry(this); | ||
}, | ||
...existing, | ||
__cache: {} | ||
}; | ||
...(existing.pjson || {}), | ||
}; | ||
return { | ||
_version: '1', | ||
pjson, | ||
channel: 'stable', | ||
home: os.homedir() || os.tmpdir(), | ||
root: path.join(__dirname, '..'), | ||
arch: os.arch() === 'ia32' ? 'x86' : os.arch(), | ||
platform: os.platform() === 'win32' ? 'windows' : os.platform(), | ||
mock: false, | ||
argv: process.argv.slice(1), | ||
version: pjson.version, | ||
defaultCommand: pjson['cli-engine'].defaultCommand, | ||
name: pjson.name, | ||
get hooks() { | ||
return hooks(this); | ||
}, | ||
get windows() { | ||
return this.platform === 'windows'; | ||
}, | ||
get userAgent() { | ||
return userAgent(this); | ||
}, | ||
get dirname() { | ||
return this.pjson['cli-engine'].dirname || this.bin; | ||
}, | ||
get shell() { | ||
return shell(this.windows); | ||
}, | ||
get bin() { | ||
return this.pjson['cli-engine'].bin || this.name; | ||
}, | ||
get debug() { | ||
return debug(this.bin || 'cli-engine') || 0; | ||
}, | ||
get dataDir() { | ||
return dir(this, 'data'); | ||
}, | ||
get configDir() { | ||
return dir(this, 'config'); | ||
}, | ||
get cacheDir() { | ||
return dir(this, 'cache', this.platform === 'darwin' ? path.join(this.home, 'Library', 'Caches') : undefined); | ||
}, | ||
get userConfig() { | ||
return loadUserConfig(this); | ||
}, | ||
get skipAnalytics() { | ||
return envSkipAnalytics(this); | ||
}, | ||
get install() { | ||
return this.userConfig.install; | ||
}, | ||
get s3() { | ||
return this.pjson['cli-engine'].s3; | ||
}, | ||
get commandsDir() { | ||
return commandsDir(this); | ||
}, | ||
get legacyConverter() { | ||
return this.pjson['cli-engine'].legacyConverter; | ||
}, | ||
get userPlugins() { | ||
return this.pjson['cli-engine'].userPlugins; | ||
}, | ||
get topics() { | ||
return topics(this); | ||
}, | ||
get errlog() { | ||
return path.join(this.cacheDir, 'error.log'); | ||
}, | ||
get npmRegistry() { | ||
return registry(this); | ||
}, | ||
...existing, | ||
__cache: {}, | ||
}; | ||
} | ||
const defaultConfig = exports.defaultConfig = buildConfig(); | ||
exports.buildConfig = buildConfig; | ||
exports.defaultConfig = buildConfig(); |
{ | ||
"name": "cli-engine-config", | ||
"description": "base cli-engine config objects and interfaces", | ||
"version": "3.4.0", | ||
"version": "4.0.0-ts.17", | ||
"author": "Jeff Dickey @dickeyxxx", | ||
@@ -13,18 +13,15 @@ "bugs": "https://github.com/heroku/cli-engine-config/issues", | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-jest": "^21.2.0", | ||
"babel-plugin-syntax-object-rest-spread": "^6.13.0", | ||
"babel-plugin-transform-class-properties": "6.24.1", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", | ||
"babel-plugin-transform-flow-strip-types": "6.22.0", | ||
"flow-bin": "^0.56.0", | ||
"flow-copy-source": "^1.2.1", | ||
"flow-typed": "^2.1.5", | ||
"@types/fs-extra": "4.0.5", | ||
"@types/jest": "21.1.8", | ||
"@types/node": "8.0.53", | ||
"del-cli": "1.1.0", | ||
"husky": "^0.14.3", | ||
"jest": "^21.2.1", | ||
"lint-staged": "^4.2.3", | ||
"mock-fs": "^4.4.1", | ||
"prettier": "^1.7.4", | ||
"lint-staged": "^5.0.0", | ||
"mock-fs": "^4.4.2", | ||
"prettier": "^1.8.2", | ||
"remap-istanbul": "^0.9.5", | ||
"rimraf": "2.6.2" | ||
"ts-jest": "21.2.3", | ||
"ts-node": "3.3.0", | ||
"typescript": "2.6.2" | ||
}, | ||
@@ -37,7 +34,3 @@ "engines": { | ||
], | ||
"homepage": "https://github.com/heroku/cli-engine-command", | ||
"jest": { | ||
"testEnvironment": "node", | ||
"rootDir": "./src" | ||
}, | ||
"homepage": "https://github.com/heroku/cli-engine-config", | ||
"keywords": [ | ||
@@ -48,20 +41,10 @@ "heroku", | ||
"license": "ISC", | ||
"lint-staged": { | ||
"src/**/*.js": [ | ||
"prettier --write", | ||
"git add" | ||
] | ||
}, | ||
"main": "lib/config.js", | ||
"repository": "heroku/cli-engine-command", | ||
"repository": "heroku/cli-engine-config", | ||
"scripts": { | ||
"build": "babel src -d lib --ignore '*.test.js'", | ||
"clean": "rimraf lib", | ||
"copy-flow": "flow-copy-source -v -i '**/*.test.js' src lib", | ||
"coverage": "cat coverage/coverage-final.json | remap-istanbul -o coverage/coverage-final.json && curl -s https://codecov.io/bash | bash", | ||
"posttest": "flow", | ||
"precommit": "lint-staged", | ||
"prepare": "npm run clean && npm run build && npm run copy-flow", | ||
"prepare": "del lib && tsc", | ||
"test": "jest" | ||
} | ||
}, | ||
"types": "./lib/config.d.ts" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
13
413
7
13445
4
1
1