diff --git a/README.md b/README.md index ef1f8e2..1078b98 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,10 @@ globalAgent=npm # custom configuration file path export NI_CONFIG_FILE="$HOME/.config/ni/nirc" + +# environment variables have higher priority than config file if presented +export NI_DEFAULT_AGENT="npm" # default "prompt" +export NI_GLOBAL_AGENT="npm" ``` ```ps diff --git a/src/config.ts b/src/config.ts index fdcc06a..22f59cb 100644 --- a/src/config.ts +++ b/src/config.ts @@ -36,6 +36,13 @@ export async function getConfig(): Promise { ? ini.parse(fs.readFileSync(rcPath, 'utf-8')) : null, ) + + if (process.env.NI_DEFAULT_AGENT) + config.defaultAgent = process.env.NI_DEFAULT_AGENT as Agent + + if (process.env.NI_GLOBAL_AGENT) + config.globalAgent = process.env.NI_GLOBAL_AGENT as Agent + const agent = await detect({ programmatic: true }) if (agent) config.defaultAgent = agent diff --git a/test/config/.nirc b/test/config/.nirc new file mode 100644 index 0000000..1edd853 --- /dev/null +++ b/test/config/.nirc @@ -0,0 +1,2 @@ +defaultAgent=npm +globalAgent=pnpm \ No newline at end of file diff --git a/test/config/config.test.ts b/test/config/config.test.ts new file mode 100644 index 0000000..71a7373 --- /dev/null +++ b/test/config/config.test.ts @@ -0,0 +1,49 @@ +import { dirname, join } from 'node:path' +import { fileURLToPath } from 'node:url' +import { beforeEach, expect, it, vi } from 'vitest' + +const __dirname = dirname(fileURLToPath(import.meta.url)) + +beforeEach(() => { + vi.unstubAllEnvs() + vi.resetModules() +}) + +vi.mock('find-up', () => ({ + findUp: vi.fn(), +})) + +it('has correct defaults', async () => { + const { getConfig } = await import('../../src/config') + const config = await getConfig() + + expect(config).toEqual({ + defaultAgent: 'prompt', + globalAgent: 'npm', + }) +}) + +it('loads .nirc', async () => { + vi.stubEnv('NI_CONFIG_FILE', join(__dirname, './.nirc')) + + const { getConfig } = await import('../../src/config') + const config = await getConfig() + + expect(config).toEqual({ + defaultAgent: 'npm', + globalAgent: 'pnpm', + }) +}) + +it('reads environment variable config', async () => { + vi.stubEnv('NI_DEFAULT_AGENT', 'npm') + vi.stubEnv('NI_GLOBAL_AGENT', 'pnpm') + + const { getConfig } = await import('../../src/config') + const config = await getConfig() + + expect(config).toEqual({ + defaultAgent: 'npm', + globalAgent: 'pnpm', + }) +})