pterm 0.0.19 → 0.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +6 -6
- package/package.json +1 -1
- package/script.js +52 -7
package/index.js
CHANGED
|
@@ -94,11 +94,11 @@ const isHttpUri = (value) => typeof value === "string" && /^https?:\/\//i.test(v
|
|
|
94
94
|
})
|
|
95
95
|
}
|
|
96
96
|
while(true) {
|
|
97
|
-
let
|
|
98
|
-
if (
|
|
99
|
-
if (path.isAbsolute(
|
|
97
|
+
let default_target = await script.default_script(uri, argv.default)
|
|
98
|
+
if (default_target) {
|
|
99
|
+
if (path.isAbsolute(default_target.uri)) {
|
|
100
100
|
await new Promise((resolve, reject) => {
|
|
101
|
-
script.start(
|
|
101
|
+
script.start(default_target, false, (packet) => {
|
|
102
102
|
if (packet.type === "result") {
|
|
103
103
|
resolve()
|
|
104
104
|
}
|
|
@@ -110,10 +110,10 @@ const isHttpUri = (value) => typeof value === "string" && /^https?:\/\//i.test(v
|
|
|
110
110
|
} else {
|
|
111
111
|
// default behavior is no browser side effect.
|
|
112
112
|
if (argv.open) {
|
|
113
|
-
let response = await util.open_url(
|
|
113
|
+
let response = await util.open_url(default_target.uri)
|
|
114
114
|
console.log({ response })
|
|
115
115
|
} else {
|
|
116
|
-
console.log(
|
|
116
|
+
console.log(default_target.uri)
|
|
117
117
|
}
|
|
118
118
|
break
|
|
119
119
|
}
|
package/package.json
CHANGED
package/script.js
CHANGED
|
@@ -30,6 +30,43 @@ class Script {
|
|
|
30
30
|
|
|
31
31
|
return cleanup; // call this to stop listening
|
|
32
32
|
}
|
|
33
|
+
normalizeTarget(target) {
|
|
34
|
+
const appendInputValue = (input, key, value) => {
|
|
35
|
+
if (Object.prototype.hasOwnProperty.call(input, key)) {
|
|
36
|
+
if (Array.isArray(input[key])) {
|
|
37
|
+
input[key].push(value)
|
|
38
|
+
} else {
|
|
39
|
+
input[key] = [input[key], value]
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
input[key] = value
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (target && typeof target === "object" && typeof target.uri === "string") {
|
|
46
|
+
return {
|
|
47
|
+
uri: target.uri,
|
|
48
|
+
input: target.input && typeof target.input === "object" ? target.input : undefined
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
let uri = target
|
|
52
|
+
let input
|
|
53
|
+
if (!/^https?:\/\//i.test(uri)) {
|
|
54
|
+
let queryIndex = uri.indexOf("?")
|
|
55
|
+
if (queryIndex >= 0) {
|
|
56
|
+
input = {}
|
|
57
|
+
let params = new URLSearchParams(uri.slice(queryIndex + 1))
|
|
58
|
+
for (let [key, value] of params.entries()) {
|
|
59
|
+
appendInputValue(input, key, value)
|
|
60
|
+
}
|
|
61
|
+
uri = uri.slice(0, queryIndex)
|
|
62
|
+
}
|
|
63
|
+
uri = path.resolve(process.cwd(), uri)
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
uri,
|
|
67
|
+
input
|
|
68
|
+
}
|
|
69
|
+
}
|
|
33
70
|
async default_script (uri, defaultSelectors) {
|
|
34
71
|
const rpc = new RPC("ws://localhost:42000")
|
|
35
72
|
const stop = () => {
|
|
@@ -51,7 +88,7 @@ class Script {
|
|
|
51
88
|
process.on("exit", (code) => {
|
|
52
89
|
stop()
|
|
53
90
|
});
|
|
54
|
-
let
|
|
91
|
+
let default_target = await new Promise((resolve, reject) => {
|
|
55
92
|
rpc.run({
|
|
56
93
|
uri,
|
|
57
94
|
mode: "open",
|
|
@@ -65,16 +102,19 @@ class Script {
|
|
|
65
102
|
// start
|
|
66
103
|
//rpc.stop({ uri })
|
|
67
104
|
stop()
|
|
68
|
-
resolve(
|
|
105
|
+
resolve({
|
|
106
|
+
uri: packet.data.uri,
|
|
107
|
+
input: packet.data.input
|
|
108
|
+
})
|
|
69
109
|
}
|
|
70
110
|
})
|
|
71
111
|
})
|
|
72
|
-
return
|
|
112
|
+
return default_target
|
|
73
113
|
}
|
|
74
114
|
async stop(argv) {
|
|
75
115
|
if (argv._.length > 1) {
|
|
76
116
|
let _uri = argv._[1]
|
|
77
|
-
const uri =
|
|
117
|
+
const { uri } = this.normalizeTarget(_uri)
|
|
78
118
|
const rpc = new RPC("ws://localhost:42000")
|
|
79
119
|
rpc.run({
|
|
80
120
|
method: "kernel.api.stop",
|
|
@@ -91,7 +131,8 @@ class Script {
|
|
|
91
131
|
const rows = process.stdout.rows;
|
|
92
132
|
const rpc = new RPC("ws://localhost:42000")
|
|
93
133
|
|
|
94
|
-
const
|
|
134
|
+
const target = this.normalizeTarget(_uri)
|
|
135
|
+
const uri = target.uri
|
|
95
136
|
|
|
96
137
|
const stop = () => {
|
|
97
138
|
this.killed = true
|
|
@@ -140,7 +181,7 @@ class Script {
|
|
|
140
181
|
}, (packet) => {
|
|
141
182
|
})
|
|
142
183
|
});
|
|
143
|
-
|
|
184
|
+
let payload = {
|
|
144
185
|
uri,
|
|
145
186
|
source: "pterm",
|
|
146
187
|
client: {
|
|
@@ -148,7 +189,11 @@ class Script {
|
|
|
148
189
|
rows,
|
|
149
190
|
source: "pterm"
|
|
150
191
|
}
|
|
151
|
-
}
|
|
192
|
+
}
|
|
193
|
+
if (target.input && Object.keys(target.input).length > 0) {
|
|
194
|
+
payload.input = target.input
|
|
195
|
+
}
|
|
196
|
+
await rpc.run(payload, (packet) => {
|
|
152
197
|
if (packet.type === "stop") {
|
|
153
198
|
rpc.stop({ uri })
|
|
154
199
|
if (kill) {
|