Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Sep 9, 2024
1 parent 910c49f commit 27b28a7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
19 changes: 17 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { readFileSync } from 'fs'
import { exit } from '~/utils'
import { StaxConfig, SetupOptions, FindContainerOptions } from '~/types'
import Staxfile from '~/staxfile'
import icons from '~/icons'
import docker from '~/docker'
import Container from '~/container'
import settings from '~/settings'
Expand Down Expand Up @@ -48,14 +49,20 @@ export default class App {
const container = Container.find(context, containerName, { mustExist: true })

if (!container) {
console.warn(`🤷 App '${containerName}@${context}' not found`)
console.warn(`${icons.warning} App '${containerName}@${context}' not found`)
return null
}
return new App(containerName, [container])
}

static exists(context: string, name: string): boolean {
return this.all(context).find(app => app.name == name) != null
}

static async setup(config: StaxConfig, options: SetupOptions = {}) {
const staxfile = new Staxfile(config)
console.log(staxfile.config)
process.exit()
const composeFile = staxfile.compile()

if (!composeFile)
Expand Down Expand Up @@ -119,10 +126,18 @@ export default class App {
this.containers.forEach(async container => container.runHooks())
}

addAlias(alias) {
addAlias(alias: string) {
const aliases = settings.read('aliases') || {}

aliases[alias] = this.name
settings.write('aliases', aliases)
}

async duplicate(newName: string) {
if (this.constructor.exists(this.context, newName))
exit(1, `${icons.error} An app named '${newName}' already exists.`)

const config = { ...this.primary.config, app: newName } as unknown as StaxConfig
return this.constructor.setup(config)
}
}
8 changes: 7 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ program.command('down')
target.down()
})

program.command('duplicate')
.argument('<name>', 'Name of application')
.argument('<new-name>', 'Name of new application')
.description('Duplicate an application')
.action(async (name, newName) => stax.find(name).duplicate(newName))

program.command('edit')
.argument('<name>', 'Name of application')
.description(`Open application in a vscode based editor`)
Expand Down Expand Up @@ -142,7 +148,7 @@ program.command('setup')
.option('-s, --staxfile <staxfile>', 'Staxfile to use for setup')
.option('-i, --inspect', 'Show the compose file')
.description('Setup an application')
.action(async (location, options) => stax.setup({ source: location, ...options, ...overrides }, options))
.action(async (location, options) => stax.setup({ source: location, ...options }, { ...options, overrides: overrides }))

program.command('shell')
.alias('sh')
Expand Down
1 change: 1 addition & 0 deletions src/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const icons = {
unknown: '❔',
local: '📁',
remote: '🌐',
error: '😡',
}

export default icons
9 changes: 8 additions & 1 deletion src/stax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ export default class Stax {
list(this.apps())
}

async setup(config: StaxConfig, options: { inspect?: boolean } = { inspect: false }) {
async setup(config: StaxConfig, options: { inspect?: boolean, overrides?: Record<string, any> } = { inspect: false }) {
// if (options.overrides) {
// for (const [ key, value ] of Object.entries(options.overrides)) {
// console.log(key, value)
// }
// }
// console.log(config)
// process.exit()
App.setup({ context: this.context, ...config } as unknown as StaxConfig, options)
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function parseAndRemoveWildcardOptions(args: string[], startsWith: string
const staxVars: Record<string, string> = {}
const filteredArgs = args.filter(arg => {
if (arg.startsWith(startsWith)) {
const [key, value] = arg.slice(startsWith.length).split('=')
const [key, value] = arg.slice(2).split('=')
staxVars[key] = value
return false
}
Expand Down

0 comments on commit 27b28a7

Please sign in to comment.