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

Commit cd23704

Browse files
committed
added hashing and UUID functions for quick execution
1 parent 3c6a8df commit cd23704

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-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.7.0 (May 2nd, 2017; hashes and UUIDs)
4+
5+
* added `$guid()`, `$hash()`, `$md5()`, `$sha1()`, `$sha256()` and `$uuid()` functions for [quick executions](https://github.com/mkloubert/vs-script-commands#quick-execution-)
6+
37
## 4.6.0 (May 1st, 2017; hex view of binary files)
48

59
* [Buffer](https://nodejs.org/api/buffer.html) results are displayed in "hex view" now, when displaying in tab

package.json

Lines changed: 4 additions & 2 deletions
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.6.0",
5+
"version": "4.7.0",
66
"publisher": "mkloubert",
77
"engines": {
88
"vscode": "^1.6.0"
@@ -432,6 +432,7 @@
432432
"@types/mocha": "^2.2.32",
433433
"@types/node": "^6.0.40",
434434
"@types/tmp": "0.0.32",
435+
"@types/uuid": "^2.0.29",
435436
"mocha": "^2.3.3",
436437
"typescript": "^2.0.3",
437438
"vscode": "^1.0.0"
@@ -455,6 +456,7 @@
455456
"html-entities": "^1.2.1",
456457
"marked": "^0.3.6",
457458
"moment": "^2.17.1",
458-
"tmp": "0.0.31"
459+
"tmp": "0.0.31",
460+
"uuid": "^3.0.1"
459461
}
460462
}

src/helpers.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// DEALINGS IN THE SOFTWARE.
2525

2626
import * as ChildProcess from 'child_process';
27+
import * as Crypto from 'crypto';
2728
import * as FS from 'fs';
2829
import * as Path from 'path';
2930
import * as Moment from 'moment';
@@ -174,6 +175,32 @@ export function getUrlParam(params: Object, name: string): string {
174175
}
175176
}
176177

