|
| 1 | +import { CmdOptions, debug, GENDIR, LIBDIR, log } from "./command" |
| 2 | +import { basename, join } from "node:path" |
| 3 | +import { cwd } from "node:process" |
| 4 | +import { |
| 5 | + pathExistsSync, |
| 6 | + writeFileSync, |
| 7 | + writeJSONSync, |
| 8 | + readJSONSync, |
| 9 | + emptyDirSync, |
| 10 | + readFileSync, |
| 11 | +} from "fs-extra" |
| 12 | +import { preludeFiles } from "devicescript-compiler" |
| 13 | + |
| 14 | +const TSCONFIG = "tsconfig.json" |
| 15 | +const MAIN = "main.ts" |
| 16 | +const GITIGNORE = ".gitignore" |
| 17 | +const PKG = "package.json" |
| 18 | + |
| 19 | +const tsConfig: any = { |
| 20 | + compilerOptions: { |
| 21 | + moduleResolution: "node", |
| 22 | + target: "es2022", |
| 23 | + module: "es2015", |
| 24 | + lib: [], |
| 25 | + strict: true, |
| 26 | + strictNullChecks: false, |
| 27 | + strictFunctionTypes: true, |
| 28 | + sourceMap: false, |
| 29 | + declaration: false, |
| 30 | + experimentalDecorators: true, |
| 31 | + preserveConstEnums: true, |
| 32 | + noImplicitThis: true, |
| 33 | + isolatedModules: true, |
| 34 | + noImplicitAny: true, |
| 35 | + types: [], |
| 36 | + }, |
| 37 | + include: ["*.ts", `${LIBDIR}/*.ts`], |
| 38 | +} |
| 39 | + |
| 40 | +export interface InitOptions { |
| 41 | + force?: boolean |
| 42 | + spaces?: number |
| 43 | +} |
| 44 | + |
| 45 | +export default function init(options: InitOptions & CmdOptions) { |
| 46 | + const { force, spaces = 4 } = options |
| 47 | + log(`Initializing files for DeviceScript project`) |
| 48 | + // tsconfig.json |
| 49 | + if (!pathExistsSync(TSCONFIG) || force) { |
| 50 | + debug(`write ${TSCONFIG}`) |
| 51 | + writeJSONSync(TSCONFIG, tsConfig, { spaces }) |
| 52 | + } else { |
| 53 | + debug(`skip ${TSCONFIG}, already exists`) |
| 54 | + } |
| 55 | + |
| 56 | + // typescript definitions |
| 57 | + emptyDirSync(LIBDIR) |
| 58 | + debug(`write ${LIBDIR}/*`) |
| 59 | + const prelude = preludeFiles() |
| 60 | + for (const fn of Object.keys(prelude)) { |
| 61 | + writeFileSync(join(LIBDIR, fn), prelude[fn]) |
| 62 | + } |
| 63 | + |
| 64 | + // .gitignore |
| 65 | + const gid = `${GENDIR}/\n` |
| 66 | + if (!pathExistsSync(GITIGNORE)) { |
| 67 | + debug(`write ${GITIGNORE}`) |
| 68 | + writeFileSync(GITIGNORE, gid, { encoding: "utf8" }) |
| 69 | + } else { |
| 70 | + const gitignore = readFileSync(GITIGNORE, { encoding: "utf8" }) |
| 71 | + if (gitignore.indexOf(gid) < 0) { |
| 72 | + debug(`update ${GITIGNORE}`) |
| 73 | + writeFileSync(GITIGNORE, `${gitignore}\n${gid}`, { |
| 74 | + encoding: "utf8", |
| 75 | + }) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // main.ts |
| 80 | + if (!pathExistsSync(MAIN)) { |
| 81 | + debug(`write ${MAIN}`) |
| 82 | + writeFileSync( |
| 83 | + MAIN, |
| 84 | + `// keep this line to force module mode |
| 85 | +export {} |
| 86 | +
|
| 87 | +`, |
| 88 | + { encoding: "utf8" } |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + // package.json |
| 93 | + let pkgChanged = false |
| 94 | + const pkg = pathExistsSync(PKG) |
| 95 | + ? readJSONSync(PKG) |
| 96 | + : { |
| 97 | + name: basename(cwd()), |
| 98 | + } |
| 99 | + if (!pkg.devicescript) { |
| 100 | + pkgChanged = true |
| 101 | + pkg.devicescript = {} |
| 102 | + } |
| 103 | + if (!pkg.scripts?.["init"]) { |
| 104 | + pkgChanged = true |
| 105 | + pkg.scripts = pkg.scripts || {} |
| 106 | + pkg.scripts["init"] = `devsc init` |
| 107 | + } |
| 108 | + if (!pkg.scripts?.["build"]) { |
| 109 | + pkgChanged = true |
| 110 | + pkg.scripts = pkg.scripts || {} |
| 111 | + pkg.scripts["build"] = `devsc build` |
| 112 | + } |
| 113 | + if (!pkg.scripts?.["start"]) { |
| 114 | + pkgChanged = true |
| 115 | + pkg.scripts = pkg.scripts || {} |
| 116 | + pkg.scripts["start"] = `devsc build --watch` |
| 117 | + } |
| 118 | + if (pkgChanged) { |
| 119 | + debug(`write ${PKG}`) |
| 120 | + writeJSONSync(PKG, pkg, { spaces }) |
| 121 | + } |
| 122 | + |
| 123 | + // help message |
| 124 | + log(`Your DeviceScript project is ready`) |
| 125 | + log(`to start the local development, run "yarn start"`) |
| 126 | + log(`to build binaries, run "yarn build"`) |
| 127 | +} |
0 commit comments