Skip to content

Commit

Permalink
feat: specify a custom output directory for our generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermendes committed Feb 7, 2025
1 parent d197c62 commit ae59217
Show file tree
Hide file tree
Showing 5 changed files with 1,983 additions and 1,617 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ yarn oac -f spec.json
Where `spec.json` is the location of the OpenAPI specification file from which
you want to generate the client.

### Custom output directory

The process described above will output the generated client files to
`node_modules/.oac`.

Alternatively, you can specify a custom output directory using the `--out`
argument, for example:

```text
yarn oac http://example.api.com/docs.json --out src/generated
```

## Usage

Once the API client has been generated it can be instantiated as follows:
Expand Down
9 changes: 4 additions & 5 deletions bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const { getOapiSpecs } = require('./get-spec');
const pkg = require(`${appRoot.path}/package.json`);

const GENERATED_FILES_DIR = path.join(SRC_DIR, 'generated');
const BUILD_DIR = path.join(appRoot.path, 'node_modules', '.oac');

/**
* Format a JSON Schema title as a TypeScript refererence.
Expand Down Expand Up @@ -360,7 +359,7 @@ const validateOapiSpec = (oapiSpec, operations) => {
/**
* Build an API client based on its OpenAPI spec.
*/
const buildClient = async (oapiSpec) => {
const buildClient = async (oapiSpec, buildDir) => {
const { title } = (oapiSpec || {}).info || {};
const outDir = path.join(GENERATED_FILES_DIR, camelCase(title));
const types = await openapiTS(oapiSpec);
Expand All @@ -378,19 +377,19 @@ const buildClient = async (oapiSpec) => {

const files = glob.sync('**.ts', { cwd: SRC_DIR, absolute: true });

compileTs(files, ModuleKind.CommonJS, BUILD_DIR);
compileTs(files, ModuleKind.CommonJS, buildDir);

console.info(`${chalk.green('✔')} ${oapiSpec.info.title} client generated`);
};

/**
* Generate all the things.
*/
module.exports.build = async () => {
module.exports.build = async (buildDir) => {
const oapiSpecs = await getOapiSpecs();

fse.emptyDirSync(GENERATED_FILES_DIR);
fse.emptyDirSync(BUILD_DIR);
fse.emptyDirSync(buildDir);

await Promise.all([...oapiSpecs.map(buildClient), buildIndexFile(oapiSpecs)]);
};
15 changes: 14 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
#!/usr/bin/env node
const appRoot = require('app-root-path');
const path = require('path');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { build } = require('./build');

const { argv } = yargs(hideBin(process.argv));

const DEFAULT_BUILD_DIR = path.join(appRoot.path, 'node_modules', '.oac');

const outPath = argv.out || DEFAULT_BUILD_DIR;
const fullOutPath = path.isAbsolute(outPath)
? outPath
: path.join(process.cwd(), outPath);

(async () => {
try {
await build();
await build(fullOutPath);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"pascal-case": "^3.1.2",
"qs": "^6.10.3",
"typeconv": "^1.7.0",
"yargs": "^17.6.2"
"yargs": "^17.7.2"
},
"peerDependencies": {
"typescript": "*"
Expand All @@ -67,5 +67,10 @@
"semantic-release": "^24.0.0",
"ts-jest": "^29.1.0",
"typescript": "^5.1.0"
},
"resolutions": {
"strip-ansi": "^6.0.1",
"string-width": "^4.2.0",
"wrap-ansi": "^7.0.0"
}
}
Loading

0 comments on commit ae59217

Please sign in to comment.