|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const execa = require('execa'); |
| 5 | +const { renameSync } = require('fs'); |
| 6 | +const stripAnsi = require('strip-ansi'); |
| 7 | + |
| 8 | +const ROOT = process.env.GITHUB_WORKSPACE ? process.env.GITHUB_WORKSPACE : path.resolve(__dirname, '../../'); |
| 9 | +const CLI_ENTRY_PATH = path.resolve(ROOT, './packages/webpack-cli/bin/cli.js'); |
| 10 | + |
| 11 | +const getPkgPath = (pkg) => { |
| 12 | + return path.resolve(ROOT, `./node_modules/@webpack-cli/${pkg}`); |
| 13 | +}; |
| 14 | + |
| 15 | +const swapPkgName = (current, next) => { |
| 16 | + console.log(` swapping ${current} with ${next}`); |
| 17 | + renameSync(getPkgPath(current), getPkgPath(next)); |
| 18 | +}; |
| 19 | + |
| 20 | +const runTest = () => { |
| 21 | + // Simulate package missing |
| 22 | + swapPkgName('serve', '.serve'); |
| 23 | + |
| 24 | + const proc = execa(CLI_ENTRY_PATH, ['serve'], { |
| 25 | + cwd: __dirname, |
| 26 | + }); |
| 27 | + |
| 28 | + proc.stdin.setDefaultEncoding('utf-8'); |
| 29 | + |
| 30 | + proc.stdout.on('data', (chunk) => { |
| 31 | + console.log(` stdout: ${chunk.toString()}`); |
| 32 | + }); |
| 33 | + |
| 34 | + return new Promise((resolve) => { |
| 35 | + setTimeout(() => { |
| 36 | + proc.kill(); |
| 37 | + }, 30000); |
| 38 | + |
| 39 | + const errorMessage = "For using this command you need to install: '@webpack-cli/serve' package"; |
| 40 | + |
| 41 | + let hasErrorMessage = false, |
| 42 | + hasPassed = false; |
| 43 | + |
| 44 | + proc.stderr.on('data', (chunk) => { |
| 45 | + let data = stripAnsi(chunk.toString()); |
| 46 | + console.log(` stderr: ${data}`); |
| 47 | + |
| 48 | + if (data.includes(errorMessage)) { |
| 49 | + hasErrorMessage = true; |
| 50 | + } |
| 51 | + |
| 52 | + if (hasErrorMessage) { |
| 53 | + hasPassed = true; |
| 54 | + proc.kill(); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + proc.on('exit', () => { |
| 59 | + swapPkgName('.serve', 'serve'); |
| 60 | + resolve(hasPassed); |
| 61 | + }); |
| 62 | + |
| 63 | + proc.on('error', () => { |
| 64 | + swapPkgName('.serve', 'serve'); |
| 65 | + resolve(false); |
| 66 | + }); |
| 67 | + }); |
| 68 | +}; |
| 69 | + |
| 70 | +const runTestWithHelp = () => { |
| 71 | + // Simulate package missing |
| 72 | + swapPkgName('serve', '.serve'); |
| 73 | + |
| 74 | + const proc = execa(CLI_ENTRY_PATH, ['help', 'serve'], { |
| 75 | + cwd: __dirname, |
| 76 | + }); |
| 77 | + |
| 78 | + proc.stdin.setDefaultEncoding('utf-8'); |
| 79 | + |
| 80 | + proc.stdout.on('data', (chunk) => { |
| 81 | + console.log(` stdout: ${chunk.toString()}`); |
| 82 | + }); |
| 83 | + |
| 84 | + return new Promise((resolve) => { |
| 85 | + setTimeout(() => { |
| 86 | + proc.kill(); |
| 87 | + }, 30000); |
| 88 | + |
| 89 | + const logMessage = "For using 'serve' command you need to install '@webpack-cli/serve' package"; |
| 90 | + const undefinedLogMessage = "Can't find and load command"; |
| 91 | + |
| 92 | + let hasLogMessage = false, |
| 93 | + hasUndefinedLogMessage = false, |
| 94 | + hasPassed = false; |
| 95 | + |
| 96 | + proc.stderr.on('data', (chunk) => { |
| 97 | + let data = stripAnsi(chunk.toString()); |
| 98 | + console.log(` stderr: ${data}`); |
| 99 | + |
| 100 | + if (data.includes(logMessage)) { |
| 101 | + hasLogMessage = true; |
| 102 | + } |
| 103 | + |
| 104 | + if (data.includes(undefinedLogMessage)) { |
| 105 | + hasUndefinedLogMessage = true; |
| 106 | + } |
| 107 | + |
| 108 | + if (hasLogMessage || hasUndefinedLogMessage) { |
| 109 | + hasPassed = true; |
| 110 | + proc.kill(); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + proc.on('exit', () => { |
| 115 | + swapPkgName('.serve', 'serve'); |
| 116 | + resolve(hasPassed); |
| 117 | + }); |
| 118 | + |
| 119 | + proc.on('error', () => { |
| 120 | + swapPkgName('.serve', 'serve'); |
| 121 | + resolve(false); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}; |
| 125 | + |
| 126 | +module.exports.run = [runTest, runTestWithHelp]; |
| 127 | +module.exports.name = 'Missing @webpack-cli/serve'; |
0 commit comments