| 32 | } |
| 33 | |
| 34 | async execute() { |
| 35 | const startTime = Date.now(); |
| 36 | |
| 37 | if (!this.args.watch) { |
| 38 | this.bundler.on('bundle-error', () => process.exit(1)); |
| 39 | if (!this.args.inspect) { |
| 40 | this.host.on('worker-error', () => process.exit(1)); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | this.bundler.bundle(); |
| 45 | const siteInitialized = this.site ? this.site.init() : Promise.resolve(); |
| 46 | |
| 47 | await Promise.all([ |
| 48 | this.host.start(), |
| 49 | this.bundler.bundled, |
| 50 | siteInitialized, |
| 51 | this.kv.init() |
| 52 | ]); |
| 53 | |
| 54 | await this.host.setWorkerCode( |
| 55 | this.bundler.code, |
| 56 | '/worker.js', |
| 57 | [], |
| 58 | this.site ? this.site.manifest : null, |
| 59 | this.kv.namespaces |
| 60 | ); |
| 61 | |
| 62 | if (this.args.watch) { |
| 63 | const update = () => |
| 64 | this.host.setWorkerCode( |
| 65 | this.bundler.code, |
| 66 | '/worker.js', |
| 67 | [], |
| 68 | this.site ? this.site.manifest : null, |
| 69 | this.kv.namespaces |
| 70 | ); |
| 71 | this.bundler.on('bundle-end', update); |
| 72 | if (this.site) { |
| 73 | this.site.on('change', update); |
| 74 | } |
| 75 | this.kv.on('change', update); |
| 76 | } |
| 77 | |
| 78 | const url = `http://localhost:${this.args.port}/`; |
| 79 | logger.success( |
| 80 | `Worker running at ${chalk.cyan.underline(url)}`, |
| 81 | Date.now() - startTime |
| 82 | ); |
| 83 | |
| 84 | const page = await this.host.pageReady; |
| 85 | await page.evaluate(url => { |
| 86 | document.body.innerHTML = ` |
| 87 | <h1>Worker Dev</h1> |
| 88 | <p>Worker running at <a href="${url}" target="_blank">${url}</a>.</p>`; |
| 89 | }, url); |
| 90 | } |
| 91 | |