Skip to content
This repository was archived by the owner on Jan 10, 2019. It is now read-only.

Commit 88439d4

Browse files
committed
added $, $readFile and $readJSON functions for quick executions
1 parent cd23704 commit 88439d4

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log (vs-script-commands)
22

3+
## 4.8.0 (May 2nd, 2017; quick execution functions)
4+
5+
* added `$` and `$readJSON()` and `$readString()` functions for [quick executions](https://github.com/mkloubert/vs-script-commands#quick-execution-)
6+
37
## 4.7.0 (May 2nd, 2017; hashes and UUIDs)
48

59
* added `$guid()`, `$hash()`, `$md5()`, `$sha1()`, `$sha256()` and `$uuid()` functions for [quick executions](https://github.com/mkloubert/vs-script-commands#quick-execution-)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ Add a `script.commands` section:
5555
| Name | Description |
5656
| ---- | --------- |
5757
| `commands` | One or more [commands](#commands-) to register. |
58+
| `disableNewVersionPopups` | Disables the display of popups that reports for a new version of that extension. Default `(false)` |
5859
| `globals` | Global data available for ALL commands defined by that extension. |
60+
| `quick` | Settings for [quick execution](#quick-execution-) feature. |
5961
| `showOutput` | Open output on startup or not. Default `(true)` |
6062
| `showInternalVSCommands` | Show internal Visual Studio Code commands in GUI or not. Default `(false)` |
6163

@@ -284,6 +286,7 @@ You can define custom settings for the feature:
284286
| Name | Description |
285287
| ---- | --------- |
286288
| `cwd` | The initial current directory for the executions. |
289+
| `disableHexView` | Do not show binary data in 'hex view'. Default: `(false)` |
287290
| `noResultInfo` | Do not show results of executions. Default: `(false)` |
288291
| `showResultInTab` | Show results in tab instead of a popup or not. Default: `(false)` |
289292
| `state` | The initial state value. |

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vs-script-commands",
33
"displayName": "Script Commands",
44
"description": "Adds additional commands to Visual Studio Code that uses scripts (JavaScript) for execution.",
5-
"version": "4.7.0",
5+
"version": "4.8.0",
66
"publisher": "mkloubert",
77
"engines": {
88
"vscode": "^1.6.0"
@@ -456,6 +456,7 @@
456456
"html-entities": "^1.2.1",
457457
"marked": "^0.3.6",
458458
"moment": "^2.17.1",
459+
"node-workflows": "^1.3.1",
459460
"tmp": "0.0.31",
460461
"uuid": "^3.0.1"
461462
}

src/quick.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import * as sc_controller from './controller';
3636
import * as sc_helpers from './helpers';
3737
import * as UUID from 'uuid';
3838
import * as vscode from 'vscode';
39+
import * as Workflows from 'node-workflows';
3940

4041

4142
/**
@@ -205,6 +206,7 @@ function _generateHelpHTML(): string {
205206
markdown += "## Functions\n";
206207
markdown += "| Name | Description |\n";
207208
markdown += "| ---- | --------- |\n";
209+
markdown += "| `$(...results: any[]): Promise<any[]>` | Executes a list of actions and returns its results. |\n";
208210
markdown += "| `$asString(val: any): string` | Returns a value as string. |\n";
209211
markdown += "| `$cwd(newPath?: string, permanent?: boolean = false): string` | Gets or sets the current working directory for the execution. |\n";
210212
markdown += "| `$disableHexView(flag?: boolean, permanent?: boolean = false): boolean` | Gets or sets if 'hex view' for binary results should be disabled or not. |\n";
@@ -228,6 +230,8 @@ function _generateHelpHTML(): string {
228230
markdown += "| `$now(): Moment.Moment` | Returns the current [time](https://momentjs.com/docs/). |\n";
229231
markdown += "| `$openHtml(html: string, tabTitle?: string): vscode.Thenable<any>` | Opens a HTML document in a new tab. |\n";
230232
markdown += "| `$readFile(path: string): Buffer` | Reads the data of a file. |\n";
233+
markdown += "| `$readJSON(file: string, encoding?: string = 'utf8'): any` | Reads a JSON file and returns the its object / value. |\n";
234+
markdown += "| `$readString(file: string, encoding?: string = 'utf8'): any` | Reads a file as string. |\n";
231235
markdown += "| `$require(id: string): any` | Loads a module from execution / extension context. |\n";
232236
markdown += "| `$setState(newValue: any): any` | Sets the value of `$state` variable and returns the new value. |\n";
233237
markdown += "| `$sha1(data: any, asBuffer: boolean = false): string` | Hashes data by SHA-1. |\n";
@@ -404,6 +408,49 @@ export function quickExecution() {
404408
});
405409

406410
let $args: any[];
411+
const $ = function(...args: any[]): Promise<any[]> {
412+
return new Promise<any>((resolve, reject) => {
413+
let wf = Workflows.create();
414+
415+
wf.next((ctx) => {
416+
ctx.result = [];
417+
});
418+
419+
if (args) {
420+
args.forEach(a => {
421+
wf.next((ctx) => {
422+
let result: any[] = ctx.result;
423+
424+
return new Promise<any>((res, rej) => {
425+
try {
426+
Promise.resolve(a).then((r) => {
427+
try {
428+
result.push(r);
429+
430+
res();
431+
}
432+
catch (e) {
433+
rej(e);
434+
}
435+
}).catch((err) => {
436+
rej(err);
437+
});
438+
}
439+
catch (e) {
440+
rej(e);
441+
}
442+
});
443+
});
444+
});
445+
}
446+
447+
return wf.start().then((result: any[]) => {
448+
resolve(result);
449+
}).catch((err) => {
450+
reject(err);
451+
});
452+
});
453+
};
407454
const $asString = function(val: any, enc?: string): string {
408455
if (sc_helpers.isNullOrUndefined(val)) {
409456
return val;
@@ -589,14 +636,40 @@ export function quickExecution() {
589636
return $me.openHtml(html, title);
590637
};
591638
const $output = $me.outputChannel;
592-
const $readFile = function(file: string) {
639+
const $readFile = function(file: string): Buffer {
593640
file = sc_helpers.toStringSafe(file);
594641
if (!Path.isAbsolute(file)) {
595642
file = Path.join(_currentDir, file);
596643
}
597644

598645
return FS.readFileSync(file);
599646
};
647+
const $readJSON = function(file: string, enc = 'utf8'): any {
648+
file = sc_helpers.toStringSafe(file);
649+
if (!Path.isAbsolute(file)) {
650+
file = Path.join(_currentDir, file);
651+
}
652+
653+
enc = sc_helpers.normalizeString(enc);
654+
if ('' === enc) {
655+
enc = 'utf8';
656+
}
657+
658+
return JSON.parse( FS.readFileSync(file).toString(enc) );
659+
};
660+
const $readString = function(file: string, enc = 'utf8'): string {
661+
file = sc_helpers.toStringSafe(file);
662+
if (!Path.isAbsolute(file)) {
663+
file = Path.join(_currentDir, file);
664+
}
665+
666+
enc = sc_helpers.normalizeString(enc);
667+
if ('' === enc) {
668+
enc = 'utf8';
669+
}
670+
671+
return FS.readFileSync(file).toString(enc);
672+
};
600673
const $require = function(id: string): any {
601674
return require(sc_helpers.toStringSafe(id));
602675
};

0 commit comments

Comments
 (0)