From 4c5819c0430c94b48fff2e77a8c054fc239beb38 Mon Sep 17 00:00:00 2001 From: DudeBro249 Date: Wed, 23 Jun 2021 23:33:15 -0700 Subject: [PATCH] :sparkles: feat(extension): finish init command --- extension/package-lock.json | 16 +++++++++++++++- extension/package.json | 7 ++++--- extension/src/commands/init.ts | 34 ++++++++++++++++++++++++++++++++++ extension/src/extension.ts | 23 +++-------------------- extension/src/utils/command.ts | 31 +++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 24 deletions(-) create mode 100644 extension/src/commands/init.ts create mode 100644 extension/src/utils/command.ts diff --git a/extension/package-lock.json b/extension/package-lock.json index e241d34..51faa22 100644 --- a/extension/package-lock.json +++ b/extension/package-lock.json @@ -1,11 +1,15 @@ { - "name": "docrunner", + "name": "docrunner-extension", "version": "0.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { + "name": "docrunner-extension", "version": "0.0.1", + "dependencies": { + "spawn-command": "^0.0.2-1" + }, "devDependencies": { "@types/glob": "^7.1.3", "@types/mocha": "^8.2.2", @@ -2999,6 +3003,11 @@ "source-map": "^0.6.0" } }, + "node_modules/spawn-command": { + "version": "0.0.2-1", + "resolved": "https://registry.npmjs.org/spawn-command/-/spawn-command-0.0.2-1.tgz", + "integrity": "sha1-YvXpRmmBwbeW3Fkpk34RycaSG9A=" + }, "node_modules/sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", @@ -5898,6 +5907,11 @@ "source-map": "^0.6.0" } }, + "spawn-command": { + "version": "0.0.2-1", + "resolved": "https://registry.npmjs.org/spawn-command/-/spawn-command-0.0.2-1.tgz", + "integrity": "sha1-YvXpRmmBwbeW3Fkpk34RycaSG9A=" + }, "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", diff --git a/extension/package.json b/extension/package.json index 9c8d150..e996112 100644 --- a/extension/package.json +++ b/extension/package.json @@ -10,14 +10,15 @@ "Other" ], "activationEvents": [ - "onCommand:docrunner.helloWorld" + "onCommand:docrunner.init" ], "main": "./dist/extension.js", "contributes": { "commands": [ { - "command": "docrunner.helloWorld", - "title": "Hello World" + "command": "docrunner.init", + "category": "Docrunner", + "title": "Init" } ] }, diff --git a/extension/src/commands/init.ts b/extension/src/commands/init.ts new file mode 100644 index 0000000..bad4ed7 --- /dev/null +++ b/extension/src/commands/init.ts @@ -0,0 +1,34 @@ +import * as vscode from 'vscode' +import { streamingRunCommand } from '../utils/command' + +const init = () => { + vscode.window.withProgress( + { + location: vscode.ProgressLocation.Notification, + title: 'Running docrunner init', + cancellable: true, + }, + async (_progress, token) => { + token.onCancellationRequested((_event) => { + vscode.window.showInformationMessage('Cancelled!') + }) + + const rootPath = vscode.workspace.workspaceFolders![0].uri.fsPath + + streamingRunCommand('docrunner', ['init'], { + onStdout: (data) => { + console.log(data.toString()) + }, + onStdErr: (error) => { + console.log(error.toString()) + }, + spawnOptions: { cwd: rootPath }, + }) + } + ) +} + +export const registerInitCommand = (context: vscode.ExtensionContext) => { + const initCommand = vscode.commands.registerCommand('docrunner.init', init) + context.subscriptions.push(initCommand) +} diff --git a/extension/src/extension.ts b/extension/src/extension.ts index 391802c..a84b185 100644 --- a/extension/src/extension.ts +++ b/extension/src/extension.ts @@ -1,25 +1,8 @@ -// The module 'vscode' contains the VS Code extensibility API -// Import the module and reference it with the alias vscode in your code below -import * as vscode from 'vscode'; +import * as vscode from 'vscode' +import { registerInitCommand } from './commands/init' -// this method is called when your extension is activated -// your extension is activated the very first time the command is executed export function activate(context: vscode.ExtensionContext) { - - // Use the console to output diagnostic information (console.log) and errors (console.error) - // This line of code will only be executed once when your extension is activated - console.log('Congratulations, your extension "docrunner" is now active!'); - - // The command has been defined in the package.json file - // Now provide the implementation of the command with registerCommand - // The commandId parameter must match the command field in package.json - let disposable = vscode.commands.registerCommand('docrunner.helloWorld', () => { - // The code you place here will be executed every time your command is executed - // Display a message box to the user - vscode.window.showInformationMessage('Hello World from Docrunner!'); - }); - - context.subscriptions.push(disposable); + registerInitCommand(context) } // this method is called when your extension is deactivated diff --git a/extension/src/utils/command.ts b/extension/src/utils/command.ts new file mode 100644 index 0000000..a7a8eb3 --- /dev/null +++ b/extension/src/utils/command.ts @@ -0,0 +1,31 @@ +import { + ChildProcessWithoutNullStreams, + spawn, + SpawnOptionsWithoutStdio, +} from 'child_process' + +interface StreamingRunCommandOptions { + onStdout: (data: Buffer) => void + onStdErr: (error: Buffer) => void + onExit?: (exitCode: number | null) => void + spawnOptions: SpawnOptionsWithoutStdio +} + +export const streamingRunCommand = ( + entryPoint: string, + commandArgs: string[], + options: StreamingRunCommandOptions +): ChildProcessWithoutNullStreams => { + const { onStdout, onStdErr, onExit, spawnOptions } = options + + const process = spawn(entryPoint, commandArgs, spawnOptions) + + process.stdout.on('data', onStdout) + + process.stderr.on('data', onStdErr) + + if (onExit) { + process.on('exit', onExit) + } + return process +}