/
index.js
executable file
·165 lines (152 loc) · 3.33 KB
/
index.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#! /usr/bin/env node
const path = require('path')
const { Transform } = require('stream')
const PromisedLife = require('promised-lifestream')
const Helper = require('./lib/helper')
const ProcessWrapper = require('./lib/process_wrapper')
const transformers = require('./lib/transformers')
let logic = null;
if (process.argv.length > 2) {
const procScriptPath = process.argv[2]
let relPath
try {
relPath = path.resolve(process.cwd(), procScriptPath)
logic = require(relPath)
} catch(e) {
console.error(`Failed to load ${relPath}`)
console.error(e)
process.exit(1)
}
}
const argv = process.argv.slice(3)
if (!logic) {
console.info('Usage) jlp <logic.js> < STDIN > STDOUT')
console.info(`
/*
logic.js example
*/
exports.process = async function(item) {
// filter
if (item.target === 'my_require') {
return item;
} else if (item instanceof Array) {
// Return multiple items
item.forEach(it => {
if (it.target === 'my_require') {
this.push(it);
}
});
}
}
// Optional
exports.finalize = async function(items) {
// sort
return this.sort(items)
}
`)
process.exit(2)
}
const { proc, finalize, before, after } = (() => {
if (typeof logic === 'object') {
return {
proc: logic.process,
finalize: logic.finalize,
before: logic.before || function() {
return Promise.resolve()
},
after: logic.after || function() {
return Promise.resolve()
}
}
} else {
// for v0.1.1 or earlier
return {
proc: logic,
finalize: null,
before: function() {
return Promise.resolve()
},
after: function() {
return Promise.resolve()
}
}
}
})()
function convertItemToStr(item) {
if (typeof item === 'string') {
return item + "\n"
} else {
return JSON.stringify(item) + "\n"
}
}
const streams = [
process.stdin,
transformers.createReadline(),
transformers.createJSONParser(),
new Transform({
objectMode: true,
transform(item, _, cb) {
new ProcessWrapper(proc)
.execute(item)
.then(result => {
result.forEach(it => this.push(it))
cb()
})
.catch(cb)
}
})
]
if (finalize) {
new Helper(before).execute(...argv)
.then(() => {
return PromisedLife(streams, { needResult: true })
})
.then(result => {
const finalizer = new Helper(finalize)
return finalizer.execute(result)
})
.then(result => {
const results = [].concat(result)
for (let item of results) {
if (item == null) continue
try {
process.stdout.write(convertItemToStr(item))
} catch(e) {
throw e
}
}
})
.then(() => {
return new Helper(after).execute(...argv)
})
.catch(err => {
process.stderr.write(err.toString())
})
} else {
const JsonizeTransform = new Transform({
objectMode: true,
transform(item, _, cb) {
if (item == null) {
cb(null);
return
}
try {
cb(null, convertItemToStr(item))
} catch(e) {
cb(e)
}
}
})
streams.push(JsonizeTransform)
streams.push(process.stdout)
new Helper(before).execute(...argv)
.then(() => {
return PromisedLife(streams)
})
.then(() => {
return new Helper(after).execute(...argv)
})
.catch(err => {
process.stderr.write(err.toString())
})
}