-
Notifications
You must be signed in to change notification settings - Fork 43
/
deno-transpile.js
107 lines (95 loc) · 3.3 KB
/
deno-transpile.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
const rimraf = require('rimraf');
const fs = require('fs');
const path = require('path');
const outDir = path.join(__dirname, '.deno');
const excluded = [
path.join(__dirname, 'src', 'syntax', 'spec-utils.ts'),
]
function traverse(dir, relative, perform) {
const rdir = path.join(outDir, relative);
if (!(fs.existsSync(rdir))) {
fs.mkdirSync(rdir)
}
for (const iname of fs.readdirSync(dir)) {
const ipath = path.join(dir, iname);
if (excluded.includes(ipath)) {
continue;
}
const rpath = path.join(relative, iname);
const st = fs.statSync(ipath);
if (st.isDirectory()) {
traverse(ipath, rpath, perform);
continue;
}
if (/\.spec\.ts$/.test(iname)) {
// ignore tests
continue;
}
perform(iname, ipath, rpath);
}
}
if (process.argv.includes('--copy')) {
// ============== COPY
rimraf.sync(outDir);
if (!(fs.existsSync(outDir))) {
fs.mkdirSync(outDir)
}
const settingsJson = path.join(outDir, '.vscode', 'settings.json');
if (!fs.existsSync(path.dirname(settingsJson))) {
fs.mkdirSync(path.dirname(settingsJson));
}
if (!(fs.existsSync(settingsJson))) {
fs.writeFileSync(settingsJson, `{
"deno.enable": true
}`)
}
traverse(path.join(__dirname, 'src'), '', (iname, ipath, rpath) => {
const ext = path.extname(iname);
switch (ext) {
case '.ts':
fs.copyFileSync(ipath, path.join(outDir, rpath));
console.log('Copied ' + rpath);
break;
}
});
fs.writeFileSync(path.join(outDir, 'mod.ts'), `export * from './index';`);
fs.copyFileSync(path.join(__dirname, 'readme.md'), path.join(outDir, 'readme.md'));
} else if (process.argv.includes('--process')) {
// ============= TRANSPILE
const bindings = {
'moo': 'https://deno.land/x/[email protected]/mod.ts',
'nearley': 'https://deno.land/x/[email protected]/mod.ts',
}
function handleTs(ipath, rpath) {
const content = fs.readFileSync(ipath, 'utf-8');
const newContent = content.replace(/^\s*(import|export)\s+([^\n]+)\s+from\s+['"]([^'"]+)['"]\;?$/mg, (_, op, what, where) => {
if (/^\./.test(where)) {
const asDir = path.join(path.dirname(ipath), where, 'index.ts');
if (fs.existsSync(asDir)) {
where = where + '/index';
}
return `${op} ${what} from '${where}.ts';`
}
let bound = bindings[where];
if (!bound) {
throw new Error('No Deno binding for dependency ' + where + ' in ' + rpath);
}
bound = typeof bound === 'string'
? { what: x => x, where: bound }
: bound
return `${op} ${bound.what(what)} from '${bound.where}';`;
});
fs.writeFileSync(ipath, newContent);
console.log('Transpiled ' + rpath);
}
traverse(outDir, '', (iname, ipath, rpath) => {
const ext = path.extname(iname);
switch (ext) {
case '.ts':
handleTs(ipath, rpath);
break;
}
});
} else {
throw new Error('Unkown transpile program arg');
}