Skip to content

rewrite to work with config file instead of command line args #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,4 @@ dist
yalc.lock
lib
yarn.lock
test/tmp/example.ts
167 changes: 161 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"prepare": "npm run build && chmod +x ./lib/bin/cli.js",
"format": "prettier --write \"src/**/*.ts\"",
"test:update": "lib/bin/cli.js test/fixtures/petstore.json --file test/fixtures/generated.ts -h",
"test": "jest"
"test": "jest",
"cli": "esr src/bin/cli.ts"
},
"files": [
"lib",
Expand All @@ -34,6 +35,7 @@
"babel-jest": "^26.6.3",
"chalk": "^4.1.0",
"del": "^6.0.0",
"esbuild": "^0.13.10",
"husky": "^4.3.6",
"jest": "^26.6.3",
"msw": "^0.25.0",
Expand All @@ -46,6 +48,7 @@
"dependencies": {
"@apidevtools/swagger-parser": "^10.0.2",
"commander": "^6.2.0",
"esbuild-runner": "^2.2.1",
"glob-to-regexp": "^0.4.1",
"oazapfts": "3.4.0",
"prettier": "^2.2.1",
Expand Down
97 changes: 29 additions & 68 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,49 @@
#!/usr/bin/env node

import * as path from 'path';
import * as fs from 'fs';
import program from 'commander';
import { dirname, resolve } from 'path';

// tslint:disable-next-line
const meta = require('../../package.json');
import { generateApi } from '../generate';
import { GenerationOptions } from '../types';
import { isValidUrl, MESSAGES, prettify } from '../utils';
import { getCompilerOptions } from '../utils/getTsConfig';
import { generateEndpoints } from '../';
import { CommonOptions, ConfigFile, OutputFileOptions } from '../types';

program
.version(meta.version)
.usage('</path/to/some-swagger.yaml>')
.option('--exportName <name>', 'change RTK Query Tree root name')
.option('--reducerPath <path>', 'pass reducer path')
.option('--baseQuery <name>', 'pass baseQuery name')
.option('--argSuffix <name>', 'pass arg suffix')
.option('--responseSuffix <name>', 'pass response suffix')
.option('--baseUrl <url>', 'pass baseUrl')
.option('--createApiImportPath <path>', 'entry point for createApi import. options: [react]')
.option('-h, --hooks', 'generate React Hooks')
.option('-c, --config <path>', 'pass tsconfig path for resolve path alias')
.option('-f, --file <filename>', 'output file name (ex: generated.api.ts)')
.parse(process.argv);
program.version(meta.version).usage('</path/to/config.js>').parse(process.argv);

if (program.args.length === 0) {
if (program.args.length === 0 || !(program.args[0].endsWith('.js') || program.args[0].endsWith('.json'))) {
program.help();
} else {
const schemaLocation = program.args[0];
run(resolve(process.cwd(), program.args[0]));
}

const schemaAbsPath = isValidUrl(schemaLocation) ? schemaLocation : path.resolve(process.cwd(), schemaLocation);
async function run(configFile: string) {
process.chdir(dirname(configFile));

const options = [
'exportName',
'reducerPath',
'baseQuery',
'argSuffix',
'responseSuffix',
'baseUrl',
'createApiImportPath',
'hooks',
'file',
'config',
] as const;
const fullConfig: ConfigFile = require(configFile);

const outputFile = program['file'];
let tsConfigFilePath = program['config'];
const outFiles: (CommonOptions & OutputFileOptions)[] = [];

if (tsConfigFilePath) {
tsConfigFilePath = path.resolve(tsConfigFilePath);
if (!fs.existsSync(tsConfigFilePath)) {
throw Error(MESSAGES.TSCONFIG_FILE_NOT_FOUND);
if ('outputFiles' in fullConfig) {
const { outputFiles, ...commonConfig } = fullConfig;
for (const [outputFile, specificConfig] of Object.entries(outputFiles)) {
outFiles.push({
...commonConfig,
...specificConfig,
outputFile,
});
}
} else {
outFiles.push(fullConfig);
}

const compilerOptions = getCompilerOptions(tsConfigFilePath);

const generateApiOptions = {
...options.reduce(
(s, key) =>
program[key]
? {
...s,
[key]: program[key],
}
: s,
{} as GenerationOptions
),
outputFile,
compilerOptions,
};
generateApi(schemaAbsPath, generateApiOptions)
.then(async (sourceCode) => {
const outputFile = program['file'];
if (outputFile) {
fs.writeFileSync(path.resolve(process.cwd(), outputFile), await prettify(outputFile, sourceCode));
} else {
console.log(await prettify(null, sourceCode));
}
})
.catch((err) => {
for (const config of outFiles) {
try {
console.log(`Generating ${config.outputFile}`);
await generateEndpoints(config);
console.log(`Done`);
} catch (err) {
console.error(err);
process.exit(1);
});
}
}
}
Loading