forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_util.js
118 lines (98 loc) · 2.37 KB
/
build_util.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
108
109
110
111
112
113
114
115
116
117
118
//@ts-check
var child_process = require('child_process')
var fs = require('fs')
var path = require('path')
var os = require('os')
var root = path.join(__dirname, '..')
var dest_bin = path.join(root, 'lib')
var dest_lib = path.join(root, 'lib', 'ocaml')
var jscomp = path.join(root, 'jscomp')
/*
* It is weird reaname cause Busy resource or lock error
*/
function copyFile(file, target) {
var stat = fs.statSync(file)
fs.createReadStream(file).pipe(
fs.createWriteStream(target,
{ mode: stat.mode }))
}
/**
* @param {string} from
* @param {string} to
*/
function renameAsync(from, to) {
console.log(from, '----->', to)
// @ts-ignore
fs.rename(from, to, (err) => {
if (err) throw err;
})
// fs.renameSync(from,to)
}
/**
* @param {string} from
* @param {string} to
*/
function poor_copy_sync(from, to) {
console.log(from, '----->', to)
fs.renameSync(from, to)
// fs.renameSync(from,to)
}
/**
* @param {string} dir
* @param {string} dest_lib
*/
function install_directory(dir, dest_lib) {
var files = fs.readdirSync(dir)
files.forEach(function (file) {
var installed_fmt = ['.cmt', '.cmti', '.cmj', '.ml', '.mli', '.cmi']
var format_file = path.parse(file)
if (format_file &&
(installed_fmt.indexOf(format_file.ext) !== -1)
) {
var from = path.join(dir, file)
var to = path.join(dest_lib, file)
renameAsync(from, to)
}
})
}
function install() {
if (!fs.existsSync(dest_bin)) {
fs.mkdirSync(dest_bin)
}
if (!fs.existsSync(dest_lib)) {
fs.mkdirSync(dest_lib)
}
var jscomp_runtime = path.join(jscomp, 'runtime')
var files = fs.readdirSync(jscomp_runtime)
files.forEach(function (file) {
var format_file = path.parse(file)
var special_files = [
'js',
'js_unsafe',
'js_internal',
'caml_exceptions',
'js_null',
'js_undefined',
'js_exn',
'js_int',
'js_float',
'js_typed_array'
]
var installed_fmt = ['.cmt', '.cmti', '.cmj']
if (
format_file &&
((special_files.indexOf(format_file.name) !== -1)
||
(installed_fmt.indexOf(format_file.ext) !== -1))
// Always copy cmt* cmj*
) {
var from = path.join(jscomp_runtime, file)
var to = path.join(dest_lib, file)
renameAsync(from, to)
}
})
install_directory(path.join(jscomp, 'stdlib'), dest_lib)
install_directory(path.join(jscomp, 'others'), dest_lib)
}
exports.install = install;
exports.poor_copy_sync = poor_copy_sync;