Skip to content

Commit

Permalink
refactor: use kernel.exec over a child process to execute sub commands
Browse files Browse the repository at this point in the history
The make:migration command optionally prompts the user to select the migration directory and fails to do so inside a child process and hence we must use the kernel.exec to execute the command within the same process
  • Loading branch information
urishabh11 authored Nov 8, 2021
1 parent c74ee5b commit a544633
Showing 1 changed file with 5 additions and 23 deletions.
28 changes: 5 additions & 23 deletions commands/MakeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
*/

import { join } from 'path'
import { promisify } from 'util'
import { execFile as childProcessExec } from 'child_process'
import { BaseCommand, args, flags } from '@adonisjs/core/build/standalone'

const exec = promisify(childProcessExec)

export default class MakeModel extends BaseCommand {
public static commandName = 'make:model'
public static description = 'Make a new Lucid model'
Expand Down Expand Up @@ -45,24 +41,10 @@ export default class MakeModel extends BaseCommand {
public controller: boolean

/**
* Executes a given command
* This command loads the application
*/
private async execCommand(command: string, commandArgs: string[]) {
const { stdout, stderr } = await exec(command, commandArgs, {
env: {
...process.env,
FORCE_COLOR: 'true',
},
})

if (stdout) {
console.log(stdout.trim())
}

if (stderr) {
console.log(stderr.trim())
throw new Error(`Command "${command}" failed`)
}
public static settings = {
loadApp: true,
}

/**
Expand All @@ -81,11 +63,11 @@ export default class MakeModel extends BaseCommand {
.appRoot(this.application.cliCwd || this.application.appRoot)

if (this.migration) {
await this.execCommand('node', ['ace', 'make:migration', this.name])
await this.kernel.exec('make:migration', [this.name])
}

if (this.controller) {
await this.execCommand('node', ['ace', 'make:controller', this.name, '--resource'])
await this.kernel.exec('make:controller', [this.name, '--resource'])
}

await this.generator.run()
Expand Down

0 comments on commit a544633

Please sign in to comment.