Add --option parameter for passing arbitrary ADL options

This commit is contained in:
David Wilson 2021-04-05 11:01:42 -07:00
Родитель b1da0b2392
Коммит 69dc7f8c4f
3 изменённых файлов: 32 добавлений и 5 удалений

Просмотреть файл

@ -14,8 +14,7 @@ import { CompilerHost } from "./types.js";
const args = yargs(process.argv.slice(2))
.scriptName("adl")
.help()
// .strict() -- TODO: Turn this back on if we add library-level option registration
.strictCommands()
.strict()
.command("compile <path>", "Compile a directory of ADL files.", (cmd) => {
return cmd
.positional("path", {
@ -28,6 +27,12 @@ const args = yargs(process.argv.slice(2))
describe:
"The output path for generated artifacts. If it does not exist, it will be created.",
})
.option("option", {
type: "array",
string: true,
describe:
"Key/value pairs that can be passed to ADL components. The format is 'key=value'. This parameter can be used multiple times to add more options.",
})
.option("nostdlib", {
type: "boolean",
default: false,
@ -57,6 +62,12 @@ const args = yargs(process.argv.slice(2))
default: "./adl-output",
describe:
"The output path for generated artifacts. If it does not exist, it will be created.",
})
.option("option", {
type: "array",
string: true,
describe:
"Key/value pairs that can be passed to ADL components. The format is 'key=value'. This parameter can be used multiple times to add more options.",
});
}
)
@ -115,9 +126,19 @@ async function getCompilerOptions(): Promise<CompilerOptions> {
const outputPath = resolve(args["output-path"]);
await mkdirp(outputPath);
const miscOptions: any = {};
for (const option of args.option || []) {
const optionParts = option.split("=");
if (optionParts.length != 2) {
throw new Error(
`The --option parameter value "${option}" must be in the format: some-option=value`
);
}
miscOptions[optionParts[0]] = optionParts[1];
}
return {
outputPath,
rawParameters: args,
miscOptions,
swaggerOutputFile: resolve(args["output-path"], "openapi.json"),
nostdlib: args["nostdlib"],
};

Просмотреть файл

@ -1,5 +1,5 @@
export interface CompilerOptions {
rawParameters?: any;
miscOptions?: any;
mainFile?: string;
outputPath?: string;
swaggerOutputFile?: string;

Просмотреть файл

@ -30,6 +30,7 @@ export interface Program {
checker?: ReturnType<typeof createChecker>;
evalAdlScript(adlScript: string, filePath?: string): void;
onBuild(cb: (program: Program) => void): void;
getOption(key: string): string | undefined;
executeModelDecorators(type: ModelType): void;
executeDecorators(type: Type): void;
executeDecorator(node: DecoratorExpressionNode, program: Program, type: Type): void;
@ -52,6 +53,7 @@ export async function createProgram(
executeModelDecorators,
executeDecorators,
executeDecorator,
getOption,
onBuild(cb) {
buildCbs.push(cb);
},
@ -261,6 +263,10 @@ export async function createProgram(
await loadAdlFile(mainPath);
}
}
function getOption(key: string): string | undefined {
return (options.miscOptions || {})[key];
}
}
export async function compile(rootDir: string, host: CompilerHost, options?: CompilerOptions) {