-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_bak.js
136 lines (125 loc) · 4.03 KB
/
index_bak.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
var stdin = require('stdin.js')
var lust = require('./lust')
var util = require('./util')
global.uicliRunCount =0;
/**
* satify one lust
* @param {*} lustInfo
*/
var satifyOneLust = function(lustInfo){
return new Promise(function(r,j){
var cycle = function(lastData){
stdin.writeLine(lust.getPromptFromLustInfo(lustInfo,lastData))
return stdin.readLine().then(data=>{
var cr =lust.checkAndUpdateValueByLustInfo(data,lustInfo,lastData)
if(cr.isPass)
{
if(!cr.isUpdate){
stdin.writeLine("add success:" + lustInfo.dotTree + " continue to add?\r\nyes/no:(no) ")
stdin.readLine().then(data1=>{
if(data1 == "true" || data1 == "yes" || data1 == "y" || data1=="Y"
|| data1 == "t"){
// if continue ,will keep ???
r()
}
else{
//console.log(lustInfo)
if(lustInfo.isArray){
//lustInfo.fJson[lustInfo.fkey] =
lustInfo.object.splice(lustInfo.index+1,1)
}
else if(lustInfo.isKey){
delete lustInfo.object[lustInfo.key]
}
r()
}
})
}
else
{
r()
}
}
else
{
stdin.writeLine(cr.message + "\r\n")
cycle(data)
}
})
}
cycle()
})
}
/**
* 获取默认值
* @param {*} json
*/
var getDefaultJson = function(json){
//deep copy json
var tgtJson = JSON.parse(JSON.stringify(json))
function cylceAllLust(){
var firstLustInfo = lust.findLustFromJson(tgtJson)
if(firstLustInfo){
lust.atuoCheckAndUpdateValueByLustInfo("",firstLustInfo)
cylceAllLust()
}
}
cylceAllLust()
return tgtJson
}
/**
* 通过ui交互方式获取realJson
* @param {json} json
*/
var uiGetJson =function(json){
if(global.uicliRunCount++ > 0){
throw Error("uicli: one more uicli runed @ same time")
}
//deep copy json
var tgtJson = Object.assign({}, json); // JSON.parse(JSON.stringify(json))
return new Promise(function(r,j){
/*
{ isKey: false,
type: 'String',
default: null,
remark: '',
isArray: true,
object: [ '???(string)', { love: '???(s)', like: [Object] } ],
index: 0,
dotTree: 'lover.hobbies[2][0]' }
*/
function cylceAllLust(){
var firstLustInfo = lust.findLustFromJson(tgtJson)
if(firstLustInfo){
lust.renderSelect(firstLustInfo)
satifyOneLust(firstLustInfo).then(cylceAllLust)
}
else{
stdin.writeLine("======================================================================\r\n")
console.log(tgtJson)
stdin.writeLine("remake the json ?\r\nyes/no:(no) ")
stdin.readLine().then(data2=>{
if(data2 == "true" || data2 == "yes" || data2 == "y" || data2=="Y"|| data2 == "t"){
tgtJson = JSON.parse(JSON.stringify(json))
cylceAllLust()
}
else
{
r(tgtJson)
}
})
}
}
cylceAllLust()
});
}
/**
* get Json by ui
*/
exports.uiGetJson = uiGetJson;
/**
* get default json
*/
exports.getDefaultJson = getDefaultJson;
exports.satifyOneLust= satifyOneLust;
//exports.findFirstLust = findFirstLust;