forked from porsager/postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transpile.cf.js
39 lines (32 loc) · 1.38 KB
/
transpile.cf.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
import fs from 'fs'
import path from 'path'
const empty = x => fs.readdirSync(x).forEach(f => fs.unlinkSync(path.join(x, f)))
, ensureEmpty = x => !fs.existsSync(x) ? fs.mkdirSync(x) : empty(x)
, root = 'cf'
, src = path.join(root, 'src')
ensureEmpty(src)
fs.readdirSync('src').forEach(name =>
fs.writeFileSync(
path.join(src, name),
transpile(fs.readFileSync(path.join('src', name), 'utf8'), name, 'src')
)
)
function transpile(x) {
const timers = x.includes('setImmediate')
? 'import { setImmediate, clearImmediate } from \'../polyfills.js\'\n'
: ''
const process = x.includes('process.')
? 'import { process } from \'../polyfills.js\'\n'
: ''
const buffer = x.includes('Buffer')
? 'import { Buffer } from \'node:buffer\'\n'
: ''
return process + buffer + timers + x
.replace('import net from \'net\'', 'import { net } from \'../polyfills.js\'')
.replace('import tls from \'tls\'', 'import { tls } from \'../polyfills.js\'')
.replace('import crypto from \'crypto\'', 'import { crypto } from \'../polyfills.js\'')
.replace('import os from \'os\'', 'import { os } from \'../polyfills.js\'')
.replace('import fs from \'fs\'', 'import { fs } from \'../polyfills.js\'')
.replace('import { performance } from \'perf_hooks\'', 'import { performance } from \'../polyfills.js\'')
.replace(/ from '([a-z_]+)'/g, ' from \'node:$1\'')
}