UNPKG

haz-cli

Version:
81 lines (80 loc) 3.15 kB
import { Flags, ux } from '@oclif/core'; import { BaseCommand } from "../../base-command.js"; import Compose from "../../docker/compose.js"; import Env from "../../haz/env.js"; import Output from "../../haz/output.js"; export default class Setup extends BaseCommand { // static args = { // service: Args.string({ // description: 'The service you want to run tests on.', // required: false, // }), // } static description = ` This command will pull the docker images down, and cycle through the .haz file to setup the environment. `; static flags = { ...BaseCommand.flags, 'start': Flags.boolean({ char: 's', default: false, description: 'Start the stack after setup.' }) }; static summary = 'Setup the docker environment'; async run() { const { flags } = await this.parse(Setup); const output = new Output(flags.verbose); await this.config.runCommand('docker:pull'); const yaml = Env.get(''); if (!yaml.docker) { output.error('No docker containers found in .haz file.'); return; } for (const container in yaml.docker) { if (yaml.docker[container].setup) { const setupCommands = []; if (Array.isArray(yaml.docker[container].setup)) { for (const command of yaml.docker[container].setup) { setupCommands.push(command); } } else { setupCommands.push(yaml.docker[container].setup); } if (setupCommands.length === 0) { output.log(`No setup instructions found for ${container}`); } else { ux.action.start('Setting up ' + container); // eslint-disable-next-line guard-for-in for (const command in setupCommands) { const process = Compose.setService(container).run(setupCommands[command].split(' ')); process.stdout.on('data', (data) => { output.log(data.toString()); }); process.stderr.on('data', (data) => { output.error(data.toString()); }); process.on('exit', (code) => { if (code === 0) { output.log(`Command ${command} ran successfully`); } else { output.error(`Command ${command} failed to run`); } }); } ux.action.stop('Setup complete'); } } else { output.log(`No setup instructions found for ${container}, skipping...`); } } if (flags.start) { await this.config.runCommand('docker:up'); } } }