From 9e6b7c8a2a67c47977b4aaa09f33a66550f69e55 Mon Sep 17 00:00:00 2001 From: Hung Luu Date: Wed, 27 Dec 2023 14:54:40 +0700 Subject: [PATCH 1/4] fix(builder): add package manager option --- packages/builder/src/cmd/init.ts | 23 +++++++++------- .../builder/src/cmd/init/installPackages.ts | 9 ++++--- .../builder/src/cmd/init/updatePackageJson.ts | 7 ++--- packages/builder/src/cmd/options.ts | 4 +++ packages/builder/src/lib/packages.ts | 26 +++++++------------ 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/packages/builder/src/cmd/init.ts b/packages/builder/src/cmd/init.ts index 7b48513..3917420 100644 --- a/packages/builder/src/cmd/init.ts +++ b/packages/builder/src/cmd/init.ts @@ -1,5 +1,5 @@ import { loggers } from '@abxvn/logger/cli' -import { path } from './options' +import { path, pm, pmChoices } from './options' import { getConfigs } from '../configs' import { ask, @@ -10,12 +10,13 @@ import { type IEditorConfigsAnswer, editorConfigs, } from './questions' -import { YARN_ENABLED, getPnpmVersion } from '../lib/packages' +import { IInstallOptions, getVersion } from '../lib/packages' import { installPackages } from './init/installPackages' import { copyConfigs } from './init/copyConfigs' import { updatePackageJson } from './init/updatePackageJson' import type { IApp } from '../interfaces' import { logSuccess } from './init/loggers' +import { argv0 } from 'process' interface IAnswers { components: IComponentAnswer @@ -31,17 +32,20 @@ const init = async function (this: IApp, options: any) { }, }) - await checkVersion() + if (!options.pm && pmChoices.includes(argv0)) options.pm = argv0 + if (!options.pm) options.pm = 'pnpm' + + await checkVersion(options.pm) const answers = await ask({ components, editorConfigs, - ...YARN_ENABLED ? { sdk } : undefined, + ...(options.pm === 'yarn' && { sdk }), }) - await installPackages({ answers, deps }, this) + await installPackages({ answers, deps, pm: options.pm }, this) await copyConfigs({ answers, deps, editor }) - await updatePackageJson({ editor, deps }) + await updatePackageJson({ editor, deps, pm: options.pm }) logSuccess('done') @@ -54,11 +58,10 @@ export default { action: init, options: [ path, + pm, ], } -const checkVersion = async () => { - const pnpmVersion = await getPnpmVersion() - - loggers.info('Versions:', 'node', process.versions.node, 'pnpm', pnpmVersion) +const checkVersion = async (pm: IInstallOptions['pm'] = 'pnpm') => { + loggers.info('Versions:', 'node', process.versions.node, pm, await getVersion(pm)) } diff --git a/packages/builder/src/cmd/init/installPackages.ts b/packages/builder/src/cmd/init/installPackages.ts index db00bff..eab0aa4 100644 --- a/packages/builder/src/cmd/init/installPackages.ts +++ b/packages/builder/src/cmd/init/installPackages.ts @@ -1,6 +1,6 @@ import { collapsible } from '@abxvn/logger/cli' import type { IApp, IConfigDeps } from '../../interfaces' -import { install, installSdk } from '../../lib/packages' +import { IInstallOptions, install, installSdk } from '../../lib/packages' import { type IComponentAnswer, components, @@ -11,8 +11,9 @@ import { logProgress, logStep } from './loggers' interface IInstallPackagesParams { answers: { components: IComponentAnswer, sdk?: ISdkAnswer } deps: IConfigDeps + pm?: IInstallOptions['pm'] } -export const installPackages = async ({ answers, deps }: IInstallPackagesParams, app: IApp) => { +export const installPackages = async ({ answers, deps, pm = 'pnpm' }: IInstallPackagesParams, app: IApp) => { deps.set('@abxvn/builder', { version: app.appVersion || '*' }) components.choices?.forEach(name => { if (!answers.components?.includes(name)) { @@ -41,14 +42,14 @@ export const installPackages = async ({ answers, deps }: IInstallPackagesParams, if (mainDependencies.length) { logProgress('install', mainDependencies.join(' ')) - await install(mainDependencies, { outputStream, errorStream }) + await install(mainDependencies, { outputStream, errorStream, pm }) outputStream.collapse(true) errorStream.collapse(true) } if (devDependencies.length) { logProgress('install dev', devDependencies.join(' ')) - await install(devDependencies, { dev: true, outputStream, errorStream }) + await install(devDependencies, { dev: true, outputStream, errorStream, pm }) outputStream.collapse(true) errorStream.collapse(true) } diff --git a/packages/builder/src/cmd/init/updatePackageJson.ts b/packages/builder/src/cmd/init/updatePackageJson.ts index 008b0c4..14ce573 100644 --- a/packages/builder/src/cmd/init/updatePackageJson.ts +++ b/packages/builder/src/cmd/init/updatePackageJson.ts @@ -2,7 +2,7 @@ import { loggers, styles } from '@abxvn/logger/cli' import { type IConfigEditor, type IConfigDeps } from '../../interfaces' import { pathExists, readJSON, writeJSON } from '../../lib/vendors' import { logProgress, logStep, logWarn } from './loggers' -import { YARN_ENABLED } from '../../lib/packages' +import { IInstallOptions } from '../../lib/packages' const { italic, bold } = styles const { info } = loggers @@ -11,8 +11,9 @@ interface IUpdatePackageJsonParams { modify?: boolean deps: IConfigDeps editor: IConfigEditor + pm?: IInstallOptions['pm'] } -export const updatePackageJson = async ({ modify = true, deps, editor }: IUpdatePackageJsonParams) => { +export const updatePackageJson = async ({ modify = true, deps, editor, pm }: IUpdatePackageJsonParams) => { const useEslint = deps.requires('eslint') const useJest = deps.requires('jest') @@ -70,7 +71,7 @@ export const updatePackageJson = async ({ modify = true, deps, editor }: IUpdate await writeJSON(packagePath, { ...json, scripts, - ...YARN_ENABLED ? workspaces : undefined, + ...(pm === 'yarn' && workspaces), }, { spaces: 2, }) diff --git a/packages/builder/src/cmd/options.ts b/packages/builder/src/cmd/options.ts index 07f6f21..203fe24 100644 --- a/packages/builder/src/cmd/options.ts +++ b/packages/builder/src/cmd/options.ts @@ -10,3 +10,7 @@ export const path = new Option('--path ', 'Specify root path for compilati export const production = new Option('--production', 'Production build') .default(false) + +export const pmChoices = ['pnpm', 'npm', 'yarn'] +export const pm = new Option('--pm ', 'Package manager') + .choices(pmChoices) diff --git a/packages/builder/src/lib/packages.ts b/packages/builder/src/lib/packages.ts index 4634c69..bcb4e28 100644 --- a/packages/builder/src/lib/packages.ts +++ b/packages/builder/src/lib/packages.ts @@ -3,8 +3,6 @@ import Module from 'module' import { resolve } from './paths' import { readFile } from './vendors' -export const YARN_ENABLED = false - const cliOptions = { env: { FORCE_COLOR: 'true', @@ -15,8 +13,8 @@ interface IWritable { write: (message: string) => void } -interface IInstallOptions { - tool?: 'pnpm' | 'yarn' | 'npm' +export interface IInstallOptions { + pm?: 'pnpm' | 'yarn' | 'npm' dev?: boolean outputStream?: IWritable errorStream?: IWritable @@ -30,12 +28,14 @@ export const install = async ( dev = false, outputStream = process.stdout, errorStream = process.stderr, - tool = 'pnpm', + pm = 'pnpm', } = options || {} - const subProcess = execa(tool, [ - tool !== 'npm' ? 'add' : 'install', - '--silent', + const subProcess = execa(pm, [ + pm !== 'npm' ? 'add' : 'install', + pm !== 'pnpm' ? '--silent' : '', + pm !== 'npm' ? '-w' : '', + pm === 'npm' ? '--save' : '', dev ? '-D' : '', ...packages, ].filter(Boolean), cliOptions) @@ -58,14 +58,8 @@ export const install = async ( await subProcess } -export const getYarnVersion = async (): Promise => { - const { stdout } = await execa('yarn', ['--version'], cliOptions) - - return stdout.match(/\d+(\.\d+)*/)?.[0] || '' -} - -export const getPnpmVersion = async (): Promise => { - const { stdout } = await execa('pnpm', ['--version'], cliOptions) +export const getVersion = async (program: string): Promise => { + const { stdout } = await execa(program, ['--version'], cliOptions) return stdout.match(/\d+(\.\d+)*/)?.[0] || '' } From 53e55cee277238620a75d0ee11c02b3ad8125112 Mon Sep 17 00:00:00 2001 From: Hung Luu Date: Wed, 27 Dec 2023 14:58:36 +0700 Subject: [PATCH 2/4] chore: update docs and changeset --- packages/builder/CHANGELOG.md | 6 ++++++ packages/builder/README.md | 8 ++++++++ packages/builder/package.json | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/builder/CHANGELOG.md b/packages/builder/CHANGELOG.md index 5c23a26..6763425 100644 --- a/packages/builder/CHANGELOG.md +++ b/packages/builder/CHANGELOG.md @@ -1,5 +1,11 @@ # @abxvn/builder +## 1.1.0 + +### Minor Changes + +- Add package manager optional option to init command (npm, pnpm, yarn) + ## 1.0.1 ### Patch Changes diff --git a/packages/builder/README.md b/packages/builder/README.md index ab9bcf0..5c148c7 100644 --- a/packages/builder/README.md +++ b/packages/builder/README.md @@ -29,6 +29,14 @@ npx @abxvn/builder init yarn dlx @abxvn/builder init ``` +Usage: @abxvn/builder init [options] +``` +Options: + --path Specify root path for compilation (default: "current folder") + --pm Optional package manager (choices: "pnpm", "npm", "yarn") + -h, --help display help for command +``` + ### Commands _Documentation coming soon_ diff --git a/packages/builder/package.json b/packages/builder/package.json index 22b27ca..65b12c9 100644 --- a/packages/builder/package.json +++ b/packages/builder/package.json @@ -1,6 +1,6 @@ { "name": "@abxvn/builder", - "version": "1.0.1", + "version": "1.1.0", "description": "Quick scaffolding code base to build web apps and games", "license": "MIT", "main": "./cli/index.js", From 66b5e242a9742b20cbbc4779a90f3a0b7139f736 Mon Sep 17 00:00:00 2001 From: Hung Luu Date: Wed, 27 Dec 2023 15:06:49 +0700 Subject: [PATCH 3/4] chore: update version in build --- packages/builder/CHANGELOG.md | 6 ++++++ packages/builder/package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/builder/CHANGELOG.md b/packages/builder/CHANGELOG.md index 6763425..6479b33 100644 --- a/packages/builder/CHANGELOG.md +++ b/packages/builder/CHANGELOG.md @@ -1,5 +1,11 @@ # @abxvn/builder +## 1.1.1 + +### Patch Changes + +- Fix version build + ## 1.1.0 ### Minor Changes diff --git a/packages/builder/package.json b/packages/builder/package.json index 65b12c9..929362a 100644 --- a/packages/builder/package.json +++ b/packages/builder/package.json @@ -1,6 +1,6 @@ { "name": "@abxvn/builder", - "version": "1.1.0", + "version": "1.1.1", "description": "Quick scaffolding code base to build web apps and games", "license": "MIT", "main": "./cli/index.js", From b43b8694ff76eba0253e45ae86a334841ee387e3 Mon Sep 17 00:00:00 2001 From: Hung Luu Date: Wed, 27 Dec 2023 15:09:29 +0700 Subject: [PATCH 4/4] fix: eslint --- packages/builder/src/cmd/init.ts | 2 +- packages/builder/src/cmd/init/installPackages.ts | 2 +- packages/builder/src/cmd/init/updatePackageJson.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/builder/src/cmd/init.ts b/packages/builder/src/cmd/init.ts index 3917420..19ebced 100644 --- a/packages/builder/src/cmd/init.ts +++ b/packages/builder/src/cmd/init.ts @@ -10,7 +10,7 @@ import { type IEditorConfigsAnswer, editorConfigs, } from './questions' -import { IInstallOptions, getVersion } from '../lib/packages' +import { type IInstallOptions, getVersion } from '../lib/packages' import { installPackages } from './init/installPackages' import { copyConfigs } from './init/copyConfigs' import { updatePackageJson } from './init/updatePackageJson' diff --git a/packages/builder/src/cmd/init/installPackages.ts b/packages/builder/src/cmd/init/installPackages.ts index eab0aa4..8020ca9 100644 --- a/packages/builder/src/cmd/init/installPackages.ts +++ b/packages/builder/src/cmd/init/installPackages.ts @@ -1,6 +1,6 @@ import { collapsible } from '@abxvn/logger/cli' import type { IApp, IConfigDeps } from '../../interfaces' -import { IInstallOptions, install, installSdk } from '../../lib/packages' +import { type IInstallOptions, install, installSdk } from '../../lib/packages' import { type IComponentAnswer, components, diff --git a/packages/builder/src/cmd/init/updatePackageJson.ts b/packages/builder/src/cmd/init/updatePackageJson.ts index 14ce573..bf69df5 100644 --- a/packages/builder/src/cmd/init/updatePackageJson.ts +++ b/packages/builder/src/cmd/init/updatePackageJson.ts @@ -2,7 +2,7 @@ import { loggers, styles } from '@abxvn/logger/cli' import { type IConfigEditor, type IConfigDeps } from '../../interfaces' import { pathExists, readJSON, writeJSON } from '../../lib/vendors' import { logProgress, logStep, logWarn } from './loggers' -import { IInstallOptions } from '../../lib/packages' +import { type IInstallOptions } from '../../lib/packages' const { italic, bold } = styles const { info } = loggers