pinokiod 7.1.16 → 7.1.17

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.
Files changed (2) hide show
  1. package/kernel/shell.js +62 -11
  2. package/package.json +1 -1
package/kernel/shell.js CHANGED
@@ -254,6 +254,61 @@ class Shell {
254
254
  const name = (shellName || '').toLowerCase()
255
255
  return name.includes('cmd.exe') || name === 'cmd'
256
256
  }
257
+ isPowerShell(shellName=this.shell) {
258
+ const name = (shellName || '').toLowerCase()
259
+ return name.includes('powershell') || name.includes('pwsh')
260
+ }
261
+ isUnresolvedTemplate(value) {
262
+ return typeof value === "string" && /^\{\{[\s\S]*\}\}$/.test(value)
263
+ }
264
+ normalizeStructuredMessage(value) {
265
+ if (Array.isArray(value)) {
266
+ return value
267
+ .map((item) => this.normalizeStructuredMessage(item))
268
+ .filter((item) => typeof item !== "undefined")
269
+ }
270
+ if (value && value.constructor === Object) {
271
+ const normalized = {}
272
+ for (const [key, item] of Object.entries(value)) {
273
+ const rendered = this.normalizeStructuredMessage(item)
274
+ if (typeof rendered !== "undefined") {
275
+ normalized[key] = rendered
276
+ }
277
+ }
278
+ return normalized
279
+ }
280
+ if (this.isUnresolvedTemplate(value)) {
281
+ return undefined
282
+ }
283
+ return value
284
+ }
285
+ quoteArgForShell(value, shellName=this.shell) {
286
+ const input = value == null ? "" : String(value)
287
+ if (this.isCmdShell(shellName)) {
288
+ return `"${input.replace(/([()%!^"<>&|])/g, '^$1')}"`
289
+ }
290
+ if (this.isPowerShell(shellName)) {
291
+ return `'${input.replace(/'/g, "''")}'`
292
+ }
293
+ return `'${input.split("'").join("'\"'\"'")}'`
294
+ }
295
+ buildStructuredMessage(message, shellName=this.shell) {
296
+ // Structured message objects are argv-like; unresolved pure-template leaves are omitted.
297
+ const normalized = this.normalizeStructuredMessage(message)
298
+ if (!normalized || (normalized.constructor === Object && Object.keys(normalized).length === 0)) {
299
+ return ""
300
+ }
301
+ const chunks = unparse(normalized)
302
+ .filter((item) => item != null)
303
+ .map((item) => this.quoteArgForShell(item, shellName))
304
+ if (chunks.length === 0) {
305
+ return ""
306
+ }
307
+ if (this.isPowerShell(shellName)) {
308
+ return `& ${chunks.join(" ")}`
309
+ }
310
+ return chunks.join(" ")
311
+ }
257
312
  async start(params, ondata) {
258
313
  this.ondata = ondata
259
314
  if (this.nudgeRestoreTimer) {
@@ -783,21 +838,17 @@ class Shell {
783
838
  delimiter = " && "; // must use &&. & doesn't necessariliy wait until the curruent command finishes
784
839
  }
785
840
  }
786
- return params.message.filter((m) => {
841
+ return params.message.map((message) => {
842
+ if (message && message.constructor === Object) {
843
+ return this.buildStructuredMessage(message)
844
+ }
845
+ return message
846
+ }).filter((m) => {
787
847
  return m && !/^\s+$/.test(m)
788
848
  }).join(delimiter)
789
849
  //return params.message.join(" && ")
790
850
  } else {
791
- // command line message
792
- let chunks = unparse(params.message).map((item) => {
793
- let tokens = item.split(" ")
794
- if (tokens.length > 1) {
795
- return `"${item}"`
796
- } else {
797
- return item
798
- }
799
- })
800
- return `${chunks.join(" ")}`
851
+ return this.buildStructuredMessage(params.message)
801
852
  }
802
853
  } else {
803
854
  return ""
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pinokiod",
3
- "version": "7.1.16",
3
+ "version": "7.1.17",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {