forked from porsager/postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transpile.deno.js
80 lines (68 loc) · 2.44 KB
/
transpile.deno.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
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 = 'deno'
, src = path.join(root, 'src')
, types = path.join(root, 'types')
, tests = path.join(root, 'tests')
ensureEmpty(src)
ensureEmpty(types)
ensureEmpty(tests)
fs.writeFileSync(
path.join(types, 'index.d.ts'),
transpile(fs.readFileSync(path.join('types', 'index.d.ts'), 'utf8'), 'index.d.ts', 'types')
)
fs.writeFileSync(
path.join(root, 'README.md'),
fs.readFileSync('README.md', 'utf8')
.replace(/### Installation[\s\S]*?(?=\n##)/m, '')
.replace(
'import postgres from \'postgres\'',
'import postgres from \'https://deno.land/x/postgresjs/mod.js\''
)
)
fs.readdirSync('src').forEach(name =>
fs.writeFileSync(
path.join(src, name),
transpile(fs.readFileSync(path.join('src', name), 'utf8'), name, 'src')
)
)
fs.readdirSync('tests').forEach(name =>
fs.writeFileSync(
path.join(tests, name),
name.endsWith('.js')
? transpile(fs.readFileSync(path.join('tests', name), 'utf8'), name, 'tests')
: fs.readFileSync(path.join('tests', name), 'utf8')
)
)
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ type: 'commonjs' }))
function transpile(x, name, folder) {
if (folder === 'tests') {
if (name === 'bootstrap.js') {
x = x.replace('export function exec(', 'function ignore(')
.replace('async function execAsync(', 'export async function exec(')
.replace(/\nexec\(/g, '\nawait exec(')
.replace('{ spawnSync }', '{ spawn }')
}
if (name === 'index.js')
x += '\n;window.addEventListener("unload", () => Deno.exit(process.exitCode))'
}
const buffer = x.includes('Buffer')
? 'import { Buffer } from \'node:buffer\'\n'
: ''
const process = x.includes('process.')
? 'import process from \'node:process\'\n'
: ''
const timers = x.includes('setImmediate')
? 'import { setImmediate, clearImmediate } from \'node:timers\'\n'
: ''
return buffer + process + timers + x
.replace(
'query.writable.push({ chunk, callback })',
'(query.writable.push({ chunk }), callback())'
)
.replace('socket.setKeepAlive(true, 1000 * keep_alive)', 'socket.setKeepAlive(true)')
.replace('import { performance } from \'perf_hooks\'', '')
.replace(/ from '([a-z_]+)'/g, ' from \'node:$1\'')
}