-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.cjs
73 lines (66 loc) · 2.63 KB
/
update.cjs
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
const fs = require('fs')
const path = require('path')
const execSync = require('child_process').execSync
const opalDirectory = path.resolve('../opal')
const args = process.argv.slice(2)
const opalSha1 = args[0]
if (process.env.SKIP_BUILD) {
// Skip build
console.log('SKIP_BUILD environment variable is true, skipping "build" task')
} else {
if (!opalSha1) {
console.error('Please specify a sha1/tag to build Opal')
process.exit(9)
}
console.log(`Building ${opalDirectory}@${opalSha1}`)
execSync(`git checkout ${opalSha1}`, { cwd: opalDirectory })
execSync(`bundle exec rake dist`, { cwd: opalDirectory })
}
// copy
const files = ['nodejs.js', 'opal.js', 'pathname.js', 'stringio.js']
for (const file of files) {
console.log(`Copy ${opalDirectory}/build/${file} to src/${file}`)
fs.copyFileSync(`${opalDirectory}/build/${file}`, `src/${file}`)
}
const opalSourceFile = `src/opal.js`
const opalSource = fs.readFileSync(opalSourceFile, 'utf8')
if (!opalSource.includes('export default Opal')) {
const data = opalSource + `
export default Opal
`
fs.writeFileSync(opalSourceFile, data, 'utf8')
}
const nodejsSourceFile = `src/nodejs.js`
const nodejsSource = fs.readFileSync(nodejsSourceFile, 'utf8')
if (!nodejsSource.includes('import __fs__ from \'fs\';')) {
const data = nodejsSource
.replace('\r\n', '\n')
.replace(/\s+var __fs__ = require\('fs'\);/, '')
.replace(/self\.__xmlhttprequest__ = require\('unxhr'\);/g, 'self.__xmlhttprequest__ = __xmlhttprequest__;')
// path
.replace(/self\.__path__ = require\('path'\);/g, 'self.__path__ = __path__;')
.replace(/\s+var __path__ = self\.__path__;/g, '')
// fs
.replace(/self\.__fs__ = require\('fs'\);/g, 'self.__fs__ = __fs__;')
.replace(/\s+var __fs__ = self\.__fs__;/g, '')
.replace(/\s+var __fs__ = require\('fs'\);/g, '')
// util
.replace(/self\.__util__ = require\('util'\);/g, 'self.__util__ = __util__;')
.replace(/\s+var __util__ = self\.__util__;/g, '')
// glob
.replace(/self\.__glob__ = require\('glob'\);/g, 'self.__glob__ = __glob__;')
.replace(/\s+var __glob__ = self\.__glob__;/g, '')
// os
.replace(/self\.__os__ = require\('os'\);/g, 'self.__os__ = __os__;')
.replace(/\s+var __os__ = self\.__os__;/g, '')
.replace(/\s+self\.\$require\("nodejs\/kernel"\);/g, '')
.replace(/\/\*[^*]+\*\/\nOpal\.modules\["nodejs\/kernel"] = function\(Opal\) {.+?(?=};)};/gs, '')
fs.writeFileSync(nodejsSourceFile, `import __fs__ from 'fs';
import __path__ from 'path';
import __util__ from 'util';
import { glob as __glob__ } from 'glob';
import __os__ from 'os';
import __xmlhttprequest__ from 'unxhr';
${data}
`)
}