@vtex/fsp-cli
Version:
A VTEX CLI
103 lines (90 loc) • 2.74 kB
text/typescript
import { Args, Command } from '@oclif/core'
import { type LoadedModule, loadModules } from '../modules.js'
type HookParams = {
modules: LoadedModule[]
account: string
}
export class Build extends Command {
static args = {
account: Args.string({
required: true,
description:
'Store name key to be considered. It must match the keys on faststore.json, otherwise the first one found will be used.',
}),
moduleList: Args.string({
required: false,
description: 'Modules to build. Separated by comma (,)',
}),
}
static description = 'Initiates the build process for the storefront project.'
static examples = ['<%= config.bin %> <%= command.id %>']
async run(): Promise<void> {
this.log('Running faststore build 🚀')
const {
args: { account, moduleList },
flags,
} = await this.parse(Build)
const modules = await loadModules(
account,
moduleList
? (currentModule: string) =>
moduleList.split(',').includes(currentModule)
: () => true
)
try {
// Analysis is now handled by the module's preBuild hook
await this.runPreTasks({ modules, account })
await this.runMainTasks({ modules, account })
await this.runPostTasks({ modules, account })
} catch (error) {
/**
* TODO: How do we deal with errors?
* Should we log any specific feedback given the failed task?
*/
this.logToStderr('Something went wrong.')
console.log(error)
}
}
private async runMainTasks({ modules, account }: HookParams): Promise<void> {
try {
await Promise.all(
modules.map(({ loadedCli, path }) => {
const { build } = loadedCli.commands
return build.run([account, path])
})
)
} catch (error) {
// FIXME: Add error handling
console.log(error)
}
}
private async runPostTasks({ modules }: HookParams): Promise<void> {
try {
await Promise.all(
modules.map(({ loadedCli }) => {
const postBuild = loadedCli.hooks?.postBuild
if (postBuild) return postBuild()
return Promise.resolve()
})
)
} catch (error) {
// FIXME: Add error handling
console.log(error)
}
}
private async runPreTasks({ modules }: HookParams): Promise<void> {
try {
await Promise.all(
modules.map(({ loadedCli }) => {
const preBuild = loadedCli.hooks?.preBuild
if (preBuild) return preBuild()
return Promise.resolve()
})
)
} catch (error) {
// FIXME: Add error handling
this.logToStderr('Something went wrong.')
console.log(error)
}
}
}