Skip to content
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
1 change: 1 addition & 0 deletions packages/gunshi/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type {
Command,
CommandContext,
CommandContextCore,
CommandContextExtension,
CommandDecorator,
CommandExamplesFetcher,
CommandRunner,
Expand Down
229 changes: 229 additions & 0 deletions packages/plugin-completion/examples/i18n.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import i18n, { defineI18n } from '@gunshi/plugin-i18n'
import { cli } from 'gunshi'
import completion from '../src/index.ts'

import type { I18nCommandContext } from '@gunshi/plugin-i18n'

const entry = defineI18n<{
extensions: {
'g:i18n': I18nCommandContext
}
}>({
name: 'root',
args: {
config: {
type: 'string',
description: 'Use specified config file',
short: 'c'
},
mode: {
type: 'string',
description: 'Set env mode',
short: 'm'
},
logLevel: {
type: 'string',
description: 'info | warn | error | silent',
short: 'l'
}
},
run: _ctx => {},
resource: async ctx => {
const i18n = ctx.extensions['g:i18n']
if (i18n.locale.toString() === 'ja-JP') {
return {
description: 'ルートコマンド',
'arg:config': '設定ファイルを指定します',
'arg:mode': '環境モードを設定します',
'arg:logLevel': 'ログレベルを設定します'
}
}
throw new Error('Unsupported locale')
}
})

const dev = defineI18n<{
extensions: {
'g:i18n': I18nCommandContext
}
}>({
name: 'dev',
description: 'Start dev server',
args: {
host: {
type: 'string',
description: 'Specify hostname',
short: 'H'
},
port: {
type: 'number',
description: 'Specify port',
short: 'p'
}
},
run: () => {},
resource: async ctx => {
const i18n = ctx.extensions['g:i18n']
if (i18n.locale.toString() === 'ja-JP') {
return {
description: '開発サーバーを起動します',
'arg:host': 'ホスト名を指定します',
'arg:port': 'ポート番号を指定します'
}
}
throw new Error('Unsupported locale')
}
})

const build = defineI18n<{
extensions: {
'g:i18n': I18nCommandContext
}
}>({
name: 'build',
description: 'Build project',
run: () => {},
resource: async ctx => {
const i18n = ctx.extensions['g:i18n']
if (i18n.locale.toString() === 'ja-JP') {
return {
description: 'プロジェクトをビルドします'
}
}
throw new Error('Unsupported locale')
}
})

const lint = defineI18n<{
extensions: {
'g:i18n': I18nCommandContext
}
}>({
name: 'lint',
description: 'Lint project',
args: {
files: {
type: 'positional',
description: 'Files to lint'
}
},
run: () => {},
resource: async ctx => {
const i18n = ctx.extensions['g:i18n']
if (i18n.locale.toString() === 'ja-JP') {
return {
description: 'プロジェクトをリントします',
'arg:files': 'リントするファイル'
}
}
throw new Error('Unsupported locale')
}
})

const subCommands = new Map<string, ReturnType<typeof defineI18n>>()
subCommands.set('dev', dev)
subCommands.set('build', build)
subCommands.set('lint', lint)

// @ts-expect-error -- TODO(kazupon): fix type
await cli(process.argv.slice(2), entry, {
name: 'vite',
version: '0.0.0',
description: 'Vite CLI',
subCommands,
plugins: [
i18n({
locale: process.env.MY_LOCALE || 'en-US'
}),
completion({
config: {
entry: {
args: {
config: {
handler: ({ locale }) =>
locale?.toString() === 'ja-JP'
? [
{ value: 'vite.config.ts', description: 'Vite設定ファイル' },
{ value: 'vite.config.js', description: 'Vite設定ファイル' }
]
: [
{ value: 'vite.config.ts', description: 'Vite config file' },
{ value: 'vite.config.js', description: 'Vite config file' }
]
},
mode: {
handler: ({ locale }) =>
locale?.toString() === 'ja-JP'
? [
{ value: 'development', description: '開発モード' },
{ value: 'production', description: '本番モード' }
]
: [
{ value: 'development', description: 'Development mode' },
{ value: 'production', description: 'Production mode' }
]
},
logLevel: {
handler: ({ locale }) =>
locale?.toString() === 'ja-JP'
? [
{ value: 'info', description: '情報レベル' },
{ value: 'warn', description: '警告レベル' },
{ value: 'error', description: 'エラーレベル' },
{ value: 'silent', description: 'サイレントレベル' }
]
: [
{ value: 'info', description: 'Info level' },
{ value: 'warn', description: 'Warn level' },
{ value: 'error', description: 'Error level' },
{ value: 'silent', description: 'Silent level' }
]
}
}
},
subCommands: {
lint: {
handler: ({ locale }) =>
locale?.toString() === 'ja-JP'
? [
{ value: 'main.ts', description: 'メインファイル' },
{ value: 'index.ts', description: 'インデックスファイル' }
]
: [
{ value: 'main.ts', description: 'Main file' },
{ value: 'index.ts', description: 'Index file' }
]
},
dev: {
args: {
port: {
handler: ({ locale }) =>
locale?.toString() === 'ja-JP'
? [
{ value: '3000', description: '開発サーバーポート' },
{ value: '8080', description: '代替ポート' }
]
: [
{ value: '3000', description: 'Development server port' },
{ value: '8080', description: 'Alternative port' }
]
},
host: {
handler: ({ locale }) =>
locale?.toString() === 'ja-JP'
? [
{ value: 'localhost', description: 'ローカルホスト' },
{ value: '0.0.0.0', description: 'すべてのインターフェース' }
]
: [
{ value: 'localhost', description: 'Localhost' },
{ value: '0.0.0.0', description: 'All interfaces' }
]
}
}
}
}
}
})
]
})
4 changes: 4 additions & 0 deletions packages/plugin-completion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
"@bombsh/tab": "https://pkg.pr.new/bombshell-dev/tab@main",
"@gunshi/plugin": "workspace:*"
},
"peerDependencies": {
"@gunshi/plugin-i18n": "workspace:*"
},
"devDependencies": {
"@gunshi/plugin-i18n": "workspace:*",
"@gunshi/shared": "workspace:*",
"@types/node": "catalog:",
"deno": "catalog:",
Expand Down
127 changes: 127 additions & 0 deletions packages/plugin-completion/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,133 @@ vite.config.js Vite config file
"
`;

exports[`i18n support > default command > suggest duplicate options 1`] = `
"--config 設定ファイルを指定します
--mode 環境モードを設定します
--logLevel ログレベルを設定します
:4
"
`;

exports[`i18n support > default command > suggest duplicate options for short option 1`] = `
"--config 設定ファイルを指定します
--mode 環境モードを設定します
--logLevel ログレベルを設定します
:4
"
`;

exports[`i18n support > default command > suggest for inputting 1`] = `
"--config 設定ファイルを指定します
--mode 環境モードを設定します
--logLevel ログレベルを設定します
:4
"
`;

exports[`i18n support > default command > suggest for long option 1`] = `
"--config 設定ファイルを指定します
:4
"
`;

exports[`i18n support > default command > suggest for short option 1`] = `
"-c 設定ファイルを指定します
:4
"
`;

exports[`i18n support > default command > suggest value if option values correctly 1`] = `
"vite.config.ts Vite設定ファイル
vite.config.js Vite設定ファイル
:4
"
`;

exports[`i18n support > no input 1`] = `
"dev 開発サーバーを起動します
build プロジェクトをビルドします
lint プロジェクトをリントします
:4
"
`;

exports[`i18n support > positional arguments > multiple positional arguments when ending with part of the value 1`] = `
"index.ts インデックスファイル
:4
"
`;

exports[`i18n support > positional arguments > single positional argument when ending with space 1`] = `
"main.ts メインファイル
index.ts インデックスファイル
:4
"
`;

exports[`i18n support > positional arguments > suggest multiple positional arguments when ending with space 1`] = `
"main.ts メインファイル
index.ts インデックスファイル
:4
"
`;

exports[`i18n support > subcommand > keep suggesting the --port option if user typed partial but didn't end with space 1`] = `
"--port ポート番号を指定します
:4
"
`;

exports[`i18n support > subcommand > not handle if unknown option 1`] = `
":4
"
`;

exports[`i18n support > subcommand > resolve value if long option and value 1`] = `
"--port=3000 開発サーバーポート
:4
"
`;

exports[`i18n support > subcommand > suggest for command only 1`] = `
"dev 開発サーバーを起動します
:4
"
`;

exports[`i18n support > subcommand > suggest for long option 1`] = `
"--port ポート番号を指定します
:4
"
`;

exports[`i18n support > subcommand > suggest for option inputting 1`] = `
"--host ホスト名を指定します
--port ポート番号を指定します
:4
"
`;

exports[`i18n support > subcommand > suggest for short option 1`] = `
"-H ホスト名を指定します
:4
"
`;

exports[`i18n support > subcommand > suggest if user ends with space after \`--port\` 1`] = `
"3000 開発サーバーポート
8080 代替ポート
:4
"
`;

exports[`i18n support > subcommand > user typed \`--port=\` and hasn't typed a space or value yet 1`] = `
"--port=3000 開発サーバーポート
--port=8080 代替ポート
:4
"
`;

exports[`no input 1`] = `
"dev Start dev server
build Build project
Expand Down
Loading