dorval
Version:
CLI tool for generating Dart/Flutter API clients from OpenAPI specifications
183 lines (176 loc) • 6.82 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
generateCommand: () => generateCommand,
loadConfig: () => loadConfig,
runCLI: () => runCLI,
watchCommand: () => watchCommand
});
module.exports = __toCommonJS(index_exports);
// src/cli.ts
var import_commander = require("commander");
// src/commands/generate.ts
var import_chalk = __toESM(require("chalk"));
var import_ora = __toESM(require("ora"));
var import_core = require("@dorval/core");
// src/config.ts
var import_cosmiconfig = require("cosmiconfig");
function getExplorer() {
return (0, import_cosmiconfig.cosmiconfigSync)("orval", {
searchPlaces: [
"orval.config.ts",
"orval.config.js",
"orval.config.cjs",
".orvalrc",
".orvalrc.json",
".orvalrc.yaml",
".orvalrc.yml",
"package.json"
]
});
}
async function loadConfig(configPath) {
const explorer = getExplorer();
const result = configPath ? explorer.load(configPath) : explorer.search();
if (!result) {
throw new Error("No configuration file found");
}
const config = result.config.default || result.config;
if (typeof config === "object" && !config.input) {
const firstKey = Object.keys(config)[0];
return config[firstKey];
}
return config;
}
// src/commands/generate.ts
async function generateCommand(options) {
const spinner = (0, import_ora.default)("Loading configuration...").start();
try {
let config;
if (options.input && options.output) {
config = {
input: options.input,
output: {
target: options.output,
client: options.client || "dio",
mode: "split",
override: {
generator: {
freezed: true,
jsonSerializable: true,
nullSafety: true
}
}
}
};
} else if (options.config) {
config = await loadConfig(options.config);
} else {
try {
config = await loadConfig();
} catch (error) {
throw new Error("Either provide a config file or use -i and -o options");
}
}
spinner.text = "Parsing OpenAPI specification...";
const files = await (0, import_core.generateDartCode)(config);
spinner.succeed(import_chalk.default.green(`\u2705 Generated ${files.length} files`));
console.log(import_chalk.default.cyan("\nGenerated files:"));
files.forEach((file) => {
console.log(import_chalk.default.gray(` - ${file.path}`));
});
if (config.hooks?.afterAllFilesWrite) {
spinner.start("Running post-generation hooks...");
spinner.succeed("Hooks completed");
}
console.log(import_chalk.default.green("\n\u2728 Generation completed successfully!"));
} catch (error) {
spinner.fail(import_chalk.default.red("Generation failed"));
console.error(import_chalk.default.red(`
Error: ${error instanceof Error ? error.message : String(error)}`));
process.exit(1);
}
}
// src/commands/watch.ts
var import_chalk2 = __toESM(require("chalk"));
var import_ora2 = __toESM(require("ora"));
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var import_core2 = require("@dorval/core");
async function watchCommand(options) {
const spinner = (0, import_ora2.default)("Starting watch mode...").start();
try {
const config = await loadConfig(options.config);
if (typeof config.input !== "string") {
throw new Error("Watch mode requires a file path input");
}
const inputPath = path.resolve(config.input);
spinner.succeed(import_chalk2.default.green(`Watching ${inputPath} for changes...`));
await (0, import_core2.generateDartCode)(config);
console.log(import_chalk2.default.cyan("\u2705 Initial generation completed"));
fs.watchFile(inputPath, async () => {
console.log(import_chalk2.default.yellow("\n\u{1F4DD} File changed, regenerating..."));
try {
await (0, import_core2.generateDartCode)(config);
console.log(import_chalk2.default.green("\u2705 Regeneration completed"));
} catch (error) {
console.error(import_chalk2.default.red(`\u274C Regeneration failed: ${error instanceof Error ? error.message : String(error)}`));
}
});
process.stdin.resume();
process.on("SIGINT", () => {
console.log(import_chalk2.default.yellow("\n\u{1F44B} Stopping watch mode..."));
fs.unwatchFile(inputPath);
process.exit(0);
});
} catch (error) {
spinner.fail(import_chalk2.default.red("Watch mode failed"));
console.error(import_chalk2.default.red(`
Error: ${error instanceof Error ? error.message : String(error)}`));
process.exit(1);
}
}
// src/cli.ts
function runCLI(args) {
const program = new import_commander.Command();
program.name("dorval").description("Generate Dart API clients from OpenAPI specifications");
program.command("generate").description("Generate Dart code").option("-c, --config <path>", "Config file path").option("-i, --input <path>", "Input spec").option("-o, --output <path>", "Output directory").action(generateCommand);
program.command("watch").description("Watch for changes").option("-c, --config <path>", "Config file path").action(watchCommand);
program.parse(args);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
generateCommand,
loadConfig,
runCLI,
watchCommand
});
//# sourceMappingURL=index.js.map