* Execute a sub-command executable. * * @private
(subcommand, args)
| 1213 | */ |
| 1214 | |
| 1215 | _executeSubCommand(subcommand, args) { |
| 1216 | args = args.slice(); |
| 1217 | const sourceExt = ['.js', '.ts', '.tsx', '.mjs', '.cjs']; |
| 1218 | |
| 1219 | function findFile(baseDir, baseName) { |
| 1220 | // Look for specified file |
| 1221 | const localBin = path.resolve(baseDir, baseName); |
| 1222 | if (fs.existsSync(localBin)) return localBin; |
| 1223 | |
| 1224 | // Stop looking if candidate already has an expected extension. |
| 1225 | if (sourceExt.includes(path.extname(baseName))) return undefined; |
| 1226 | |
| 1227 | // Try all the extensions. |
| 1228 | const foundExt = sourceExt.find((ext) => |
| 1229 | fs.existsSync(`${localBin}${ext}`), |
| 1230 | ); |
| 1231 | if (foundExt) return `${localBin}${foundExt}`; |
| 1232 | |
| 1233 | return undefined; |
| 1234 | } |
| 1235 | |
| 1236 | // Not checking for help first. Unlikely to have mandatory and executable, and can't robustly test for help flags in external command. |
| 1237 | this._checkForMissingMandatoryOptions(); |
| 1238 | this._checkForConflictingOptions(); |
| 1239 | |
| 1240 | // executableFile and executableDir might be full path, or just a name |
| 1241 | let executableFile = |
| 1242 | subcommand._executableFile || `${this._name}-${subcommand._name}`; |
| 1243 | let executableDir = this._executableDir || ''; |
| 1244 | if (this._scriptPath) { |
| 1245 | let resolvedScriptPath; // resolve possible symlink for installed npm binary |
| 1246 | try { |
| 1247 | resolvedScriptPath = fs.realpathSync(this._scriptPath); |
| 1248 | } catch { |
| 1249 | resolvedScriptPath = this._scriptPath; |
| 1250 | } |
| 1251 | executableDir = path.resolve( |
| 1252 | path.dirname(resolvedScriptPath), |
| 1253 | executableDir, |
| 1254 | ); |
| 1255 | } |
| 1256 | |
| 1257 | // Look for a local file in preference to a command in PATH. |
| 1258 | if (executableDir) { |
| 1259 | let localFile = findFile(executableDir, executableFile); |
| 1260 | |
| 1261 | // Legacy search using prefix of script name instead of command name |
| 1262 | if (!localFile && !subcommand._executableFile && this._scriptPath) { |
| 1263 | const legacyName = path.basename( |
| 1264 | this._scriptPath, |
| 1265 | path.extname(this._scriptPath), |
| 1266 | ); |
| 1267 | if (legacyName !== this._name) { |
| 1268 | localFile = findFile( |
| 1269 | executableDir, |
| 1270 | `${legacyName}-${subcommand._name}`, |
| 1271 | ); |
| 1272 | } |
no test coverage detected