Skip to content

Commit

Permalink
Restyled by prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits committed Nov 28, 2023
1 parent 1006038 commit bbf458f
Showing 1 changed file with 93 additions and 75 deletions.
168 changes: 93 additions & 75 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {program} = require("commander");
const {exec} = require("child_process");
const { program } = require("commander");
const { exec } = require("child_process");
let folder = "images";

const fs = require("fs");
Expand All @@ -8,33 +8,37 @@ let cmd = [];
program.name("main.js").description("CLI to build image docker");

function getSelectfolder(options) {
const selectfolder = process.env.npm_config_folder != undefined
? process.env.npm_config_folder
: options.folder;
const selectfolder =
process.env.npm_config_folder != undefined
? process.env.npm_config_folder
: options.folder;

return selectfolder;
}

function getLatest(options) {
const latest = process.env.npm_config_latest != undefined
? process.env.npm_config_latest
: options.latest;
const latest =
process.env.npm_config_latest != undefined
? process.env.npm_config_latest
: options.latest;

return latest;
}

function getXdebug(options) {
const xdebug = process.env.npm_config_xdebug != undefined
? process.env.npm_config_xdebug
: options.xdebug;
const xdebug =
process.env.npm_config_xdebug != undefined
? process.env.npm_config_xdebug
: options.xdebug;

return xdebug;
}

function getVersions(folder) {
const versions = fs.readdirSync(`images/${folder}`, {withFileTypes : true})
.filter((item) => item.isDirectory())
.map((item) => item.name);
const versions = fs
.readdirSync(`images/${folder}`, { withFileTypes: true })
.filter((item) => item.isDirectory())
.map((item) => item.name);

return versions;
}
Expand All @@ -45,71 +49,85 @@ function setVersionImage(selectfolder, version) {
return versionimage;
}

