From f1b5762435fe880335e4701c356b26af5866a9fb Mon Sep 17 00:00:00 2001 From: Kien La Date: Sun, 11 Aug 2024 13:46:00 -0400 Subject: [PATCH] setup from a Staxfile --- src/setup.ts | 4 ++++ src/staxfile/compiler.ts | 22 ++++++++++++++-------- src/staxfile/compose.ts | 24 ++++++++++++++++++++++++ src/staxfile/dockerfilex.ts | 12 ++++++++++-- 4 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 src/staxfile/compose.ts diff --git a/src/setup.ts b/src/setup.ts index b9b10ee..e3c2af2 100644 --- a/src/setup.ts +++ b/src/setup.ts @@ -3,6 +3,7 @@ import { load } from 'js-yaml' import { exit, isFile, fileExists } from '~/utils' import App from '~/app' import Container from '~/container' +import Compiler from '~/staxfile/compiler' import docker from '~/docker' function findDockerComposeFile(location: string): string | undefined { @@ -42,6 +43,9 @@ export default async function setup(contextName: string, location: string) { if (container) return exit(1, `👿 Container '${location}@${contextName}' has already been setup. Use 'rebuild' if you want to rebuild it.`) + if (fileExists(`${location}/Staxfile`)) + location = new Compiler(`${location}/Staxfile`).compile().composeFile + if (!(composeFile = findDockerComposeFile(location))) return exit(1, `👿 Couldn't setup a container for '${original}'`) diff --git a/src/staxfile/compiler.ts b/src/staxfile/compiler.ts index 1d2c508..2632b62 100644 --- a/src/staxfile/compiler.ts +++ b/src/staxfile/compiler.ts @@ -1,9 +1,10 @@ import { readFileSync, writeFileSync } from 'fs' -import { load } from 'js-yaml' import { exit, fileExists } from '~/utils' import path from 'path' import tmp from 'tmp' +import yaml from 'js-yaml' import Dockerfile from './dockerfilex' +import Compose from './compose' export default class Compiler { public staxfile: string @@ -15,19 +16,19 @@ export default class Compiler { exit(1, `Staxfile not found: ${staxfile}`) this.staxfile = staxfile - this.config = load(readFileSync(this.staxfile, 'utf-8')) + this.config = yaml.load(readFileSync(this.staxfile, 'utf-8')) this.baseDir = path.dirname(path.resolve(this.staxfile)) tmp.setGracefulCleanup() } public compile() { - const files = { dockerfile: undefined, compose: undefined } + const files = { dockerFile: undefined, composeFile: undefined } this.insideBaseDir(() => { - files.dockerfile = this.compileDockerfile() + files.dockerFile = this.compileDockerfile() + files.composeFile = this.compileCompose() }) - console.log(files) return files } @@ -43,10 +44,15 @@ export default class Compiler { } private compileDockerfile(): string { - const dockerfile = new Dockerfile(this.config.defaults.build).compile() const file = tmp.fileSync({ tmpdir: this.baseDir, postfix: 'dockerfile' }) - - writeFileSync(file.name, dockerfile) + const dockerfile = new Dockerfile(this.config.defaults.build).compile({ outputFile: file.name}) + this.config.defaults.build.dockerfile = file.name return file.name } + + private compileCompose(): string { + const tmpfile = tmp.fileSync({ tmpdir: this.baseDir, postfix: 'compose' }) + const yaml = new Compose(this.config).compile({ outputFile: tmpfile.name }) + return tmpfile.name + } } diff --git a/src/staxfile/compose.ts b/src/staxfile/compose.ts new file mode 100644 index 0000000..ddae68b --- /dev/null +++ b/src/staxfile/compose.ts @@ -0,0 +1,24 @@ +import yaml from 'js-yaml' +import { writeFileSync } from 'fs' +import { deepRemoveKeys } from "~/utils" + +interface CompileOptions { + outputFile?: string +} + +export default class Compose { + public config: any + + constructor(config: any) { + this.config = structuredClone(config) + } + + compile(options: CompileOptions): string { + const config = yaml.dump(deepRemoveKeys(this.config, [ 'defaults', 'modules' ])) + + if (options?.outputFile) + writeFileSync(options.outputFile, config, 'utf-8') + + return config + } +} diff --git a/src/staxfile/dockerfilex.ts b/src/staxfile/dockerfilex.ts index 0eb016f..befa1de 100644 --- a/src/staxfile/dockerfilex.ts +++ b/src/staxfile/dockerfilex.ts @@ -1,5 +1,5 @@ import path from 'path' -import { readFileSync } from 'fs' +import { readFileSync, writeFileSync } from 'fs' import { exit, fileExists } from '~/utils' interface BuildOptions { @@ -8,6 +8,10 @@ interface BuildOptions { modules: string[]; } +interface CompileOptions { + outputFile?: string; +} + export default class Dockerfile { public build: BuildOptions @@ -15,7 +19,7 @@ export default class Dockerfile { this.build = options } - compile() { + compile(options: CompileOptions): string { const modules = this.loadModules() let text = "" @@ -32,6 +36,10 @@ export default class Dockerfile { }) text = text.replaceAll('# $stax.section args', this.args()) + + if (options?.outputFile) + writeFileSync(options.outputFile, text, 'utf-8') + return text }