11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst fs = require('fs'); 41cb0ef41Sopenharmony_ciconst path = require('path'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// Create an object of all benchmark scripts 71cb0ef41Sopenharmony_ciconst benchmarks = {}; 81cb0ef41Sopenharmony_cifs.readdirSync(__dirname) 91cb0ef41Sopenharmony_ci .filter((name) => { 101cb0ef41Sopenharmony_ci return name !== 'fixtures' && 111cb0ef41Sopenharmony_ci fs.statSync(path.resolve(__dirname, name)).isDirectory(); 121cb0ef41Sopenharmony_ci }) 131cb0ef41Sopenharmony_ci .forEach((category) => { 141cb0ef41Sopenharmony_ci benchmarks[category] = fs.readdirSync(path.resolve(__dirname, category)) 151cb0ef41Sopenharmony_ci .filter((filename) => filename[0] !== '.' && filename[0] !== '_'); 161cb0ef41Sopenharmony_ci }); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cifunction CLI(usage, settings) { 191cb0ef41Sopenharmony_ci if (process.argv.length < 3) { 201cb0ef41Sopenharmony_ci this.abort(usage); // Abort will exit the process 211cb0ef41Sopenharmony_ci } 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci this.usage = usage; 241cb0ef41Sopenharmony_ci this.optional = {}; 251cb0ef41Sopenharmony_ci this.items = []; 261cb0ef41Sopenharmony_ci this.test = false; 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci for (const argName of settings.arrayArgs) { 291cb0ef41Sopenharmony_ci this.optional[argName] = []; 301cb0ef41Sopenharmony_ci } 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci let currentOptional = null; 331cb0ef41Sopenharmony_ci let mode = 'both'; // Possible states are: [both, option, item] 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci for (const arg of process.argv.slice(2)) { 361cb0ef41Sopenharmony_ci if (arg === '--') { 371cb0ef41Sopenharmony_ci // Only items can follow -- 381cb0ef41Sopenharmony_ci mode = 'item'; 391cb0ef41Sopenharmony_ci } else if (mode === 'both' && arg[0] === '-') { 401cb0ef41Sopenharmony_ci // Optional arguments declaration 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci if (arg[1] === '-') { 431cb0ef41Sopenharmony_ci currentOptional = arg.slice(2); 441cb0ef41Sopenharmony_ci } else { 451cb0ef41Sopenharmony_ci currentOptional = arg.slice(1); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci if (settings.boolArgs && settings.boolArgs.includes(currentOptional)) { 491cb0ef41Sopenharmony_ci this.optional[currentOptional] = true; 501cb0ef41Sopenharmony_ci mode = 'both'; 511cb0ef41Sopenharmony_ci } else { 521cb0ef41Sopenharmony_ci // Expect the next value to be option related (either -- or the value) 531cb0ef41Sopenharmony_ci mode = 'option'; 541cb0ef41Sopenharmony_ci } 551cb0ef41Sopenharmony_ci } else if (mode === 'option') { 561cb0ef41Sopenharmony_ci // Optional arguments value 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci if (settings.arrayArgs.includes(currentOptional)) { 591cb0ef41Sopenharmony_ci this.optional[currentOptional].push(arg); 601cb0ef41Sopenharmony_ci } else { 611cb0ef41Sopenharmony_ci this.optional[currentOptional] = arg; 621cb0ef41Sopenharmony_ci } 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci // The next value can be either an option or an item 651cb0ef41Sopenharmony_ci mode = 'both'; 661cb0ef41Sopenharmony_ci } else if (arg === 'test') { 671cb0ef41Sopenharmony_ci this.test = true; 681cb0ef41Sopenharmony_ci } else if (['both', 'item'].includes(mode)) { 691cb0ef41Sopenharmony_ci // item arguments 701cb0ef41Sopenharmony_ci this.items.push(arg); 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci // The next value must be an item 731cb0ef41Sopenharmony_ci mode = 'item'; 741cb0ef41Sopenharmony_ci } else { 751cb0ef41Sopenharmony_ci // Bad case, abort 761cb0ef41Sopenharmony_ci this.abort(usage); 771cb0ef41Sopenharmony_ci } 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci} 801cb0ef41Sopenharmony_cimodule.exports = CLI; 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ciCLI.prototype.abort = function(msg) { 831cb0ef41Sopenharmony_ci console.error(msg); 841cb0ef41Sopenharmony_ci process.exit(1); 851cb0ef41Sopenharmony_ci}; 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ciCLI.prototype.benchmarks = function() { 881cb0ef41Sopenharmony_ci const paths = []; 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci if (this.items.includes('all')) { 911cb0ef41Sopenharmony_ci this.items = Object.keys(benchmarks); 921cb0ef41Sopenharmony_ci } 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci for (const category of this.items) { 951cb0ef41Sopenharmony_ci if (benchmarks[category] === undefined) { 961cb0ef41Sopenharmony_ci console.error(`The "${category}" category does not exist.`); 971cb0ef41Sopenharmony_ci process.exit(1); 981cb0ef41Sopenharmony_ci } 991cb0ef41Sopenharmony_ci for (const scripts of benchmarks[category]) { 1001cb0ef41Sopenharmony_ci if (this.shouldSkip(scripts)) continue; 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci paths.push(path.join(category, scripts)); 1031cb0ef41Sopenharmony_ci } 1041cb0ef41Sopenharmony_ci } 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci return paths; 1071cb0ef41Sopenharmony_ci}; 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ciCLI.prototype.shouldSkip = function(scripts) { 1101cb0ef41Sopenharmony_ci const filters = this.optional.filter || []; 1111cb0ef41Sopenharmony_ci const excludes = this.optional.exclude || []; 1121cb0ef41Sopenharmony_ci let skip = filters.length > 0; 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci for (const filter of filters) { 1151cb0ef41Sopenharmony_ci if (scripts.lastIndexOf(filter) !== -1) { 1161cb0ef41Sopenharmony_ci skip = false; 1171cb0ef41Sopenharmony_ci } 1181cb0ef41Sopenharmony_ci } 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci for (const exclude of excludes) { 1211cb0ef41Sopenharmony_ci if (scripts.lastIndexOf(exclude) !== -1) { 1221cb0ef41Sopenharmony_ci skip = true; 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci } 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci return skip; 1271cb0ef41Sopenharmony_ci}; 128