-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.js
139 lines (126 loc) · 3.72 KB
/
compile.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const fs = require("fs")
var shell = require("shelljs")
var colors = require("colors");
const preCompile = code => `([]+[])[({}+[])[4]](([]+[])[({}+[])[5]](43)+(![]+{})[0]+(!![]+{})[2]+([][[]]+[])[1]+({}+[])[5]+(!![]+{})[0]+([][[]]+[])[5]+({}+[])[1]+([][[]]+[])[1]+([]+[])[({}+[])[5]](40)+([]+[])[({}+[])[5]](41)+([]+[])[({}+[])[5]](123)+${code}+([]+[])[({}+[])[5]](10)+([]+[])[({}+[])[5]](125)+([]+[])[({}+[])[5]](40)+([]+[])[({}+[])[5]](41)+([]+[])[({}+[])[5]](59))`
const { exec, charConvert} = require('./new.js')
if (!process || !process.argv){
throw new Error("unknown running environment, using node instead")
}
Date.prototype.format = function (fmt) {
let ret;
const opt = {
"Y+": this.getFullYear().toString(), // 年
"m+": (this.getMonth() + 1).toString(), // 月
"d+": this.getDate().toString(), // 日
"H+": this.getHours().toString(), // 时
"M+": this.getMinutes().toString(), // 分
"S+": this.getSeconds().toString() // 秒
};
for (let k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
};
};
return fmt;
}
const argv = require('yargs')
.option('o', {
alias : 'output',
demand: true,
default: 'out.js',
describe: 'the compile destination, default is out.js',
type: 'string'
})
.option('r', {
alias : 'run',
demand: false,
default: false,
describe: 'run after compile, default is false',
type: 'boolean'
})
.option('h', {
alias : 'help',
demand: false,
default: false,
describe: 'print help',
type: 'boolean'
})
.option('w', {
alias : 'watch',
demand: false,
default: false,
describe: 'watch the input files',
type: 'boolean'
})
.argv
// 过滤不合法的input path
args = argv._.filter(e => /^\S+\.js$|^-o$/.test(e))
args = args.filter(e => fs.existsSync(e))
// 如果没有提供input file
if (args.length === 0){
console.log(colors.red('no compile target found!'))
throw new Error("no compile target found")
}
// 是否监控文件变化
if (argv.w){
watchFile(argv._, argv.o)
} else {
compile(argv._, argv.o)
// run after compile
if (argv.r){
run(argv.o)
}
}
function watchFile(input, output){
let fileData = {}
function c_(files){
let codeBody = []
files.forEach(f => {
fileData[f] = charConvert(fs.readFileSync(f, "utf-8") + '\n')
})
for (k in fileData){
codeBody.push(fileData[k])
}
let code = codeBody.join('+')
let outputCode = preCompile(code)
fs.writeFileSync(output, outputCode)
console.log(colors.gray(new Date().format("HH:MM:SS") + ` +${files.join(',')}:\n`) + colors.bgGreen("compile finished."))
if (argv.r){
run(argv.o)
}
}
// first compile
c_(input)
input.forEach(f => {
fs.watchFile(f,{
persistent: true,
interval: 1000
},function(curr, prev){
if(curr.mtime >= prev.mtime){
c_([f])
}
});
})
}
// single compile
function compile(input, output){
let codeBody = ""
input.forEach(e => {
codeBody += fs.readFileSync(e, "utf-8") + "\n"
});
let code = `+function(){${codeBody}}();`
let outputCode = exec(charConvert(code))
fs.writeFileSync(output, outputCode)
console.log(colors.bgGreen("compile finished."))
}
function run(target){
const st = new Date()
const runCode = shell.exec(`cat ./assets/pre.js ./fundamental.js ${target} | node`).code
if (runCode !== 0){
console.log(colors.bgRed(`\nExit: ${runCode}.`))
console.log(colors.red(`it may caused by the unsupport function call. Try running in your browser.`))
} else {
console.log(colors.gray(`\nScript finished after ${((new Date() - st) / 1000).toFixed(3)}s`))
}
}