From f9589bbdf930f17e93096b00f075b8454211c6d2 Mon Sep 17 00:00:00 2001 From: Emile Rolley Date: Tue, 1 Oct 2024 17:40:31 +0200 Subject: [PATCH] feat(commands/init): add the --pkg-manager flag --- package.json | 3 +-- src/commands/init.ts | 28 ++++++++++++++++++++++------ test/commands/base.test.ts | 3 +-- test/commands/init.test.ts | 3 ++- yarn.lock | 23 +++++++++++------------ 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 5bf2ce3..a3c344f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commands/init.ts b/src/commands/init.ts index cf2ddb4..f1c26d2 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -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 = {} @@ -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, + } public async run(): Promise { p.intro(chalk.bgHex('#2975d1')(' publicodes init ')) + const { flags } = await this.parse(Init) const pkgJSON = getPackageJson() if (pkgJSON) { @@ -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!') @@ -116,8 +134,6 @@ function askPackageJsonInfo(): Promise { ) } -type PackageManager = 'npm' | 'yarn' | 'pnpm' | 'bun' - function findPackageManager(): PackageManager | undefined { if (fs.existsSync('yarn.lock')) { return 'yarn' diff --git a/test/commands/base.test.ts b/test/commands/base.test.ts index 394f498..32578f8 100644 --- a/test/commands/base.test.ts +++ b/test/commands/base.test.ts @@ -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') }) }) }) diff --git a/test/commands/init.test.ts b/test/commands/init.test.ts index 4754524..cd3f39e 100644 --- a/test/commands/init.test.ts +++ b/test/commands/init.test.ts @@ -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 () => { @@ -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') expect(stdout).toContain('package.json file written') diff --git a/yarn.lock b/yarn.lock index 61596f9..2c9d851 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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/rollup-android-arm-eabi@4.18.0": version "4.18.0" resolved "https://registry.yarnpkg.com/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.18.0.tgz#bbd0e616b2078cd2d68afc9824d1fadb2f2ffd27" @@ -2125,15 +2136,3 @@ yn@3.1.1: 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==