|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | + |
| 5 | +export function setupTreeView() { |
| 6 | + const rootPath = |
| 7 | + vscode.workspace.workspaceFolders && |
| 8 | + vscode.workspace.workspaceFolders.length > 0 |
| 9 | + ? vscode.workspace.workspaceFolders[0].uri.fsPath |
| 10 | + : undefined; |
| 11 | + console.log("Creating tree view"); |
| 12 | + vscode.window.createTreeView("nodeDependencies", { |
| 13 | + treeDataProvider: new NodeDependenciesProvider( |
| 14 | + "/home/heyzec/Documents/NUS/FYP/sa-vscode", |
| 15 | + ), |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +const hardcode = [ |
| 20 | + ["Rune Trials", "Apr 01, 2025, 9:00 PM"], |
| 21 | + ["Rune Reading", "Apr 02, 2025, 9:00 PM"], |
| 22 | + ["Beyond the Second Dimension", "Apr 03, 2025, 9:00 PM"], |
| 23 | + ["Curve Introduction", "Apr 04, 2025, 9:00 PM"], |
| 24 | + ["Curve Manipulation", "Apr 05, 2025, 9:00 PM"], |
| 25 | + ["Beyond the First Dimension", "Apr 06, 2025, 9:00 PM"], |
| 26 | + ["Premorseal Communications", "Apr 07, 2025, 9:00 PM"], |
| 27 | + ["POTS and Pans", "Apr 08, 2025, 9:00 PM"], |
| 28 | + ["Musical Diversions", "Apr 09, 2025, 11:00 PM"], |
| 29 | + ["Search and Rescue", "Apr 10, 2025, 11:00 PM"], |
| 30 | + ["Sorting Things Out", "Apr 11, 2025, 11:00 PM"], |
| 31 | + ["Robotic Trials", "Apr 12, 2025, 11:00 PM"], |
| 32 | + ["Moving about on Planet Y", "Apr 13, 2025, 7:00 PM"], |
| 33 | + ["Finding ELDRIC", "Apr 14, 2025, 7:00 PM"], |
| 34 | + ["Know Your Environment", "Apr 15, 2025, 7:00 PM"], |
| 35 | + ["Corrective Sky Surgery", "Apr 16, 2025, 7:00 PM"], |
| 36 | + ["Reuse Your Pairs", "Apr 17, 2025, 7:00 PM"], |
| 37 | + ["Streaming the Anomaly", "Apr 18, 2025, 7:00 PM"], |
| 38 | + ["The Anomaly in Focus", "Apr 19, 2025, 7:00 PM"], |
| 39 | + ["The Essence of the Source", "Apr 20, 2025, 7:00 PM"], |
| 40 | +]; |
| 41 | +export class NodeDependenciesProvider |
| 42 | + implements vscode.TreeDataProvider<Dependency | DependencyFolder> |
| 43 | +{ |
| 44 | + constructor(private workspaceRoot: string) {} |
| 45 | + |
| 46 | + // impl |
| 47 | + getTreeItem(element: Dependency): vscode.TreeItem { |
| 48 | + return element; |
| 49 | + } |
| 50 | + |
| 51 | + // impl |
| 52 | + getChildren( |
| 53 | + element?: Dependency, |
| 54 | + ): Thenable<(Dependency | DependencyFolder)[]> { |
| 55 | + if (!element) { |
| 56 | + return Promise.resolve([ |
| 57 | + new DependencyFolder( |
| 58 | + "Missions", |
| 59 | + "", |
| 60 | + vscode.TreeItemCollapsibleState.Expanded, |
| 61 | + ), |
| 62 | + new DependencyFolder( |
| 63 | + "Quests", |
| 64 | + "", |
| 65 | + vscode.TreeItemCollapsibleState.Collapsed, |
| 66 | + ), |
| 67 | + new DependencyFolder( |
| 68 | + "Path", |
| 69 | + "", |
| 70 | + vscode.TreeItemCollapsibleState.Collapsed, |
| 71 | + ), |
| 72 | + ]); |
| 73 | + } |
| 74 | + return Promise.resolve( |
| 75 | + hardcode.map( |
| 76 | + (entry) => |
| 77 | + new Dependency( |
| 78 | + entry[0], |
| 79 | + entry[1], |
| 80 | + vscode.TreeItemCollapsibleState.None, |
| 81 | + ), |
| 82 | + ), |
| 83 | + ); |
| 84 | + |
| 85 | + // console.log("Attempt to child") |
| 86 | + // if (!this.workspaceRoot) { |
| 87 | + // vscode.window.showInformationMessage("No dependency in empty workspace"); |
| 88 | + // return Promise.resolve([]); |
| 89 | + // } |
| 90 | + |
| 91 | + // if (element) { |
| 92 | + // return Promise.resolve( |
| 93 | + // this.getDepsInPackageJson( |
| 94 | + // path.join( |
| 95 | + // this.workspaceRoot, |
| 96 | + // "node_modules", |
| 97 | + // element.label, |
| 98 | + // "package.json", |
| 99 | + // ), |
| 100 | + // ), |
| 101 | + // ); |
| 102 | + // } else { |
| 103 | + // const packageJsonPath = path.join(this.workspaceRoot, "package.json"); |
| 104 | + // if (this.pathExists(packageJsonPath)) { |
| 105 | + // return Promise.resolve(this.getDepsInPackageJson(packageJsonPath)); |
| 106 | + // } else { |
| 107 | + // vscode.window.showInformationMessage("Workspace has no package.json"); |
| 108 | + // return Promise.resolve([]); |
| 109 | + // } |
| 110 | + // } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Given the path to package.json, read all its dependencies and devDependencies. |
| 115 | + */ |
| 116 | + private getDepsInPackageJson(packageJsonPath: string): Dependency[] { |
| 117 | + if (this.pathExists(packageJsonPath)) { |
| 118 | + const toDep = (moduleName: string, version: string): Dependency => { |
| 119 | + if ( |
| 120 | + this.pathExists( |
| 121 | + path.join(this.workspaceRoot, "node_modules", moduleName), |
| 122 | + ) |
| 123 | + ) { |
| 124 | + return new Dependency( |
| 125 | + moduleName, |
| 126 | + version, |
| 127 | + vscode.TreeItemCollapsibleState.Collapsed, |
| 128 | + ); |
| 129 | + } else { |
| 130 | + return new Dependency( |
| 131 | + moduleName, |
| 132 | + version, |
| 133 | + vscode.TreeItemCollapsibleState.None, |
| 134 | + ); |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")); |
| 139 | + |
| 140 | + const deps = packageJson.dependencies |
| 141 | + ? Object.keys(packageJson.dependencies).map((dep) => |
| 142 | + toDep(dep, packageJson.dependencies[dep]), |
| 143 | + ) |
| 144 | + : []; |
| 145 | + const devDeps = packageJson.devDependencies |
| 146 | + ? Object.keys(packageJson.devDependencies).map((dep) => |
| 147 | + toDep(dep, packageJson.devDependencies[dep]), |
| 148 | + ) |
| 149 | + : []; |
| 150 | + return deps.concat(devDeps); |
| 151 | + } else { |
| 152 | + return []; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private pathExists(p: string): boolean { |
| 157 | + try { |
| 158 | + fs.accessSync(p); |
| 159 | + } catch (err) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + return true; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +class DependencyFolder extends vscode.TreeItem { |
| 167 | + constructor( |
| 168 | + public readonly label: string, |
| 169 | + private version: string, |
| 170 | + public readonly collapsibleState: vscode.TreeItemCollapsibleState, |
| 171 | + ) { |
| 172 | + super(label, collapsibleState); |
| 173 | + this.tooltip = `${this.label}-${this.version}`; |
| 174 | + this.description = this.version; |
| 175 | + } |
| 176 | + |
| 177 | + iconPath = { |
| 178 | + light: path.join(__filename, "..", "..", "assets", "icon.png"), |
| 179 | + dark: path.join(__filename, "..", "..", "assets", "icon.png"), |
| 180 | + }; |
| 181 | +} |
| 182 | + |
| 183 | +class Dependency extends vscode.TreeItem { |
| 184 | + constructor( |
| 185 | + public readonly label: string, |
| 186 | + private version: string, |
| 187 | + public readonly collapsibleState: vscode.TreeItemCollapsibleState, |
| 188 | + ) { |
| 189 | + super(label, collapsibleState); |
| 190 | + this.tooltip = `${this.label}-${this.version}`; |
| 191 | + this.description = this.version; |
| 192 | + } |
| 193 | +} |
0 commit comments