-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
139 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, test, expect } from '@jest/globals'; | ||
import path from 'node:path'; | ||
import { generateOptionsAndSwagger } from '../src/core'; | ||
|
||
const cwd = process.cwd(); | ||
const url = path.resolve(cwd, './__tests__/input/swagger.json'); | ||
|
||
describe('core_test', () => { | ||
test('generate_swagger_options', async () => { | ||
const result = await generateOptionsAndSwagger({ | ||
url, | ||
}); | ||
expect(result).toBeDefined(); | ||
}); | ||
|
||
test('generate_swagger_rcconfig', async () => { | ||
const result = await generateOptionsAndSwagger({ | ||
url, | ||
useRcConfig: true, | ||
}); | ||
expect(result).toBeDefined(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,83 +1,9 @@ | ||
#!/usr/bin/env node | ||
import { program } from 'commander'; | ||
import { version } from '../package.json'; | ||
import { toTs, toMd, toMock, mockServer, toRequest } from '../src'; | ||
import { createProgram } from './command'; | ||
|
||
process.on('unhandledRejection', (err) => { | ||
throw err; | ||
}); | ||
|
||
program.version(version).usage('<command> [options]'); | ||
|
||
program.arguments('<command>').action(() => { | ||
program.outputHelp(); | ||
}); | ||
|
||
program | ||
.command('to-ts') | ||
.description('Convert swagger to typescript declaration') | ||
.option('-u, --url <url>', 'Swagger link to generate, support relative path or remote url') | ||
.option('-o --output <output>', 'Specify output file path', './swagger.types.ts') | ||
.option( | ||
'--no-autoRequired', | ||
'Do not generate the property as required automatically when there is no required array in definitions' | ||
) | ||
.action(async (options) => { | ||
await toTs(options); | ||
}); | ||
|
||
program | ||
.command('to-request') | ||
.description( | ||
'Convert swagger to request function, generate <output> file and <output>.types file' | ||
) | ||
.option('-u, --url <url>', 'Swagger link to generate, support relative path or remote url') | ||
.option('-o --output <output>', 'Specify output file path', './swagger.request.ts') | ||
.option( | ||
'--no-autoRequired', | ||
'Do not generate the property as required automatically when there is no required array in definitions' | ||
) | ||
.action(async (options) => { | ||
await toRequest(options); | ||
}); | ||
|
||
program | ||
.command('to-md') | ||
.description('Convert swagger to markdown docs') | ||
.option('-u, --url <url>', 'Swagger link to generate, support relative path or remote url') | ||
.option('-o --output <output>', 'Specify output file path', './swagger.docs.md') | ||
.option( | ||
'--no-autoMock', | ||
'Do not generate the mock samples automatically when there is no example in schema' | ||
) | ||
.action(async (options) => { | ||
await toMd(options); | ||
}); | ||
|
||
program | ||
.command('to-mock') | ||
.description('Convert swagger to mock json') | ||
.option('-u, --url <url>', 'Swagger link to generate, support relative path or remote url') | ||
.option('-o --output <output>', 'Specify output file path', './swagger.mock.json') | ||
.option( | ||
'--no-autoMock', | ||
'Do not generate the mock response automatically when there is no example in schema' | ||
) | ||
.action(async (options) => { | ||
await toMock(options); | ||
}); | ||
|
||
program | ||
.command('mock-server') | ||
.description('Start a local server to return the mock interface') | ||
.option('-u, --url <url>', 'Swagger link, support relative path or remote url') | ||
.option('-p, --port <port>', 'Mock server port', '3000') | ||
.option( | ||
'--no-autoMock', | ||
'Do not generate the mock response automatically when there is no example in schema' | ||
) | ||
.action(async (options) => { | ||
await mockServer(options); | ||
}); | ||
|
||
const program = createProgram(); | ||
program.parse(process.argv); |
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,85 @@ | ||
import { Command } from 'commander'; | ||
import * as pkg from '../package.json'; | ||
import { toTs, toRequest, toMd, toMock, mockServer } from '../src'; | ||
|
||
const createProgram = () => { | ||
const program = new Command(); | ||
|
||
program.version(pkg.version); | ||
program.usage('<command> [options]'); | ||
|
||
program.arguments('<command>').action(() => { | ||
program.outputHelp(); | ||
}); | ||
|
||
program | ||
.command('to-ts') | ||
.description('Convert swagger to typescript declaration') | ||
.option('-u, --url <url>', 'Swagger link to generate, support relative path or remote url') | ||
.option('-o --output <output>', 'Specify output file path', './swagger.types.ts') | ||
.option( | ||
'--no-autoRequired', | ||
'Do not generate the property as required automatically when there is no required array in definitions' | ||
) | ||
.action(async (options) => { | ||
await toTs(options, true); | ||
}); | ||
|
||
program | ||
.command('to-request') | ||
.description( | ||
'Convert swagger to request function, generate <output> file and <output>.types file' | ||
) | ||
.option('-u, --url <url>', 'Swagger link to generate, support relative path or remote url') | ||
.option('-o --output <output>', 'Specify output file path', './swagger.request.ts') | ||
.option( | ||
'--no-autoRequired', | ||
'Do not generate the property as required automatically when there is no required array in definitions' | ||
) | ||
.action(async (options) => { | ||
await toRequest(options, true); | ||
}); | ||
|
||
program | ||
.command('to-md') | ||
.description('Convert swagger to markdown docs') | ||
.option('-u, --url <url>', 'Swagger link to generate, support relative path or remote url') | ||
.option('-o --output <output>', 'Specify output file path', './swagger.docs.md') | ||
.option( | ||
'--no-autoMock', | ||
'Do not generate the mock samples automatically when there is no example in schema' | ||
) | ||
.action(async (options) => { | ||
await toMd(options, true); | ||
}); | ||
|
||
program | ||
.command('to-mock') | ||
.description('Convert swagger to mock json') | ||
.option('-u, --url <url>', 'Swagger link to generate, support relative path or remote url') | ||
.option('-o --output <output>', 'Specify output file path', './swagger.mock.json') | ||
.option( | ||
'--no-autoMock', | ||
'Do not generate the mock response automatically when there is no example in schema' | ||
) | ||
.action(async (options) => { | ||
await toMock(options, true); | ||
}); | ||
|
||
program | ||
.command('mock-server') | ||
.description('Start a local server to return the mock interface') | ||
.option('-u, --url <url>', 'Swagger link, support relative path or remote url') | ||
.option('-p, --port <port>', 'Mock server port', '3000') | ||
.option( | ||
'--no-autoMock', | ||
'Do not generate the mock response automatically when there is no example in schema' | ||
) | ||
.action(async (options) => { | ||
await mockServer(options, true); | ||
}); | ||
|
||
return program; | ||
}; | ||
|
||
export { createProgram }; |
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
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
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