1 | import * as build from "@proerd/buildscript"
|
2 | import { resolve, relative } from "path"
|
3 | import * as fs from "fs"
|
4 | const libroot = resolve(__dirname, "..")
|
5 |
|
6 | export function buildscript(projectRoot: string) {
|
7 | const tsPath = resolve(projectRoot, "node_modules", ".bin", "tsc")
|
8 | const serverTsConfigPath = resolve(projectRoot, "server", "tsconfig.json")
|
9 | const relativeTo = (to: string) => relative(projectRoot, to)
|
10 |
|
11 | const tasks = {
|
12 | scaffold() {
|
13 | checkNCreate(["server", "tsconfig.json"], () => JSON.stringify(serverTsconfig, null, 2))
|
14 | checkNCreate(["server", "index.ts"], () => loadScaffoldFile("server-index.txt"))
|
15 | checkNCreate(["pages", "index.tsx"], () => loadScaffoldFile("client-index.txt"))
|
16 | checkNCreate(["pages-content", "_common", "main.scss"], () => "")
|
17 | checkNCreate(["static", "hello.txt"], () => "Use this folder to host static assets.")
|
18 | checkNCreate(["static", "robots.txt"], () => "")
|
19 | checkNCreate([".babelrc.js"], () => loadScaffoldFile("babelrc.txt"))
|
20 | checkNCreate(["tsconfig.json"], () => JSON.stringify(clientTsConfig, null, 2))
|
21 | checkNCreate([".gitignore"], () => loadScaffoldFile("gitignore.scaff.txt"))
|
22 | checkNCreate(["pages", "client-global.d.ts"], () =>
|
23 | loadScaffoldFile("client-global-types.txt"),
|
24 | )
|
25 | checkNCreate(["pages", "_document.tsx"], () => loadScaffoldFile("_document.txt"))
|
26 | checkNCreate([".vscode", "launch.json"], () => loadScaffoldFile("vscode-launch.txt"))
|
27 | checkNCreate(["jest.server.config.js"], () => loadScaffoldFile("jest-config-server.txt"))
|
28 | checkNCreate(["jest.client.config.js"], () => loadScaffoldFile("jest-config-client.txt"))
|
29 | checkNCreate([".prettierrc"], () => loadScaffoldFile("prettier.txt"))
|
30 | checkNCreate(["server", "server-global-types.d.ts"], () => "")
|
31 |
|
32 | const pjspath = resolve(projectRoot, "package.json")
|
33 | const packagejson = require(pjspath)
|
34 | packagejson.scripts = packagejson.scripts || {}
|
35 |
|
36 | packagejson.scripts = Object.assign({}, packagejson.scripts, {
|
37 | compileWithoutErrors: "tsc -p ./server || exit 0",
|
38 | postinstall: "yarn run compileWithoutErrors",
|
39 | dev: "yarn run compileWithoutErrors && node ./.nextpress/index",
|
40 | start: "node ./.nextpress/index",
|
41 | testServer: 'jest -c="jest.server.config.js"',
|
42 | testClient: 'jest -c="jest.client.config.js"',
|
43 | })
|
44 | fs.writeFileSync(pjspath, JSON.stringify(packagejson, null, 2))
|
45 | },
|
46 |
|
47 | async compileServer() {
|
48 | await build.spawn(`${tsPath} -p ${relativeTo(serverTsConfigPath)} -w`)
|
49 | },
|
50 | }
|
51 |
|
52 | return {
|
53 | run() {
|
54 | build.runTask(tasks)
|
55 | },
|
56 | tool: build,
|
57 | tasks,
|
58 | }
|
59 |
|
60 | function checkNCreate(paths: string[], content: () => string | Buffer) {
|
61 | let folders = paths.slice(0, paths.length - 1)
|
62 | let it = 0
|
63 | let current = projectRoot
|
64 | while (it < folders.length) {
|
65 | current = resolve(current, paths[it])
|
66 | try {
|
67 | fs.statSync(current)
|
68 | } catch (err) {
|
69 | fs.mkdirSync(current)
|
70 | fs.statSync(current)
|
71 | }
|
72 | it++
|
73 | }
|
74 | let filepath = resolve(projectRoot, ...paths)
|
75 | try {
|
76 | fs.statSync(filepath)
|
77 | } catch (err) {
|
78 | console.log("Creating", filepath)
|
79 | fs.writeFileSync(filepath, content())
|
80 | }
|
81 | }
|
82 |
|
83 | function loadScaffoldFile(pathInsideScaffoldFolder: string) {
|
84 | return fs.readFileSync(resolve(libroot, "scaffolds", pathInsideScaffoldFolder))
|
85 | }
|
86 | }
|
87 |
|
88 | const serverTsconfig = {
|
89 | compilerOptions: {
|
90 | target: "es2017",
|
91 | module: "commonjs",
|
92 | moduleResolution: "node",
|
93 | outDir: "../.nextpress",
|
94 | strict: true,
|
95 | noUnusedLocals: true,
|
96 | sourceMap: true,
|
97 | pretty: false,
|
98 | },
|
99 | include: ["."],
|
100 | exclude: ["__tests__"],
|
101 | }
|
102 |
|
103 | const clientTsConfig = {
|
104 | compilerOptions: {
|
105 | target: "esnext",
|
106 | module: "commonjs",
|
107 | moduleResolution: "node",
|
108 | noUnusedLocals: true,
|
109 | skipDefaultLibCheck: true,
|
110 | lib: ["es2015", "dom"],
|
111 | jsx: "react",
|
112 | strict: true,
|
113 | sourceMap: false,
|
114 | esModuleInterop: true,
|
115 | experimentalDecorators: true,
|
116 | downlevelIteration: true,
|
117 | },
|
118 | include: ["pages", "app"],
|
119 | }
|