Skip to content

Commit

Permalink
feat(generators/app): se agrega la pregunta runPackageScripts
Browse files Browse the repository at this point in the history
  • Loading branch information
cristopher1 committed Apr 5, 2024
1 parent 00a0dc4 commit 9eb4a35
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export default class GeneratorKoa2ApiGenerator extends Generator {
description: 'Select a database driver.',
required: false,
})

this.option('runPackageScripts', {
type: Boolean,
description:
'Do you want to automatically run the scripts that configure the project, then installing the dependencies?',
default: false,
required: false,
})
}

initializing() {
Expand Down Expand Up @@ -64,6 +72,22 @@ export default class GeneratorKoa2ApiGenerator extends Generator {
},
],
},
{
type: 'list',
name: 'runPackageScripts',
message: `Do you want to automatically run the scripts that configure the package, then installing the dependencies?`,
choices: [
{
name: 'yes',
value: true,
},
{
name: 'no',
value: false,
},
],
when: () => !this.options.runPackageScripts,
},
]

const answers = await this.prompt(prompts)
Expand All @@ -73,6 +97,8 @@ export default class GeneratorKoa2ApiGenerator extends Generator {
this.#answers = {
projectName: this.options.projectName,
databaseName: DataProcessor.filterDatabaseName(databaseName),
runPackageScripts:
this.options.runPackageScripts || answers.runPackageScripts || false,
}
}

Expand Down Expand Up @@ -205,6 +231,32 @@ export default class GeneratorKoa2ApiGenerator extends Generator {
this.packageJson.merge(packageJsonContent)
}

#getDependencyManager(dependencyManagers) {
for (const dependencyManager of dependencyManagers) {
this.log(`Find ${dependencyManager}`)
const isAvailable = this.#dependencyManagerAvailable(dependencyManager)
if (isAvailable) {
return dependencyManager
}
}
}

#dependencyManagerAvailable(name) {
try {
this.spawnSync(`${name}`, ['--version'])
return true
} catch (err) {
return false
}
}

#runPackageScripts(dependencyManager) {
console.log('\n********** Run scripts from package.json **********')
const scriptArguments = [['init'], ['format:fix']]
for (const args of scriptArguments)
this.spawnSync(`${dependencyManager}`, ['run', ...args])
}

#runGoodBye() {
this.log('\n')
this.log('****************************************************')
Expand All @@ -220,6 +272,16 @@ export default class GeneratorKoa2ApiGenerator extends Generator {
}

end() {
const dependencyManagers = ['yarn', 'npm']
const { runPackageScripts } = this.#answers

if (runPackageScripts) {
const dependencyManager = this.#getDependencyManager(dependencyManagers)
this.log(`using ${dependencyManager}`)

this.#runPackageScripts(dependencyManager)
}

this.#runGoodBye()
}
}

0 comments on commit 9eb4a35

Please sign in to comment.