mirror of https://github.com/Seich/Beau.git
58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
const yaml = require('js-yaml');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const dotenv = require('dotenv');
|
|
const { Command, flags } = require('@oclif/command');
|
|
const { Spinner } = require('clui');
|
|
|
|
const Beau = require('../../src/beau');
|
|
|
|
class Base extends Command {
|
|
async init() {
|
|
const spinnerSprite = ['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷'];
|
|
this.spinner = new Spinner('', spinnerSprite);
|
|
}
|
|
|
|
openConfigFile(configFile) {
|
|
if (!fs.existsSync(configFile)) {
|
|
this.error(`The config file, ${configFile} was not found.`);
|
|
this.exit(1);
|
|
}
|
|
|
|
return yaml.safeLoad(fs.readFileSync(configFile, 'utf-8'));
|
|
}
|
|
|
|
loadConfig(configFile, params = []) {
|
|
const config = this.openConfigFile(configFile);
|
|
const env = dotenv.config().parsed || {};
|
|
params = dotenv.parse(params.reduce((a, p) => a + '\n' + p, ''));
|
|
|
|
const envParams = { _: Object.assign(env, params) };
|
|
|
|
const configFileDir = path.dirname(
|
|
path.resolve(process.cwd(), configFile)
|
|
);
|
|
|
|
process.chdir(configFileDir);
|
|
|
|
return new Beau(config, envParams);
|
|
}
|
|
}
|
|
|
|
Base.flags = {
|
|
config: flags.string({
|
|
char: 'c',
|
|
description: 'The configuration file to be used.',
|
|
default: 'beau.yml'
|
|
}),
|
|
verbose: flags.boolean({
|
|
char: 'V',
|
|
description: `Show all additional information available for a command.`
|
|
}),
|
|
'no-format': flags.boolean({
|
|
description: `Disables color formatting for usage on external tools.`
|
|
})
|
|
};
|
|
|
|
module.exports = Base;
|