|
3 | 3 | const execa = require('execa')
|
4 | 4 | const fs = require('fs-extra')
|
5 | 5 | const which = require('which')
|
| 6 | +const path = require('path') |
| 7 | + |
| 8 | +async function startServer (dir) { |
| 9 | + async function serveFrom (path) { |
| 10 | + return new Promise((resolve, reject) => { |
| 11 | + let output = '' |
| 12 | + |
| 13 | + const proc = execa.command(`http-server ${path} -a 127.0.0.1`, { |
| 14 | + cwd: __dirname |
| 15 | + }) |
| 16 | + proc.all.on('data', (data) => { |
| 17 | + process.stdout.write(data) |
| 18 | + |
| 19 | + const line = data.toString('utf8') |
| 20 | + output += line |
| 21 | + |
| 22 | + if (output.includes('Hit CTRL-C to stop the server')) { |
| 23 | + // find the port |
| 24 | + const port = output.match(/http:\/\/127.0.0.1:(\d+)/)[1] |
| 25 | + |
| 26 | + if (!port) { |
| 27 | + throw new Error(`Could not find port in ${output}`) |
| 28 | + } |
| 29 | + |
| 30 | + resolve({ |
| 31 | + stop: () => { |
| 32 | + console.info('Stopping server') |
| 33 | + proc.kill('SIGINT', { |
| 34 | + forceKillAfterTimeout: 2000 |
| 35 | + }) |
| 36 | + }, |
| 37 | + url: `http://127.0.0.1:${port}` |
| 38 | + }) |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + proc.then(() => {}, (err) => reject(err)) |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + const serverPaths = [ |
| 47 | + path.join(dir, 'build'), |
| 48 | + path.join(dir, 'dist'), |
| 49 | + path.join(dir, 'public') |
| 50 | + ] |
| 51 | + |
| 52 | + for (const p of serverPaths) { |
| 53 | + if (fs.existsSync(p)) { |
| 54 | + return serveFrom(p) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // running a bare index.html file |
| 59 | + const files = [ |
| 60 | + path.join(dir, 'index.html') |
| 61 | + ] |
| 62 | + |
| 63 | + for (const f of files) { |
| 64 | + if (fs.existsSync(f)) { |
| 65 | + console.info('Found bare file', f) |
| 66 | + |
| 67 | + if (!fs.existsSync(path.resolve(dir, '../../dist'))) { |
| 68 | + console.info('Building IPFS') |
| 69 | + const proc = execa.command('npm run build', { |
| 70 | + cwd: path.resolve(dir, '../../'), |
| 71 | + env: { |
| 72 | + ...process.env, |
| 73 | + CI: true // needed for some "clever" build tools |
| 74 | + } |
| 75 | + }) |
| 76 | + proc.all.on('data', (data) => { |
| 77 | + process.stdout.write(data) |
| 78 | + }) |
| 79 | + |
| 80 | + await proc |
| 81 | + } |
| 82 | + |
| 83 | + return Promise.resolve({ |
| 84 | + url: `file://${f}`, |
| 85 | + stop: () => { } |
| 86 | + }) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + throw new Error('Browser examples must contain a `public`, `dist` or `build` folder or an `index.html` file') |
| 91 | +} |
| 92 | + |
| 93 | +function ephemeralPort (min = 49152, max = 65535) { |
| 94 | + return Math.floor(Math.random() * (max - min + 1) + min) |
| 95 | +} |
6 | 96 |
|
7 | 97 | async function isExecutable (command) {
|
8 | 98 | try {
|
@@ -57,5 +147,7 @@ async function waitForOutput (expectedOutput, command, args = [], opts = {}) {
|
57 | 147 | }
|
58 | 148 |
|
59 | 149 | module.exports = {
|
| 150 | + startServer, |
| 151 | + ephemeralPort, |
60 | 152 | waitForOutput
|
61 | 153 | }
|
0 commit comments