@serpent/common-cli
Version:
通用的 cli 相关的函数
285 lines (251 loc) • 7.43 kB
JavaScript
;
var _commonjsHelpers = require('./_commonjsHelpers-ed042b00.cjs');
var fs$1 = require('fs');
var path$1 = require('path');
var require$$1 = require('os');
var tslib_es6 = require('./tslib.es6-8ea38f41.cjs');
var project = require('./project-7917b261.cjs');
require('module');
require('assert');
require('./context-e98d804e.cjs');
require('./exists-4a19b288.cjs');
require('./readJsonFile-f1989956.cjs');
var main$1 = {exports: {}};
var name = "dotenv";
var version$1 = "16.0.3";
var description = "Loads environment variables from .env file";
var main = "lib/main.js";
var types = "lib/main.d.ts";
var exports$1 = {
".": {
require: "./lib/main.js",
types: "./lib/main.d.ts",
"default": "./lib/main.js"
},
"./config": "./config.js",
"./config.js": "./config.js",
"./lib/env-options": "./lib/env-options.js",
"./lib/env-options.js": "./lib/env-options.js",
"./lib/cli-options": "./lib/cli-options.js",
"./lib/cli-options.js": "./lib/cli-options.js",
"./package.json": "./package.json"
};
var scripts = {
"dts-check": "tsc --project tests/types/tsconfig.json",
lint: "standard",
"lint-readme": "standard-markdown",
pretest: "npm run lint && npm run dts-check",
test: "tap tests/*.js --100 -Rspec",
prerelease: "npm test",
release: "standard-version"
};
var repository = {
type: "git",
url: "git://github.com/motdotla/dotenv.git"
};
var keywords = [
"dotenv",
"env",
".env",
"environment",
"variables",
"config",
"settings"
];
var readmeFilename = "README.md";
var license = "BSD-2-Clause";
var devDependencies = {
"@types/node": "^17.0.9",
decache: "^4.6.1",
dtslint: "^3.7.0",
sinon: "^12.0.1",
standard: "^16.0.4",
"standard-markdown": "^7.1.0",
"standard-version": "^9.3.2",
tap: "^15.1.6",
tar: "^6.1.11",
typescript: "^4.5.4"
};
var engines = {
node: ">=12"
};
var require$$3 = {
name: name,
version: version$1,
description: description,
main: main,
types: types,
exports: exports$1,
scripts: scripts,
repository: repository,
keywords: keywords,
readmeFilename: readmeFilename,
license: license,
devDependencies: devDependencies,
engines: engines
};
const fs = fs$1;
const path = path$1;
const os = require$$1;
const packageJson = require$$3;
const version = packageJson.version;
const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
// Parser src into an Object
function parse (src) {
const obj = {};
// Convert buffer to string
let lines = src.toString();
// Convert line breaks to same format
lines = lines.replace(/\r\n?/mg, '\n');
let match;
while ((match = LINE.exec(lines)) != null) {
const key = match[1];
// Default undefined or null to empty string
let value = (match[2] || '');
// Remove whitespace
value = value.trim();
// Check if double quoted
const maybeQuote = value[0];
// Remove surrounding quotes
value = value.replace(/^(['"`])([\s\S]*)\1$/mg, '$2');
// Expand newlines if double quoted
if (maybeQuote === '"') {
value = value.replace(/\\n/g, '\n');
value = value.replace(/\\r/g, '\r');
}
// Add to object
obj[key] = value;
}
return obj
}
function _log (message) {
console.log(`[dotenv@${version}][DEBUG] ${message}`);
}
function _resolveHome (envPath) {
return envPath[0] === '~' ? path.join(os.homedir(), envPath.slice(1)) : envPath
}
// Populates process.env from .env file
function config (options) {
let dotenvPath = path.resolve(process.cwd(), '.env');
let encoding = 'utf8';
const debug = Boolean(options && options.debug);
const override = Boolean(options && options.override);
if (options) {
if (options.path != null) {
dotenvPath = _resolveHome(options.path);
}
if (options.encoding != null) {
encoding = options.encoding;
}
}
try {
// Specifying an encoding returns a string instead of a buffer
const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding }));
Object.keys(parsed).forEach(function (key) {
if (!Object.prototype.hasOwnProperty.call(process.env, key)) {
process.env[key] = parsed[key];
} else {
if (override === true) {
process.env[key] = parsed[key];
}
if (debug) {
if (override === true) {
_log(`"${key}" is already defined in \`process.env\` and WAS overwritten`);
} else {
_log(`"${key}" is already defined in \`process.env\` and was NOT overwritten`);
}
}
}
});
return { parsed }
} catch (e) {
if (debug) {
_log(`Failed to load ${dotenvPath} ${e.message}`);
}
return { error: e }
}
}
const DotenvModule = {
config,
parse
};
main$1.exports.config = DotenvModule.config;
main$1.exports.parse = DotenvModule.parse;
main$1.exports = DotenvModule;
var mainExports = main$1.exports;
var dotenv = /*@__PURE__*/_commonjsHelpers.getDefaultExportFromCjs(mainExports);
function getEnv(options) {
var defaultEnv = options.defaultEnv;
var env = tslib_es6.__assign({}, defaultEnv);
Object.keys(defaultEnv).forEach(function (key) {
env[key] = getEnvSingly(key, options);
});
return env;
}
function getEnvSingly(key, options) {
var prefixedKey = options.prefix + key;
var value = process.env[prefixedKey];
var defaultEnv = options.defaultEnv;
if (!defaultEnv.hasOwnProperty(key))
throw new Error("Environment variable(\"".concat(prefixedKey, "\") not exists"));
if (typeof value === 'string') {
if (typeof defaultEnv[key] === 'string') {
return value;
}
else {
try {
return JSON.parse(value);
}
catch (e) {
throw new Error("Environment variable(".concat(prefixedKey, ") value(").concat(value, ") is not valid json"));
}
}
}
else {
return defaultEnv[key];
}
}
function setEnv(options) {
setEnvFromEnvFile();
return setEnvFromOptions(options);
}
function setEnvFromEnvFile() {
var envFile = project.tryGetProjectFile('.env');
if (envFile) {
dotenv.config({ path: envFile });
}
}
function setEnvFromOptions(options) {
var env = getEnv(options);
Object.entries(options.defaultEnv).forEach(function (_a) {
var key = _a[0]; _a[1];
setEnvSingly(key, env[key], options);
});
return env;
}
function setEnvSingly(key, value, options) {
var _a = options, defaultEnv = _a.defaultEnv, prefix = _a.prefix;
var prefixedKey = prefix + key;
if (!defaultEnv.hasOwnProperty(key))
throw new Error("Environment variable(\"".concat(prefixedKey, "\") not exists"));
var defaultValue = defaultEnv[key];
process.env[prefixedKey] = typeof defaultValue === 'string' ? value + '' : JSON.stringify(value);
}
function initEnv(options) {
var result = {};
setEnv(options);
Object.entries(options.defaultEnv).forEach(function (_a) {
var key = _a[0]; _a[1];
Object.defineProperty(result, key, {
enumerable: true,
get: function () {
return getEnvSingly(key, options);
},
set: function (value) {
return setEnvSingly(key, value, options);
},
});
});
return result;
}
exports.initEnv = initEnv;