Skip to content

Commit

Permalink
feat: support bun
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-jerry committed Feb 13, 2024
1 parent ddb308e commit 1b3c29e
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 15 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"homepage": "https://github.com/0x-jerry/onrm",
"license": "MIT",
"dependencies": {
"@iarna/toml": "^2.2.5",
"chalk": "^5.3.0",
"cli-highlight": "^2.1.11",
"inquirer": "^9.2.14",
Expand All @@ -38,6 +39,7 @@
},
"devDependencies": {
"@0x-jerry/x-release": "^0.4.0",
"@types/iarna__toml": "^2.0.5",
"@types/inquirer": "^9.0.7",
"@types/minimist": "^1.2.5",
"@types/shelljs": "^0.8.15",
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { getConfig, type ONRMConfig, ONRMRC, type RegistryConfig, saveConfig } from './config'
import inquirer from 'inquirer'
import { npm, RegistryManager, yarn } from './registryManager'
import { bun, npm, RegistryManager, yarn } from './registryManager'
import chalk from 'chalk'
import { printTable } from './print'
import highlight from 'cli-highlight'

const managers: Record<string, RegistryManager> = {
npm,
yarn
yarn,
bun
}

async function add(name: string, registry: string, homeUrl: string = '') {
Expand Down Expand Up @@ -88,14 +89,14 @@ function use(name: string, type?: 'npm' | 'yarn') {
}

if (type) {
managers[type].setConfig('registry', registryConf.registry)
managers[type].setRegistry(registryConf.registry)
console.log(`Set registry(${chalk.yellow(name)}) for [${chalk.green(type)}] successful!`)
return
}

for (const key in managers) {
const manager = managers[key]
manager.setConfig('registry', registryConf.registry)
manager.setRegistry(registryConf.registry)
}

console.log(
Expand All @@ -113,7 +114,7 @@ function _printRegistry(registries: ONRMConfig['registries']) {
const manager = managers[key]
used.push({
type: key,
registry: manager.getConfig('registry')
registry: manager.getRegistry()
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/registryManager/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import shelljs from 'shelljs'
export abstract class RegistryManager {
protected abstract checkIsExist(): boolean

abstract setConfig(key: string, value: string): boolean
abstract getConfig(key: string): string
abstract setRegistry(value: string): boolean
abstract getRegistry(): string
abstract getVersion(): string

private _isExist?: boolean
Expand Down
64 changes: 64 additions & 0 deletions src/registryManager/bun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { RegistryManager } from './base'
import shelljs from 'shelljs'
import fs from 'fs'
import os from 'os'
import path from 'path'
import toml from '@iarna/toml'

const CONFIG_FILE = '.bunfig.toml'

class Bun extends RegistryManager {
configPath = `${path.join(os.homedir(), CONFIG_FILE)}`

protected checkIsExist(): boolean {
return !!shelljs.which('bun')
}

setRegistry(value: string): boolean {
if (!this.isExist()) {
return false
} else {
const conf = this.readBunConfig()

conf.install ||= {}
conf.install.registry = value

this.saveBunConfig(conf)
return true
}
}

getRegistry(): string {
if (!this.isExist()) {
return ''
}

const conf = this.readBunConfig()

return conf.install?.registry || 'https://registry.npmjs.org/'
}

getVersion(): string {
return this.isExist() ? this.exec('bun --version') : ''
}

readBunConfig(): any {
try {
if (!fs.existsSync(this.configPath)) {
return {}
}

const file = fs.readFileSync(this.configPath, 'utf-8')
return toml.parse(file)
} catch (error) {
console.log(`Parse ${this.configPath} failed`, error)
return {}
}
}

saveBunConfig(config: any) {
fs.writeFileSync(this.configPath, toml.stringify(config))
}
}

export const bun = new Bun()
1 change: 1 addition & 0 deletions src/registryManager/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './base'
export * from './npm'
export * from './yarn'
export * from './bun'
8 changes: 4 additions & 4 deletions src/registryManager/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ class Npm extends RegistryManager {
return !!shelljs.which('npm')
}

setConfig(key: string, value: string): boolean {
setRegistry(value: string): boolean {
if (!this.isExist()) {
return false
} else {
this.exec(`npm config set ${key} ${value}`)
this.exec(`npm config set registry ${value}`)
return true
}
}

getConfig(key: string): string {
getRegistry(): string {
if (!this.isExist()) {
return ''
}

return this.exec(`npm config get ${key}`)
return this.exec(`npm config get registry`)
}

getVersion(): string {
Expand Down
8 changes: 4 additions & 4 deletions src/registryManager/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ class Yarn extends RegistryManager {
return this.isExist() ? this.exec('yarn -v') : ''
}

setConfig(key: string, value: string): boolean {
setRegistry(value: string): boolean {
if (!this.isExist()) {
return false
} else {
this.exec(`yarn config set ${key} ${value}`)
this.exec(`yarn config set registry ${value}`)
return true
}
}

getConfig(key: string): string {
getRegistry(): string {
if (!this.isExist()) {
return ''
}

const stdout = this.exec(`yarn config get ${key}`)
const stdout = this.exec(`yarn config get registry`)

return stdout.replace('warning package.json: No license field', '').trim()
}
Expand Down

0 comments on commit 1b3c29e

Please sign in to comment.