Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

Commit 69c94f8

Browse files
committed
patch: devtools command
1 parent 2945f77 commit 69c94f8

File tree

5 files changed

+67
-35
lines changed

5 files changed

+67
-35
lines changed

cli/src/build.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
DEVS_BYTECODE_FILE,
1515
} from "devicescript-compiler"
1616
import { CmdOptions } from "./command"
17-
import { startDevTools } from "./devtools"
17+
import { devtools } from "./devtools"
1818

1919
function jacsFactory() {
2020
let d = require("devicescript-vm")
@@ -28,11 +28,11 @@ function jacsFactory() {
2828
return d()
2929
}
3030

31-
async function getHost(options: BuildOptions) {
31+
async function getHost(options: BuildOptions & CmdOptions) {
3232
const inst = options.noVerify ? undefined : await jacsFactory()
3333
inst?.jacsInit()
3434

35-
const outdir = options.outDir
35+
const outdir = options.outDir
3636
ensureDirSync(outdir)
3737

3838
const jacsHost = {
@@ -74,7 +74,7 @@ async function compileBuf(buf: Buffer, options: BuildOptions) {
7474
return res.binary
7575
}
7676

77-
export interface BuildOptions extends CmdOptions {
77+
export interface BuildOptions {
7878
noVerify?: boolean
7979
library?: boolean
8080
outDir?: string
@@ -84,12 +84,12 @@ export interface BuildOptions extends CmdOptions {
8484
mainFileName?: string
8585
}
8686

87-
export async function build(file: string, options: BuildOptions) {
87+
export async function build(file: string, options: BuildOptions & CmdOptions) {
8888
file = file || "main.ts"
8989
options = options || {}
9090
options.outDir = options.outDir || "./built"
9191
options.mainFileName = file
92-
92+
9393
await buildOnce(file, options)
9494
if (options.watch) await buildWatch(file, options)
9595
}
@@ -110,10 +110,10 @@ async function buildWatch(file: string, options: BuildOptions) {
110110
watch(file, work)
111111

112112
// start watching bytecode file
113-
await startDevTools(bytecodeFile)
113+
await devtools({ ...options, bytecodeFile })
114114
}
115115

116-
async function buildOnce(file: string, options: BuildOptions) {
116+
async function buildOnce(file: string, options: BuildOptions & CmdOptions) {
117117
try {
118118
if (!pathExistsSync(file))
119119
throw new Error(`source file ${file} not found`)

cli/src/cli.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { build } from "./build"
2-
import pkg from "../package.json"
31
import { program } from "commander"
2+
import pkg from "../package.json"
3+
import { build } from "./build"
4+
import { devtools } from "./devtools"
45

56
export async function mainCli() {
67
program
@@ -26,6 +27,16 @@ export async function mainCli() {
2627
.arguments("[file.ts]")
2728
.action(build)
2829

30+
program
31+
.command("devtools")
32+
.description("launches a local deveplopement tools server")
33+
.option("--internet", "allow connections from non-localhost")
34+
.option(
35+
"--localhost",
36+
"use localhost:8000 instead of the internet dashboard"
37+
)
38+
.action(devtools)
39+
2940
program.parse(process.argv)
3041
}
3142

cli/src/command.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export interface CmdOptions {
22
verbose?: boolean
33
noVerify?: boolean
4-
library?: boolean
54
}

cli/src/devtools.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import https from "https"
66
import url from "url"
77
import net from "net"
88
import fs from "fs"
9+
import { CmdOptions } from "./command"
910

1011
const log = console.log
1112
const debug = console.debug
@@ -43,14 +44,15 @@ function fetchProxy(localhost: boolean): Promise<string> {
4344
})
4445
}
4546

46-
export async function startDevTools(
47-
bytecodeFile: string,
48-
options?: {
49-
internet?: boolean
50-
localhost?: boolean
51-
}
52-
) {
53-
const { internet, localhost } = options || {}
47+
export interface DevToolsOptions {
48+
internet?: boolean
49+
localhost?: boolean
50+
51+
bytecodeFile?: string
52+
}
53+
54+
export async function devtools(options: DevToolsOptions & CmdOptions) {
55+
const { internet, localhost, bytecodeFile } = options
5456
const port = 8081
5557
const tcpPort = 8082
5658
const listenHost = internet ? undefined : "127.0.0.1"
@@ -66,16 +68,18 @@ export async function startDevTools(
6668
const clients: WebSocket[] = []
6769

6870
// upload DeviceScript file is needed
69-
const sendDeviceScript = () => {
70-
const bytecode = fs.readFileSync(bytecodeFile)
71-
debug(`refresh bytecode...`)
72-
const msg = JSON.stringify({
73-
type: "bytecode",
74-
channel: "devicescript",
75-
bytecode: bytecode.toString("hex"),
76-
})
77-
clients.forEach(c => c.send(msg))
78-
}
71+
const sendDeviceScript = bytecodeFile
72+
? () => {
73+
const bytecode = fs.readFileSync(bytecodeFile)
74+
debug(`refresh bytecode...`)
75+
const msg = JSON.stringify({
76+
type: "bytecode",
77+
channel: "devicescript",
78+
bytecode: bytecode.toString("hex"),
79+
})
80+
clients.forEach(c => c.send(msg))
81+
}
82+
: undefined
7983

8084
const server = http.createServer(function (req, res) {
8185
const parsedUrl = url.parse(req.url)
@@ -140,6 +144,8 @@ export async function startDevTools(
140144
server.listen(port, listenHost)
141145
tcpServer.listen(tcpPort, listenHost)
142146

143-
debug(`watch ${bytecodeFile}`)
144-
fs.watch(bytecodeFile, sendDeviceScript)
147+
if (bytecodeFile) {
148+
debug(`watch ${bytecodeFile}`)
149+
fs.watch(bytecodeFile, sendDeviceScript)
150+
}
145151
}

website/docs/Dev/cli.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## Setup
44

5-
* install [Node.js](https://nodejs.org/en/download/)
6-
* install the CLI using npm or yarn
5+
- install [Node.js](https://nodejs.org/en/download/)
6+
- install the CLI using npm or yarn
77

88
```bash
99
npm install -g devicescript
@@ -16,15 +16,15 @@ yarn add devicescript
1616

1717
The command tool is named `devicescript` or `devsc` for short.
1818

19-
## Build
19+
## build
2020

2121
The `build` command compiles a DeviceScript file (default is `main.ts`), using the resolution rules in `tsconfig.json`. It is the default command.
2222

2323
```bash
2424
devsc build main.ts
2525
```
2626

27-
### watch
27+
### --watch
2828

2929
To automatically rebuild your program based on file changes,
3030
add `--watch`.
@@ -44,3 +44,19 @@ To access the developer tools outside localhost, add `--internet`
4444
```bash
4545
devsc build --watch --internet
4646
```
47+
48+
## devtools
49+
50+
The `devtools` command launches the developer tool server, without trying to build a project.
51+
52+
```bash
53+
devsc devtools
54+
```
55+
56+
#### --internet
57+
58+
To access the developer tools outside localhost, add `--internet`
59+
60+
```bash
61+
devsc devtools --internet
62+
```

0 commit comments

Comments
 (0)