@@ -126,7 +126,8 @@ function createTSServerInstance() {
126126 * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
127127 */
128128 function sendCommand ( command , timeout = 5000 ) {
129- if ( command . command === "open" || command . command === "geterr" || command . command === "geterrForProject" || command . command === "saveto" ) {
129+ if ( command . command === "open" || command . command === "geterr" || command . command === "geterrForProject"
130+ || command . command === "saveto" || command . command === "reloadProjects" ) {
130131 // For 'open' command, resolve immediately as no response is expected
131132 // geterr and geterrForProject returns result as events so resolve geterr and wait for response in events
132133 // saveTo command also does not return any response
@@ -1180,6 +1181,159 @@ function createTSServerInstance() {
11801181 return sendCommand ( command ) ;
11811182 }
11821183
1184+ /**
1185+ * Sends a 'projectInfo' request to the TypeScript Server to retrieve information about the TypeScript
1186+ * project associated with a specific file. This includes details about the project's configuration,
1187+ * the files included, and the status of the language service.
1188+ *
1189+ * @param {string } filePath - The path to the TypeScript file.
1190+ * @param {boolean } needFileNameList - Indicates whether the list of file names in the project is needed.
1191+ * @param {string } [projectFileName] - Optional. The name of the project file (absolute pathname required)
1192+ * that contains the TypeScript file.
1193+ *
1194+ * @returns {Promise<Object> } A promise that resolves with the project information, which includes:
1195+ * - `configFileName`: A string representing the path to the project's
1196+ * configuration file (tsconfig.json), if available.
1197+ * - `fileNames`: An array of strings representing the file names in the project,
1198+ * included if `needFileNameList` is true.
1199+ * - `languageServiceDisabled`: A boolean indicating whether the language service
1200+ * is disabled for this project.
1201+ *
1202+ * Example usage:
1203+ * ```
1204+ * projectInfo('path/to/file.ts', true, 'path/to/project.tsconfig.json').then(info => {
1205+ * console.log('Project information:', info);
1206+ * });
1207+ * ```
1208+ * This function is useful for tools and IDEs to gain insights into the structure and configuration
1209+ * of a TypeScript project.
1210+ */
1211+ function projectInfo ( filePath , needFileNameList = false , projectFileName = "" ) {
1212+ const command = {
1213+ command : "projectInfo" ,
1214+ arguments : {
1215+ file : filePath ,
1216+ needFileNameList : needFileNameList ,
1217+ projectFileName : projectFileName
1218+ }
1219+ } ;
1220+ return sendCommand ( command ) ;
1221+ }
1222+
1223+ /**
1224+ * Sends a 'reloadProjects' request to the TypeScript Server. This command instructs
1225+ * the server to reload all projects it has loaded. It is particularly useful when
1226+ * project configurations or file structures have changed, ensuring that tsserver
1227+ * is synchronized with the current state of the projects.
1228+ *
1229+ * @returns {Promise<void> } A promise that resolves when the server has reloaded the projects.
1230+ * Note that there is no direct response from the server for this command,
1231+ * so the promise resolves as soon as the command is sent.
1232+ *
1233+ * Example usage:
1234+ * ```
1235+ * reloadProjects().then(() => {
1236+ * console.log('All projects reloaded');
1237+ * });
1238+ * ```
1239+ * This function is essential for maintaining project synchronization with tsserver, particularly
1240+ * after significant changes to project configurations or file structures.
1241+ */
1242+ function reloadProjects ( ) {
1243+ const command = {
1244+ command : "reloadProjects"
1245+ } ;
1246+ return sendCommand ( command ) ;
1247+ }
1248+
1249+ /**
1250+ * Sends an 'openExternalProject' request to the TypeScript Server. This command opens an external project
1251+ * with the provided configuration, which includes the project's file name, root files, compiler options,
1252+ * and type acquisition settings. The server responds with an acknowledgement.
1253+ *
1254+ * @param {Object } project - The external project configuration, which includes:
1255+ * - `projectFileName`: The name or path of the project file.
1256+ * - `rootFiles`: An array of objects representing the root files in the project.
1257+ * Each object includes `fileName` and other optional properties like
1258+ * `scriptKind` and `hasMixedContent`.
1259+ * - `options`: The compiler options for the project.
1260+ * - `typeAcquisition`: Optional type acquisition settings for the project.
1261+ *
1262+ * @returns {Promise<Object> } A promise that resolves when the server has acknowledged the opening of the external project.
1263+ * The response object contains standard response fields such as:
1264+ * - `success`: A boolean indicating whether the request was successful.
1265+ * - `request_seq`: The sequence number of the request.
1266+ * - `command`: The command requested.
1267+ * - `message`: An optional success or error message.
1268+ *
1269+ * Example usage:
1270+ * ```
1271+ * const externalProject = {
1272+ * projectFileName: 'path/to/external/project',
1273+ * rootFiles: [{ fileName: 'path/to/rootFile1.ts', scriptKind: 'ts', hasMixedContent: true }],
1274+ * options: { noImplicitAny: true, strictNullChecks: true },
1275+ * typeAcquisition: { enable: true, include: ['node'] }
1276+ * };
1277+ * openExternalProject(externalProject).then(response => {
1278+ * console.log('External project opened:', response);
1279+ * });
1280+ * ```
1281+ * This function is particularly useful for integrating tsserver with environments where
1282+ * project configurations are defined externally.
1283+ */
1284+ function openExternalProject ( project ) {
1285+ const command = {
1286+ command : "openExternalProject" ,
1287+ arguments : project
1288+ } ;
1289+ return sendCommand ( command ) ;
1290+ }
1291+
1292+ /**
1293+ * Sends an 'openExternalProjects' request to the TypeScript Server. This command is used to
1294+ * open multiple external projects simultaneously. Each project configuration includes the
1295+ * project's file name, root files, compiler options, and type acquisition settings. The server
1296+ * responds with an acknowledgement.
1297+ *
1298+ * @param {Object[] } projects - An array of external project configurations. Each configuration includes:
1299+ * - `projectFileName`: The name or path of the project file.
1300+ * - `rootFiles`: An array of objects representing the root files in the project.
1301+ * - `options`: The compiler options for the project.
1302+ * - `typeAcquisition`: Optional type acquisition settings for the project.
1303+ *
1304+ * @returns {Promise<Object> } A promise that resolves when the server has acknowledged
1305+ * opening the external projects. The response object contains
1306+ * standard response fields like:
1307+ * - `success`: A boolean indicating whether the request was successful.
1308+ * - `request_seq`: The sequence number of the request.
1309+ * - `command`: The command requested.
1310+ * - `message`: An optional success or error message.
1311+ *
1312+ * Example usage:
1313+ * ```
1314+ * const externalProjects = [
1315+ * {
1316+ * projectFileName: 'path/to/external/project1',
1317+ * rootFiles: [{ fileName: 'path/to/rootFile1.ts', scriptKind: 'ts', hasMixedContent: true }],
1318+ * options: { noImplicitAny: true },
1319+ * typeAcquisition: { enable: true, include: ['node'] }
1320+ * },
1321+ * // ... more projects ...
1322+ * ];
1323+ * openExternalProjects(externalProjects).then(response => {
1324+ * console.log('External projects opened:', response);
1325+ * });
1326+ * ```
1327+ * This function is especially useful for environments where multiple TypeScript projects are managed externally.
1328+ */
1329+ function openExternalProjects ( projects ) {
1330+ const command = {
1331+ command : "openExternalProjects" ,
1332+ arguments : { projects : projects }
1333+ } ;
1334+ return sendCommand ( command ) ;
1335+ }
1336+
11831337 /**
11841338 * Terminates the TypeScript Server process.
11851339 * Warning: Use this function with caution. Prefer using the exitServer function for a graceful shutdown.
@@ -1228,6 +1382,10 @@ function createTSServerInstance() {
12281382 signatureHelp,
12291383 status,
12301384 typeDefinition,
1385+ projectInfo,
1386+ reloadProjects,
1387+ openExternalProject,
1388+ openExternalProjects,
12311389 exitServer
12321390 } ;
12331391}
0 commit comments