-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcommon.js
171 lines (145 loc) · 4.86 KB
/
common.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
166
167
168
169
170
171
/*
* This file is part of the xPack project (http://xpack.github.io).
* Copyright (c) 2017 Liviu Ionescu. All rights reserved.
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose is hereby granted, under the terms of the MIT license.
*
* If a copy of the license was not distributed with this file, it can
* be obtained from https://opensource.org/license/mit.
*/
'use strict'
/* eslint valid-jsdoc: "error" */
/* eslint max-len: [ "error", 80, { "ignoreUrls": true } ] */
// ----------------------------------------------------------------------------
// https://nodejs.org/docs/latest-v12.x/api/index.htm
import assert from 'assert'
import { spawn } from 'child_process'
import { Console } from 'console'
import fs from 'fs'
import { Writable } from 'stream'
import * as tar from 'tar'
import zlib from 'zlib'
// ----------------------------------------------------------------------------
// ES6: `import { CliHelp } from './utils/cli-helps.js'
import { Xpm } from '../lib/main.js'
// ----------------------------------------------------------------------------
/**
* Test common options, like --version, --help, etc.
*/
// ----------------------------------------------------------------------------
const nodeBin = process.env.npm_node_execpath || process.env.NODE ||
process.execPath
const executableName = './bin/xpm.js'
const programName = 'xpm'
/**
* @class Main
*/
// export
export class Common {
/**
* @summary Run xpm in a separate process.
*
* @async
* @param {string[]} args Command line arguments
* @param {Object} spawnOpts Optional spawn options.
* @returns {{code: number, stdout: string, stderr: string}} Exit
* code and captured output/error streams.
*
* @description
* Spawn a separate process to run node with the given arguments and
* return the exit code and the stdio streams captured in strings.
*/
static async xpmCli (args, spawnOpts = {}) {
return new Promise((resolve, reject) => {
spawnOpts.env = spawnOpts.env || process.env
// Runs in project root.
// console.log(`Current directory: ${process.cwd()}`)
let stdout = ''
let stderr = ''
const cmd = [executableName]
const child = spawn(nodeBin, cmd.concat(args), spawnOpts)
assert(child.stderr)
child.stderr.on('data', (chunk) => {
// console.log(chunk.toString())
stderr += chunk
})
assert(child.stdout)
child.stdout.on('data', (chunk) => {
// console.log(chunk.toString())
stdout += chunk
})
child.on('error', (err) => {
reject(err)
})
child.on('close', (code) => {
resolve({ code, stdout, stderr })
})
})
}
/**
* @summary Run xpm as a library call.
*
* @async
* @param {string[]} args Command line arguments
* @param {Object} ctx Optional context.
* @returns {{code: number, stdout: string, stderr: string}} Exit
* code and captured output/error streams.
*
* @description
* Call the application directly, as a regular module, and return
* the exit code and the stdio streams captured in strings.
*/
static async xpmLib (args, ctx = null) {
assert(Xpm !== null, 'no application class')
// Create two streams to local strings.
let stdout = ''
const ostream = new Writable({
write (chunk, encoding, callback) {
stdout += chunk.toString()
callback()
}
})
let stderr = ''
const errstream = new Writable({
write (chunk, encoding, callback) {
stderr += chunk.toString()
callback()
}
})
const _console = new Console(ostream, errstream)
const context = await Xpm.initialiseContext(ctx, programName, _console)
const app = new Xpm(context)
const code = await app.main(args)
return { code, stdout, stderr }
}
/**
* @summary Extract files from a .tgz archive into a folder.
*
* @async
* @param {string} tgzPath Path to archive file.
* @param {string} destPath Path to destination folder.
* @returns {undefined} Nothing.
*/
static async extractTgz (tgzPath, destPath) {
return new Promise((resolve, reject) => {
fs.createReadStream(tgzPath)
.on('error', (er) => { reject(er) })
.pipe(zlib.createGunzip())
.on('error', (er) => { reject(er) })
.pipe(tar.Extract({ path: destPath }))
.on('error', (er) => { reject(er) })
.on('end', () => { resolve() })
})
}
}
// ----------------------------------------------------------------------------
// Node.js specific export definitions.
// By default, `module.exports = {}`.
// The Main class is added as a property to this object.
// module.exports.Common = Common
// In ES6, it would be:
// export class Common { ... }
// ...
// import { Common } from 'common.js'
// ----------------------------------------------------------------------------