Skip to content

Commit

Permalink
Merge branch 'acacode:main' into patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Untaek authored Dec 1, 2024
2 parents 9fdcd04 + 28bb7fe commit a479b2d
Show file tree
Hide file tree
Showing 65 changed files with 1,469 additions and 1,984 deletions.
7 changes: 7 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
# shellcheck shell=bash
export NODE_VERSIONS="${HOME}/.nvm/versions/node"
export NODE_VERSION_PREFIX="v"
use node
layout node
corepack enable
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jobs:
- name: Set-up Node.js
uses: actions/setup-node@v4
with:
node-version: 22
check-latest: true
node-version-file: .nvmrc

- run: corepack enable

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ jobs:
- name: Set-up Node.js
uses: actions/setup-node@v4
with:
node-version: 22
check-latest: true
node-version-file: .nvmrc

- run: corepack enable

Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ Options:
--js generate js api module with declaration file (default: false)
--module-name-index <number> determines which path index should be used for routes separation (example: GET:/fruits/getFruit -> index:0 -> moduleName -> fruits) (default: 0)
--module-name-first-tag splits routes based on the first tag (default: false)
--disableStrictSSL disabled strict SSL (default: false)
--disableProxy disabled proxy (default: false)
--axios generate axios http client (default: false)
--unwrap-response-data unwrap the data item from the response (default: false)
--disable-throw-on-error Do not throw an error when response.ok is not true (default: false)
Expand Down Expand Up @@ -403,7 +401,7 @@ type PrimitiveTypeStructValue =
| string
| ((
schema: Record<string, any>,
parser: import("./src/schema-parser/schema-parser").SchemaParser,
parser: import("./src/schema-parser/schema-parser").SchemaParser
) => string);

type PrimitiveTypeStruct = Record<
Expand All @@ -416,7 +414,7 @@ type PrimitiveTypeStruct = Record<
>;

declare const primitiveTypeConstructs: (
struct: PrimitiveTypeStruct,
struct: PrimitiveTypeStruct
) => Partial<PrimitiveTypeStruct>;

