From 78a33f78c3ce8cb53bba57298336fcf444dbc009 Mon Sep 17 00:00:00 2001 From: plylrnsdy Date: Sat, 24 Nov 2018 13:55:57 +0800 Subject: [PATCH 1/7] import user module in new file or selected file. --- src/extension.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index c51f112..4ded00e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -307,7 +307,10 @@ class NodeRepl extends EventEmitter { super(); if(workspace && Array.isArray(workspace.workspaceFolders)) { - this.basePath = workspace.workspaceFolders[0].uri.fsPath; + let doc = window.activeTextEditor.document; + this.basePath = (doc.isUntitled) + ? workspace.workspaceFolders[0].uri.fsPath + : workspace.getWorkspaceFolder(Uri.file(doc.fileName)).uri.fsPath; } } @@ -530,10 +533,13 @@ class NodeRepl extends EventEmitter { } } catch(ex) { - let path; + let doc = window.activeTextEditor.document; + let path = Path.join(this.basePath, 'node_modules', name); - if( Fs.existsSync(path = Path.join(this.basePath, 'node_modules', name)) == false) - path = Path.normalize(Path.join(this.basePath, name)); + if(Fs.existsSync(path) === false) + path = (doc.isUntitled) + ? Path.normalize(Path.join(this.basePath, name)) + : Path.join(Path.dirname(doc.fileName), name); return `require('${path.replace(/\\/g, '\\\\')}')`; } From 1900ec2ae0b555ad7479261fc1e135f194953795 Mon Sep 17 00:00:00 2001 From: plylrnsdy Date: Sat, 24 Nov 2018 14:02:58 +0800 Subject: [PATCH 2/7] fix reading property 'document' of undifined --- src/extension.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 4ded00e..b450f49 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -154,7 +154,7 @@ class ReplExtension { this.repl = new NodeRepl(); this.changeActiveDisposable = window.onDidChangeActiveTextEditor(async (editor) => { - if(this.editor.document === editor.document) { + if(this.editor && this.editor.document === editor.document) { this.interpret(); } }); From a0d9752a547e58eb437bb0dc22be93361b51df23 Mon Sep 17 00:00:00 2001 From: plylrnsdy Date: Sat, 24 Nov 2018 14:25:55 +0800 Subject: [PATCH 3/7] Fix cannot import native module or third party module --- src/extension.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index b450f49..213e11c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -529,7 +529,7 @@ class NodeRepl extends EventEmitter { return code.replace(regex, (str, par, name) => { try { if(require(name)) { - return name; + return str; } } catch(ex) { From 15282ee7a030bcc7325917b4d9c6efe7dc763e6b Mon Sep 17 00:00:00 2001 From: plylrnsdy Date: Sat, 24 Nov 2018 14:56:03 +0800 Subject: [PATCH 4/7] prompt the `basePath` in output panel; remove useless object. --- src/extension.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 213e11c..3301815 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -151,8 +151,6 @@ class ReplExtension { public async init() { outputWindow.appendLine(`Initializing REPL extension`) - this.repl = new NodeRepl(); - this.changeActiveDisposable = window.onDidChangeActiveTextEditor(async (editor) => { if(this.editor && this.editor.document === editor.document) { this.interpret(); @@ -311,6 +309,7 @@ class NodeRepl extends EventEmitter { this.basePath = (doc.isUntitled) ? workspace.workspaceFolders[0].uri.fsPath : workspace.getWorkspaceFolder(Uri.file(doc.fileName)).uri.fsPath; + outputWindow.appendLine(`Working at: ${this.basePath}`); } } From 5e9c888ec2723513f82836e47799dcd9a42e7324 Mon Sep 17 00:00:00 2001 From: plylrnsdy Date: Sat, 24 Nov 2018 15:44:11 +0800 Subject: [PATCH 5/7] pormpt node version --- src/extension.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 3301815..c25613a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -149,7 +149,7 @@ class ReplExtension { } public async init() { - outputWindow.appendLine(`Initializing REPL extension`) + outputWindow.appendLine(`Initializing REPL extension in Node ${process.version}`) this.changeActiveDisposable = window.onDidChangeActiveTextEditor(async (editor) => { if(this.editor && this.editor.document === editor.document) { @@ -309,6 +309,7 @@ class NodeRepl extends EventEmitter { this.basePath = (doc.isUntitled) ? workspace.workspaceFolders[0].uri.fsPath : workspace.getWorkspaceFolder(Uri.file(doc.fileName)).uri.fsPath; + outputWindow.appendLine(`Working at: ${this.basePath}`); } } From 2c23c5b34d771385c55cf48d29ca3c419f14b0da Mon Sep 17 00:00:00 2001 From: plylrnsdy Date: Sat, 24 Nov 2018 20:04:12 +0800 Subject: [PATCH 6/7] Fix console cannot log the rest of arguments beyond the first. --- src/extension.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index c25613a..f3df988 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -414,14 +414,14 @@ class NodeRepl extends EventEmitter { value: function(line: number) { return Object.assign({}, repl.context.console, { - log: function(text) { - repl.context.console.log(`\`{${line}}\`${text}`); + log: function(text, ...args) { + repl.context.console.log(`\`{${line}}\`${text}`, ...args); }, - warn: function(text) { - repl.context.console.log(`\`{${line}}\`${text}`); + warn: function(text, ...args) { + repl.context.console.log(`\`{${line}}\`${text}`, ...args); }, - error: function(text) { - repl.context.console.log(`\`{${line}}\`${text}`); + error: function(text, ...args) { + repl.context.console.log(`\`{${line}}\`${text}`, ...args); } }) } From f83894f435ff446c1365e2631bc29d9953f46832 Mon Sep 17 00:00:00 2001 From: plylrnsdy Date: Sat, 24 Nov 2018 20:56:04 +0800 Subject: [PATCH 7/7] Fix console's output not equal it should be, such as `console.log({ a: 1 })` should be output `'{ a: 1 }'` instead of `'[object Object]'`. --- src/extension.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index f3df988..f2224b8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -23,6 +23,7 @@ import { EventEmitter } from 'events'; import * as Repl from 'repl'; import * as Path from 'path'; import * as Fs from 'fs'; +import * as Util from 'util'; import { Writable, Readable } from 'stream'; let replExt: ReplExtension; @@ -413,16 +414,14 @@ class NodeRepl extends EventEmitter { Object.defineProperty(repl.context, '_console', { value: function(line: number) { + let _log = function (text, ...args) { + repl.context.console.log(`\`{${line}}\`${typeof text === 'string' ? text : Util.inspect(text)}`, ...args); + } + return Object.assign({}, repl.context.console, { - log: function(text, ...args) { - repl.context.console.log(`\`{${line}}\`${text}`, ...args); - }, - warn: function(text, ...args) { - repl.context.console.log(`\`{${line}}\`${text}`, ...args); - }, - error: function(text, ...args) { - repl.context.console.log(`\`{${line}}\`${text}`, ...args); - } + log: _log, + warn: _log, + error: _log }) }