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

Commit 6e93522

Browse files
committed
added findFiles() functions and methods
1 parent fdce8c8 commit 6e93522

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Change Log (vs-script-commands)
22

3+
## 4.5.0 (May 1st, 2017; find files)
4+
5+
* added `$findFiles()` function for [quick executions](https://github.com/mkloubert/vs-script-commands#quick-execution-)
6+
* added `findFiles()` method for [ScriptCommandExecutorArguments](https://mkloubert.github.io/vs-script-commands/interfaces/_contracts_.scriptcommandexecutorarguments.html) interface
7+
38
## 4.4.0 (May 1st, 2017; added support for Markdown and HTML parsing)
49

5-
* added `$fromMarkdown()`, `$htmlEncode()` and `$log()` for [quick executions](https://github.com/mkloubert/vs-script-commands#quick-execution-)
10+
* added `$fromMarkdown()`, `$htmlEncode()` and `$log()` functions for [quick executions](https://github.com/mkloubert/vs-script-commands#quick-execution-)
611
* added `fromMarkdown()` and `htmlEncode()` methods for [ScriptCommandExecutorArguments](https://mkloubert.github.io/vs-script-commands/interfaces/_contracts_.scriptcommandexecutorarguments.html) interface
712

813
## 4.3.0 (May 1st, 2017; quick JavaScript execution - $output)

package.json

Lines changed: 1 addition & 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.4.1",
5+
"version": "4.5.0",
66
"publisher": "mkloubert",
77
"engines": {
88
"vscode": "^1.6.0"

src/contracts.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,15 @@ export interface ScriptCommandExecutorArguments {
398398
* Gets the context of that extension.
399399
*/
400400
readonly extension: vscode.ExtensionContext;
401+
/**
402+
* Finds files inside current workspace using glob patterns.
403+
*
404+
* @param {string} pattern The pattern.
405+
* @param {string|string[]} [ignore] The pattern(s) of files to ignore.
406+
*
407+
* @return {Promise<string[]>} The promise.
408+
*/
409+
readonly findFiles: (pattern: string, ignore?: string | string[]) => Promise<string[]>;
401410
/**
402411
* Converts Markdown to HTML.
403412
*

src/controller.ts

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

2626
import * as Events from 'events';
27+
import * as Glob from 'glob';
2728
import * as HtmlEntities from 'html-entities';
2829
import * as Marked from 'marked';
2930
import * as Moment from 'moment';
@@ -760,6 +761,42 @@ export class ScriptCommandController extends Events.EventEmitter implements vsco
760761
},
761762
events: undefined,
762763
extension: undefined,
764+
findFiles: (pattern, ignore?) => {
765+
return new Promise<string[]>((resolve, reject) => {
766+
try {
767+
pattern = sc_helpers.toStringSafe(pattern);
768+
if ('' === pattern.trim()) {
769+
pattern = '**';
770+
}
771+
772+
ignore = sc_helpers.asArray(ignore)
773+
.map(x => sc_helpers.toStringSafe(x))
774+
.filter(x => '' !== x.trim());
775+
776+
Glob(pattern, <any>{
777+
cwd: vscode.workspace.rootPath,
778+
root: vscode.workspace.rootPath,
779+
dot: true,
780+
nocase: true,
781+
nodir: true,
782+
nosort: false,
783+
realpath: false,
784+
ignore: ignore,
785+
absolute: true,
786+
}, (err, files) => {
787+
if (err) {
788+
reject(err);
789+
}
790+
else {
791+
resolve(files);
792+
}
793+
});
794+
}
795+
catch (e) {
796+
reject(e);
797+
}
798+
});
799+
},
763800
fromMarkdown: (markdown) => {
764801
markdown = sc_helpers.toStringSafe(markdown);
765802

src/quick.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import * as FS from 'fs';
2727
import * as FSExtra from 'fs-extra';
2828
import * as HtmlEntities from 'html-entities';
29+
import * as Glob from 'glob';
2930
import * as Marked from 'marked';
3031
import * as Moment from 'moment';
3132
import * as Path from 'path';
@@ -208,6 +209,7 @@ function _generateHelpHTML(): string {
208209
markdown += "| `$execute(scriptPath: string, ...args: any[]): any` | Executes a script ([module](https://mkloubert.github.io/vs-script-commands/interfaces/_quick_.scriptmodule.html)). |\n";
209210
markdown += "| `$executeCommand(command: string, ...args: any[]): vscode.Thenable<any>` | Executes a command. |\n";
210211
markdown += "| `$exists(path: string): boolean` | Checks if a path exists. |\n";
212+
markdown += "| `$findFiles(globPattern: string, ignore?: string | string[]): string[]` | Finds files using [glob patterns](https://github.com/isaacs/node-glob). |\n";
211213
markdown += "| `$fromMarkdown(markdown: string): string` | Converts [Markdown](https://guides.github.com/features/mastering-markdown/) to HTML. |\n";
212214
markdown += "| `$help(): vscode.Thenable<any>` | Shows this help document. |\n";
213215
markdown += "| `$htmlEncode(str: string): string` | Encodes the HTML entities in a string. |\n";
@@ -307,6 +309,7 @@ function _generateHTMLForResult(expr: string, result: any): string {
307309
return html;
308310
}
309311

312+
310313
/**
311314
* Does a "quick execution".
312315
*/
@@ -326,7 +329,7 @@ export function quickExecution() {
326329
}).then((_expr) => {
327330
let _noResultInfo = _permanentNoResultInfo;
328331
let _showResultInTab = _permanentShowResultInTab;
329-
let _completed = (err: any, result?: any) => {
332+
const _completed = (err: any, result?: any) => {
330333
_state = $state;
331334

332335
if (err) {
@@ -394,6 +397,7 @@ export function quickExecution() {
394397
};
395398

396399
let _currentDir = _permanentCurrentDirectory;
400+
397401
const $cwd = function(newPath?: string, permanent = false) {
398402
if (arguments.length > 0) {
399403
newPath = sc_helpers.toStringSafe(arguments[0]);
@@ -461,6 +465,28 @@ export function quickExecution() {
461465
return FS.existsSync(path);
462466
};
463467
const $extension = $me.context;
468+
const $findFiles = function(pattern: string, ignore?: string[]): string[] {
469+
pattern = sc_helpers.toStringSafe(pattern);
470+
if ('' === pattern.trim()) {
471+
pattern = '**';
472+
}
473+
474+
ignore = sc_helpers.asArray(ignore)
475+
.map(x => sc_helpers.toStringSafe(x))
476+
.filter(x => '' !== x.trim());
477+
478+
return Glob.sync(pattern, <any>{
479+
cwd: _currentDir,
480+
root: _currentDir,
481+
dot: true,
482+
nocase: true,
483+
nodir: true,
484+
nosort: false,
485+
realpath: false,
486+
ignore: ignore,
487+
absolute: true,
488+
});
489+
};
464490
const $fromMarkdown = function(markdown: string): string {
465491
return _fromMarkdown(markdown);
466492
};

0 commit comments

Comments
 (0)