178+
/**
179+
* Hashes data.
180+
*
181+
* @param {string} algo The algorithm to use.
182+
* @param {string|Buffer} data
183+
* @param {boolean} [raw] Return hash in binary format or as (hext) string.
184+
*/
185+
export function hash(algo: string, data: string | Buffer, asBuffer = false): string | Buffer {
186+
if (isNullOrUndefined(data)) {
187+
return data;
188+
}
189+
190+
algo = normalizeString(algo);
191+
if ('' === algo) {
192+
algo = 'sha256';
193+
}
194+
195+
asBuffer = toBooleanSafe(asBuffer);
196+
197+
let hash = Crypto.createHash(algo)
198+
.update(data)
199+
.digest();
200+
201+
return asBuffer ? hash : hash.toString('hex');
202+
}
203+
177204
/**
178205
* Checks if the string representation of a value is empty
179206
* or contains whitespaces only.

src/quick.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import * as Path from 'path';
3434
import * as sc_contracts from './contracts';
3535
import * as sc_controller from './controller';
3636
import * as sc_helpers from './helpers';
37+
import * as UUID from 'uuid';
3738
import * as vscode from 'vscode';
3839

3940

@@ -214,21 +215,27 @@ function _generateHelpHTML(): string {
214215
markdown += "| `$exists(path: string): boolean` | Checks if a path exists. |\n";
215216
markdown += "| `$findFiles(globPattern: string, ignore?: string[]): string[]` | Finds files using [glob patterns](https://github.com/isaacs/node-glob). |\n";
216217
markdown += "| `$fromMarkdown(markdown: string): string` | Converts [Markdown](https://guides.github.com/features/mastering-markdown/) to HTML. |\n";
218+
markdown += "| `$guid(v4: boolean = true): string` | Alias for `$uuid`. |\n";
219+
markdown += "| `$hash(algorithm: string, data: any, asBuffer: boolean = false): string` | Hashes data. |\n";
217220
markdown += "| `$help(): vscode.Thenable<any>` | Shows this help document. |\n";
218221
markdown += "| `$htmlEncode(str: string): string` | Encodes the HTML entities in a string. |\n";
219222
markdown += "| `$info(msg: string): vscode.Thenable<any>` | Shows an info popup. |\n";
220223
markdown += "| `$log(msg: any): void` | Logs a message. |\n";
221224
markdown += "| `$lstat(path: string): fs.Stats` | Gets information about a path. |\n";
225+
markdown += "| `$md5(data: any, asBuffer: boolean = false): string` | Hashes data by MD5. |\n";
222226
markdown += "| `$mkdir(dir: string): void` | Creates a directory (with all its sub directories). |\n";
223227
markdown += "| `$noResultInfo(flag?: boolean, permanent?: boolean = false): boolean` | Gets or sets if result should be displayed or not. |\n";
224228
markdown += "| `$now(): Moment.Moment` | Returns the current [time](https://momentjs.com/docs/). |\n";
225229
markdown += "| `$openHtml(html: string, tabTitle?: string): vscode.Thenable<any>` | Opens a HTML document in a new tab. |\n";
226230
markdown += "| `$readFile(path: string): Buffer` | Reads the data of a file. |\n";
227231
markdown += "| `$require(id: string): any` | Loads a module from execution / extension context. |\n";
228232
markdown += "| `$setState(newValue: any): any` | Sets the value of `$state` variable and returns the new value. |\n";
233+
markdown += "| `$sha1(data: any, asBuffer: boolean = false): string` | Hashes data by SHA-1. |\n";
234+
markdown += "| `$sha256(data: any, asBuffer: boolean = false): string` | Hashes data by SHA-256. |\n";
229235
markdown += "| `$showResultInTab(flag?: boolean, permanent?: boolean = false): boolean` | Gets or sets if result should be shown in a tab window or a popup. |\n";
230236
markdown += "| `$toHexView(val: any): string` | Converts a value, like a buffer or string, to 'hex view'. |\n";
231237
markdown += "| `$unlink(path: string): boolean` | Removes a file or folder. |\n";
238+
markdown += "| `$uuid(v4: boolean = true): string` | Generates a new unique ID. |\n";
232239
markdown += "| `$warn(msg: string): vscode.Thenable<any>` | Shows a warning popup. |\n";
233240
markdown += "| `$writeFile(path: string, data: any): void` | Writes data to a file. |\n";
234241
markdown += "\n";
@@ -521,6 +528,12 @@ export function quickExecution() {
521528
return _fromMarkdown(markdown);
522529
};
523530
const $globals = $me.getGlobals();
531+
const $guid = function(v4 = true) {
532+
return sc_helpers.toBooleanSafe(v4) ? UUID.v4() : UUID.v1();
533+
};
534+
const $hash = function(algo: string, data: string | Buffer, asBuffer = false): string | Buffer {
535+
return sc_helpers.hash(algo, data, asBuffer);
536+
};
524537
const $help = function() {
525538
return $me.openHtml(_generateHelpHTML(),
526539
'[vs-script-commands] Quick execution');
@@ -555,6 +568,9 @@ export function quickExecution() {
555568
FSExtra.mkdirsSync(dir);
556569
};
557570
let $maxDepth = 64;
571+
const $md5 = function(data: string | Buffer, asBuffer = false): string | Buffer {
572+
return sc_helpers.hash('md5', data, asBuffer);
573+
};
558574
const $noResultInfo = function(flag?: boolean, permanent = false): boolean {
559575
if (arguments.length > 0) {
560576
_noResultInfo = sc_helpers.toBooleanSafe(flag);
@@ -587,6 +603,12 @@ export function quickExecution() {
587603
const $setState = function(val: any): any {
588604
return $state = val;
589605
};
606+
const $sha1 = function(data: string | Buffer, asBuffer = false): string | Buffer {
607+
return sc_helpers.hash('sha1', data, asBuffer);
608+
};
609+
const $sha256 = function(data: string | Buffer, asBuffer = false): string | Buffer {
610+
return sc_helpers.hash('sha256', data, asBuffer);
611+
};
590612
const $showResultInTab = function(flag?: boolean, permanent = false): boolean {
591613
if (arguments.length > 0) {
592614
_showResultInTab = sc_helpers.toBooleanSafe(flag);
@@ -617,6 +639,9 @@ export function quickExecution() {
617639
FS.unlinkSync(path);
618640
}
619641
};
642+
const $uuid = function(v4 = true) {
643+
return sc_helpers.toBooleanSafe(v4) ? UUID.v4() : UUID.v1();
644+
};
620645
const $warn = function(msg: string) {
621646
return vscode.window
622647
.showWarningMessage( sc_helpers.toStringSafe(msg) );

0 commit comments

Comments
 (0)