Skip to content

Commit

Permalink
setup from a Staxfile
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Aug 11, 2024
1 parent bfa2f6e commit f1b5762
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}'`)

Expand Down
22 changes: 14 additions & 8 deletions src/staxfile/compiler.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

Expand All @@ -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
}
}
24 changes: 24 additions & 0 deletions src/staxfile/compose.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
12 changes: 10 additions & 2 deletions src/staxfile/dockerfilex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { readFileSync } from 'fs'
import { readFileSync, writeFileSync } from 'fs'
import { exit, fileExists } from '~/utils'

interface BuildOptions {
Expand All @@ -8,14 +8,18 @@ interface BuildOptions {
modules: string[];
}

interface CompileOptions {
outputFile?: string;
}

export default class Dockerfile {
public build: BuildOptions

constructor(options: BuildOptions) {
this.build = options
}

compile() {
compile(options: CompileOptions): string {
const modules = this.loadModules()
let text = ""

Expand All @@ -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
}

Expand Down

0 comments on commit f1b5762

Please sign in to comment.