UNPKG

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