forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.ts
More file actions
664 lines (574 loc) · 18.6 KB
/
init.ts
File metadata and controls
664 lines (574 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import { debug, fatal, GENDIR, LIBDIR, log, verboseLog } from "./command"
import { basename, dirname, join, resolve } from "node:path"
import {
pathExistsSync,
writeFileSync,
writeJSONSync,
readFileSync,
ensureDirSync,
readdir,
readFile,
existsSync,
} from "fs-extra"
import { build } from "./build"
import { spawnSync, execSync } from "node:child_process"
import { assert, clone, randomUInt } from "jacdac-ts"
import { addReqHandler } from "./sidedata"
import type {
SideAddBoardReq,
SideAddBoardResp,
SideAddNpmReq,
SideAddNpmResp,
SideAddServiceReq,
SideAddServiceResp,
SideAddSettingsReq,
SideAddSettingsResp,
SideAddSimReq,
SideAddSimResp,
SideAddTestReq,
SideAddTestResp,
} from "./sideprotocol"
import { addBoard } from "./addboard"
import { readJSON5Sync } from "./jsonc"
import {
MIN_NODE_VERSION,
MARKETPLACE_EXTENSION_ID,
} from "@devicescript/interop"
import { TSDOC_TAGS } from "@devicescript/compiler"
const MAIN = "src/main.ts"
const GITIGNORE = ".gitignore"
const IMPORT_PREFIX = `import * as ds from "@devicescript/core"`
const IS_PATCH = "__isPatch__"
const FORCE = "__force__"
type FileSet = Record<string, Object | string>
const serviceFiles: FileSet = {
"services/README.md": `# Services
Add custom service definition in this folder.
- [Read documentation](http://microsoft.github.io/devicescript/developer/custom-services)
`,
}
const testFiles: FileSet = {
"package.json": {
[IS_PATCH]: true,
devDependencies: {
"@devicescript/test": "latest",
},
},
}
const npmFiles: FileSet = {
"package.json": {
[IS_PATCH]: true,
main: "./src/index.ts",
license: "MIT",
devicescript: {
library: true,
},
files: ["src/*.ts", "devsconfig.json"],
keywords: ["devicescript"],
scripts: {
"build:docs":
"npx typedoc ./src/index.ts --tsconfig ./src/tsconfig.json",
},
},
"src/index.ts": `${IMPORT_PREFIX}\n\n`,
"src/tsdoc.json": {
$schema:
"https://developer.microsoft.com/en-us/json-schemas/tsdoc/v0/tsdoc.schema.json",
extends: ["typedoc/tsdoc.json"],
noStandardTags: false,
tagDefinitions: TSDOC_TAGS.map(tag => ({
tagName: `@${tag}`,
syntaxKind: "modifier",
})),
},
".github/workflows/build.yml": `name: Build
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${MIN_NODE_VERSION}
- run: npm ci
- run: npm run build
- run: npm test
`,
}
const settingsFiles: FileSet = {
".env.defaults": `# Store common settings here.
# You can commit this file to source control, make sure there are no secrets.
`,
".env.local": `# Store your secrets here. Overrides values in .env
# Do not commit this file to source control
`,
}
const simFiles: FileSet = {
".vscode/launch.json": {
[IS_PATCH]: true,
configurations: [
{
name: "Sim",
request: "launch",
runtimeArgs: ["-r", "ts-node/register"],
args: ["${workspaceFolder}/sim/app.ts"],
skipFiles: ["<node_internals>/**"],
type: "node",
env: {
TS_NODE_PROJECT: "${workspaceFolder}/sim/tsconfig.json",
},
},
],
compounds: [
{
name: "DeviceScript+Sim",
configurations: ["DeviceScript", "Sim"],
stopAll: true,
},
],
},
"package.json": {
[IS_PATCH]: true,
devDependencies: {
nodemon: "^2.0.20",
typescript: "^4.9.5",
"ts-node": "^10.9.1",
},
scripts: {
[FORCE]: true,
"build:sim": "cd sim && tsc --outDir ../.devicescript/sim",
build: "npm run build:devicescript && npm run build:sim",
"watch:sim":
"cd sim && nodemon --watch './**' --ext 'ts,json' --exec 'ts-node ./app.ts --project ./tsconfig.json'",
watch: "npm run watch:devicescript & npm run watch:sim",
},
},
"sim/runtime.ts": `import "websocket-polyfill"
import { Blob } from "buffer"
globalThis.Blob = Blob as any
import customServices from "../.devicescript/services.json"
import { createWebSocketBus } from "jacdac-ts"
/**
* A Jacdac bus that will connect to the devicescript local server.
*
* \`\`\`example
* import { bus } from "./runtime"
* \`\`\`
*/
export const bus = createWebSocketBus({
busOptions: {
services: customServices as jdspec.ServiceSpec[],
},
})
`,
"sim/README.md": `# Simulators (node.js)
This folder contains a Node.JS/TypeScript application that can be executed side-by-side with
the DeviceScript debugger and simulators. The application uses the [Jacdac TypeScript package](https://microsoft.github.io/jacdac-docs/clients/javascript/)
to communicate with DeviceScript.
The default entry point file is \`./sim/app.ts\`, which uses the Jacdac bus from \`./sim/runtime.ts\` to communicate
with the rest of the DeviceScript execution.
## Editing and using the sim
- open a new terminal
- launch the simulator in watch mode (restarts on every change)
npm run watch:sim
- edit the DeviceScript part of the application as usual. The sim process will automatically connect
to the VS Code extension
`,
"sim/app.ts": `import { bus } from "./runtime"\n\n`,
"sim/tsconfig.json": {
type: "module",
compilerOptions: {
lib: ["es2022", "dom"],
module: "commonjs",
target: "es2022",
strict: true,
esModuleInterop: true,
skipLibCheck: true,
forceConsistentCasingInFileNames: true,
moduleResolution: "node",
resolveJsonModule: true,
},
include: ["./*.ts", "../node_modules/*"],
},
}
const optionalFiles: FileSet = {
".devcontainer/devcontainer.json": {
image: "mcr.microsoft.com/devcontainers/universal:2",
features: {
"ghcr.io/devcontainers/features/node:1": {
version: MIN_NODE_VERSION,
},
"ghcr.io/devcontainers/features/github-cli:1": {},
},
},
"src/tsconfig.json": {
compilerOptions: {
moduleResolution: "node",
target: "es2022",
module: "es2022",
lib: [],
strict: true,
strictNullChecks: false,
strictFunctionTypes: true,
sourceMap: false,
declaration: false,
experimentalDecorators: true,
preserveConstEnums: true,
noImplicitThis: true,
isolatedModules: true,
noImplicitAny: true,
moduleDetection: "force",
forceConsistentCasingInFileNames: true,
types: [],
jsx: "preserve",
},
include: ["**/*.ts", "**/*.tsx", `../${LIBDIR}/core/src/*.ts`],
},
".prettierrc": {
arrowParens: "avoid",
trailingComma: "es5",
semi: false,
tabWidth: 4,
},
".vscode/extensions.json": {
recommendations: [MARKETPLACE_EXTENSION_ID, "esbenp.prettier-vscode"],
},
".vscode/launch.json": {
version: "0.2.0",
configurations: [
{
name: "DeviceScript",
type: "devicescript",
request: "launch",
program: "${workspaceFolder}/" + MAIN,
deviceId: "${command:deviceScriptSimulator}",
stopOnEntry: false,
},
],
},
".yarnrc.yml": `
# Force Yarn v2+ to use node_modules
nodeLinker: node-modules`,
"devsconfig.json": {},
"package.json": {
version: "0.0.0",
private:
"Please use 'yarn devs add npm' to make this a publishable package",
dependencies: {},
devDependencies: {
"@devicescript/cli": "latest",
},
scripts: {
setup: "devicescript build --quiet", // generates node_modules/@devicescript/* files
"build:devicescript": "devicescript build src/main.ts",
postinstall: "npm run setup",
build: "npm run build:devicescript",
"watch:devicescript": `devicescript devtools ${MAIN}`,
watch: "npm run watch:devicescript",
"test:devicescript":
"devicescript run src/main.ts --test --test-self-exit",
test: "npm run test:devicescript",
start: "npm run watch",
},
},
[MAIN]: `${IMPORT_PREFIX}
setInterval(async () => {
console.log(":)")
}, 1000)\n`,
"README.md": `# - project name -
This project uses [DeviceScript](https://microsoft.github.io/devicescript/).
## Project structures
\`\`\`
.devicescript reserved folder for devicescript generated files
src/main.ts default DeviceScript entry point
...
\`\`\`
## Local/container development
- install [Node.js LTS 16+](https://nodejs.org/en/download)
\`\`\`bash
nvm install 16
nvm use 16
\`\`\`
- install DeviceScript compiler and tools
\`\`\`bash
npm install
\`\`\`
### Using Visual Studio Code
- open the project folder in code
\`\`\`bash
code .
\`\`\`
- install the [DeviceScript extension](https://microsoft.github.io/devicescript/getting-started/vscode)
- start debugging!
### Using the command line
- start the watch build and developer tools server
\`\`\`bash
npm run watch
\`\`\`
- navigate to devtools page (see terminal output)
to use the simulators or deploy to hardware.
- open \`src/main.ts\` in your favorite TypeScript IDE and start editing.
`,
}
export interface InitOptions {
force?: boolean
spaces?: number
install?: boolean
board?: string
}
function patchJSON(fn: string, data: any) {
debug(`patch ${fn}`)
const existing = readJSON5Sync(fn)
const isObj = (o: any) => o && typeof o == "object" && !Array.isArray(o)
const doPatch = (trg: any, src: any) => {
const force = !!src[FORCE]
for (const k of Object.keys(src)) {
if (k == IS_PATCH || k == FORCE) continue
if (trg[k] === undefined || trg[k] === "" || force) trg[k] = src[k]
else if (
Array.isArray(src[k]) &&
Array.isArray(trg[k]) &&
src[k][0]?.name
) {
for (const elt of src[k]) {
assert(!!elt.name)
if (!trg[k].find((e: any) => e.name == elt.name))
trg[k].push(elt)
}
} else if (isObj(trg[k]) && isObj(src[k])) {
doPatch(trg[k], src[k])
}
}
}
doPatch(existing, data)
return existing
}
function writeFiles(dir: string, options: InitOptions, files: FileSet) {
const { spaces = 4 } = options
const cwd = resolve(dir || "./")
ensureDirSync(cwd)
process.chdir(cwd) // just in case
Object.entries(files).forEach(([fnr, data]) => {
// tsconfig.json
const fn = join(cwd, fnr)
const isPatch = typeof data == "object" && (data as any)[IS_PATCH]
if (isPatch) data = patchJSON(fn, data)
if (isPatch || !pathExistsSync(fn) || options.force) {
debug(`write ${fn}`)
const dn = dirname(fn)
if (dn) ensureDirSync(dn)
if (typeof data === "string")
writeFileSync(fn, data, { encoding: "utf8" })
else writeJSONSync(fn, data, { spaces })
} else {
debug(`skip ${fn}, already exists`)
}
})
return cwd
}
async function runInstall(cwd: string, options: InitOptions) {
if (options.install) {
const yarnlock = pathExistsSync(join(cwd, "yarn.lock"))
const cmd = yarnlock ? "yarn" : "npm"
log(`install dependencies using ${cmd}...`)
spawnSync(cmd, ["install"], {
shell: true,
stdio: "inherit",
cwd,
})
}
}
function finishAdd(message: string, files: string[] = []) {
log(``)
log(message)
if (files.length) {
const filesmsg = (files.length > 1 ? "one of " : "") + files.join(", ")
log(`Start by editing ${filesmsg}`)
}
log(``)
return {
files,
}
}
export async function init(dir: string | undefined, options: InitOptions) {
log(`Configuring DeviceScript project`)
// write template files
const cwd = writeFiles(dir, options, optionalFiles)
// patch .gitignore
const gids = ["node_modules", GENDIR, ".env.local", ".env.*.local"]
const gitignoren = join(cwd, GITIGNORE)
if (!pathExistsSync(gitignoren)) {
debug(`write ${gitignoren}`)
writeFileSync(gitignoren, gids.join("\n"), {
encoding: "utf8",
})
} else {
let gitignore = readFileSync(gitignoren, { encoding: "utf8" })
let needsWrite = false
gids.forEach(gid => {
if (gitignore.indexOf(gid) < 0) {
needsWrite = true
gitignore += `\n${gid}/`
}
})
if (needsWrite) {
debug(`update ${GITIGNORE}`)
writeFileSync(gitignoren, gitignore, {
encoding: "utf8",
})
}
}
await runInstall(cwd, options)
// patch main.ts with board info
if (options.board) {
const mainFn = join(cwd, MAIN)
if (pathExistsSync(mainFn)) {
const main = readFileSync(mainFn, { encoding: "utf8" })
if (!main.includes(`from "@dsboard/${options.board}"`)) {
const importStmt = `import { pins, board } from "@dsboard/${options.board}"\n`
const newMain = importStmt + main
writeFileSync(mainFn, newMain, { encoding: "utf8" })
}
}
}
// build to get node_modules/@devicescript/* files etc
await build(MAIN, {})
return finishAdd(
`Your DeviceScript project is initialized.\n` +
`To get more help, https://microsoft.github.io/devicescript/getting-started/`,
["package.json", MAIN]
)
}
export interface AddSimOptions extends InitOptions {}
export async function addSim(options: AddSimOptions) {
log(`Adding simulator support`)
const cwd = writeFiles(".", options, simFiles)
await runInstall(cwd, options)
return finishAdd(`Simulator support added.`, ["sim/app.ts"])
}
export interface AddServiceOptions extends InitOptions {
name: string
}
export async function addService(options: AddServiceOptions) {
const { name } = options
if (!name) fatal("--name argument required; example: --name 'Light Level'")
const id = options.name.toLowerCase().replace(/\s+/g, "")
log(`Adding service "${name}"`)
const num = randomUInt(0xfff_ffff) | 0x1000_0000
const serviceFile = "services/" + id + ".md"
const files = Object.assign(
{
[serviceFile]: `# ${name}
identifier: 0x${num.toString(16)}
extends: _sensor
Measures ${name}.
## Registers
ro level: u0.16 / @ reading
A measure of ${name}.
`,
},
serviceFiles
)
const cwd = writeFiles(".", options, files)
await runInstall(cwd, options)
return finishAdd(`Added service ${name}`, [serviceFile])
}
export interface AddNpmOptions extends InitOptions {
license?: string
name?: string
}
export interface AddSettingsOptions extends InitOptions {}
export function execCmd(cmd: string) {
try {
return execSync(cmd, { encoding: "utf-8" }).trim()
} catch {
return ""
}
}
export async function addSettings(options: AddSettingsOptions) {
const files = clone(settingsFiles)
const cwd = writeFiles(".", options, files)
return finishAdd(
`Prepared .env.* files, please add settings in those files.`,
Object.keys(files)
)
}
export async function addNpm(options: AddNpmOptions) {
const files = clone(npmFiles)
const pkg = files["package.json"] as any
pkg.license = options.license ?? "MIT"
if (!pkg.author) {
let uname = execCmd("git config --get user.name")
if (uname) {
uname += " <" + execCmd("git config --get user.email") + ">"
debug(`set author to ${uname}`)
pkg.author = uname
}
}
let url = ""
if (!pkg.repository && pathExistsSync(".git")) {
url = execCmd("git remote get-url origin")
if (url)
pkg.repository = {
type: "git",
url: url,
}
}
if (!pkg.version) pkg.version = "0.0.0"
if (!pkg.name)
pkg.name =
options.name || url
? url.replace(/.*\//, "")
: basename(resolve("."))
pkg.private = false
let lst = await readdir("src")
lst = lst.filter(f => !f.startsWith("main") && f.endsWith(".ts"))
for (const fn of lst) {
files["src/index.ts"] += `export * from "./${fn.slice(0, -3)}"\n`
}
const cwd = writeFiles(".", options, files)
await runInstall(cwd, options)
return finishAdd(`Prepared package.json for publishing, please review.`, [
"package.json",
"src/index.ts",
])
}
export async function addTest(options: AddTestOptions) {
const files = clone(testFiles)
if (existsSync("./src/main.ts")) {
let main = await readFile("./src/main.ts", { encoding: "utf8" })
if (!main.includes("@devicescript/test"))
main =
`import { describe, test, expect } from "@devicescript/test";\n` +
main
files["src/main.ts"] = main
}
const cwd = writeFiles(".", options, files)
await runInstall(cwd, options)
return finishAdd(
`Added test package to package.json, added "runTest" to main.ts, please review.`,
["package.json", "src/main.ts"]
)
}
export interface AddTestOptions extends InitOptions {}
export function initAddCmds() {
addReqHandler<SideAddBoardReq, SideAddBoardResp>("addBoard", d =>
addBoard(d.data)
)
addReqHandler<SideAddServiceReq, SideAddServiceResp>("addService", d =>
addService(d.data)
)
addReqHandler<SideAddSimReq, SideAddSimResp>("addSim", d => addSim(d.data))
addReqHandler<SideAddNpmReq, SideAddNpmResp>("addNpm", d => addNpm(d.data))
addReqHandler<SideAddSettingsReq, SideAddSettingsResp>("addSettings", d =>
addSettings(d.data)
)
addReqHandler<SideAddTestReq, SideAddTestResp>("addTest", d =>
addTest(d.data)
)
}