-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathbuild-schema-design-schema
More file actions
executable file
·57 lines (50 loc) · 1.8 KB
/
build-schema-design-schema
File metadata and controls
executable file
·57 lines (50 loc) · 1.8 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
#!/usr/bin/env node
const path = require("path");
const fs = require("fs");
const schemaMarkdownPath = `${__dirname}/../src/pages/postgraphile/postgresql-schema-design.md`;
const schemaMarkdownFile = fs.readFileSync(schemaMarkdownPath, "utf8");
// State for our filterer.
let isInsideMarkdownSQLCode = false;
// Create a file with just our schema’s SQL.
const schemaSQLFile = `
-- This file was automatically generated from
-- /src/pages/postgraphile/postgresql-schema-design.md which contains a complete
-- explanation of how this schema works and why certain decisions were made. If
-- you are looking for a comprehensive tutorial, definitely check it out as this
-- file is a little tough to read.
--
-- If you want to contribute to this file, please change the
-- /src/pages/postgraphile/postgresql-schema-design.md file and then rebuild
-- this file :)
begin;
${schemaMarkdownFile
// Split our file up into all of its lines.
.split("\n")
// Reduce our file back so that it is just SQL.
.reduce((sql, line) => {
// If we are inside some SQL code, we want to include this code in our
// final file.
if (isInsideMarkdownSQLCode) {
// If this line closes our SQL, do not include this line, but do
// change our state.
if (line === "```") {
isInsideMarkdownSQLCode = false;
return `${sql}\n`;
}
return `${sql}${line}\n`;
} else {
// If we are not inside some SQL code, we should just return back the
// code so far.
// If this line opens a block of SQL, change our state.
if (line === "```sql") isInsideMarkdownSQLCode = true;
return sql;
}
}, "")}
commit;
`.slice(1);
// Output that file to our build directory wrapped in a transaction.
fs.writeFileSync(
`${__dirname}/../static/schema-design.sql`,
schemaSQLFile,
"utf8"
);