forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntimeDeps.js
38 lines (28 loc) · 1.21 KB
/
runtimeDeps.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
//@ts-check
var fs = require('fs')
var path = require('path')
var runtimeDir = path.join(__dirname, '..', 'jscomp', 'runtime')
var jsDir = path.join(__dirname, '..', 'lib', 'js')
var files = fs.readdirSync(runtimeDir, 'utf8')
var possibleJsFiles = [... new Set(files.filter(x => x.endsWith('.ml') || x.endsWith('.mli')).map(x => x.substr(0, x.indexOf('.'))))]
// possibleJsFiles.map(x=>path.join(jsDir,x + ".js")).every(x=>fs.existsSync(x))
function getDeps(text) {
var deps = []
text.replace(/(\/\*[\w\W]*?\*\/|\/\/[^\n]*|[.$]r)|\brequire\s*\(\s*["']([^"']*)["']\s*\)/g, function (_, ignore, id) {
if (!ignore) deps.push(id);
});
return deps;
}
function readDeps(name) {
var jsFile = path.join(jsDir, name + ".js")
var fileContent = fs.readFileSync(jsFile, 'utf8')
return getDeps(fileContent).map(x => path.parse(x).name)
}
function create() {
var deps = possibleJsFiles.filter(x => readDeps(x).length !== 0).map(x => `${x}.cmj: ${(readDeps(x).map(x => x + '.cmj')).join(' ')}`).reduce((x, y) => x + '\n' + y)
fs.writeFileSync(path.join(__dirname, '..', 'jscomp', 'runtime', '.extradepend'), deps+'\n', 'utf8')
}
if (require.main === module){
create()
}
exports.create = create