Skip to content

Commit

Permalink
feat: execute script
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 18, 2019
1 parent 0b31b46 commit 4ff335b
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
out/
dist/
examples/**/*.*
vendor/**/*.*
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"editor.formatOnSave": true,
"editor.defaultFormatter": "vscode.json-language-features"
},
"typescript.tsdk": "node_modules\\typescript\\lib"
"typescript.tsdk": "node_modules\\typescript\\lib",
"cSpell.words": [
"wenyan"
]
}
53 changes: 53 additions & 0 deletions package-lock.json

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

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
"scopeName": "source.wenyan",
"path": "./syntaxes/wenyan.tmGrammar.json"
}
],
"commands": [
{
"command": "extension.wenyan-lang.execute",
"title": "Execute Script",
"category": "文言 Wenyan"
}
]
},
"standard-version": {
Expand Down
14 changes: 14 additions & 0 deletions src/commands/execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { window } from 'vscode'
import { Exec } from '../exec'

export async function execute () {
const document = window.activeTextEditor?.document
if (document?.languageId === 'wenyan') {
try {
console.log(await Exec(document.uri.fsPath, { exec: true }))
}
catch (e) {
console.error(e)
}
}
}
12 changes: 12 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { commands } from 'vscode'
import { ExtensionModule } from '../module'
import { execute } from './execute'
import { CommandKeys } from './keys'

const m: ExtensionModule = () => {
return [
commands.registerCommand(CommandKeys.execute, execute),
]
}

export default m
4 changes: 4 additions & 0 deletions src/commands/keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export enum CommandKeys {
execute = 'extension.wenyan-lang.execute',
}
11 changes: 11 additions & 0 deletions src/ctx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ExtensionContext } from 'vscode'

let _ctx: ExtensionContext

export function setCTX (ctx: ExtensionContext) {
_ctx = ctx
}

export function getCTX () {
return _ctx
}
54 changes: 54 additions & 0 deletions src/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import path from 'path'
import cp from 'child_process'
import { getCTX } from './ctx'

export interface ExecuteOptions {
exec?: boolean
lang?: 'js' | 'py'
roman?: string
}

export function getWenyanPath () {
const ctx = getCTX()
// TODO: configurable
return path.join(ctx.extensionPath, 'vendor', 'wenyan.js')
}

export function getOptionsString (options?: ExecuteOptions) {
const parts = []
if (!options)
return ''

if (options.exec) {
parts.push('--exec')
parts.push('true')
}

if (options.roman) {
parts.push('--roman')
parts.push(options.roman)
}

if (options.lang) {
parts.push('--lang')
parts.push(options.lang)
}

return parts.join(' ')
}

export function Exec (filename: string, options?: ExecuteOptions) {
const cmd = `node "${getWenyanPath()}" "${filename}" ${getOptionsString(options)}`
console.log(`Executing ${cmd}`)
return new Promise<string>((resolve, reject) => {
cp.exec(cmd, (err, stdout, stderr) => {
if (err) {
console.log(`error: ${err}`)
reject(err)
}
else {
resolve(stdout)
}
})
})
}
5 changes: 5 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { ExtensionContext } from 'vscode'
import dynamicSnippets from './editor/dynamicSnippets'
import commands from './commands'
import { setCTX } from './ctx'

export function activate (ctx: ExtensionContext) {
setCTX(ctx)

const modules = [
dynamicSnippets,
commands,
]

const disposables = modules.flatMap(m => m(ctx))
Expand Down
2 changes: 2 additions & 0 deletions vendor/wenyan.js

Large diffs are not rendered by default.

0 comments on commit 4ff335b

Please sign in to comment.