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

feat: add pkg import command #4

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
22 changes: 22 additions & 0 deletions packages/radashi-helper/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ app
await execute(pr, app.rawArgs.slice(1))
})

app
.command('pkg [subcommand]', 'Manage your bundled dependencies')
.allowUnknownOptions()
.action(async () => {
const pkg = cac('radashi pkg')

pkg.option(
'-C, --dir <dir>',
'Set the directory where your Radashi is located',
)

pkg
.command('import <name>', 'Add a bundled dependency')
.alias('add')
.action(async (name: string, flags) => {
const { importPackage } = await import('./pkg-import')
await importPackage(name, flags)
})

await execute(pkg, app.rawArgs.slice(1))
})

app
.command('open [query]', 'Open function files in your editor')
.option('-s, --source', 'Open the source file')
Expand Down
32 changes: 32 additions & 0 deletions packages/radashi-helper/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ interface OptionalConfig {
* The editor to use when opening a new function.
*/
editor?: string
/**
* Packages that have been imported into the project.
*/
vendor?: VendorConfig[]
}

/**
Expand All @@ -34,6 +38,34 @@ export interface UserConfig extends OptionalConfig {
formats?: ('esm' | 'cjs')[]
}

/**
* The config for a package that has been imported into the project.
*/
interface VendorConfig {
/** The name of the package. */
name: string
/**
* The exports to include from the package. If undefined, all
* exports are included.
*
* Note that type exports are also affected by this option.
*/
include?: string[]
/**
* The exports to exclude from the package. If undefined, no exports
* are excluded.
*
* Note that type exports are also affected by this option.
*/
exclude?: string[]
/**
* The exports to rename from the package. Any exports listed here
* are automatically added to the `include` array (if not already
* present).
*/
rename?: Record<string, string>
}

export interface Config
extends Required<Omit<UserConfig, keyof OptionalConfig>>,
OptionalConfig {}
Expand Down
40 changes: 40 additions & 0 deletions packages/radashi-helper/src/pkg-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { exec } from 'exec'
import type { CommonOptions } from './cli/options'
import { getEnv } from './env'
import { debug } from './util/debug'
import { RadashiError } from './util/error'
import { stdio } from './util/stdio'
import { updateRadashiConfig } from './util/updateRadashiConfig'

export interface ImportPackageOptions extends CommonOptions {
exact?: boolean
}

export async function importPackage(
name: string,
options: ImportPackageOptions = {},
) {
const env = options.env ?? getEnv(options.dir)

if (env.pkg.devDependencies && name in env.pkg.devDependencies) {
debug(`Package '${name}' is already installed in node_modules.`)
} else {
debug(`Package '${name}' is not found in node_modules. Installing...`)

const { exitCode } = await exec('pnpm', ['install', '-D', name], {
cwd: env.root,
stdio,
reject: false,
})

if (exitCode !== 0) {
throw new RadashiError(
`Failed to install package '${name}'. See the logs above.`,
)
}
}

await updateRadashiConfig(env, {
vendor: [...(env.config.vendor ?? []), { name }],
})
}