Skip to content

Commit

Permalink
add feature about custom options to use own tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukuha committed Jun 13, 2024
1 parent 75327a4 commit d8a92e4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/tsc/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { runTsc } from '@volar/typescript/lib/quickstart/runTsc';
import * as vue from '@vue/language-core';
import { createCliOptions } from './options';

const windowsPathReg = /\\/g;

export function run() {

let runExtensions = ['.vue'];

const customOptions = createCliOptions();

const extensionsChangedException = new Error('extensions changed');
const main = () => runTsc(
require.resolve('typescript/lib/tsc'),
runExtensions,
(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,
Expand Down
36 changes: 36 additions & 0 deletions packages/tsc/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
interface Options {
Project: 'project',
};
interface CustomOptions extends Record<Options[keyof Options], string> {};

const ARGS_ACCESS_NUM = 2;
const OPTIONS = {
Project: 'project',
} as const satisfies Options;
const OPTIONS_PREFIX = '--';

export function createCliOptions(): CustomOptions | undefined {
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;
};

0 comments on commit d8a92e4

Please sign in to comment.