Skip to content

Commit

Permalink
Add a sucrase-node CLI that wraps node (#288)
Browse files Browse the repository at this point in the history
Fixes #262
  • Loading branch information
alangpierce authored Jun 30, 2018
1 parent c6879ba commit d31351a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Run on a directory:
sucrase ./srcDir -d ./outDir --transforms typescript,imports
```

Register a require hook with some [reasonable defaults](src/register.ts):
Compile on-the-fly via a require hook with some [reasonable defaults](src/register.ts):

```js
// Register just one extension.
Expand All @@ -110,6 +110,12 @@ import "sucrase/register/ts";
import "sucrase/register";
```

Compile on-the-fly via a drop-in replacement for node:

```
sucrase-node index.ts
```

Call from JS directly:

```js
Expand Down
18 changes: 18 additions & 0 deletions bin/sucrase-node
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node
const Module = require("module");
const {resolve} = require("path");

/*
* Simple wrapper around node that first registers Sucrase with default settings.
*
* This is meant for simple use cases, and doesn't support custom Node/V8 args,
* executing a code snippet, a REPL, or other things that you might find in
* node, babel-node, or ts-node. For more advanced use cases, you can use
* `node -r sucrase/register` or register a require hook programmatically from
* your own code.
*/
require("../register");

process.argv.splice(1, 1);
process.argv[1] = resolve(process.argv[1]);
Module.runMain();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"bin": {
"sucrase": "./bin/sucrase"
"sucrase": "./bin/sucrase",
"sucrase-node": "./bin/sucrase-node"
},
"scripts": {
"build": "script/sucrase-node script/build.ts",
Expand Down
18 changes: 13 additions & 5 deletions script/sucrase-node
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#!/usr/bin/env node
const Module = require("module");
const {resolve} = require("path");

// Hacky wrapper around Sucrase for internal scripts. Not yet part of the
// published package since that requires more work to get right.
/*
* Simple wrapper around node that first registers Sucrase with default settings.
*
* This is meant for simple use cases, and doesn't support custom Node/V8 args,
* executing a code snippet, a REPL, or other things that you might find in
* node, babel-node, or ts-node. For more advanced use cases, you can use
* `node -r sucrase/register` or register a require hook programmatically from
* your own code.
*/
require("sucrase/register");

const file = process.argv[2];
process.argv.splice(1, 1);
// eslint-disable-next-line import/no-dynamic-require
require(`../${file}`);
process.argv[1] = resolve(process.argv[1]);
Module.runMain();

0 comments on commit d31351a

Please sign in to comment.