Skip to content

Commit

Permalink
feat(commands/init): add the --pkg-manager flag
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Oct 1, 2024
1 parent af5a39c commit f9589bb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
"glob": "^10.4.1",
"path": "^0.12.7",
"publicodes": "^1.3.3",
"yaml": "^2.4.5",
"yocto-spinner": "^0.1.0"
"yaml": "^2.4.5"
},
"devDependencies": {
"@oclif/test": "^4.0.9",
Expand Down
28 changes: 22 additions & 6 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import fs from 'fs'
import path from 'path'
import { execSync } from 'node:child_process'
import { Command } from '@oclif/core'
import { Command, Flags } from '@oclif/core'
import * as p from '@clack/prompts'
import chalk from 'chalk'
import yoctospinner from 'yocto-spinner'

import { basePackageJson, getPackageJson, PackageJson } from '../utils/pjson'
import { OptionFlag } from '@oclif/core/lib/interfaces'

type PackageManager = 'npm' | 'yarn' | 'pnpm' | 'bun'

export default class Init extends Command {
static override args = {}
Expand All @@ -27,11 +29,24 @@ manager. Otherwise, it will update the existing package.json file.
},
]

static override flags = {}
static override flags = {
'pkg-manager': Flags.string({
char: 'p',
summary: 'The package manager to use',
description: `
The package manager that will be used to install dependencies. If not provided,
the command will try to detect the package manager based on the lock files
present in the project directory, otherwise it will prompt the user to choose
one.
`,
options: ['npm', 'yarn', 'pnpm', 'bun'],
}) as OptionFlag<PackageManager | undefined>,
}

public async run(): Promise<void> {
p.intro(chalk.bgHex('#2975d1')(' publicodes init '))

const { flags } = await this.parse(Init)
const pkgJSON = getPackageJson()

if (pkgJSON) {
Expand All @@ -43,7 +58,10 @@ manager. Otherwise, it will update the existing package.json file.
this.updatePackageJson(pjson)
}

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

p.outro('🚀 publicodes is ready to use!')
Expand Down Expand Up @@ -116,8 +134,6 @@ function askPackageJsonInfo(): Promise<PackageJson> {
)
}

type PackageManager = 'npm' | 'yarn' | 'pnpm' | 'bun'

function findPackageManager(): PackageManager | undefined {
if (fs.existsSync('yarn.lock')) {
return 'yarn'
Expand Down
3 changes: 1 addition & 2 deletions test/commands/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ describe('publicodes --help', () => {

runInDir('tmp', async () => {
const { stdout } = await cli.execCommand('--help')

expect(stdout).toContain('init initialize a new project')
expect(stdout).toContain('init Initialize a new project')

Check failure on line 9 in test/commands/base.test.ts

View workflow job for this annotation

GitHub Actions / main

Unhandled error

AssertionError: expected '' to contain 'init Initialize a new project' - Expected + Received - init Initialize a new project ❯ test/commands/base.test.ts:9:22 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ Module.runInDir test/cli-utils.ts:91:5 This error originated in "test/commands/base.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "test/commands/base.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
})
})
})
3 changes: 2 additions & 1 deletion test/commands/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CLIExecutor, runInDir } from '../cli-utils'
import fs from 'fs'
import { PackageJson } from '../../src/utils/pjson'
import path from 'path'
import {} from '@oclif/test'

describe('publicodes init', () => {
it('should update existing package.json', async () => {
Expand All @@ -11,7 +12,7 @@ describe('publicodes init', () => {
runInDir('tmp', async (cwd) => {
execSync('yarn init -y')

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

expect(stdout).toContain('existing package.json file')

Check failure on line 17 in test/commands/init.test.ts

View workflow job for this annotation

GitHub Actions / main

Unhandled error

AssertionError: expected '' to contain 'existing package.json file' - Expected + Received - existing package.json file ❯ test/commands/init.test.ts:17:22 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ Module.runInDir test/cli-utils.ts:91:5 This error originated in "test/commands/init.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "test/commands/init.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
expect(stdout).toContain('package.json file written')
Expand Down
23 changes: 11 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,17 @@
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==

"@publicodes/tools@^1.2.5":
version "1.2.5"
resolved "https://registry.yarnpkg.com/@publicodes/tools/-/tools-1.2.5.tgz#ab3177a027d530718057600f78512473a392d8e7"
integrity sha512-Cv2nhqd8ucKhFNi6g9aiUqV+PghAdZXk0ECmzXUQjr7AWYVoQZo+/b6rnGcR5uuwg3HOoYqRY8sUenvz80I2sg==
dependencies:
"@types/node" "^18.11.18"
glob "^10.4.1"
path "^0.12.7"
publicodes "^1.3.3"
yaml "^2.4.5"

"@rollup/[email protected]":
version "4.18.0"
resolved "https://registry.yarnpkg.com/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.18.0.tgz#bbd0e616b2078cd2d68afc9824d1fadb2f2ffd27"
Expand Down Expand Up @@ -2125,15 +2136,3 @@ [email protected]:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

yocto-spinner@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-spinner/-/yocto-spinner-0.1.0.tgz#a6579796928a179acbe7a0ffd697e2df7d7a8a4c"
integrity sha512-sBra0N4uhNn5UibnOz/HJxB1a0tzZ5zXbqnDe+tfRR3BGy+BmOrzrnQCZRJI7C++JiSZaPygPeNon4QCUmMQ4g==
dependencies:
yoctocolors "^2.1.1"

yoctocolors@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/yoctocolors/-/yoctocolors-2.1.1.tgz#e0167474e9fbb9e8b3ecca738deaa61dd12e56fc"
integrity sha512-GQHQqAopRhwU8Kt1DDM8NjibDXHC8eoh1erhGAJPEyveY9qqVeXvVikNKrDz69sHowPMorbPUrH/mx8c50eiBQ==

0 comments on commit f9589bb

Please sign in to comment.