forked from jeff-zucker/solid-file-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cjs2esm
executable file
·39 lines (33 loc) · 1.13 KB
/
cjs2esm
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
#!/usr/bin/env node
const fs = require('fs');
// define ./src scripts to convert here
//
let scripts = [
'index',
'folderUtils',
'solid-shell-client'
];
// preparing cjs files for conversion
// ----------------------------------
// comment out import statements with //import at line start
// put /*cjs*/ at line start of function declaration (where "export" will go)
// start multi-line cjs-only code with a line starting with //cjs-start
// end multi-line cjs-only code with a line starting with //cjs-end
for(s in scripts){
let cjsText = fs.readFileSync( "./src/"+scripts[s]+".js", 'utf8');
let esmText = processCJS( cjsText );
fs.writeFileSync( "./lib/"+scripts[s]+".js", esmText );
}
function processCJS(text){
text = text.split(/\n/);
let esm = "";
for(var i=0;i<text.length;i++){
let line = text[i];
line = line.replace(/^\/\*cjs\*\/ /,'export ');
line = line.replace(/^\/\/ import/,'import');
line = line.replace(/^\/\/ cjs-start/,'// cjs-start\n/*');
line = line.replace(/^\/\/ cjs-end/,'*/\n// cjs-end');
esm += line + "\n";
}
return esm;
}