forked from porsager/postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranspile.deno.js
78 lines (66 loc) · 2.69 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
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')
, tests = path.join(root, 'tests')
ensureEmpty(src)
ensureEmpty(tests)
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') {
// Ignore tests that use node create stream functions not supported in deno yet
x = x.replace(/(t\('Copy from file works)/, 'n$1')
.replace(/(t\('Copy from abort works)/, 'n$1')
.replace(/(t\('Large object)/, 'n$1')
}
}
const buffer = x.includes('Buffer')
? 'import { Buffer } from \'https://deno.land/std@0.120.0/node/buffer.ts\'\n'
: ''
const process = x.includes('process.')
? 'import process from \'https://deno.land/std@0.120.0/node/process.ts\'\n'
: ''
const timers = x.includes('setImmediate')
? 'import { setImmediate, clearImmediate } from \'../polyfills.js\'\n'
: ''
const hmac = x.includes('createHmac')
? 'import { HmacSha256 } from \'https://deno.land/std@0.120.0/hash/sha256.ts\'\n'
: ''
return hmac + buffer + process + timers + x
.replace(/setTimeout\((.*)\)\.unref\(\)/g, '(window.timer = setTimeout($1), Deno.unrefTimer(window.timer), window.timer)')
.replace(
'crypto.createHmac(\'sha256\', key).update(x).digest()',
'Buffer.from(new HmacSha256(key).update(x).digest())'
)
.replace(
'query.writable.push({ chunk, callback })',
'(query.writable.push({ chunk }), callback())'
)
.replace(/.setKeepAlive\([^)]+\)/g, '')
.replace(/import net from 'net'/, 'import { net } from \'../polyfills.js\'')
.replace(/import tls from 'tls'/, 'import { tls } from \'../polyfills.js\'')
.replace(/ from '([a-z_]+)'/g, ' from \'https://deno.land/std@0.120.0/node/$1.ts\'')
}