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
101 lines (94 loc) · 2.87 KB
/
init.ts
File metadata and controls
101 lines (94 loc) · 2.87 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
import { CmdOptions, debug, GENDIR, LIBDIR, log } from "./command"
import { dirname, join } from "node:path"
import {
pathExistsSync,
writeFileSync,
writeJSONSync,
emptyDirSync,
readFileSync,
ensureDirSync,
} from "fs-extra"
import { preludeFiles } from "@devicescript/compiler"
const MAIN = "main.ts"
const GITIGNORE = ".gitignore"
const optionalFiles: Record<string, any> = {
"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`],
},
".prettierrc": {
arrowParens: "avoid",
semi: false,
tabWidth: 4,
},
".vscode/extensions.json": {
recommendations: ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint"],
},
}
export interface InitOptions {
force?: boolean
spaces?: number
}
export default function init(options: InitOptions & CmdOptions) {
const { force, spaces = 4 } = 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)
writeJSONSync(fn, data, { spaces })
} else {
debug(`skip ${fn}, already exists`)
}
})
// typescript definitions
emptyDirSync(LIBDIR)
debug(`write ${LIBDIR}/*`)
const prelude = preludeFiles()
for (const fn of Object.keys(prelude)) {
writeFileSync(join(LIBDIR, fn), prelude[fn])
}
// .gitignore
const gid = `${GENDIR}/\n`
if (!pathExistsSync(GITIGNORE)) {
debug(`write ${GITIGNORE}`)
writeFileSync(GITIGNORE, gid, { encoding: "utf8" })
} else {
const gitignore = readFileSync(GITIGNORE, { encoding: "utf8" })
if (gitignore.indexOf(gid) < 0) {
debug(`update ${GITIGNORE}`)
writeFileSync(GITIGNORE, `${gitignore}\n${gid}`, {
encoding: "utf8",
})
}
}
// main.ts
if (!pathExistsSync(MAIN)) {
debug(`write ${MAIN}`)
writeFileSync(MAIN, `// put your code here!\n`, { encoding: "utf8" })
}
// help message
log(`Your DeviceScript project is ready.`)
log(`to start the local development, run "yarn start"`)
log(`to build binaries, run "yarn build"`)
}