program.command("build:php")
.description("build php images")
.option("--folder <folder>", "images version")
.option("--latest", "latest")
.action(async (options) => {
const selectfolder = getSelectfolder(options);
versions = getVersions("phpfpm");
versions.forEach((version) => {
let versionimage = setVersionImage(selectfolder, version);
if (selectfolder == undefined || selectfolder == version ||
selectfolder.split(version).length - 1 == 1) {
cmd.push(`mkdir -p build/phpfpm/${versionimage}`);
cmd.push(`cp images/phpfpm/${version}/Dockerfile build/phpfpm/${
versionimage}/Dockerfile`);
cmd.push(`sed -i 's/VERSIONIMAGE/php:${
versionimage}-fpm/' build/phpfpm/${versionimage}/Dockerfile`);
cmd.push(`docker build -t koromerzhin/php:${
versionimage}-fpm build/phpfpm/${
versionimage} --target build-phpfpm`);
if (getXdebug(options) == 'on') {
cmd.push(`docker build -t koromerzhin/php:${
versionimage}-fpm-xdebug build/phpfpm/${
versionimage} --target build-phpfpm-xdebug`);
}
if (getLatest(options) == 'on') {
cmd.push(`docker image tag koromerzhin/php:${
versionimage}-fpm koromerzhin/php:fpm-latest`);
if (getXdebug(options) == 'on') {
cmd.push(`docker image tag koromerzhin/php:${
versionimage}-fpm-xdebug koromerzhin/php:fpm-latest-xdebug`);
}
}
program
.command("build:php")
.description("build php images")
.option("--folder <folder>", "images version")
.option("--latest", "latest")
.action(async (options) => {
const selectfolder = getSelectfolder(options);
versions = getVersions("phpfpm");
versions.forEach((version) => {
let versionimage = setVersionImage(selectfolder, version);
if (
selectfolder == undefined ||
selectfolder == version ||
selectfolder.split(version).length - 1 == 1
) {
cmd.push(`mkdir -p build/phpfpm/${versionimage}`);
cmd.push(
`cp images/phpfpm/${version}/Dockerfile build/phpfpm/${versionimage}/Dockerfile`,
);
cmd.push(
`sed -i 's/VERSIONIMAGE/php:${versionimage}-fpm/' build/phpfpm/${versionimage}/Dockerfile`,
);
cmd.push(
`docker build -t koromerzhin/php:${versionimage}-fpm build/phpfpm/${versionimage} --target build-phpfpm`,
);
if (getXdebug(options) == "on") {
cmd.push(
`docker build -t koromerzhin/php:${versionimage}-fpm-xdebug build/phpfpm/${versionimage} --target build-phpfpm-xdebug`,
);
}
});
versions = getVersions("php-apache");
versions.forEach((version) => {
let versionimage = setVersionImage(selectfolder, version);
if (selectfolder == undefined || selectfolder == version ||
selectfolder.split(version).length - 1 == 1) {
cmd.push(`mkdir -p build/php-apache/${versionimage}`);
cmd.push(`cp images/php-apache/${
version}/Dockerfile build/php-apache/${versionimage}/Dockerfile`);
cmd.push(`sed -i 's/VERSIONIMAGE/php:${
versionimage}-apache/' build/php-apache/${
versionimage}/Dockerfile`);
cmd.push(`docker build -t koromerzhin/php:${
versionimage}-apache build/php-apache/${
versionimage} --target build-php-apache`);
if (getXdebug(options) == 'on') {
cmd.push(`docker build -t koromerzhin/php:${
versionimage}-apache-xdebug build/php-apache/${
versionimage} --target build-php-apache-xdebug`);
if (getLatest(options) == "on") {
cmd.push(
`docker image tag koromerzhin/php:${versionimage}-fpm koromerzhin/php:fpm-latest`,
);
if (getXdebug(options) == "on") {
cmd.push(
`docker image tag koromerzhin/php:${versionimage}-fpm-xdebug koromerzhin/php:fpm-latest-xdebug`,
);
}
if (getLatest(options) != undefined) {
cmd.push(`docker image tag koromerzhin/php:${
versionimage}-apache koromerzhin/php:apache-latest`);
if (getXdebug(options) == 'on') {
cmd.push(`docker image tag koromerzhin/php:${
versionimage}-apache-xdebug koromerzhin/php:apache-latest-xdebug`);
}
}
}
});
versions = getVersions("php-apache");
versions.forEach((version) => {
let versionimage = setVersionImage(selectfolder, version);
if (
selectfolder == undefined ||
selectfolder == version ||
selectfolder.split(version).length - 1 == 1
) {
cmd.push(`mkdir -p build/php-apache/${versionimage}`);
cmd.push(
`cp images/php-apache/${version}/Dockerfile build/php-apache/${versionimage}/Dockerfile`,
);
cmd.push(
`sed -i 's/VERSIONIMAGE/php:${versionimage}-apache/' build/php-apache/${versionimage}/Dockerfile`,
);
cmd.push(
`docker build -t koromerzhin/php:${versionimage}-apache build/php-apache/${versionimage} --target build-php-apache`,
);
if (getXdebug(options) == "on") {
cmd.push(
`docker build -t koromerzhin/php:${versionimage}-apache-xdebug build/php-apache/${versionimage} --target build-php-apache-xdebug`,
);
}
if (getLatest(options) != undefined) {
cmd.push(
`docker image tag koromerzhin/php:${versionimage}-apache koromerzhin/php:apache-latest`,
);
if (getXdebug(options) == "on") {
cmd.push(
`docker image tag koromerzhin/php:${versionimage}-apache-xdebug koromerzhin/php:apache-latest-xdebug`,
);
}
}
});
saveInFile(cmd, "php", selectfolder);
}
});
saveInFile(cmd, "php", selectfolder);
});

function saveInFile(cmd, image, selectfolder) {
let file = `build-${image}`;
Expand All @@ -118,7 +136,7 @@ function saveInFile(cmd, image, selectfolder) {
}
file += ".sh";
let content = cmd.join("\n");
fs.writeFile(file, "#!/bin/bash -x\n" + content, function(err) {
fs.writeFile(file, "#!/bin/bash -x\n" + content, function (err) {
if (err) {
return console.log(err);
}
Expand Down

0 comments on commit bbf458f

Please sign in to comment.