11cb0ef41Sopenharmony_ci#!/usr/bin/env node 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst which = require('../lib') 41cb0ef41Sopenharmony_ciconst argv = process.argv.slice(2) 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst usage = (err) => { 71cb0ef41Sopenharmony_ci if (err) { 81cb0ef41Sopenharmony_ci console.error(`which: ${err}`) 91cb0ef41Sopenharmony_ci } 101cb0ef41Sopenharmony_ci console.error('usage: which [-as] program ...') 111cb0ef41Sopenharmony_ci process.exit(1) 121cb0ef41Sopenharmony_ci} 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciif (!argv.length) { 151cb0ef41Sopenharmony_ci return usage() 161cb0ef41Sopenharmony_ci} 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cilet dashdash = false 191cb0ef41Sopenharmony_ciconst [commands, flags] = argv.reduce((acc, arg) => { 201cb0ef41Sopenharmony_ci if (dashdash || arg === '--') { 211cb0ef41Sopenharmony_ci dashdash = true 221cb0ef41Sopenharmony_ci return acc 231cb0ef41Sopenharmony_ci } 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci if (!/^-/.test(arg)) { 261cb0ef41Sopenharmony_ci acc[0].push(arg) 271cb0ef41Sopenharmony_ci return acc 281cb0ef41Sopenharmony_ci } 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci for (const flag of arg.slice(1).split('')) { 311cb0ef41Sopenharmony_ci if (flag === 's') { 321cb0ef41Sopenharmony_ci acc[1].silent = true 331cb0ef41Sopenharmony_ci } else if (flag === 'a') { 341cb0ef41Sopenharmony_ci acc[1].all = true 351cb0ef41Sopenharmony_ci } else { 361cb0ef41Sopenharmony_ci usage(`illegal option -- ${flag}`) 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci return acc 411cb0ef41Sopenharmony_ci}, [[], {}]) 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_cifor (const command of commands) { 441cb0ef41Sopenharmony_ci try { 451cb0ef41Sopenharmony_ci const res = which.sync(command, { all: flags.all }) 461cb0ef41Sopenharmony_ci if (!flags.silent) { 471cb0ef41Sopenharmony_ci console.log([].concat(res).join('\n')) 481cb0ef41Sopenharmony_ci } 491cb0ef41Sopenharmony_ci } catch (err) { 501cb0ef41Sopenharmony_ci process.exitCode = 1 511cb0ef41Sopenharmony_ci } 521cb0ef41Sopenharmony_ci} 53