@procore/core-scripts
Version:
A CLI to enhance your development experience
76 lines • 2.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCommand = void 0;
const tslib_1 = require("tslib");
const command_1 = tslib_1.__importDefault(require("@oclif/command"));
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const dotenv_1 = tslib_1.__importDefault(require("dotenv"));
const dotenv_expand_1 = tslib_1.__importDefault(require("dotenv-expand"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
const Workspace_1 = require("./Workspace");
class BaseCommand extends command_1.default {
constructor() {
super(...arguments);
this.env = 'development';
this.isCI = !!process.env.CI;
}
async init() {
const context = fs_1.default.realpathSync(process.cwd());
this.workspace = new Workspace_1.Workspace(this.env, context);
this.debug(`${this.config.name}: ${chalk_1.default.green(this.config.version)}`);
this.debug(`Running on CI: ${chalk_1.default.green(`${this.isCI}`)}`);
this.setupEnv();
}
newline() {
this.log('');
}
setupEnv() {
this.setNodePath();
this.loadEnvVariables();
this.setEnvMode();
}
setNodePath() {
process.env.NODE_PATH = (process.env.NODE_PATH || '')
.split(path_1.default.delimiter)
.filter((folder) => folder && !path_1.default.isAbsolute(folder))
.map((folder) => this.workspace.resolve(folder))
.join(path_1.default.delimiter);
}
loadEnvVariables() {
const dotenvPath = this.workspace.resolve('.env');
const dotenvFiles = [
`${dotenvPath}.${this.env}.local`,
`${dotenvPath}.${this.env}`,
this.env !== 'test' && `${dotenvPath}.local`,
dotenvPath,
].filter(Boolean);
dotenvFiles.filter(fs_1.default.existsSync).forEach((filePath) => {
this.debug(`Loading Environment Variables File: ${filePath}`);
dotenv_expand_1.default.expand(dotenv_1.default.config({ path: filePath }));
});
}
setEnvMode() {
if (!['development', 'test', 'production'].includes(this.env)) {
this.log(chalk_1.default.red([
`"${this.env}" mode is not supported. Please use one of the `,
'following mode:\n\n',
'- development\n',
'- test\n',
'- production\n',
].join('')));
this.exit(1);
}
process.env.BABEL_ENV = this.env;
process.env.NODE_ENV = this.env;
this.debug(`Current Environment Mode: ${chalk_1.default.green(this.env)}`);
}
async catch(error) {
if (error === '') {
this.exit(1);
}
this.error(error);
}
}
exports.BaseCommand = BaseCommand;
//# sourceMappingURL=BaseCommand.js.map