|
| 1 | +import type ts from 'typescript/lib/tsserverlibrary'; |
| 2 | +import { ConfigManager, Configuration } from './config-manager'; |
| 3 | +import { SvelteSnapshotManager } from './svelte-snapshots'; |
| 4 | +import { getConfigPathForProject, isSvelteFilePath } from './utils'; |
| 5 | + |
| 6 | +export interface TsFilesSpec { |
| 7 | + include?: readonly string[]; |
| 8 | + exclude?: readonly string[]; |
| 9 | +} |
| 10 | + |
| 11 | +export class ProjectSvelteFilesManager { |
| 12 | + private files = new Set<string>(); |
| 13 | + private directoryWatchers = new Set<ts.FileWatcher>(); |
| 14 | + |
| 15 | + private static instances = new Map<string, ProjectSvelteFilesManager>(); |
| 16 | + |
| 17 | + static getInstance(projectName: string) { |
| 18 | + return this.instances.get(projectName); |
| 19 | + } |
| 20 | + |
| 21 | + constructor( |
| 22 | + private readonly typescript: typeof ts, |
| 23 | + private readonly project: ts.server.Project, |
| 24 | + private readonly serverHost: ts.server.ServerHost, |
| 25 | + private readonly snapshotManager: SvelteSnapshotManager, |
| 26 | + private parsedCommandLine: ts.ParsedCommandLine, |
| 27 | + configManager: ConfigManager |
| 28 | + ) { |
| 29 | + if (configManager.getConfig().enable) { |
| 30 | + this.setupWatchers(); |
| 31 | + this.updateProjectSvelteFiles(); |
| 32 | + } |
| 33 | + |
| 34 | + configManager.onConfigurationChanged(this.onConfigChanged.bind(this)); |
| 35 | + ProjectSvelteFilesManager.instances.set(project.getProjectName(), this); |
| 36 | + } |
| 37 | + |
| 38 | + updateProjectConfig(serviceHost: ts.LanguageServiceHost) { |
| 39 | + const parsedCommandLine = serviceHost.getParsedCommandLine?.( |
| 40 | + getConfigPathForProject(this.project) |
| 41 | + ); |
| 42 | + |
| 43 | + if (!parsedCommandLine) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + this.disposeWatchersAndFiles(); |
| 48 | + this.parsedCommandLine = parsedCommandLine; |
| 49 | + this.setupWatchers(); |
| 50 | + this.updateProjectSvelteFiles(); |
| 51 | + } |
| 52 | + |
| 53 | + getFiles() { |
| 54 | + return Array.from(this.files); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Create directory watcher for include and exclude |
| 59 | + * The watcher in tsserver doesn't support svelte file |
| 60 | + * It won't add new created svelte file to root |
| 61 | + */ |
| 62 | + private setupWatchers() { |
| 63 | + for (const directory in this.parsedCommandLine.wildcardDirectories) { |
| 64 | + if ( |
| 65 | + !Object.prototype.hasOwnProperty.call( |
| 66 | + this.parsedCommandLine.wildcardDirectories, |
| 67 | + directory |
| 68 | + ) |
| 69 | + ) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + const watchDirectoryFlags = this.parsedCommandLine.wildcardDirectories[directory]; |
| 74 | + const watcher = this.serverHost.watchDirectory( |
| 75 | + directory, |
| 76 | + this.watcherCallback.bind(this), |
| 77 | + watchDirectoryFlags === this.typescript.WatchDirectoryFlags.Recursive, |
| 78 | + this.parsedCommandLine.watchOptions |
| 79 | + ); |
| 80 | + |
| 81 | + this.directoryWatchers.add(watcher); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private watcherCallback(fileName: string) { |
| 86 | + if (!isSvelteFilePath(fileName)) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // We can't just add the file to the project directly, because |
| 91 | + // - the casing of fileName is different |
| 92 | + // - we don't know whether the file was added or deleted |
| 93 | + this.updateProjectSvelteFiles(); |
| 94 | + } |
| 95 | + |
| 96 | + private updateProjectSvelteFiles() { |
| 97 | + const fileNamesAfter = this.readProjectSvelteFilesFromFs(); |
| 98 | + const removedFiles = new Set(...this.files); |
| 99 | + const newFiles = fileNamesAfter.filter((fileName) => { |
| 100 | + const has = this.files.has(fileName); |
| 101 | + if (has) { |
| 102 | + removedFiles.delete(fileName); |
| 103 | + } |
| 104 | + return !has; |
| 105 | + }); |
| 106 | + |
| 107 | + for (const newFile of newFiles) { |
| 108 | + this.addFileToProject(newFile); |
| 109 | + this.files.add(newFile); |
| 110 | + } |
| 111 | + for (const removedFile of removedFiles) { |
| 112 | + this.removeFileFromProject(removedFile, false); |
| 113 | + this.files.delete(removedFile); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private addFileToProject(newFile: string) { |
| 118 | + this.snapshotManager.create(newFile); |
| 119 | + const snapshot = this.project.projectService.getScriptInfo(newFile); |
| 120 | + |
| 121 | + if (snapshot) { |
| 122 | + this.project.addRoot(snapshot); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private readProjectSvelteFilesFromFs() { |
| 127 | + const fileSpec: TsFilesSpec = this.parsedCommandLine.raw; |
| 128 | + const { include, exclude } = fileSpec; |
| 129 | + |
| 130 | + if (include?.length === 0) { |
| 131 | + return []; |
| 132 | + } |
| 133 | + |
| 134 | + return this.typescript.sys |
| 135 | + .readDirectory( |
| 136 | + this.project.getCurrentDirectory() || process.cwd(), |
| 137 | + ['.svelte'], |
| 138 | + exclude, |
| 139 | + include |
| 140 | + ) |
| 141 | + .map(this.typescript.server.toNormalizedPath); |
| 142 | + } |
| 143 | + |
| 144 | + private onConfigChanged(config: Configuration) { |
| 145 | + this.disposeWatchersAndFiles(); |
| 146 | + |
| 147 | + if (config.enable) { |
| 148 | + this.setupWatchers(); |
| 149 | + this.updateProjectSvelteFiles(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private removeFileFromProject(file: string, exists = true) { |
| 154 | + const info = this.project.getScriptInfo(file); |
| 155 | + |
| 156 | + if (info) { |
| 157 | + this.project.removeFile(info, exists, true); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private disposeWatchersAndFiles() { |
| 162 | + this.directoryWatchers.forEach((watcher) => watcher.close()); |
| 163 | + this.directoryWatchers.clear(); |
| 164 | + |
| 165 | + this.files.forEach((file) => this.removeFileFromProject(file)); |
| 166 | + this.files.clear(); |
| 167 | + } |
| 168 | + |
| 169 | + dispose() { |
| 170 | + this.disposeWatchersAndFiles(); |
| 171 | + |
| 172 | + ProjectSvelteFilesManager.instances.delete(this.project.getProjectName()); |
| 173 | + } |
| 174 | +} |
0 commit comments