From 92baef5270b13a3cff37ffa59267d9f8b938f713 Mon Sep 17 00:00:00 2001 From: Emile Rolley Date: Thu, 3 Oct 2024 11:26:17 +0200 Subject: [PATCH] refactor(command/init): try to have a simplier run function --- src/commands/compile.ts | 4 +-- src/commands/init.ts | 55 +++++++++++++++++++++++++------------- src/utils/pjson.ts | 6 ++--- test/commands/init.test.ts | 2 -- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/commands/compile.ts b/src/commands/compile.ts index 4dcf9fa..2baae5a 100644 --- a/src/commands/compile.ts +++ b/src/commands/compile.ts @@ -8,7 +8,7 @@ import { RawRules } from '../commons' import { exitWithError, runWithSpinner } from '../utils/cli' import { resolveRuleTypes, RuleType } from '../compilation/ruleTypes' import Engine from 'publicodes' -import { getPackageJson } from '../utils/pjson' +import { readPackageJson } from '../utils/pjson' export default class Compile extends Command { static override args = { @@ -76,7 +76,7 @@ the package.json file under the \`publicodes\` key. For example: const rawRules = await parseFiles(filesToCompile, { verbose: false }) const engine = await initEngine(rawRules) - const pkgName = getPackageJson()?.name ?? path.basename(process.cwd()) + const pkgName = readPackageJson()?.name ?? path.basename(process.cwd()) // Create output directory if it doesn't exist if (!fs.existsSync(outputDir)) { diff --git a/src/commands/init.ts b/src/commands/init.ts index f66a28b..90013dc 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -11,7 +11,7 @@ import { runWithSpinner, Spinner, } from '../utils/cli' -import { basePackageJson, getPackageJson, PackageJson } from '../utils/pjson' +import { basePackageJson, PackageJson, readPackageJson } from '../utils/pjson' import { OptionFlag } from '@oclif/core/lib/interfaces' import { spawn } from 'child_process' @@ -67,27 +67,13 @@ installation.`, p.intro(chalk.bgHex('#2975d1')(' publicodes init ')) const { flags } = await this.parse(Init) - let pkgJSON = getPackageJson() const currentDir = path.basename(process.cwd()) + const pkgJSON = await getPackageJson(currentDir, flags.yes) - if (pkgJSON) { - p.log.info(`Updating existing ${chalk.bold('package.json')} file`) - } 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(currentDir) - } this.updatePackageJson(pkgJSON) - const pkgManager: PackageManager = - flags['pkg-manager'] ?? - findPackageManager() ?? - (flags.yes ? 'npm' : await askPackageManager()) + const pkgManager = await getPackageManager(flags['pkg-manager'], flags.yes) + // const extraTools = await getExtraTools(flags.yes) const shouldInstall = flags['no-install'] === undefined && !flags.yes @@ -131,6 +117,7 @@ installation.`, } pkgJSON.devDependencies = { ...pkgJSON.devDependencies, + // NOTE: to test with the packaged version '@publicodes/tools': `^${this.config.pjson.version}`, } pkgJSON.scripts = { @@ -153,6 +140,38 @@ installation.`, } } +async function getPackageJson( + currentDir: string, + useDefault: boolean, +): Promise { + const localPkgJson = readPackageJson() + + if (localPkgJson) { + return localPkgJson + } + + if (useDefault) { + return { ...basePackageJson, name: currentDir } + } + + return await askPackageJsonInfo(currentDir) +} + +async function getPackageManager( + flagedPkgManager: PackageManager | undefined, + useDefault: boolean, +): Promise { + if (flagedPkgManager) { + return flagedPkgManager + } + const currentPkgManager = findPackageManager() + if (currentPkgManager) { + return currentPkgManager + } + + return useDefault ? 'npm' : await askPackageManager() +} + function askPackageJsonInfo(currentDir: string): Promise { return p.group( { diff --git a/src/utils/pjson.ts b/src/utils/pjson.ts index cb5e93b..d2cf1e3 100644 --- a/src/utils/pjson.ts +++ b/src/utils/pjson.ts @@ -51,10 +51,10 @@ export const basePackageJson: PackageJson = { }, } -export function getPackageJson(): PackageJson | undefined { +export function readPackageJson(): PackageJson | undefined { try { - return JSON.parse(fs.readFileSync('package.json', 'utf8')) - } catch (error) { + return JSON.parse(fs.readFileSync('package.json', 'utf-8')) + } catch (e) { return undefined } } diff --git a/test/commands/init.test.ts b/test/commands/init.test.ts index 94c6741..e2af78d 100644 --- a/test/commands/init.test.ts +++ b/test/commands/init.test.ts @@ -13,7 +13,6 @@ describe('publicodes init', () => { const { stdout } = await cli.execCommand('init -y -p yarn') - expect(stdout).toContain('Updating existing package.json file') expect(stdout).toContain('package.json file written') expect(stdout).toContain('Dependencies installed') expect(stdout).toContain('Files generated') @@ -37,7 +36,6 @@ describe('publicodes init', () => { 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') expect(stdout).toContain('Files generated') expect(stdout).toContain('New to Publicodes?')