|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +// Create an object of all benchmark scripts |
| 7 | +const benchmarks = {}; |
| 8 | +fs.readdirSync(__dirname) |
| 9 | + .filter(function(name) { |
| 10 | + return fs.statSync(path.resolve(__dirname, name)).isDirectory(); |
| 11 | + }) |
| 12 | + .forEach(function(category) { |
| 13 | + benchmarks[category] = fs.readdirSync(path.resolve(__dirname, category)) |
| 14 | + .filter((filename) => filename[0] !== '.' && filename[0] !== '_'); |
| 15 | + }); |
| 16 | + |
| 17 | +function CLI(usage, settings) { |
| 18 | + if (!(this instanceof CLI)) return new CLI(usage, settings); |
| 19 | + |
| 20 | + if (process.argv.length < 3) { |
| 21 | + this.abort(usage); // abort will exit the process |
| 22 | + } |
| 23 | + |
| 24 | + this.usage = usage; |
| 25 | + this.optional = {}; |
| 26 | + this.items = []; |
| 27 | + |
| 28 | + for (const argName of settings.arrayArgs) { |
| 29 | + this.optional[argName] = []; |
| 30 | + } |
| 31 | + |
| 32 | + let currentOptional = null; |
| 33 | + let mode = 'both'; // possible states are: [both, option, item] |
| 34 | + |
| 35 | + for (const arg of process.argv.slice(2)) { |
| 36 | + if (arg === '--') { |
| 37 | + // Only items can follow -- |
| 38 | + mode = 'item'; |
| 39 | + } else if (['both', 'option'].includes(mode) && arg[0] === '-') { |
| 40 | + // Optional arguments declaration |
| 41 | + |
| 42 | + if (arg[1] === '-') { |
| 43 | + currentOptional = arg.slice(2); |
| 44 | + } else { |
| 45 | + currentOptional = arg.slice(1); |
| 46 | + } |
| 47 | + |
| 48 | + // Default the value to true |
| 49 | + if (!settings.arrayArgs.includes(currentOptional)) { |
| 50 | + this.optional[currentOptional] = true; |
| 51 | + } |
| 52 | + |
| 53 | + // expect the next value to be option related (either -- or the value) |
| 54 | + mode = 'option'; |
| 55 | + } else if (mode === 'option') { |
| 56 | + // Optional arguments value |
| 57 | + |
| 58 | + if (settings.arrayArgs.includes(currentOptional)) { |
| 59 | + this.optional[currentOptional].push(arg); |
| 60 | + } else { |
| 61 | + this.optional[currentOptional] = arg; |
| 62 | + } |
| 63 | + |
| 64 | + // the next value can be either an option or an item |
| 65 | + mode = 'both'; |
| 66 | + } else if (['both', 'item'].includes(mode)) { |
| 67 | + // item arguments |
| 68 | + this.items.push(arg); |
| 69 | + |
| 70 | + // the next value must be an item |
| 71 | + mode = 'item'; |
| 72 | + } else { |
| 73 | + // Bad case, abort |
| 74 | + this.abort(usage); |
| 75 | + return; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +module.exports = CLI; |
| 80 | + |
| 81 | +CLI.prototype.abort = function(msg) { |
| 82 | + console.error(msg); |
| 83 | + process.exit(1); |
| 84 | +}; |
| 85 | + |
| 86 | +CLI.prototype.benchmarks = function() { |
| 87 | + const paths = []; |
| 88 | + const filter = this.optional.filter || false; |
| 89 | + |
| 90 | + for (const category of this.items) { |
| 91 | + for (const scripts of benchmarks[category]) { |
| 92 | + if (filter && scripts.lastIndexOf(filter) === -1) continue; |
| 93 | + |
| 94 | + paths.push(path.join(category, scripts)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return paths; |
| 99 | +}; |
0 commit comments