|
| 1 | +import {spawn} from 'node:child_process'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +type DevServerOptions = { |
| 5 | + id?: number; |
| 6 | + port?: number; |
| 7 | + projectPath?: string; |
| 8 | + customerAccountPush?: boolean; |
| 9 | + envFile?: string; |
| 10 | + storeKey?: string; |
| 11 | +}; |
| 12 | + |
| 13 | +export class DevServer { |
| 14 | + process: ReturnType<typeof spawn> | undefined; |
| 15 | + port: number; |
| 16 | + projectPath: string; |
| 17 | + customerAccountPush: boolean; |
| 18 | + capturedUrl?: string; |
| 19 | + id?: number; |
| 20 | + envFile?: string; |
| 21 | + storeKey?: string; |
| 22 | + |
| 23 | + constructor(options: DevServerOptions = {}) { |
| 24 | + this.id = options.id; |
| 25 | + this.storeKey = options.storeKey; |
| 26 | + this.port = options.port ?? 3100; |
| 27 | + this.projectPath = |
| 28 | + options.projectPath ?? path.join(__dirname, '../../templates/skeleton'); |
| 29 | + this.customerAccountPush = options.customerAccountPush ?? false; |
| 30 | + this.envFile = options.envFile; |
| 31 | + } |
| 32 | + |
| 33 | + getUrl() { |
| 34 | + return this.capturedUrl || `http://localhost:${this.port}`; |
| 35 | + } |
| 36 | + |
| 37 | + start() { |
| 38 | + if (this.process) { |
| 39 | + throw new Error(`Server ${this.id} is already running`); |
| 40 | + } |
| 41 | + |
| 42 | + return new Promise((resolve, reject) => { |
| 43 | + const args = ['run', 'dev', '--']; |
| 44 | + if (this.customerAccountPush) { |
| 45 | + args.push('--customer-account-push'); |
| 46 | + } |
| 47 | + |
| 48 | + if (this.envFile) { |
| 49 | + args.push('--env-file', this.envFile); |
| 50 | + } |
| 51 | + |
| 52 | + this.process = spawn('npm', args, { |
| 53 | + cwd: this.projectPath, |
| 54 | + env: { |
| 55 | + ...process.env, |
| 56 | + NODE_ENV: 'development', |
| 57 | + SHOPIFY_HYDROGEN_FLAG_PORT: this.port.toString(), |
| 58 | + }, |
| 59 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 60 | + }); |
| 61 | + |
| 62 | + let started = false; |
| 63 | + const timeout = setTimeout(() => { |
| 64 | + if (!started) { |
| 65 | + this.stop(); |
| 66 | + reject(new Error(`Server ${this.id} failed to start within timeout`)); |
| 67 | + } |
| 68 | + }, 60000); |
| 69 | + |
| 70 | + let localUrl: string | undefined; |
| 71 | + let tunnelUrl: string | undefined; |
| 72 | + |
| 73 | + const handleOutput = (output: string) => { |
| 74 | + if (!localUrl) { |
| 75 | + localUrl = output.match(/(http:\/\/localhost:\d+)/)?.[1]; |
| 76 | + } |
| 77 | + if (this.customerAccountPush && !tunnelUrl) { |
| 78 | + tunnelUrl = output.match(/(https:\/\/[^\s]+)/)?.[1]; |
| 79 | + } |
| 80 | + |
| 81 | + if (!started && output.includes('success')) { |
| 82 | + started = true; |
| 83 | + clearTimeout(timeout); |
| 84 | + this.capturedUrl = tunnelUrl || localUrl; |
| 85 | + const port = this.capturedUrl?.match(/:(\d+)/)?.[1]; |
| 86 | + if (port) { |
| 87 | + this.port = parseInt(port, 10); |
| 88 | + } |
| 89 | + if (!this.id) { |
| 90 | + this.id = |
| 91 | + this.port || parseInt((Math.random() * 1000).toFixed(0), 10); |
| 92 | + } |
| 93 | + console.log( |
| 94 | + `[test-server ${this.id}] Server started on ${this.capturedUrl} [${this.storeKey}]`, |
| 95 | + ); |
| 96 | + // Give the tunnel a bit more time to ensure everything is ready |
| 97 | + setTimeout(resolve, tunnelUrl ? 5000 : 0); |
| 98 | + } |
| 99 | + |
| 100 | + if ( |
| 101 | + output.includes('log in to Shopify') || |
| 102 | + output.includes('User verification code:') |
| 103 | + ) { |
| 104 | + clearTimeout(timeout); |
| 105 | + this.stop(); |
| 106 | + reject( |
| 107 | + new Error( |
| 108 | + 'Not logged in to Shopify CLI. Run: cd templates/skeleton && npx shopify auth login', |
| 109 | + ), |
| 110 | + ); |
| 111 | + } else if ( |
| 112 | + output.includes('Failed to prompt') || |
| 113 | + output.includes('Select a shop to log in') |
| 114 | + ) { |
| 115 | + clearTimeout(timeout); |
| 116 | + this.stop(); |
| 117 | + reject( |
| 118 | + new Error( |
| 119 | + 'Storefront not linked. Run: cd templates/skeleton && npx shopify hydrogen link', |
| 120 | + ), |
| 121 | + ); |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + if (this.process.stdout) { |
| 126 | + this.process.stdout.on('data', (data) => { |
| 127 | + const output = data.toString(); |
| 128 | + // !started && console.log(output); |
| 129 | + handleOutput(output); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + if (this.process.stderr) { |
| 134 | + this.process.stderr.on('data', (data) => { |
| 135 | + const output = data.toString(); |
| 136 | + // !started && console.log(output); |
| 137 | + handleOutput(output); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + this.process.on('error', (error) => { |
| 142 | + clearTimeout(timeout); |
| 143 | + reject(error); |
| 144 | + }); |
| 145 | + |
| 146 | + this.process.on('exit', (code) => { |
| 147 | + if (!started) { |
| 148 | + clearTimeout(timeout); |
| 149 | + reject(new Error(`Server ${this.id} exited with code ${code}`)); |
| 150 | + } |
| 151 | + }); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + stop() { |
| 156 | + return new Promise((resolve) => { |
| 157 | + if (!this.process) return resolve(false); |
| 158 | + console.log(`[test-server ${this.id}] Stopping server...`); |
| 159 | + |
| 160 | + this.process.on('exit', () => { |
| 161 | + this.process = undefined; |
| 162 | + resolve(true); |
| 163 | + }); |
| 164 | + |
| 165 | + this.process.kill('SIGTERM'); |
| 166 | + |
| 167 | + setTimeout(() => { |
| 168 | + this.process?.kill('SIGKILL'); |
| 169 | + }, 5000); |
| 170 | + }); |
| 171 | + } |
| 172 | +} |
0 commit comments