Skip to content

Commit de640de

Browse files
committed
compile when needed
1 parent 968744e commit de640de

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/container.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { csvKeyValuePairs, exit } from '~/utils'
22
import docker from '~/docker'
3+
import setup from '~/setup'
34
import Hooks from '~/hooks'
5+
import Staxfile from '~/staxfile'
46

57
interface FindOptions {
68
warn?: boolean
@@ -10,6 +12,7 @@ interface FindOptions {
1012
export default class Container {
1113
public attributes: Record<string, any>
1214
private _labels: Record<string, string>
15+
private _composeFile: string
1316
private hooks: Hooks
1417

1518
constructor(attributes: Record<string, any>) {
@@ -25,6 +28,10 @@ export default class Container {
2528
return this.attributes.ID
2629
}
2730

31+
get staxfile(): string {
32+
return this.labels['dev.stax.staxfile']
33+
}
34+
2835
get app(): string {
2936
return this.labels['dev.stax.app']
3037
}
@@ -65,6 +72,11 @@ export default class Container {
6572
return this.labels['com.docker.compose.project.config_files']
6673
}
6774

75+
get composeFile(): string {
76+
this._composeFile ||= new Staxfile(this.staxfile).compile().composeFile
77+
return this._composeFile
78+
}
79+
6880
static all(contextName: string): Container[] {
6981
return docker.ps(contextName)
7082
.map(attributes => new Container(attributes))
@@ -85,23 +97,23 @@ export default class Container {
8597
}
8698

8799
async down() {
88-
return docker.compose(this.contextName, 'stop', this.name)
100+
return docker.compose(this.contextName, 'stop', this.composeFile)
89101
}
90102

91103
async up() {
92-
return docker.compose(this.contextName, 'start', this.name, { exit: true })
104+
return docker.compose(this.contextName, 'start', this.composeFile, { exit: true })
93105
}
94106

95107
async remove() {
96-
return docker.compose(this.contextName, 'rm --stop --force --volumes', this.name)
108+
return docker.compose(this.contextName, 'rm --stop --force --volumes', this.composeFile)
97109
}
98110

99111
async exec(command: string) {
100112
return docker.container(`exec -it ${this.name} ${command}`)
101113
}
102114

103115
async rebuild() {
104-
await docker.compose(this.contextName, `up --detach --force-recreate ${this.name}`, this.configFile, { exit: true })
116+
setup(this.contextName, this.staxfile)
105117
this.hooks.onPostBuild()
106118
}
107119

src/docker.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import { exit, verifyFile } from '~/utils'
1+
import { verifyFile } from '~/utils'
22
import { run, capture } from '~/shell'
3-
import Container from '~/container'
43

5-
async function compose(contextName: string, command: string, path: string, options: Record<string,any> = {}) {
4+
async function compose(contextName: string, command: string, composeFile: string, options: Record<string,any> = {}) {
65
const base = `docker compose --project-name ${contextName}`
76

87
options = { append: true, ...options, env: { COMPOSE_IGNORE_ORPHANS: "1" } }
8+
options.exit && verifyFile(composeFile)
99

10-
// See if path is actually a container name
11-
if (Container.find(contextName, path))
12-
return run(`${base} ${command}${options.append ? ` ${path}` : ''}`, options)
13-
14-
options.exit && verifyFile(path)
15-
16-
return run(`${base} -f ${path} ${command}`, options)
10+
return run(`${base} -f ${composeFile} ${command}`, options)
1711
}
1812

1913
async function container(command: string) {

src/setup.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { readFileSync } from 'fs'
22
import { load } from 'js-yaml'
3-
import { exit, isDirectory, fileExists, isFile } from '~/utils'
3+
import { exit, isDirectory, fileExists } from '~/utils'
44
import App from '~/app'
5-
import Container from '~/container'
65
import Staxfile from '~/staxfile'
76
import docker from '~/docker'
87

0 commit comments

Comments
 (0)