forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddboard.ts
More file actions
61 lines (51 loc) · 1.75 KB
/
Copy pathaddboard.ts
File metadata and controls
61 lines (51 loc) · 1.75 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
import { normalizeDeviceConfig } from "@devicescript/compiler"
import { existsSync } from "fs"
import { mkdirp } from "fs-extra"
import { writeFile } from "fs/promises"
import { randomUInt } from "jacdac-ts"
import { join } from "path"
import { fatal, log } from "./command"
import { setupFlashBoards, showAllBoards } from "./flash"
export interface AddBoardOptions {
base: string
name: string
board?: string
force?: boolean
}
const boardsPath = "boards"
export async function addBoard(options: AddBoardOptions) {
const cfg = setupFlashBoards()
const baseBoard = cfg.boards[options.base]
if (!baseBoard) {
showAllBoards("", "--base")
fatal(
options.base ? `invalid --base "${options.base}"` : `missing --base`
)
}
if (!options.name) {
fatal(`missing --name argument`)
}
if (!options.board) {
options.board = options.name
.toLowerCase()
.replace(/[^a-z0-9]+/g, " ")
.trim()
.replace(/\s+/g, "_")
log(`using --board ${options.board}`)
}
if (cfg.boards[options.board])
fatal(`board '${options.board}' already exists`)
await mkdirp(boardsPath)
const boardJsonPath = join(boardsPath, options.board + ".board.json")
if (!options.force && existsSync(boardJsonPath))
fatal(`file ${boardJsonPath} already exists; use --force to overwrite`)
const board = normalizeDeviceConfig(baseBoard, {
ignoreFirmwareUrl: true,
ignoreId: true,
})
board.devName = options.name
board.productId = "0x" + (randomUInt(0xfff_ffff) | 0x3000_0000).toString(16)
writeFile(boardJsonPath, JSON.stringify(board, null, 4))
log(`created ${boardJsonPath}`)
return { files: [boardJsonPath] }
}