From 5b6e9b0769fbaffd1e84fc948c2afbb4e9f731ad Mon Sep 17 00:00:00 2001 From: Tsukuha Kawakami Date: Thu, 13 Jun 2024 19:04:47 +0900 Subject: [PATCH] add feature about custom options to use own tsconfig --- packages/tsc/index.ts | 5 ++++- packages/tsc/options.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 packages/tsc/options.ts diff --git a/packages/tsc/index.ts b/packages/tsc/index.ts index 120a15534d..1da4481a25 100644 --- a/packages/tsc/index.ts +++ b/packages/tsc/index.ts @@ -1,5 +1,6 @@ import { runTsc } from '@volar/typescript/lib/quickstart/runTsc'; import * as vue from '@vue/language-core'; +import { createCliOptions } from './options' const windowsPathReg = /\\/g; @@ -7,6 +8,8 @@ export function run() { let runExtensions = ['.vue']; + const customOptions = createCliOptions(); + const extensionsChangedException = new Error('extensions changed'); const main = () => runTsc( require.resolve('typescript/lib/tsc'), @@ -14,7 +17,7 @@ export function run() { (ts, options) => { const { configFilePath } = options.options; const vueOptions = typeof configFilePath === 'string' - ? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions + ? vue.createParsedCommandLine(ts, ts.sys, (customOptions?.project || configFilePath).replace(windowsPathReg, '/')).vueOptions : vue.resolveVueCompilerOptions({}); const allExtensions = [ ...vueOptions.extensions, diff --git a/packages/tsc/options.ts b/packages/tsc/options.ts new file mode 100644 index 0000000000..3b33b5dfed --- /dev/null +++ b/packages/tsc/options.ts @@ -0,0 +1,36 @@ +interface Options { + Project: 'project', +}; +interface CustomOptions extends Record {}; + +const ARGS_ACCESS_NUM = 2; +const OPTIONS = { + Project: 'project', +} as const satisfies Options; +const OPTIONS_PREFIX = '--'; + +export function createCliOptions() { + const args = structuredClone(process.argv); + let parsedOptions: CustomOptions | undefined + args.slice(ARGS_ACCESS_NUM).forEach((arg, i) => { + const options = Object.values(OPTIONS) + const optionStrings = options.map(t => `${OPTIONS_PREFIX}${t}`); + if (!optionStrings.includes(arg)) { + return; + } + if (args[i + 1] == null) { + return; + } + const findIndex = optionStrings.findIndex(s => s === arg); + if (findIndex !== -1 && !!options[findIndex]) { + parsedOptions = { + ...parsedOptions, + [options[findIndex]]: arg[i + 1], + }; + // required to remove custom options and value for executing runTsc() + process.argv = process.argv.filter((_, j) => j !== i + ARGS_ACCESS_NUM && j !== i + ARGS_ACCESS_NUM + 1); + } + args.slice(1); + }); + return parsedOptions; +};