TypscriptLanguageExtension.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import * as EditorEvents from "../../editor/EditorEvents";
  23. /**
  24. * Resource extension that supports the web view typescript extension
  25. */
  26. export default class TypescriptLanguageExtension implements Editor.HostExtensions.ResourceService, Editor.HostExtensions.ProjectService {
  27. name: string = "HostTypeScriptLanguageExtension";
  28. description: string = "This service supports the typscript webview extension.";
  29. /**
  30. * Indicates if this project contains typescript files.
  31. * @type {Boolean}
  32. */
  33. private isTypescriptProject = false;
  34. private serviceRegistry: Editor.HostExtensions.HostServiceLocator = null;
  35. /**
  36. * Determines if the file name/path provided is something we care about
  37. * @param {string} path
  38. * @return {boolean}
  39. */
  40. private isValidFiletype(path: string): boolean {
  41. if (this.isTypescriptProject) {
  42. const ext = Atomic.getExtension(path);
  43. if (ext == ".ts") {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. /**
  50. * Seed the language service with all of the relevant files in the project. This updates the tsconifg.atomic file in
  51. * the root of the resources directory.
  52. */
  53. private loadProjectFiles() {
  54. let projectFiles: Array<string> = [];
  55. //scan all the files in the project for any typescript files so we can determine if this is a typescript project
  56. Atomic.fileSystem.scanDir(ToolCore.toolSystem.project.resourcePath, "*.ts", Atomic.SCAN_FILES, true).forEach(filename => {
  57. projectFiles.push(Atomic.addTrailingSlash(ToolCore.toolSystem.project.resourcePath) + filename);
  58. this.isTypescriptProject = true;
  59. });
  60. // only build out a tsconfig.atomic if we actually have typescript files in the project
  61. if (this.isTypescriptProject) {
  62. // First we need to load in a copy of the lib.core.d.ts that is necessary for the hosted typescript compiler
  63. projectFiles.push(Atomic.addTrailingSlash(Atomic.addTrailingSlash(ToolCore.toolEnvironment.toolDataDir) + "TypeScriptSupport") + "lib.core.d.ts");
  64. // Load up a copy of the duktape.d.ts
  65. projectFiles.push(Atomic.addTrailingSlash(Atomic.addTrailingSlash(ToolCore.toolEnvironment.toolDataDir) + "TypeScriptSupport") + "duktape.d.ts");
  66. // Look in a 'typings' directory for any typescript definition files
  67. const typingsDir = Atomic.addTrailingSlash(ToolCore.toolSystem.project.projectPath) + "typings";
  68. Atomic.fileSystem.scanDir(typingsDir, "*.d.ts", Atomic.SCAN_FILES, true).forEach(filename => {
  69. projectFiles.push(Atomic.addTrailingSlash(typingsDir) + filename);
  70. });
  71. let files = projectFiles.map((f: string) => {
  72. if (f.indexOf(ToolCore.toolSystem.project.resourcePath) != -1) {
  73. // if we are in the resources directory, just pass back the path from resources down
  74. return f.replace(Atomic.addTrailingSlash(ToolCore.toolSystem.project.projectPath), "");
  75. } else {
  76. // otherwise return the full path
  77. return f;
  78. }
  79. });
  80. let tsConfig = {
  81. files: files
  82. };
  83. let filename = Atomic.addTrailingSlash(ToolCore.toolSystem.project.resourcePath) + "tsconfig.atomic";
  84. let script = new Atomic.File(filename, Atomic.FILE_WRITE);
  85. try {
  86. script.writeString(JSON.stringify(tsConfig));
  87. script.flush();
  88. } finally {
  89. script.close();
  90. }
  91. }
  92. }
  93. /**
  94. * Inject this language service into the registry
  95. * @return {[type]} True if successful
  96. */
  97. initialize(serviceRegistry: Editor.HostExtensions.HostServiceLocator) {
  98. // We care about both resource events as well as project events
  99. serviceRegistry.resourceServices.register(this);
  100. serviceRegistry.projectServices.register(this);
  101. this.serviceRegistry = serviceRegistry;
  102. }
  103. /**
  104. * Handle the delete. This should delete the corresponding javascript file
  105. * @param {Editor.EditorEvents.DeleteResourceEvent} ev
  106. */
  107. delete(ev: Editor.EditorEvents.DeleteResourceEvent) {
  108. if (this.isValidFiletype(ev.path)) {
  109. console.log(`${this.name}: received a delete resource event`);
  110. // Delete the corresponding js file
  111. let jsFile = ev.path.replace(/\.ts$/, ".js");
  112. let jsFileAsset = ToolCore.assetDatabase.getAssetByPath(jsFile);
  113. if (jsFileAsset) {
  114. console.log(`${this.name}: deleting corresponding .js file`);
  115. ToolCore.assetDatabase.deleteAsset(jsFileAsset);
  116. let eventData: EditorEvents.DeleteResourceEvent = {
  117. path: jsFile
  118. };
  119. this.serviceRegistry.sendEvent(EditorEvents.DeleteResourceNotification, eventData);
  120. // rebuild the tsconfig.atomic
  121. this.loadProjectFiles();
  122. }
  123. }
  124. }
  125. /**
  126. * Handle the rename. Should rename the corresponding .js file
  127. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  128. */
  129. rename(ev: Editor.EditorEvents.RenameResourceEvent) {
  130. if (this.isValidFiletype(ev.path)) {
  131. console.log(`${this.name}: received a rename resource event`);
  132. // Rename the corresponding js file
  133. let jsFile = ev.path.replace(/\.ts$/, ".js");
  134. let jsFileNew = ev.newPath.replace(/\.ts$/, ".js"); // rename doesn't want extension
  135. let jsFileAsset = ToolCore.assetDatabase.getAssetByPath(jsFile);
  136. if (jsFileAsset) {
  137. console.log(`${this.name}: renaming corresponding .js file`);
  138. jsFileAsset.rename(ev.newName);
  139. let eventData: EditorEvents.RenameResourceEvent = {
  140. path: jsFile,
  141. newPath: jsFileNew,
  142. newName: ev.newName,
  143. asset: jsFileAsset
  144. };
  145. this.serviceRegistry.sendEvent(EditorEvents.RenameResourceNotification, eventData);
  146. // rebuild the tsconfig.atomic
  147. this.loadProjectFiles();
  148. }
  149. }
  150. }
  151. /**
  152. * Handles the save event and detects if a typescript file has been added to a non-typescript project
  153. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  154. * @return {[type]}
  155. */
  156. save(ev: Editor.EditorEvents.SaveResourceEvent) {
  157. // let's check to see if we have created a typescript file
  158. if (!this.isTypescriptProject) {
  159. if (Atomic.getExtension(ev.path) == ".ts") {
  160. this.isTypescriptProject = true;
  161. this.loadProjectFiles();
  162. }
  163. }
  164. }
  165. /*** ProjectService implementation ****/
  166. /**
  167. * Called when the project is being loaded to allow the typscript language service to reset and
  168. * possibly compile
  169. */
  170. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  171. // got a load, we need to reset the language service
  172. console.log(`${this.name}: received a project loaded event for project at ${ev.path}`);
  173. this.loadProjectFiles();
  174. }
  175. }