Skip to content

Commit

Permalink
feat: add package-manager detector logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gearonix committed Mar 12, 2024
1 parent f984082 commit 72a0cea
Show file tree
Hide file tree
Showing 18 changed files with 378 additions and 34 deletions.
5 changes: 5 additions & 0 deletions bin/gx.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

'use strict'

import '../dist/main.mjs'
4 changes: 2 additions & 2 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineBuildConfig } from 'unbuild'
import { resolve } from 'path'

export default defineBuildConfig({
entries: ['bin/gx'],
entries: ['src/main'],
declaration: true,
clean: true,
rollup: {
Expand All @@ -19,4 +19,4 @@ export default defineBuildConfig({
alias: {
'@': resolve(__dirname, 'src')
}
})
})
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
"url": "git+https://github.com/antfu/pkg-placeholder.git"
},
"bin": {
"gx": "dist/bin/gx.cjs"
"gx": "bin/gx.mjs"
},
"bugs": "https://github.com/antfu/pkg-placeholder/issues",
"keywords": [],
"sideEffects": false,
"exports": {
".": {
"types": "./dist/bin/gx.d.ts",
"import": "./dist/bin/gx.mjs",
"require": "./dist/bin/gx.cjs"
"types": "./dist/main.d.ts",
"import": "./dist/main.mjs",
"require": "./dist/main.cjs"
}
},
"main": "./dist/bin/gx.mjs",
"module": "./dist/bin/gx.mjs",
"types": "./dist/bin/gx.d.ts",
"main": "./dist/main.cjs",
"module": "./dist/main.mjs",
"types": "./dist/main.d.ts",
"typesVersions": {
"*": {
"*": [
Expand Down Expand Up @@ -75,11 +75,13 @@
"*": "eslint . --fix"
},
"dependencies": {
"@neodx/fs": "^0.0.11",
"@neodx/std": "^0.3.0",
"@nestjs/common": "^10.3.3",
"@nestjs/core": "^10.3.3",
"@nestjs/platform-express": "^10.3.3",
"eslint-kit": "^10.19.0",
"execa": "^8.0.1",
"nest-commander": "^3.12.5"
}
}
41 changes: 29 additions & 12 deletions pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Module } from '@nestjs/common'
import { InitCommand } from '@/commands/init.command'
import { PackageManagerModule } from '@/pkg-manager'

@Module({
imports: [],
imports: [PackageManagerModule],
providers: [InitCommand]
})
export class AppModule {}
24 changes: 15 additions & 9 deletions src/commands/init.command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { AnyRecord } from '@neodx/std'
import { Command, CommandRunner, Option } from 'nest-commander'
import { Command, CommandRunner } from 'nest-commander'
import type { AbstractPackageManager } from '@/pkg-manager'
import { InjectPackageManager } from '@/pkg-manager'

@Command({
name: 'init',
Expand All @@ -9,18 +11,22 @@ import { Command, CommandRunner, Option } from 'nest-commander'
description: ''
})
export class InitCommand extends CommandRunner {
constructor(
@InjectPackageManager() private readonly manager: AbstractPackageManager
) {
super()
}

public async run(param: string[], options: AnyRecord) {
console.log({
param,
options
options,
manager: this.manager
})
}

@Option({
flags: '-n, --number [number]',
description: ''
})
public parseNumber(val: string): number {
return Number(val)
// console.log(this.manager.agent)
const workspaces = await this.manager.getWorkspaces()

console.log(workspaces)
}
}
4 changes: 1 addition & 3 deletions bin/gx.ts → src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env node

import { CommandFactory } from 'nest-commander'
import { AppModule } from '../src/app'
import { AppModule } from './app'

async function bootstrap() {
await CommandFactory.run(AppModule, ['error', 'warn'])
Expand Down
4 changes: 4 additions & 0 deletions src/pkg-manager/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { AbstractPackageManager } from './managers/abstract.pkg-manager'
export { PACKAGE_MANAGER } from './pkg-manager.consts'
export { InjectPackageManager } from './pkg-manager.decorator'
export { PackageManagerModule } from './pkg-manager.module'
18 changes: 18 additions & 0 deletions src/pkg-manager/managers/abstract.pkg-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { execa as $ } from 'execa'
import type { WorkspaceProject } from '@/pkg-manager/pkg-manager.types'

export abstract class AbstractPackageManager {
constructor(private command: string) {}

public abstract getWorkspaces(): Promise<WorkspaceProject[]>

public async run(args: string[]) {
const output = await $(this.command, args)

return output.stdout
}

public get agent() {
return this.command
}
}
50 changes: 50 additions & 0 deletions src/pkg-manager/managers/bun.pkg-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { scan } from '@neodx/fs'
import { resolve } from 'node:path'
import * as process from 'process'
import { AbstractPackageManager } from '@/pkg-manager/managers/abstract.pkg-manager'
import { PackageManager } from '@/pkg-manager/pkg-manager.consts'
import type { WorkspaceProject } from '@/pkg-manager/pkg-manager.types'
import type { PackageJson } from '@/shared/json'
import { readJson } from '@/shared/json'

// TODO: add yarn@berry support
// TODO: split file structure to modules

export class BunPackageManager extends AbstractPackageManager {
constructor() {
super(PackageManager.BUN)
}

public async getWorkspaces(): Promise<WorkspaceProject[]> {
const packageJson = await readJson('package.json')

if (!packageJson) return []

const rawWorkspaces = packageJson.workspaces

const workspaces = Array.isArray(rawWorkspaces)
? rawWorkspaces
: rawWorkspaces!.packages

const projectPatterns = await scan(
process.cwd(),
workspaces.map((match) => resolve(match, 'package.json'))
)

const bunWorkspaces = await Promise.all(
projectPatterns.map(async (pattern) => {
const scopedPkgJson = (await readJson(pattern)) as PackageJson

const workspaceName = scopedPkgJson.name ?? null
const workspaceDir = resolve(pattern, '..')

return {
name: workspaceName,
location: workspaceDir
}
})
)

return bunWorkspaces
}
}
Loading

0 comments on commit 72a0cea

Please sign in to comment.