forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
154 lines (125 loc) · 3.33 KB
/
utils.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
'use strict'
const fs = require('fs-extra')
const path = require('path')
const execa = require('execa')
const which = require('which')
async function startServer (dir) {
async function serveFrom (path) {
return new Promise((resolve, reject) => {
let output = ''
const proc = execa.command(`http-server ${path} -a 127.0.0.1`, {
cwd: __dirname
})
proc.all.on('data', (data) => {
process.stdout.write(data)
const line = data.toString('utf8')
output += line
if (output.includes('Hit CTRL-C to stop the server')) {
// find the port
const port = output.match(/http:\/\/127.0.0.1:(\d+)/)[1]
if (!port) {
throw new Error(`Could not find port in ${output}`)
}
resolve({
stop: () => {
console.info('Stopping server')
proc.kill('SIGINT', {
forceKillAfterTimeout: 2000
})
},
url: `http://127.0.0.1:${port}`
})
}
})
proc.then(() => {}, (err) => reject(err))
})
}
// start something..
const serverPaths = [
path.join(dir, 'build'),
path.join(dir, 'dist'),
path.join(dir, 'public')
]
for (const p of serverPaths) {
if (fs.existsSync(p)) {
return serveFrom(p)
}
}
// running a bare index.html file
const files = [
path.join(dir, 'index.html')
]
for (const f of files) {
if (fs.existsSync(f)) {
console.info('Found bare file', f)
if (!fs.existsSync(path.resolve(dir, '../../dist'))) {
console.info('Building IPFS')
const proc = execa.command('npm run build', {
cwd: path.resolve(dir, '../../'),
env: {
...process.env,
CI: true // needed for some "clever" build tools
}
})
proc.all.on('data', (data) => {
process.stdout.write(data)
})
await proc
}
return Promise.resolve({
url: `file://${f}`,
stop: () => {}
})
}
}
throw new Error('Browser examples must contain a `public`, `dist` or `build` folder or an `index.html` file')
}
function ephemeralPort (min = 49152, max = 65535) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
async function isExecutable (command) {
try {
await fs.access(command, fs.constants.X_OK)
return true
} catch (err) {
if (err.code === 'ENOENT') {
return isExecutable(await which(command))
}
if (err.code === 'EACCES') {
return false
}
throw err
}
}
async function waitForOutput (expectedOutput, command, args = [], opts = {}) {
if (!await isExecutable(command)) {
args.unshift(command)
command = 'node'
}
const proc = execa(command, args, opts)
let output = ''
let time = 120000
let timeout = setTimeout(() => {
throw new Error(`Did not see "${expectedOutput}" in output from "${[command].concat(args).join(' ')}" after ${time/1000}s`)
}, time)
proc.all.on('data', (data) => {
process.stdout.write(data)
output += data.toString('utf8')
if (output.includes(expectedOutput)) {
clearTimeout(timeout)
proc.kill()
}
})
try {
await proc
} catch (err) {
if (!err.killed) {
throw err
}
}
}
module.exports = {
startServer,
ephemeralPort,
waitForOutput
}