Skip to content

Commit

Permalink
refactor(command/init): try to have a simplier run function
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Oct 3, 2024
1 parent af207df commit 92baef5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)) {
Expand Down
55 changes: 37 additions & 18 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -131,6 +117,7 @@ installation.`,
}
pkgJSON.devDependencies = {
...pkgJSON.devDependencies,
// NOTE: to test with the packaged version
'@publicodes/tools': `^${this.config.pjson.version}`,
}
pkgJSON.scripts = {
Expand All @@ -153,6 +140,38 @@ installation.`,
}
}

async function getPackageJson(
currentDir: string,
useDefault: boolean,
): Promise<PackageJson> {
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<PackageManager> {
if (flagedPkgManager) {
return flagedPkgManager
}
const currentPkgManager = findPackageManager()
if (currentPkgManager) {
return currentPkgManager
}

return useDefault ? 'npm' : await askPackageManager()
}

function askPackageJsonInfo(currentDir: string): Promise<PackageJson> {
return p.group(
{
Expand Down
6 changes: 3 additions & 3 deletions src/utils/pjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 0 additions & 2 deletions test/commands/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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?')
Expand Down

0 comments on commit 92baef5

Please sign in to comment.