Skip to content

Commit

Permalink
Saving options in .particle-rc (#924)
Browse files Browse the repository at this point in the history
* Saving options in .particle-rc

* Moving config write to the `writing()` function

* Initializing cliVersion and removing log
  • Loading branch information
mealeyst authored Jul 31, 2020
1 parent 336b859 commit c949d93
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
26 changes: 25 additions & 1 deletion packages/generator-particle-base/src/generators/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ module.exports = class extends Generator {
// configuration will come from the constructor argument
configuration: Answers
packageJson: Record<string, any>
cliVersion = ''

constructor(args: any, opts: any) {
super(args, opts)
for (let i = 0; i < args.length; i++) {
const value = args[i]
const regex = /(cli-version=)(\d.*)/
const matches = regex.exec(value)
if (matches) {
this.cliVersion = matches[2]
}
}
// makes config a required argument
this.option('configuration', {
type: String,
Expand Down Expand Up @@ -64,6 +73,21 @@ module.exports = class extends Generator {
)
}

/**
* Creeate a particle config file
*/

_writeParticleConfig() {
fs.writeFileSync(
'.particle-rc',
JSON.stringify(
{ ...this.configuration, ...{ 'cli-version': this.cliVersion } },
null,
2
)
)
}

async _promptUser() {
// Initialize storybook
const results: ConfigurationAnswers = await this.prompt(configurationPrompt)
Expand All @@ -82,7 +106,6 @@ module.exports = class extends Generator {
options: configOptions[results.config],
}
}

this.packageJson.name = results.projectName
}

Expand Down Expand Up @@ -111,6 +134,7 @@ module.exports = class extends Generator {

writing() {
this._createPackageJson()
this._writeParticleConfig()

// Installs all dependencies
this.npmInstall()
Expand Down
14 changes: 11 additions & 3 deletions packages/particle-cli/bin/particle-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ program
.description('Scaffold your project from a set of prompts.')
.action(function () {
// runs yeoman under the hood and resolves the yeoman module directly
spawn('yo', [require.resolve('@phase2/generator-particle-base')], {
stdio: 'inherit',
})
spawn(
'yo',
[
require.resolve('@phase2/generator-particle-base'),
`cli-version=${pkg.version}`,
'example-arg',
],
{
stdio: 'inherit',
}
)
})

// allow commander to parse `process.argv`
Expand Down

0 comments on commit c949d93

Please sign in to comment.