()
| 132 | // Is only called once as part of the constructor |
| 133 | // It is expected that this instance will be disposed if this method returns false |
| 134 | private watchFiles(): boolean { |
| 135 | // Only watch any files, if both man dir and DESCRIPTION exist |
| 136 | if(!isFileSafe(this.descriptionPath) || !isDirSafe(this.manDir)){ |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // Prepare listeners |
| 141 | const errorListener = () => { |
| 142 | console.log(`Disposing previewer for pkgDir: ${this.packageDir}`); |
| 143 | void this.dispose(true); |
| 144 | }; |
| 145 | const descriptionListener: fs.WatchListener<string> = () => { |
| 146 | this.cachedPackageInfo = undefined; |
| 147 | this.callPreviewListener(); |
| 148 | }; |
| 149 | const manDirListener: fs.WatchListener<string | null> = (event: fs.WatchEventType, filename: string | null) => { |
| 150 | if(this.isDisposed){ |
| 151 | return; |
| 152 | } |
| 153 | if(!isDirSafe(this.manDir)){ |
| 154 | this.dispose(true); |
| 155 | return; |
| 156 | } |
| 157 | if (filename === null) { |
| 158 | return; |
| 159 | } |
| 160 | const fullPath = path.join(this.manDir, filename); |
| 161 | // The cache is only initialized when it is needed for the first time: |
| 162 | if(this.cachedRdAliases){ |
| 163 | const rdAlias = getRdAlias(fullPath); |
| 164 | if(rdAlias){ |
| 165 | this.cachedRdAliases.set(fullPath, rdAlias); |
| 166 | } else{ |
| 167 | this.cachedRdAliases.delete(fullPath); |
| 168 | } |
| 169 | } |
| 170 | this.callPreviewListener(); |
| 171 | }; |
| 172 | |
| 173 | // Watch man dir/DESCRIPTION |
| 174 | const descWatcher = fs.watch(this.descriptionPath, {encoding: 'utf-8'}); |
| 175 | descWatcher.on('change', descriptionListener); |
| 176 | descWatcher.on('error', errorListener); |
| 177 | this.fileWatchers.push(descWatcher); |
| 178 | |
| 179 | const manDirWatcher = fs.watch(this.manDir, {encoding: 'utf-8'}); |
| 180 | manDirWatcher.on('change', manDirListener); |
| 181 | manDirWatcher.on('error', errorListener); |
| 182 | this.fileWatchers.push(manDirWatcher); |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | public getPackageInfo(): LocalPackageInfo | undefined { |
| 188 | if(this.cachedPackageInfo){ |
no test coverage detected