diff --git a/src/commands/add/prettier.ts b/src/commands/add/prettier.ts index e6199de..6440526 100644 --- a/src/commands/add/prettier.ts +++ b/src/commands/add/prettier.ts @@ -49,45 +49,12 @@ hello world from ./src/hello.ts! ], {onCancel: () => this.error('User canceled prompt.')}) if (shouldCommit) { - const {config} = await this.getConfig() - if (!config?.git?.user) { - this.error('Missing config key git.user for git commits') - } - if (!config?.git?.email) { - this.error('Missing config key git.email for git commits') - } - await gitConfig({ - dir: '.', - path: 'user.name', - value: config?.git?.user, - }) - await gitConfig({ - dir: '.', - path: 'user.email', - value: config?.git?.email, - }) - const changes = (await statusMatrix({dir: '.', pattern: '**'})) - .filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1)) - if (changes.length > 0) { - this.error('There is unsaved changed in the git repository, aborting') - } - } - - const addDevDependency = async (name: string): Promise => { - await this.runWithSpinner(`Adding ${name} dependency`, async () => { - const versionToInstall = await latestVersion(name) - await system.exec(`yarn add -D ${name}@${versionToInstall}`) - if (shouldCommit) { - await add({filepath: 'package.json', dir: '.'}) - await add({filepath: 'yarn.lock', dir: '.'}) - await commit({dir: '.', message: `:heavy_plus_sign: add ${name}@${versionToInstall}`}) - } - }) + this.initGit() } - await addDevDependency('prettier') - await addDevDependency('husky') - await addDevDependency('pretty-quick') + await this.addDevDependency('prettier', shouldCommit) + await this.addDevDependency('husky', shouldCommit) + await this.addDevDependency('pretty-quick', shouldCommit) await this.runWithSpinner('Adding package.json scripts', async () => { const packageJsonWithDeps = filesystem.read(packagePath, 'json') @@ -146,5 +113,89 @@ hello world from ./src/hello.ts! await commit({dir: '.', message: ':art: apply prettier style to project'}) } }) + + await this.handleMaybeEslint(shouldCommit) + } + + async initGit(): Promise { + const {config} = await this.getConfig() + if (!config?.git?.user) { + this.error('Missing config key git.user for git commits') + } + if (!config?.git?.email) { + this.error('Missing config key git.email for git commits') + } + await gitConfig({ + dir: '.', + path: 'user.name', + value: config?.git?.user, + }) + await gitConfig({ + dir: '.', + path: 'user.email', + value: config?.git?.email, + }) + const changes = (await statusMatrix({dir: '.', pattern: '**'})) + .filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1)) + if (changes.length > 0) { + this.error('There is unsaved changed in the git repository, aborting') + } + } + + async addDevDependency(name: string, shouldCommit: boolean): Promise { + await this.runWithSpinner(`Adding ${name} dependency`, async () => { + const versionToInstall = await latestVersion(name) + await system.exec(`yarn add -D ${name}@${versionToInstall}`) + if (shouldCommit) { + await add({filepath: 'package.json', dir: '.'}) + await add({filepath: 'yarn.lock', dir: '.'}) + await commit({dir: '.', message: `:heavy_plus_sign: add ${name}@${versionToInstall}`}) + } + }) + } + + async handleMaybeEslint(shouldCommit: boolean) { + const eslintPath = filesystem.path('.', '.eslintrc') + const hasEslintFile = filesystem.isFile(eslintPath) + + if (!hasEslintFile) { + return + } + + const {shouldOverrideEslint} = await prompts( + [ + { + type: 'confirm', + message: 'Eslint found in the project, do you want to add eslint prettier config ?', + name: 'shouldOverrideEslint', + initial: true, + }, + ], + {onCancel: () => this.error('User canceled prompt.')}, + ) + + if (!shouldOverrideEslint) { + return + } + await this.addDevDependency('eslint-config-prettier', shouldCommit) + await this.addDevDependency('eslint-plugin-prettier', shouldCommit) + + await this.runWithSpinner('Updating .eslintrc', async () => { + const eslintConfig = filesystem.read(eslintPath, 'json') + const finalEslintConfig = { + ...eslintConfig, + extends: [ + ...eslintConfig.extends, + 'plugin:prettier/recommended', + ], + } + + await filesystem.write(eslintPath, finalEslintConfig, {jsonIndent: 2}) + + if (shouldCommit) { + await add({filepath: '.eslintrc', dir: '.'}) + await commit({dir: '.', message: ':wrench: update eslint to use prettier'}) + } + }) } }