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