Skip to content

Commit

Permalink
feat: config to specify executable path
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 19, 2019
1 parent ba7215f commit d9821c7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -52,6 +52,7 @@ Punctuations will be automatically replaced to fit the language.
- [x] Dynamic Snippets
- [x] Execute
- [x] Compile
- [ ] Compile to Python
- [ ] Code Completion
- [ ] Language Server
- [ ] Rendering
Expand Down
25 changes: 25 additions & 0 deletions src/config.ts
@@ -0,0 +1,25 @@
import path from 'path'
import { workspace } from 'vscode'
import { EXT_NAMESPACE } from './meta'
import { getCTX } from './ctx'

export class Config {
static get executablePath () {
return this.getConfig<string>('executablePath') || path.join(getCTX().extensionPath, 'vendor', 'wenyan.js')
}

private static getConfig<T = any> (key: string): T | undefined {
const config = workspace
.getConfiguration(EXT_NAMESPACE)
.get<T>(key)

return config
}

// @ts-ignore
private static async setConfig (key: string, value: any, isGlobal = false) {
return await workspace
.getConfiguration(EXT_NAMESPACE)
.update(key, value, isGlobal)
}
}
11 changes: 2 additions & 9 deletions src/exec.ts
@@ -1,7 +1,6 @@
import path from 'path'
import cp from 'child_process'
import { getCTX } from './ctx'
import { Log } from './log'
import { Config } from './config'

export interface ExecuteOptions {
exec?: boolean
Expand All @@ -11,12 +10,6 @@ export interface ExecuteOptions {
output?: 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)
Expand All @@ -33,7 +26,7 @@ export function getOptionsString (options?: ExecuteOptions) {
}

export function Exec (filename: string, options?: ExecuteOptions) {
const cmd = `node "${getWenyanPath()}" "${filename}" ${getOptionsString(options)}`
const cmd = `node "${Config.executablePath}" "${filename}" ${getOptionsString(options)}`
Log.info(`💻 ${cmd}`)
return new Promise<string>((resolve, reject) => {
cp.exec(cmd, (err, stdout, stderr) => {
Expand Down

0 comments on commit d9821c7

Please sign in to comment.