-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract common method for parsing scriptArgs
- Loading branch information
1 parent
d628279
commit 17f2c0d
Showing
3 changed files
with
38 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* To customize proposals for multiple environments, we pass the "variant" | ||
* identifier in scriptArgs. The variant must match a knownVariant. | ||
* | ||
* @param {import('./externalTypes.js').DeployScriptEndownments} endowments | ||
* @param {string} name a name to use in error messages or Usage suggestions. | ||
* @param {string[]} knownVariants | ||
*/ | ||
export const parseScriptArgs = async (endowments, name, knownVariants) => { | ||
const { scriptArgs } = endowments; | ||
const variantOrConfig = scriptArgs?.[0]; | ||
console.log(`${name}`, variantOrConfig); | ||
|
||
const Usage = `agoric run ${name}.js ${[...knownVariants, '<json-config>'].join(' | ')}`; | ||
const opts = {}; | ||
|
||
if (typeof variantOrConfig === 'string') { | ||
if (variantOrConfig[0] === '{') { | ||
try { | ||
opts.config = JSON.parse(variantOrConfig); | ||
} catch (err) { | ||
throw Error(`Failed to parse config argument ${variantOrConfig}`); | ||
} | ||
} else { | ||
opts.variant = variantOrConfig; | ||
} | ||
} else { | ||
console.error(Usage); | ||
throw Error(Usage); | ||
} | ||
|
||
return opts; | ||
}; |