Skip to content

Commit

Permalink
Merge pull request #74 from DudeBro249/feat/init-command
Browse files Browse the repository at this point in the history
feat(extension): finish init command
  • Loading branch information
srinivasayush authored Jun 24, 2021
2 parents 7b3ea49 + 4c5819c commit 793e68c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 24 deletions.
16 changes: 15 additions & 1 deletion extension/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
},
Expand Down
34 changes: 34 additions & 0 deletions extension/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -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)
}
23 changes: 3 additions & 20 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 31 additions & 0 deletions extension/src/utils/command.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 793e68c

Please sign in to comment.