@delgee/docker-builder
Version:
A CLI to build docker files
83 lines (65 loc) • 2.05 kB
JavaScript
import fs from 'fs-extra';
import chalk from 'chalk';
import path from 'path';
import inquirer from 'inquirer';
import execa from 'execa';
const execaStream = (command, argument) => {
return new Promise((resolve, reject) => {
const pipe = execa(command, argument).all;
pipe.on('data', buffer => {
process.stdout.write(buffer.toString());
});
pipe.on('end', () => {
resolve();
});
pipe.on('error', error => {
reject(error);
});
});
};
export async function dockerBuild(options) {
try {
options = {
...options,
targetDirectory: options.targetDirectory || process.cwd()
};
let isBuilderExists = await fs.exists(path.join(options.targetDirectory, '.builderrc'));
if (!isBuilderExists) {
let isCreateRcfile = await inquirer.prompt({
type: 'confirm',
name: 'isCreateFile',
message: '.builderrc file not found. Should we create it?'
});
if (isCreateRcfile.isCreateFile) {
let registryNameAnswer = await inquirer.prompt({
type: 'input',
name: 'registry',
message: 'Please enter registry name'
});
if (!registryNameAnswer.registry) {
throw new Error('bad registry name');
}
await fs.writeFile(
path.join(options.targetDirectory, '.builderrc'),
JSON.stringify({ registry: registryNameAnswer.registry }, null, 2),
'utf8'
);
} else {
throw new Error('.builderrc file not found');
}
}
const fileString = await fs.readFile(path.join(options.targetDirectory, '.builderrc'), 'utf8');
const { registry } = JSON.parse(fileString);
await execa('cd', options.targetDirectory);
await execaStream('docker', ['build', '-t', registry, '.']);
console.log();
if (options.isPush) {
await execaStream('docker', ['push', registry]);
}
process.exit(0);
} catch (e) {
console.log(chalk.red(e.message));
console.error(e);
process.exit(1);
}
}