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
190 lines (165 loc) · 4.77 KB
/
init.ts
File metadata and controls
190 lines (165 loc) · 4.77 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
import { CmdOptions, debug, GENDIR, LIBDIR, log } from "./command"
import { dirname } from "node:path"
import {
pathExistsSync,
writeFileSync,
writeJSONSync,
emptyDirSync,
readFileSync,
ensureDirSync,
} from "fs-extra"
import { saveLibFiles } from "./build"
import { spawnSync } from "node:child_process"
const MAIN = "main.ts"
const GITIGNORE = ".gitignore"
const IMPORT_PREFIX = `import * as ds from "@devicescript/core"`
const optionalFiles: Record<string, Object | string> = {
"tsconfig.json": {
compilerOptions: {
moduleResolution: "node",
target: "es2022",
module: "es2015",
lib: [],
strict: true,
strictNullChecks: false,
strictFunctionTypes: true,
sourceMap: false,
declaration: false,
experimentalDecorators: true,
preserveConstEnums: true,
noImplicitThis: true,
isolatedModules: true,
noImplicitAny: true,
moduleDetection: "force",
types: [],
},
include: ["**/*.ts", `${LIBDIR}/*.ts`],
exclude: ["**/node_modules/*"],
},
".prettierrc": {
arrowParens: "avoid",
semi: false,
tabWidth: 4,
},
".vscode/extensions.json": {
recommendations: ["esbenp.prettier-vscode"],
},
"devsconfig.json": {},
"package.json": {
version: "0.0.0",
private: true,
dependencies: {},
devDependencies: {
"@devicescript/cli": "*",
},
scripts: {
setup: "devicescript init",
build: "devicescript build",
watch: "devicescript devtools main.ts",
start: "yarn watch",
},
},
"README.md": `# - project name -
This project uses [DeviceScript](https://microsoft.github.io/devicescript/).
## Local/container development
- install node.js 16+
\`\`\`bash
nvm install 18
nvm use 18
\`\`\`
- install dependencies
\`\`\`bash
yarn 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
yarn watch
\`\`\`
- navigate to devtools page (see terminal output)
to use the simulators or deploy to hardware.
- open \`main.ts\` in your favorite TypeScript IDE and start editing.
`,
}
export interface InitOptions {
force?: boolean
spaces?: number
install?: boolean
}
export async function init(options: InitOptions & CmdOptions) {
const { force, spaces = 4, install } = options
log(`Initializing files for DeviceScript project`)
Object.keys(optionalFiles).forEach(fn => {
// tsconfig.json
if (!pathExistsSync(fn) || force) {
const data = optionalFiles[fn]
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`)
}
})
// typescript definitions
emptyDirSync(LIBDIR)
debug(`write ${LIBDIR}/*`)
await saveLibFiles({})
// .gitignore
const gids = ["node_modules", GENDIR]
if (!pathExistsSync(GITIGNORE)) {
debug(`write ${GITIGNORE}`)
writeFileSync(GITIGNORE, gids.join("\n"), { encoding: "utf8" })
} else {
let gitignore = readFileSync(GITIGNORE, { encoding: "utf8" })
let needsWrite = false
gids.forEach(gid => {
if (gitignore.indexOf(gid) < 0) {
needsWrite = true
gitignore += `\n${gid}/`
}
})
if (needsWrite) {
debug(`update ${GITIGNORE}`)
writeFileSync(GITIGNORE, gitignore, {
encoding: "utf8",
})
}
}
// main.ts
if (!pathExistsSync(MAIN)) {
debug(`write ${MAIN}`)
writeFileSync(
MAIN,
`${IMPORT_PREFIX}\n\nds.everyMs(1000, () => {\n console.log(":)")\n})\n`,
{
encoding: "utf8",
}
)
}
if (install) {
const npm = pathExistsSync("package-lock.json")
const cmd = npm ? "npm" : "yarn"
log(`install dependencies...`)
spawnSync(cmd, ["install"], {
shell: true,
stdio: "inherit",
})
}
// help message
log(``)
log(`Your DeviceScript project is initialized.`)
log(
`To get more help, https://microsoft.github.io/devicescript/getting-started/ .`
)
log(``)
}