Skip to content
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
"author": {
"name": "Ron S.",
"url": "http://twitter.com/ron"
"url": "https://twitter.com/ron"
},
"license": "MIT",
"repository": {
Expand Down
33 changes: 22 additions & 11 deletions src/installer/lib/patcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ const getHeader = () => `/// tsp: ${tspPackageJSON.version}\n\n`;
/**
* Generate insertion code for module-patch
*/
const generatePatch = (isTSC: boolean) =>
jsPatchSrc +
`\nObject.assign(tsp, { isTSC: ${isTSC}, tspVersion: '${tspPackageJSON.version}' });\n\n`;
const generatePatch = (isTSC: boolean, isTSServer: boolean, isTSServerLibrary: boolean) => {
// language=JavaScript
return jsPatchSrc + `\n
Object.assign(tsp, {
isTSC: ${isTSC},
isTSServer: ${isTSServer},
isTSServerLibrary: ${isTSServerLibrary},
tspVersion: '${tspPackageJSON.version}'
});\n\n`;
};

/**
* Validate TSModule and TSPackage before patching
Expand Down Expand Up @@ -78,6 +85,13 @@ const patchModuleDiagnostics = (tsModule: TSModule, tsPackage: TSPackage, source
restCode.substr(emitPos);
}

const locateFile = (filename: string, tsPackage: TSPackage): string => {
return [
path.join(tsPackage.packageDir, BACKUP_DIRNAME, filename),
path.join(tsPackage.libDir, filename)
].filter(f => fs.existsSync(f))[0];
}


/* ********************************************************************************************************************
* Patch
Expand All @@ -93,20 +107,17 @@ export function patchTSModule(tsModule: TSModule, tsPackage: TSPackage) {

/* Install patch */
const isTSC = (filename === 'tsc.js');
const patchSrc = generatePatch(isTSC);
const isTSServer = (filename === 'tsserver.js');
const isTSServerLibrary = (filename === 'tsserverlibrary.js');
const patchSrc = generatePatch(isTSC, isTSServer, isTSServerLibrary);

/* Add diagnostic modification support */
const moduleSrc = patchModuleDiagnostics(tsModule, tsPackage);

try {
if (isTSC) {
/* Select non-patched typescript.js */
const tsFile =
[
path.join(tsPackage.packageDir, BACKUP_DIRNAME, 'typescript.js'),
path.join(tsPackage.libDir, 'typescript.js')
]
.filter(f => fs.existsSync(f))[0];
// Select non-patched typescript.js
const tsFile = locateFile('typescript.js', tsPackage);

/* Get TSC-specific module slice */
const ver = tsPackage.version.split('.')
Expand Down
2 changes: 1 addition & 1 deletion src/patch/lib/createProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace tsp {

/* Get Config */
const projectConfig = getProjectConfig(options, rootNames);
if (tsp.isTSC) {
if (tsp.isTSC || tsp.isTSServer || tsp.isTSServerLibrary) {
options = projectConfig.compilerOptions;
if (createOpts) createOpts.options = options;
}
Expand Down
2 changes: 2 additions & 0 deletions src/patch/lib/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace tsp {
Object.defineProperties(ts, {
isTSC: { get: () => tsp.isTSC, enumerable: true },
isTSServer: { get: () => tsp.isTSServer, enumerable: true },
isTSServerLibrary: { get: () => tsp.isTSServerLibrary, enumerable: true },
tspVersion: { get: () => tsp.tspVersion, enumerable: true },
PluginCreator: { get: () => tsp.PluginCreator, enumerable: true },
originalCreateProgram: { value: ts.createProgram, enumerable: true },
Expand Down
2 changes: 2 additions & 0 deletions src/patch/lib/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace tsp {

export const diagnosticMap: typeof ts.diagnosticMap = new WeakMap();
export declare let isTSC: boolean;
export declare let isTSServer: boolean;
export declare let isTSServerLibrary: boolean;
export declare let tspVersion: string;

/* ********************************************************* *
Expand Down