Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: builder init package manager #21

Merged
merged 4 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @abxvn/builder

## 1.1.1

### Patch Changes

- Fix version build

## 1.1.0

### Minor Changes

- Add package manager optional option to init command (npm, pnpm, yarn)

## 1.0.1

### Patch Changes
Expand Down
8 changes: 8 additions & 0 deletions packages/builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ npx @abxvn/builder init
yarn dlx @abxvn/builder init
```

Usage: @abxvn/builder init [options]
```
Options:
--path <path> Specify root path for compilation (default: "current folder")
--pm <name> Optional package manager (choices: "pnpm", "npm", "yarn")
-h, --help display help for command
```

### Commands

_Documentation coming soon_
Expand Down
2 changes: 1 addition & 1 deletion packages/builder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@abxvn/builder",
"version": "1.0.1",
"version": "1.1.1",
"description": "Quick scaffolding code base to build web apps and games",
"license": "MIT",
"main": "./cli/index.js",
Expand Down
23 changes: 13 additions & 10 deletions packages/builder/src/cmd/init.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -10,12 +10,13 @@ import {
type IEditorConfigsAnswer,
editorConfigs,
} from './questions'
import { YARN_ENABLED, getPnpmVersion } 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'
import type { IApp } from '../interfaces'
import { logSuccess } from './init/loggers'
import { argv0 } from 'process'

interface IAnswers {
components: IComponentAnswer
Expand All @@ -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<IAnswers>({
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')

Expand All @@ -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))
}
9 changes: 5 additions & 4 deletions packages/builder/src/cmd/init/installPackages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { collapsible } from '@abxvn/logger/cli'
import type { IApp, IConfigDeps } from '../../interfaces'
import { install, installSdk } from '../../lib/packages'
import { type IInstallOptions, install, installSdk } from '../../lib/packages'
import {
type IComponentAnswer,
components,
Expand All @@ -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)) {
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions packages/builder/src/cmd/init/updatePackageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { type IInstallOptions } from '../../lib/packages'

const { italic, bold } = styles
const { info } = loggers
Expand All @@ -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')

Expand Down Expand Up @@ -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,
})
Expand Down
4 changes: 4 additions & 0 deletions packages/builder/src/cmd/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export const path = new Option('--path <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 <name>', 'Package manager')
.choices(pmChoices)
26 changes: 10 additions & 16 deletions packages/builder/src/lib/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -58,14 +58,8 @@ export const install = async (
await subProcess
}

export const getYarnVersion = async (): Promise<string> => {
const { stdout } = await execa('yarn', ['--version'], cliOptions)

return stdout.match(/\d+(\.\d+)*/)?.[0] || ''
}

export const getPnpmVersion = async (): Promise<string> => {
const { stdout } = await execa('pnpm', ['--version'], cliOptions)
export const getVersion = async (program: string): Promise<string> => {
const { stdout } = await execa(program, ['--version'], cliOptions)

return stdout.match(/\d+(\.\d+)*/)?.[0] || ''
}
Expand Down