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

Make ts-node eval public for node REPL consumption #1121

Merged
merged 21 commits into from Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -92,6 +92,7 @@
"@types/source-map-support": "^0.5.0",
"axios": "^0.19.0",
"chai": "^4.0.1",
"get-stream": "^6.0.0",
"lodash": "^4.17.15",
"mocha": "^6.2.2",
"ntypescript": "^1.201507091536.1",
Expand Down
36 changes: 33 additions & 3 deletions src/index.spec.ts
Expand Up @@ -10,7 +10,9 @@ import * as promisify from 'util.promisify'
import { sync as rimrafSync } from 'rimraf'
import { createRequire, createRequireFromPath } from 'module'
import { pathToFileURL } from 'url'
import Module = require('module')
import type * as Module from 'module'
import { PassThrough } from 'stream'
import * as getStream from 'get-stream'

const execP = promisify(exec)

Expand All @@ -25,7 +27,7 @@ const SOURCE_MAP_REGEXP = /\/\/# sourceMappingURL=data:application\/json;charset
const testsDirRequire = (createRequire || createRequireFromPath)(join(TEST_DIR, 'index.js')) // tslint:disable-line

// Set after ts-node is installed locally
let { register, create, VERSION }: typeof tsNodeTypes = {} as any
let { register, create, VERSION, createReplService }: typeof tsNodeTypes = {} as any

// Pack and install ts-node locally, necessary to test package "exports"
before(async function () {
Expand All @@ -34,7 +36,7 @@ before(async function () {
await execP(`npm install`, { cwd: TEST_DIR })
const packageLockPath = join(TEST_DIR, 'package-lock.json')
existsSync(packageLockPath) && unlinkSync(packageLockPath)
;({ register, create, VERSION } = testsDirRequire('ts-node'))
;({ register, create, VERSION, createReplService } = testsDirRequire('ts-node'))
})

describe('ts-node', function () {
Expand Down Expand Up @@ -370,6 +372,34 @@ describe('ts-node', function () {
cp.stdin!.end('\nconst a = 123\n.type a')
})

it('REPL can be created via API', async () => {
const stdin = new PassThrough()
const stdout = new PassThrough()
const stderr = new PassThrough()
const replService = createReplService({
stdin,
stdout,
stderr
})
const service = create(replService.evalStateAwareHostFunctions)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a newcomer, I experienced some confusion seeing the replService juxtaposed with the service. I noticed that the Register type applies to the service and that naming discrepancy was also a head-scratcher for me. With some digging, I kind of see what they represent.

Nothing immediately applicable, but just sharing some thoughts, as I recall y'all considering what might make the repo easier on newbies!

Copy link
Collaborator

@cspotcode cspotcode Nov 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't like the name of Register. IMO it should be Service so that if you do import {Service} from 'ts-node'; then it's intuitive. I'm guessing the Register name made more sense in the past. We can rename it to Service and export by the legacy Register name for backwards-compatibility.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another question:
Given that our create() function returns a Register, which I'd like to rename to Service, does it make sense to rename createReplService() to createRepl() for consistency? create(): Service, createRepl(): ReplService?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that would make a lot more sense! I might even rename the ReplService type to TsRepl, or TsNodeRepl.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking it's best to avoid putting TsNode into the names of exports, to match TypeScript and node's APIs. For example:

  • import {Duplex} from 'streams'; (it's called Duplex, not DuplexStream)
  • import {LanguageService} from 'typescript'; It's named LanguageService instead of TsLanguageService, and lots of usages I've seen do a wildcard import to add the ts prefix: import * as ts from 'typescript'; ts.LanguageService

replService.setService(service)
replService.start()
stdin.write(
'const a = 123\n' +
'.type a\n'
)
stdin.end()
await promisify(setTimeout)(100)
stdout.end()
stderr.end()
expect(await getStream(stderr)).to.equal('')
expect(await getStream(stdout)).to.equal(
'> \'use strict\'\n' +
'> const a: 123\n' +
'> '
)
})

it('should support require flags', function (done) {
exec(`${cmd} -r ./tests/hello-world -pe "console.log('success')"`, function (err, stdout) {
expect(err).to.equal(null)
Expand Down