-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin.js
executable file
·131 lines (117 loc) · 4.62 KB
/
bin.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
#! /usr/bin/env node
const path = require('path')
const fs = require('fs')
const program = require('commander')
const aok = require('./index')
const uitls = require('lisa.utils')
//const rimraf = require('rimraf')
//const dl = require('lisa.dl')
const cleanup = require('./cleanup')
const child_process = require('child_process')
program.version(require('./package.json').version)
.usage(' [path] path可以为本地路径(本地目录时为工作目录)、可以为远程或本地zip文件,可以是git路径')
.option('--type <type>', '资源类型,local代表本地路径','local', 'zip','git')
.option('-r --rpath [value]','相对于工作目录的aok路径,默认值为工作目录的根目录')
.option('-p --port [value]', '端口号,默认是11540')
.option('-s --staticPath [value]', '静态路径,当path为zip文件或者git路径时,静态路径的相对路径是针对于工作目录而言的' )
.option('-l --list',"不启动,只查看可用资源")
.option('-x --ext' , "是否加载扩展,默认不加载")
.option('-i --ignore [value]','ignore文件路径, 默认是工作目录下的 .aokignore , aokignore使用完全与.gitignore一致')
.option('--no-cors','关闭跨域')
.option('--no-staticGoFirst','取消优先mount静态路径')
//.option('-d --dirty','只有path为zip文件或者git路径时有效,默认是纯净模式,执行完将删除临时文件')
.option('-w --workspace [value]','只有path为zip文件或者git路径时有效, 默认为.aok文件夹,当workspace文件夹中存在内容时,不会再次解压或者下载资源')
.parse(process.argv)
var rPath = '.'
if(program.args.length > 0)
{
rPath = program.args[0]
}
var run =(rp,static)=>{
var options = {}
options.port = program.port || 11540
options.nocors = !program.cors
options.ext = program.ext || false
options.nostaticGoFirst = !program.staticGoFirst
options.ignore = path.resolve(rp,(program.ignore || '.aokignore'))
if(program.rpath){
rp = path.resolve(rp,program.rpath)
}
//console.log(program.list)
if(program.list){
aok.list(rp,static,options)
}else
aok.mount(rp,static,options)
}
if(program.type =='zip' || program.type =='git' || uitls.endWith(rPath,'.zip') || uitls.startWith(rPath,'http')){
//workspace
var ws = path.resolve(process.cwd(), (program.workspace || '.aok'))
//相对静态路径
var static = program.staticPath
if(static){
static = resolveStaticPath(ws,static)// path.resolve(ws,static)
}
if(fs.existsSync(ws)){
console.log('workspace already created')
run(ws,static)
// cleanup.Cleanup(()=>{
// //stop fist
// aok.down()
// console.log(ws )
// rimraf(ws,error=>{
// console.log('自动删除目录'+ ws+'失败,请手动删除:' + error)
// })
// })
}
else{
//todo git zip
var type = program.type
if(type != 'zip' && type != 'git'){
type =uitls.endWith(rPath,'.zip') ? 'zip' : 'git'
}
// cleanup.Cleanup(()=>{
// //stop fist
// //aok.down()
// console.log('生成临时文件目录为:' + ws)
// //console.log(program.dirty)
// if(!program.dirty){
// console.log('auto delete : ' + ws)
// rimraf(ws,error=>{
// console.log('自动删除目录'+ ws+'失败,请手动删除:' + error)
// })
// }
// })
// dl.getRepo(rPath,ws,type).then(()=>{
// run(ws,static)
// })
child_process.exec(`ldl ${rPath} ${ws} --type ${type}` , (err,out,stdErr)=>{
//console.log(stdErr)
if(err){
console.log(`下载远程文件出错: ldl ${rPath} ${ws} --type ${type}`)
console.log("如果没有安装lisa.dl 请执行 npm i -g lisa.dl")
}else{
console.log(out)
run(ws,static)
}
})
}
}else
{
rPath = path.resolve(process.cwd(),rPath)
if(fs.existsSync(rPath)){
var static = program.staticPath
if(static){
static = resolveStaticPath(process.cwd(), static)// path.resolve(process.cwd(),static)
}
run(rPath,static)
}else{
console.log('path not exsits')
}
}
function resolveStaticPath(workplace , statics){
var realPaths = []
statics.split(',').forEach(element => {
realPaths.push(path.resolve(workplace,element))
});
return realPaths.join(',')
}