Skip to content

Commit

Permalink
Update runTsc.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Aug 17, 2024
1 parent b9243ff commit 587e51d
Showing 1 changed file with 46 additions and 48 deletions.
94 changes: 46 additions & 48 deletions packages/typescript/lib/quickstart/runTsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,47 @@ export let getLanguagePlugins: (ts: typeof import('typescript'), options: ts.Cre
setup?(language: Language<string>): void,
} = () => [];

export function runTsc(
tscPath: string,
options: string[] | {
extraSupportedExtensions: string[];
extraExtensionsToRemove: string[];
},
_getLanguagePlugins: typeof getLanguagePlugins
) {
getLanguagePlugins = _getLanguagePlugins;

const proxyApiPath = require.resolve('../node/proxyCreateProgram');
const readFileSync = fs.readFileSync;

(fs as any).readFileSync = (...args: any[]) => {
if (args[0] === tscPath) {
let tsc = (readFileSync as any)(...args) as string;

let extraSupportedExtensions: string[];
let extraExtensionsToRemove: string[];
if (Array.isArray(options)) {
extraSupportedExtensions = options;
extraExtensionsToRemove = [];
}
else {
extraSupportedExtensions = options.extraSupportedExtensions;
extraExtensionsToRemove = options.extraExtensionsToRemove;
}

return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove);
}
return (readFileSync as any)(...args);
};

try {
require(tscPath);
} finally {
(fs as any).readFileSync = readFileSync;
delete require.cache[tscPath];
}
}

/**
* Replaces the code of typescript to add support for additional extensions and language plugins.
*
Expand All @@ -17,15 +58,14 @@ export let getLanguagePlugins: (ts: typeof import('typescript'), options: ts.Cre
* @param getLanguagePluginsFile - The file to get language plugins from.
* @returns The modified typescript code.
*/
export function replaceTscContent(
export function transformTscContent(
tsc: string,
proxyApiPath: string,
extraSupportedExtensions: string[],
extraExtensionsToRemove: string[],
getLanguagePluginsFile = __filename,
getLanguagePluginsFile = __filename
) {

const needPatchExtenstions = extraSupportedExtensions.filter(ext => !extraExtensionsToRemove.includes(ext));
const neededPatchExtenstions = extraSupportedExtensions.filter(ext => !extraExtensionsToRemove.includes(ext));

// Add allow extensions
if (extraSupportedExtensions.length) {
Expand All @@ -40,8 +80,8 @@ export function replaceTscContent(
tsc = replace(tsc, /extensionsToRemove = .*(?=;)/, s => s + `.concat([${extsText}])`);
}
// Support for basename.xxx to basename.xxx.d.ts
if (needPatchExtenstions.length) {
const extsText = needPatchExtenstions.map(ext => `"${ext}"`).join(', ');
if (neededPatchExtenstions.length) {
const extsText = neededPatchExtenstions.map(ext => `"${ext}"`).join(', ');
tsc = replace(tsc, /function changeExtension\(/, s => `function changeExtension(path, newExtension) {
return [${extsText}].some(ext => path.endsWith(ext))
? path + newExtension
Expand All @@ -64,48 +104,6 @@ export function replaceTscContent(
return tsc;
}

export function runTsc(
tscPath: string,
options: string[] | {
extraSupportedExtensions: string[];
extraExtensionsToRemove: string[];
},
_getLanguagePlugins: typeof getLanguagePlugins
) {

getLanguagePlugins = _getLanguagePlugins;

const proxyApiPath = require.resolve('../node/proxyCreateProgram');
const readFileSync = fs.readFileSync;

(fs as any).readFileSync = (...args: any[]) => {
if (args[0] === tscPath) {
let tsc = (readFileSync as any)(...args) as string;

let extraSupportedExtensions: string[];
let extraExtensionsToRemove: string[];
if (Array.isArray(options)) {
extraSupportedExtensions = options;
extraExtensionsToRemove = [];
}
else {
extraSupportedExtensions = options.extraSupportedExtensions;
extraExtensionsToRemove = options.extraExtensionsToRemove;
}

return replaceTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove);
}
return (readFileSync as any)(...args);
};

try {
require(tscPath);
} finally {
(fs as any).readFileSync = readFileSync;
delete require.cache[tscPath];
}
}

function replace(text: string, ...[search, replace]: Parameters<String['replace']>) {
const before = text;
text = text.replace(search, replace);
Expand Down

0 comments on commit 587e51d

Please sign in to comment.