Skip to content

Commit

Permalink
Merge pull request #75 from DudeBro249/feat/run-command
Browse files Browse the repository at this point in the history
feat(extension): finish run command
  • Loading branch information
srinivasayush authored Jun 26, 2021
2 parents 793e68c + 81df6ab commit fca93cf
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 60 deletions.
1 change: 0 additions & 1 deletion extension/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
},
{
"name": "Extension Tests",
Expand Down
16 changes: 0 additions & 16 deletions extension/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": [
"$ts-webpack-watch",
"$tslint-webpack-watch"
],
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "npm",
"script": "test-watch",
Expand Down
18 changes: 2 additions & 16 deletions extension/package-lock.json

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

11 changes: 10 additions & 1 deletion extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"Other"
],
"activationEvents": [
"onCommand:docrunner.init"
"onCommand:docrunner.init",
"onCommand:docrunner.run"
],
"main": "./dist/extension.js",
"contributes": {
Expand All @@ -19,6 +20,11 @@
"command": "docrunner.init",
"category": "Docrunner",
"title": "Init"
},
{
"command": "docrunner.run",
"category": "Docrunner",
"title": "Run"
}
]
},
Expand Down Expand Up @@ -48,5 +54,8 @@
"vscode-test": "^1.5.2",
"webpack": "^5.38.1",
"webpack-cli": "^4.7.0"
},
"dependencies": {
"strip-ansi": "^6.0.0"
}
}
31 changes: 6 additions & 25 deletions extension/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
import * as vscode from 'vscode'
import { streamingRunCommand } from '../utils/command'
import { runCommandWithProgressOutput } 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 },
})
}
)
runCommandWithProgressOutput('docrunner', ['init'], {
location: vscode.ProgressLocation.Notification,
title: 'Running docrunner run',
cancellable: true,
})
}

export const registerInitCommand = (context: vscode.ExtensionContext) => {
Expand Down
16 changes: 16 additions & 0 deletions extension/src/commands/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import stripAnsi = require('strip-ansi')
import * as vscode from 'vscode'
import { runCommandWithProgressOutput } from '../utils/command'

const run = () => {
runCommandWithProgressOutput('docrunner', ['run'], {
location: vscode.ProgressLocation.Notification,
title: 'Running docrunner run',
cancellable: true,
})
}

export const registerRunCommand = (context: vscode.ExtensionContext) => {
const runCommand = vscode.commands.registerCommand('docrunner.run', run)
context.subscriptions.push(runCommand)
}
2 changes: 2 additions & 0 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as vscode from 'vscode'
import { registerInitCommand } from './commands/init'
import { registerRunCommand } from './commands/run'

export function activate(context: vscode.ExtensionContext) {
registerInitCommand(context)
registerRunCommand(context)
}

// this method is called when your extension is deactivated
Expand Down
33 changes: 32 additions & 1 deletion extension/src/utils/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
spawn,
SpawnOptionsWithoutStdio,
} from 'child_process'
import stripAnsi = require('strip-ansi')
import * as vscode from 'vscode'

interface StreamingRunCommandOptions {
onStdout: (data: Buffer) => void
Expand All @@ -11,7 +13,7 @@ interface StreamingRunCommandOptions {
spawnOptions: SpawnOptionsWithoutStdio
}

export const streamingRunCommand = (
export const runCommandWithOutputCallbacks = (
entryPoint: string,
commandArgs: string[],
options: StreamingRunCommandOptions
Expand All @@ -29,3 +31,32 @@ export const streamingRunCommand = (
}
return process
}

export const runCommandWithProgressOutput = (
entryPoint: string,
commandArgs: string[],
options: vscode.ProgressOptions
) => {
vscode.window.withProgress(options, async (_progress, token) => {
token.onCancellationRequested((_event) => {
vscode.window.showInformationMessage('Cancelled!')
})

const rootPath = vscode.workspace.workspaceFolders![0].uri.fsPath

runCommandWithOutputCallbacks(entryPoint, commandArgs, {
onStdout: async (data) => {
const dataString = stripAnsi(data.toString())
if (dataString.includes('Warning')) {
await vscode.window.showWarningMessage(dataString)
}
await vscode.window.showInformationMessage(dataString)
},
onStdErr: async (error) => {
const errorString = stripAnsi(error.toString())
await vscode.window.showErrorMessage(errorString)
},
spawnOptions: { cwd: rootPath },
})
})
}

0 comments on commit fca93cf

Please sign in to comment.