Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntax highlight for the input #1991

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add --preprocessor option
bimusiek committed Apr 14, 2023
commit e3bfe287e422c846047017ec95037fb7003206a5
1 change: 1 addition & 0 deletions api-extractor/ts-node.api.md
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ export interface CreateOptions {
emit?: boolean;
esm?: boolean;
experimentalReplAwait?: boolean;
preprocessor?: string;
experimentalSpecifierResolution?: 'node' | 'explicit';
experimentalTsImportSpecifiers?: boolean;
// (undocumented)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -161,6 +161,7 @@
"acorn-walk": "^8.1.1",
"arg": "^4.1.0",
"diff": "^4.0.1",
"glob": "^10.0.0",
"make-error": "^1.1.1",
"pretty-repl": "^3.1.1",
"v8-compile-cache-lib": "^3.0.1"
6 changes: 6 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -140,6 +140,7 @@ function parseArgv(argv: string[], entrypointArgs: Record<string, any>) {
'--scope': Boolean,
'--scopeDir': String,
'--noExperimentalReplAwait': Boolean,
'--preprocessor': String,
'--experimentalSpecifierResolution': String,

// Aliases.
@@ -218,6 +219,7 @@ function parseArgv(argv: string[], entrypointArgs: Record<string, any>) {
'--scope': scope = undefined,
'--scopeDir': scopeDir = undefined,
'--noExperimentalReplAwait': noExperimentalReplAwait,
'--preprocessor': preprocessor,
'--experimentalSpecifierResolution': experimentalSpecifierResolution,
'--esm': esm,
_: restArgs,
@@ -257,6 +259,7 @@ function parseArgv(argv: string[], entrypointArgs: Record<string, any>) {
scope,
scopeDir,
noExperimentalReplAwait,
preprocessor,
experimentalSpecifierResolution,
esm,
};
@@ -278,6 +281,7 @@ Options:

--esm Bootstrap with the ESM loader, enabling full ESM support
--swc Use the faster swc transpiler
--preprocessor Runs at the start of CLI

-h, --help Print CLI usage
-v, --version Print module version information. -vvv to print additional information
@@ -355,6 +359,7 @@ function phase3(payload: BootstrapState) {
scopeDir,
esm,
experimentalSpecifierResolution,
preprocessor,
} = payload.parseArgvResult;
const { cwd } = payload.phase2Result!;

@@ -398,6 +403,7 @@ function phase3(payload: BootstrapState) {
esm,
experimentalSpecifierResolution:
experimentalSpecifierResolution as ExperimentalSpecifierResolution,
preprocessor,
});

// If ESM is enabled through the parsed tsconfig, stage4 should be run in a child
2 changes: 2 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -401,6 +401,7 @@ function filterRecognizedTsConfigTsNodeOptions(jsonObject: any): {
scopeDir,
moduleTypes,
experimentalReplAwait,
preprocessor,
swc,
experimentalResolver,
esm,
@@ -414,6 +415,7 @@ function filterRecognizedTsConfigTsNodeOptions(jsonObject: any): {
compilerOptions,
emit,
experimentalReplAwait,
preprocessor,
files,
ignore,
ignoreDiagnostics,
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -301,6 +301,10 @@ export interface CreateOptions {
* to get default, automatic behavior.
*/
experimentalReplAwait?: boolean;
/**
* Runs at the start of CLI.
*/
preprocessor?: string;
/**
* Override certain paths to be compiled and executed as CommonJS or ECMAScript modules.
* When overridden, the tsconfig "module" and package.json "type" fields are overridden, and
20 changes: 11 additions & 9 deletions src/repl.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import type * as _diff from 'diff';
import { homedir } from 'os';
import { join } from 'path';
import {
Recoverable,
ReplOptions,
REPLServer,
} from 'repl';
import {
start as nodeReplStart,
} from 'pretty-repl';
import { join, basename } from 'path';
import { Recoverable, ReplOptions, REPLServer } from 'repl';
import { start as nodeReplStart } from 'pretty-repl';
import { Context, createContext, Script } from 'vm';
import { Service, CreateOptions, TSError, env } from './index';
import { readFileSync, statSync } from 'fs';
@@ -19,6 +13,7 @@ import type * as Module from 'module';
import { builtinModules } from 'module';
import { tsSupportsMtsCtsExts } from './file-extensions';

import { globSync } from 'glob';
// Lazy-loaded.
let _processTopLevelAwait: (src: string) => string | null;
function getProcessTopLevelAwait() {
@@ -423,6 +418,13 @@ export function createRepl(options: CreateReplOptions = {}) {
.map((name) => `declare import ${name} = require('${name}')`)
.join(';')}\n`;
}
if (service!.options.preprocessor) {
const preprocessorContent = readFileSync(
service!.options.preprocessor,
'utf8'
);
state.input += `${preprocessorContent}; void 0;\n`;
}
}

reset();
5 changes: 5 additions & 0 deletions src/test/helpers/preprocessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const PreprocessorEnabled = true;

export function isPreprocessorEnabled() {
return PreprocessorEnabled;
}
46 changes: 46 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -2021,6 +2021,18 @@ __metadata:
languageName: node
linkType: hard

"glob@npm:^10.0.0":
version: 10.0.0
resolution: "glob@npm:10.0.0"
dependencies:
fs.realpath: ^1.0.0
minimatch: ^9.0.0
minipass: ^5.0.0
path-scurry: ^1.6.4
checksum: 3852a6b847106c431d87fb3e8cccb6cfc4449de3ab5d0216c44d4e2da2616df220058050d16811c42f0c2148ad8981da828227ae5c5ab798091ef27c903429f6
languageName: node
linkType: hard

"glob@npm:^7.1.3, glob@npm:^7.1.4, glob@npm:^7.1.6, glob@npm:^7.1.7":
version: 7.2.3
resolution: "glob@npm:7.2.3"
@@ -2679,6 +2691,13 @@ __metadata:
languageName: node
linkType: hard

"lru-cache@npm:^9.0.0":
version: 9.0.2
resolution: "lru-cache@npm:9.0.2"
checksum: 83961e582f4458f56bb33e11a9c16b2a5b693f625650c465d92bf2e47c9869de36a5930ea7368106f3915f66ed05414ab89eaa8af9f66c86dd53234af8362e9a
languageName: node
linkType: hard

"lunr@npm:^2.3.9":
version: 2.3.9
resolution: "lunr@npm:2.3.9"
@@ -2821,6 +2840,15 @@ __metadata:
languageName: node
linkType: hard

"minimatch@npm:^9.0.0":
version: 9.0.0
resolution: "minimatch@npm:9.0.0"
dependencies:
brace-expansion: ^2.0.1
checksum: 7bd57899edd1d1b0560f50b5b2d1ea4ad2a366c5a2c8e0a943372cf2f200b64c256bae45a87a80915adbce27fa36526264296ace0da57b600481fe5ea3e372e5
languageName: node
linkType: hard

"minipass-collect@npm:^1.0.2":
version: 1.0.2
resolution: "minipass-collect@npm:1.0.2"
@@ -2881,6 +2909,13 @@ __metadata:
languageName: node
linkType: hard

"minipass@npm:^5.0.0":
version: 5.0.0
resolution: "minipass@npm:5.0.0"
checksum: 425dab288738853fded43da3314a0b5c035844d6f3097a8e3b5b29b328da8f3c1af6fc70618b32c29ff906284cf6406b6841376f21caaadd0793c1d5a6a620ea
languageName: node
linkType: hard

"minizlib@npm:^2.1.1, minizlib@npm:^2.1.2":
version: 2.1.2
resolution: "minizlib@npm:2.1.2"
@@ -3208,6 +3243,16 @@ __metadata:
languageName: node
linkType: hard

"path-scurry@npm:^1.6.4":
version: 1.6.4
resolution: "path-scurry@npm:1.6.4"
dependencies:
lru-cache: ^9.0.0
minipass: ^5.0.0
checksum: bd5262b51dc35b0d6f0b1d4fa4445789839982bd649904f18fe43717ecc3021d2313a80768b56cd0428f5ca50d740a6c609e747cd6a053efaa802e07eb5b7b18
languageName: node
linkType: hard

"path-type@npm:^4.0.0":
version: 4.0.0
resolution: "path-type@npm:4.0.0"
@@ -3894,6 +3939,7 @@ __metadata:
diff: ^4.0.1
dprint: ^0.25.0
expect: 27.0.2
glob: ^10.0.0
lodash: ^4.17.15
make-error: ^1.1.1
nyc: ^15.0.1