-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathone_node.js
More file actions
executable file
·89 lines (79 loc) · 1.92 KB
/
one_node.js
File metadata and controls
executable file
·89 lines (79 loc) · 1.92 KB
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
#!/usr/bin/env node --harmony
// ONEJS Runtime
// Copyright (C) 2014 ONEJS
"use strict"
global.ONE = {}
ONE.nodejs_boot_ = function(){
// include other parts
global.ONE = ONE
require('./one_base.js')
require('./one_parser.js')
require('./one_genjs.js')
require('./one_ast.js')
ONE.init()
ONE.init_ast()
// load our first argument, parse dependencies and fire up
var args = process.argv.slice()
var watcher
for( var i = 0;i<args.length;i++){
if(args[i] =='-w') args.splice(i,1), watcher = true
}
var root = args.length > 2 ? args[2] : 'index'
root = root.replace(/\.n$/,"")
var fs = require('fs')
// make a little filewatcher and do auto restarting
var stats = {}
var watch = 'mtime'
var watches = {}
var delta = 0
function watchFile(file){
if(watches[file]) return
stats[file] = fs.statSync(file)[watch].toString()
watches[file] = setInterval(function(){
var stat = fs.statSync(file)
if(stat[watch].toString() != stats[file]){
stats[file] = stat[watch].toString()
if(Date.now() - delta > 2000){
delta = Date.now()
console.log('-- restarting -- '+Date())
reload()
}
}
},50)
}
function loadFile( obj, module ){
var file = module +'.n'
try{
var code = fs.readFileSync(file).toString()
if(watcher) watchFile( file )
}
catch (e){
console.log('Cant open '+file, e)
process.exit(-1)
}
// skip #! header
if(code.charCodeAt(0) == 35 &&
code.charCodeAt(1) == 33){
var pos = 0, len = code.length
while(pos < len) if(code.charCodeAt(++pos)==10) break
code = code.slice(pos)
}
var ast = obj.parse('->{'+code+'\n}', file)
ast.getDependencies().forEach(function(file){
loadFile( obj, file )
})
//try{
obj.$[module] = obj.eval(ast, module)
//}catch(e){
//console.log(e)
//}
}
function reload(){
var obj = ONE.Base.new()
loadFile( obj, root )
var call = obj.$[root]
if(call)call.call(obj)
}
reload()
}
ONE.nodejs_boot_()