-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgen-examples.js
137 lines (109 loc) · 4.08 KB
/
gen-examples.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import fs from 'fs';
import path from 'path';
// Get list of examples
const examples = fs.readdirSync('./src/Examples').filter(x => x.includes('.purs'));
// Write Examples.js file
const examplesJsPath = path.join('./src', 'Examples.js');
const examplesJsContent = examples.map(example => {
const lex = example[0].toLowerCase() + example.slice(1, -5);
const examplePath = path.join('./src/Examples', example);
const fileContent = fs.readFileSync(examplePath, 'utf-8').split('\n');
let IN_IMPORTS = false;
const O = [];
fileContent.forEach(line => {
if (line.startsWith('import')) {
IN_IMPORTS = true;
O.push(line);
} else {
if (IN_IMPORTS) {
O.push('import Deku.Toplevel (runInBody)');
IN_IMPORTS = false;
O.push(line);
} else if (line.startsWith('main')) {
return;
} else if (line.includes('let quit')) {
return;
} else {
O.push(line);
}
}
});
const joined = O.join('\n')
.replace('app :: ExampleSignature', 'main :: Effect Unit')
.replace('app runExample =', 'main =')
.replace('append <$> pure quit <*> runExample', 'void $ runInBody')
.replace('runExample', 'void $ runInBody');
return `export const ${lex}URL = ${JSON.stringify(joined)};\n`;
}).join('');
fs.writeFileSync(examplesJsPath, examplesJsContent);
// Write Examples.purs file
const examplesPursPath = path.join('./src', 'Examples.purs');
let examplesPursContent = `
module Examples (Examples(..), ExampleADT(..), examples, exampleToString, exampleToSlug, exampleToApp) where
import Data.Newtype (class Newtype, unwrap)
import ExampleAssitant (ExampleSignature)
`;
examples.forEach(example => {
const lex = example.slice(0, -5);
examplesPursContent += `import Examples.${lex} as ${lex}\n`;
});
examples.forEach(example => {
const lex = example[0].toLowerCase() + example.slice(1, -5);
examplesPursContent += `foreign import ${lex}URL :: String\n`;
});
examplesPursContent += `
newtype Examples = Examples {
`;
examples.forEach((example, index) => {
const lex = example[0].toLowerCase() + example.slice(1, -5);
examplesPursContent += ` ${lex} :: String${index === examples.length - 1 ? '\n' : ',\n'}`;
});
examplesPursContent += `
}
derive instance Newtype Examples _
examples :: Examples
examples = Examples {
`;
examples.forEach((example, index) => {
const lex = example[0].toLowerCase() + example.slice(1, -5);
examplesPursContent += ` ${lex}: ${lex}URL${index === examples.length - 1 ? '\n' : ',\n'}`;
});
examplesPursContent += `
}
data ExampleADT = ${examples.map(example => example.slice(0, -5)).join(' | ')}
exampleToString :: ExampleADT -> String
`;
examples.forEach(example => {
const lex = example[0].toLowerCase() + example.slice(1, -5);
examplesPursContent += `exampleToString ${example.slice(0, -5)} = (unwrap examples).${lex}\n`;
});
examplesPursContent += `
exampleToSlug :: ExampleADT -> String
`;
examples.forEach(example => {
const lex = example[0].toLowerCase() + example.slice(1, -5);
examplesPursContent += `exampleToSlug ${example.slice(0, -5)} = "${example.slice(0, -5)}"\n`;
});
examplesPursContent += `
exampleToApp :: ExampleADT -> ExampleSignature
`;
examples.forEach(example => {
const bex = example.slice(0, -5);
examplesPursContent += `exampleToApp ${bex} = ${bex}.app\n`;
});
fs.writeFileSync(examplesPursPath, examplesPursContent);
// Write index.js file
const indexPath = path.join('./src', 'index.js');
const indexContent = [
...examples.map(example => {
const lex = example[0].toLowerCase() + example.slice(1, -5);
const bex = example.slice(0, -5);
return `import * as ${bex} from "PureScript/Examples.${bex}/index.js";`;
}),
`const toStart = import.meta.env.VITE_START;\n`,
examples.map(example => {
const bex = example.slice(0, -5);
return `toStart === "${bex}" ? ${bex}.main() : `;
}).join('') + 'main();'
].join('\n');
fs.writeFileSync(indexPath, indexContent);