Skip to content

Commit

Permalink
feat(command/init): add --yes flag
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Oct 3, 2024
1 parent 9566a25 commit af207df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
34 changes: 18 additions & 16 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ one.
specified package manager (or the detected one). Use this flag to skip the
installation.`,
}),
install: Flags.boolean({
char: 'i',
summary: 'Install dependencies.',
hidden: true,
yes: Flags.boolean({
char: 'y',
summary: 'Skip all prompts and use the default values.',
}),
}

Expand All @@ -69,28 +68,33 @@ installation.`,

const { flags } = await this.parse(Init)
let pkgJSON = getPackageJson()
const currentDir = path.basename(process.cwd())

if (pkgJSON) {
p.log.info(`Updating existing ${chalk.bold('package.json')} file`)
this.updatePackageJson(pkgJSON)
} else if (flags.yes) {
p.log.step(
`Creating a new ${chalk.bold('package.json')} file with default values`,
)
pkgJSON = basePackageJson
pkgJSON.name = currentDir
} else {
p.log.step(`Creating a new ${chalk.bold('package.json')} file`)
pkgJSON = await askPackageJsonInfo()
this.updatePackageJson(pkgJSON)
pkgJSON = await askPackageJsonInfo(currentDir)
}
this.updatePackageJson(pkgJSON)

const pkgManager: PackageManager =
flags['pkg-manager'] ??
findPackageManager() ??
(await askPackageManager())
(flags.yes ? 'npm' : await askPackageManager())

const shouldInstall =
flags.install ||
(flags['no-install'] === undefined
flags['no-install'] === undefined && !flags.yes
? await p.confirm({
message: 'Do you want to install the dependencies?',
})
: !flags['no-install'])
: !flags['no-install']

if (shouldInstall) {
await installDeps(pkgManager)
Expand Down Expand Up @@ -149,9 +153,7 @@ installation.`,
}
}

function askPackageJsonInfo(): Promise<PackageJson> {
const currentDir = path.basename(process.cwd())

function askPackageJsonInfo(currentDir: string): Promise<PackageJson> {
return p.group(
{
name: () =>
Expand Down Expand Up @@ -322,8 +324,8 @@ ${pkgManager} run doc
`
}

const BASE_PUBLICODES = `# Règles d'exemples automatiquement générées
# Supprimez ce fichier ou ajoutez vos propres règles
const BASE_PUBLICODES = `# Règles d'exemples automatiquement générées.
# Supprimez ce fichier ou ajoutez vos propres règles.
salaire net: salaire brut - cotisations salariales
Expand Down
8 changes: 4 additions & 4 deletions test/commands/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('publicodes init', () => {
runInDir('tmp', async (cwd) => {
execSync('yarn init -y')

const { stdout } = await cli.execCommand('init -p yarn --install')
const { stdout } = await cli.execCommand('init -y -p yarn')

expect(stdout).toContain('Updating existing package.json file')
expect(stdout).toContain('package.json file written')
Expand All @@ -35,7 +35,7 @@ describe('publicodes init', () => {
runInDir('tmp', async (cwd) => {
execSync('yarn init -y')

const { stdout } = await cli.execCommand('init -p yarn --no-install')
const { stdout } = await cli.execCommand('init -y --no-install -p yarn')

expect(stdout).toContain('Updating existing package.json file')
expect(stdout).toContain('package.json file written')
Expand All @@ -47,8 +47,8 @@ describe('publicodes init', () => {
getExpectedBasePackageJson(cwd, packageJson),
)

expect(!fs.existsSync('node_modules')).toBe(true)
expect(!fs.existsSync('yarn.lock')).toBe(true)
expect(fs.existsSync('node_modules')).toBe(false)
expect(fs.existsSync('yarn.lock')).toBe(false)
expect(fs.existsSync('README.md')).toBe(true)
expect(fs.existsSync('src/base.publicodes')).toBe(true)
})
Expand Down

0 comments on commit af207df

Please sign in to comment.