Skip to content

Commit

Permalink
remove getArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
Casheeew committed Dec 28, 2023
1 parent 8d5d215 commit f460549
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 87 deletions.
58 changes: 39 additions & 19 deletions dev/bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import path from 'path';
import readline from 'readline';
import {buildLibs} from '../build-libs.js';
import {ManifestUtil} from '../manifest-util.js';
import {getAllFiles, getArgs, testMain} from '../util.js';
import {getAllFiles, testMain} from '../util.js';
import {parseArgs} from 'util';

const dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -218,24 +219,42 @@ function ensureFilesExist(directory, files) {
}
}


/**
* @param {string[]} argv
*/
export async function main(argv) {
const args = getArgs(argv, new Map(/** @type {[key: string, value: (boolean|null|number|string|string[])][]} */ ([
['all', false],
['default', false],
['manifest', null],
['dry-run', false],
['dry-run-build-zip', false],
['yomitan-version', '0.0.0.0'],
[null, []]
])));

const dryRun = /** @type {boolean} */ (args.get('dry-run'));
const dryRunBuildZip = /** @type {boolean} */ (args.get('dry-run-build-zip'));
const yomitanVersion = /** @type {string} */ (args.get('yomitan-version'));
/** @type {import('util').ParseArgsConfig['options']} */
const parseArgsConfigOptions = {
'all': {
type: 'boolean',
default: false
},
'default': {
type: 'boolean',
default: false
},
'manifest': {
type: 'string'
},
'dry-run': {
type: 'boolean',
default: false
},
'dry-run-build-zip': {
type: 'boolean',
default: false
},
'yomitan-version': {
type: 'string',
default: '0.0.0.0'
}
};

const {values: args} = parseArgs({args: argv, options: parseArgsConfigOptions});

const dryRun = /** @type {boolean} */ (args['dry-run']);
const dryRunBuildZip = /** @type {boolean} */ (args['dry-run-build-zip']);
const yomitanVersion = /** @type {string} */ (args['yomitan-version']);

const manifestUtil = new ManifestUtil();

Expand All @@ -247,14 +266,15 @@ export async function main(argv) {
try {
await buildLibs();
const variantNames = /** @type {string[]} */ ((
argv.length === 0 || args.get('all') ?
manifestUtil.getVariants().filter(({buildable}) => buildable !== false).map(({name}) => name) :
args.get(null)
// eslint-disable-next-line dot-notation
argv.length === 0 || args['all'] ?
manifestUtil.getVariants().filter(({buildable}) => buildable !== false).map(({name}) => name) : []
));
await build(buildDir, extDir, manifestUtil, variantNames, manifestPath, dryRun, dryRunBuildZip, yomitanVersion);
} finally {
// Restore manifest
const manifestName = /** @type {?string} */ ((!args.get('default') && args.get('manifest') !== null) ? args.get('manifest') : null);
// eslint-disable-next-line dot-notation
const manifestName = /** @type {?string} */ ((!args['default'] && typeof args['manifest'] !== 'undefined') ? args['manifest'] : null);
const restoreManifest = manifestUtil.getManifest(manifestName);
process.stdout.write('Restoring manifest...\n');
if (!dryRun) {
Expand Down
68 changes: 0 additions & 68 deletions dev/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,74 +21,6 @@ import JSZip from 'jszip';
import path from 'path';
import {parseJson} from './json.js';

/**
* @param {string[]} args
* @param {Map<?string, (boolean|null|number|string|string[])>} argMap
* @returns {Map<?string, (boolean|null|number|string|string[])>}
*/
export function getArgs(args, argMap) {
let key = null;
let canKey = true;
let onKey = false;
for (const arg of args) {
onKey = false;

if (canKey && arg.startsWith('--')) {
if (arg.length === 2) {
canKey = false;
key = null;
onKey = false;
} else {
key = arg.substring(2);
onKey = true;
}
}

const target = argMap.get(key);

switch (typeof target) {
case 'boolean':
argMap.set(key, true);
key = null;
break;
case 'number':
argMap.set(key, target + 1);
key = null;
break;
case 'string':
if (!onKey) {
argMap.set(key, arg);
key = null;
}
break;
case 'object':
if (target === null) {
if (!onKey) {
argMap.set(key, arg);
key = null;
}
return argMap;
} else if (Array.isArray(target)) {
if (!onKey) {
target.push(arg);
key = null;
}
return argMap;
} else {
console.error(`Unknown argument: ${arg}`);
key = null;
}
break;
default:
console.error(`Unknown argument: ${arg}`);
key = null;
break;
}
}

return argMap;
}

/**
* @param {string} baseDirectory
* @param {?(fileName: string, isDirectory: boolean) => boolean} predicate
Expand Down

0 comments on commit f460549

Please sign in to comment.