From 81998a87a1b236a1ead60931b730e572a4fd56a6 Mon Sep 17 00:00:00 2001 From: Jon Manning Date: Fri, 24 May 2024 15:45:44 +1000 Subject: [PATCH] Add command to create new .yarnproject files --- CHANGELOG.md | 1 + package.json | 8 +++++++- src/extension.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f4a438..5fa0c5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Updated VS Code engine from 1.63 to 1.74. - Commands that depend on the language server being online will now only appear in the command palette if the language server has started. - The extension will now activate when the workspace contains a .yarn or .yarnproject file, rather than waiting for a .yarn file to be opened. +- Added a command to create a new `.yarnproject` file in the workspace. ### Removed diff --git a/package.json b/package.json index 83ce7e6..9447bb7 100644 --- a/package.json +++ b/package.json @@ -139,7 +139,13 @@ "command": "yarnspinner.exportDebugOutput", "category": "Yarn Spinner", "title": "Export Debug Output", - "enablement": "config.yarnspinner.enableExperimentalFeatures" + "enablement": "config.yarnspinner.enableExperimentalFeatures && yarnspinner.languageServerLaunched" + }, + { + "command": "yarnspinner.createProject", + "category": "Yarn Spinner", + "title": "Create New Yarn Project...", + "enablement": "yarnspinner.languageServerLaunched" } ], "configurationDefaults": { diff --git a/src/extension.ts b/src/extension.ts index bb3843d..7670a23 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -520,6 +520,36 @@ async function launchLanguageServer(context: vscode.ExtensionContext, configs: v }); })); + context.subscriptions.push(vscode.commands.registerCommand("yarnspinner.createProject", async () => { + // Create a new Yarn Project. + + const workspaceURI = getActiveWorkspaceUri(); + + let defaultDestinationURI: vscode.Uri | undefined; + if (workspaceURI) { + defaultDestinationURI = vscode.Uri.joinPath(workspaceURI, `Project.yarnproject`); + } + + const destinationUri = await vscode.window.showSaveDialog({ + defaultUri: defaultDestinationURI + }); + + if (!destinationUri) { + return; + } + + const params: languageClient.ExecuteCommandParams = { + command: "yarnspinner.getEmptyYarnProjectJSON", + arguments: [ ] + }; + + + let json = await client.sendRequest(languageClient.ExecuteCommandRequest.type, params); + fs.writeFileSync(destinationUri.fsPath, json); + + vscode.commands.executeCommand('revealInExplorer', destinationUri); + })) + // Enable commands that depend upon the language server being online and the above commands being registered vscode.commands.executeCommand('setContext', 'yarnspinner.languageServerLaunched', true);