Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Sep 26, 2024
1 parent 5c907cf commit 963bd8d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default class Location {
this.context = context
this.app = app
this.source = source

if (!Location.isGitUrl(source) && existsSync(source))
this.source = path.resolve(source)
}

static isGitUrl(url: string): boolean {
Expand All @@ -27,7 +30,7 @@ export default class Location {
return containerLocation.container ? containerLocation : new GitLocation(context, app, location)
}

return new Location(context, app, location && path.resolve(location))
return new Location(context, app, location)
}

get basename(): string {
Expand Down
22 changes: 16 additions & 6 deletions src/staxfile/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ export default class Expressions {

async evaluate(name: string, args: any[]): Promise<string> {
const cacheKey = this.getCacheKey(name, args)
if (!name.startsWith('stax.') && Expressions.cache[cacheKey] !== undefined) {
console.log(`Cache hit for ${cacheKey}: ${Expressions.cache[cacheKey]}`)

if (!name.startsWith('stax.') && Expressions.cache[cacheKey] !== undefined)
return Expressions.cache[cacheKey]
}

const result = this.evaluateUncached(name, args)
Expressions.cache[cacheKey] = result
return result
return Expressions.cache[cacheKey] = await this.evaluateUncached(name, args)
}

private async evaluateUncached(name: string, args: any[]): Promise<string> {
Expand All @@ -35,6 +32,7 @@ export default class Expressions {
if (name === 'user_id') return process.getuid().toString()
if (name === 'dasherize') return dasherize(args[0])
if (name === 'test') return this.test(args[0], args[1]).toString()
if (name === 'prompt') return await this.prompt(args[0], args[1])

this.staxfile.warnings.add(`Invalid template expression: ${name}`)
}
Expand All @@ -43,6 +41,18 @@ export default class Expressions {
return `${this.staxfile.context}:${this.staxfile.app}:${name}:${JSON.stringify(args)}`
}

private async prompt(message: string, defaultValue: string): Promise<string> {
const response = await inquirer.prompt([
{
type: 'input',
name: 'result',
message,
default: defaultValue,
},
])
return response.result
}

private fetchConfigValue(name: string): string {
const key = name.slice(5) // strip 'stax.' prefix

Expand Down
18 changes: 13 additions & 5 deletions src/staxfile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ export default class Staxfile {
this.warnings = new Set<string>()
this.compose = yaml.load(readFileSync(this.staxfile, 'utf-8'))

this.compose.stax = await this.renderCompose(this.compose.stax)
this.config = new Config({ ...this.config, ...this.compose.stax })
this.compose = await this.renderCompose()

this.compose = await this.renderCompose(this.compose)
this.updateServices()
this.compose = await this.renderCompose() // need to re-render after updating services since template expressions may have been added
this.compose = await this.renderCompose(this.compose) // Re-render after updating services
// console.log(this.compose); console.log(this.config);process.exit()

if (this.generatedWarnings.length > 0)
exit(1, this.generatedWarnings.join('\n'))
Expand All @@ -91,8 +94,8 @@ export default class Staxfile {
return [...this.warnings]
}

private async renderCompose(): Promise<Record<string, any>> {
const renderedYaml = await this.render(yaml.dump(this.compose, { lineWidth: -1 }))
private async renderCompose(attributes: Record<string, any>): Promise<Record<string, any>> {
const renderedYaml = await this.render(yaml.dump(attributes, { lineWidth: -1 }))
return yaml.load(renderedYaml)
}

Expand All @@ -101,7 +104,12 @@ export default class Staxfile {

text = await renderTemplate(text, async (name, args) => {
matches += 1
return await this.expressions.evaluate(name, args)

const x = await this.expressions.evaluate(name, args)
if (name == 'prompt') {
console.log(name, args, x)
}
return x
})

return matches > 0 ? await this.render(text) : text
Expand Down

0 comments on commit 963bd8d

Please sign in to comment.