generateApi({
Expand Down
46 changes: 20 additions & 26 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from "node:path";
import * as url from "node:url";
import { defineCommand, runMain } from "citty";
import { consola } from "consola";
import packageJson from "./package.json" with { type: "json" };
Expand Down Expand Up @@ -53,6 +54,9 @@ const generateTemplatesCommand = defineCommand({
},
},
run: async ({ args }) => {
if (args.debug) consola.level = Number.MAX_SAFE_INTEGER;
if (args.silent) consola.level = 0;

await generateTemplates({
cleanOutput: args["clean-output"],
httpClientType: args["http-client"],
Expand Down Expand Up @@ -175,27 +179,17 @@ const generateCommand = defineCommand({
type: "string",
description:
"determines which path index should be used for routes separation (example: GET:/fruits/getFruit -> index:0 -> moduleName -> fruits)",
default: codeGenBaseConfig.moduleNameIndex,
default: codeGenBaseConfig.moduleNameIndex.toString(),
},
"module-name-first-tag": {
type: "boolean",
description: "splits routes based on the first tag",
default: codeGenBaseConfig.moduleNameFirstTag,
},
disableStrictSSL: {
type: "boolean",
description: "disabled strict SSL",
default: codeGenBaseConfig.disableStrictSSL,
},
disableProxy: {
type: "boolean",
description: "disabled proxy",
default: codeGenBaseConfig.disableProxy,
},
axios: {
type: "boolean",
description: "generate axios http client",
default: codeGenBaseConfig.httpClientType === HTTP_CLIENT.AXIOS,
default: false,
},
"unwrap-response-data": {
type: "boolean",
Expand Down Expand Up @@ -223,12 +217,12 @@ const generateCommand = defineCommand({
default: codeGenBaseConfig.defaultResponseType,
},
"type-prefix": {
type: "boolean",
type: "string",
description: "data contract name prefix",
default: codeGenBaseConfig.typePrefix,
},
"type-suffix": {
type: "boolean",
type: "string",
description: "data contract name suffix",
default: codeGenBaseConfig.typeSuffix,
},
Expand Down Expand Up @@ -277,27 +271,29 @@ const generateCommand = defineCommand({
"custom-config": {
type: "string",
description: "custom config: primitiveTypeConstructs, hooks, ... ",
default: "",
},
},
run: async ({ args }) => {
let customConfig = null;
let customConfigPath: string | undefined;
if (args.debug) consola.level = Number.MAX_SAFE_INTEGER;
if (args.silent) consola.level = 0;

let customConfig;

if (args["custom-config"]) {
try {
customConfigPath = path.resolve(process.cwd(), args["custom-config"]);
const customConfigPath = url
.pathToFileURL(path.resolve(process.cwd(), args["custom-config"]))
.toString();
customConfig = await import(customConfigPath);
customConfig = customConfig.default || customConfig;
if (customConfig) {
consola.info(`Found custom config at: ${customConfigPath}`);
}
} catch (error) {
consola.error("Error loading custom config:", error);
}
}

if (customConfig) {
consola.info(`Found custom config at: ${customConfigPath}`);
}

await generateApi({
addReadonly: args["add-readonly"],
anotherArrayType: args["another-array-type"],
Expand All @@ -306,8 +302,6 @@ const generateCommand = defineCommand({
debug: args.debug,
defaultResponseAsSuccess: args["default-as-success"],
defaultResponseType: args["default-response"],
disableProxy: args.disableProxy,
disableStrictSSL: args.disableStrictSSL,
disableThrowOnError: args["disable-throw-on-error"],
enumNamesAsValues: args["enum-names-as-values"],
extractEnums: args["extract-enums"],
Expand All @@ -325,11 +319,11 @@ const generateCommand = defineCommand({
args["http-client"] || args.axios
? HTTP_CLIENT.AXIOS
: HTTP_CLIENT.FETCH,
input: path.resolve(process.cwd(), args.path),
input: path.resolve(process.cwd(), args.path as string),
modular: args.modular,
moduleNameFirstTag: args["module-name-first-tag"],
moduleNameIndex: +args["module-name-index"] || 0,
output: path.resolve(process.cwd(), args.output || "."),
output: path.resolve(process.cwd(), (args.output as string) || "."),
patch: args.patch,
silent: args.silent,
singleHttpClient: args["single-http-client"],
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,26 @@
"eta": "^2.2.0",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"nanoid": "^3.3.7",
"prettier": "~3.3.3",
"nanoid": "^5.0.9",
"prettier": "~3.4.1",
"swagger-schema-official": "2.0.0-bab6bed",
"swagger2openapi": "^7.0.8",
"typescript": "~5.6.2"
"typescript": "~5.7.2"
},
"devDependencies": {
"@biomejs/biome": "1.9.2",
"@biomejs/biome": "1.9.4",
"@tsconfig/node18": "18.2.4",
"@tsconfig/strictest": "2.0.5",
"@types/js-yaml": "4.0.9",
"@types/lodash": "4.17.7",
"@types/node": "22.5.5",
"@types/lodash": "4.17.13",
"@types/node": "22.10.1",
"@types/swagger2openapi": "7.0.4",
"axios": "1.7.7",
"tsup": "8.3.0",
"vitest": "2.1.1"
"axios": "1.7.8",
"openapi-types": "12.1.3",
"tsup": "8.3.5",
"vitest": "2.1.6"
},
"packageManager": "[email protected].0",
"packageManager": "[email protected].1",
"engines": {
"node": ">=18.0.0"
},
Expand Down
53 changes: 24 additions & 29 deletions src/code-formatter.js → src/code-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import * as prettier from "prettier";
import * as typescript from "typescript";
import type { CodeGenConfig } from "./configuration.js";

class CodeFormatter {
/**
* @type {CodeGenConfig}
*/
config;
export class CodeFormatter {
config: CodeGenConfig;

constructor({ config }) {
constructor(config: CodeGenConfig) {
this.config = config;
}

removeUnusedImports = (content) => {
removeUnusedImports = (content: string) => {
const tempFileName = "file.ts";

const host = new TsLanguageServiceHost(tempFileName, content);
Expand All @@ -20,6 +18,7 @@ class CodeFormatter {
const fileTextChanges = languageService.organizeImports(
{ type: "file", fileName: tempFileName },
{ newLineCharacter: typescript.sys.newLine },
undefined,
)[0];

if (fileTextChanges?.textChanges.length) {
Expand All @@ -35,11 +34,7 @@ class CodeFormatter {
return content;
};

/**
* @param content
* @returns {Promise<string>}
*/
prettierFormat = async (content) => {
prettierFormat = async (content: string) => {
const formatted = await prettier.format(
content,
this.config.prettierOptions,
Expand All @@ -48,7 +43,7 @@ class CodeFormatter {
};

formatCode = async (
code,
code: string,
{ removeUnusedImports = true, prettierFormat = true } = {},
) => {
if (removeUnusedImports) {
Expand All @@ -62,22 +57,24 @@ class CodeFormatter {
}

class TsLanguageServiceHost {
constructor(fileName, content) {
fileName: string;
content: string;
compilerOptions: typescript.CompilerOptions;

constructor(fileName: string, content: string) {
this.fileName = fileName;
this.content = content;
const tsconfig = typescript.findConfigFile(
fileName,
typescript.sys.fileExists,
);

Object.assign(this, {
fileName,
content,
compilerOptions: tsconfig
? typescript.convertCompilerOptionsFromJson(
typescript.readConfigFile(tsconfig, typescript.sys.readFile).config
.compilerOptions,
).options
: typescript.getDefaultCompilerOptions(),
});
this.compilerOptions = tsconfig
? typescript.convertCompilerOptionsFromJson(
typescript.readConfigFile(tsconfig, typescript.sys.readFile).config
.compilerOptions,
"",
).options
: typescript.getDefaultCompilerOptions();
}

getNewLine() {
Expand All @@ -101,16 +98,14 @@ class TsLanguageServiceHost {
getScriptSnapshot() {
return typescript.ScriptSnapshot.fromString(this.content);
}
readFile(fileName, encoding) {
readFile(fileName: string, encoding: string) {
if (fileName === this.fileName) {
return this.content;
}

return typescript.sys.readFile(fileName, encoding);
}
fileExists(path) {
fileExists(path: string) {
return typescript.sys.fileExists(path);
}
}

export { CodeFormatter };
Loading

0 comments on commit a479b2d

Please sign in to comment.