Skip to content

Commit

Permalink
Merge pull request #788 from acacode/esm
Browse files Browse the repository at this point in the history
ESM
  • Loading branch information
smorimoto authored Jun 24, 2024
2 parents fb60b78 + 9d04dfc commit 57c3a2c
Show file tree
Hide file tree
Showing 99 changed files with 341 additions and 1,240 deletions.
6 changes: 1 addition & 5 deletions cli/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ const skip_command = Symbol("skip");

const reservedOptions = ["version", "help"];

module.exports = {
root_command,
skip_command,
reservedOptions,
};
export { root_command, skip_command, reservedOptions };
12 changes: 5 additions & 7 deletions cli/execute.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require("lodash");
const { root_command, skip_command } = require("./constants");
const { parseArgs } = require("./parse-args");
const didYouMean = require("didyoumean");
import didYouMean from "didyoumean";
import _ from "lodash";
import { root_command, skip_command } from "./constants.js";
import { parseArgs } from "./parse-args.js";

didYouMean.threshold = 0.5;

Expand Down Expand Up @@ -177,6 +177,4 @@ const processArgs = (commands, args) => {
};
};

module.exports = {
execute,
};
export { execute };
13 changes: 7 additions & 6 deletions cli/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
type CliStructOption = {
flags?: string;
description?: string;
default?: unknown;
description?: string;
flags?: string;
internal?: { name?: string; formatter?: (value: any) => any };
required?: boolean;
};

type CliStruct = {
inherited?: string | null;
name?: string;
alias?: string;
version?: string;
commands?: CliStruct[];
description?: string;
inherited?: string | null;
name?: string;
options: CliStructOption[];
commands?: CliStruct[];
version?: string;
};

type ExecuteOptions = {
Expand Down
16 changes: 7 additions & 9 deletions cli/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const _ = require("lodash");
const { reservedOptions, root_command } = require("./constants");
const { processOption } = require("./process-option");
const { execute } = require("./execute");
const { displayHelp } = require("./operations/display-help");
const { displayVersion } = require("./operations/display-version");
import _ from "lodash";
import { reservedOptions, root_command } from "./constants.js";
import { execute } from "./execute.js";
import { displayHelp } from "./operations/display-help.js";
import { displayVersion } from "./operations/display-version.js";
import { processOption } from "./process-option.js";

const cli = (input) => {
const commands = {};
Expand Down Expand Up @@ -91,6 +91,4 @@ const cli = (input) => {
return instance;
};

module.exports = {
cli,
};
export { cli };
8 changes: 3 additions & 5 deletions cli/operations/display-help.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _ = require("lodash");
const { root_command } = require("../constants");
import _ from "lodash";
import { root_command } from "../constants.js";

const generateOptionsOutput = (options) =>
options.reduce(
Expand Down Expand Up @@ -174,6 +174,4 @@ ${command.description}`
${outputTest}`);
};

module.exports = {
displayHelp,
};
export { displayHelp };
2 changes: 1 addition & 1 deletion cli/operations/display-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ const displayVersion = (instance) => {
console.log(instance.input.version);
};

module.exports = { displayVersion };
export { displayVersion };
4 changes: 1 addition & 3 deletions cli/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ const parseArgs = (args, type) => {
}
};

module.exports = {
parseArgs,
};
export { parseArgs };
6 changes: 2 additions & 4 deletions cli/process-option.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _ = require("lodash");
import _ from "lodash";

const optionFormatters = {
number: (str) => +str,
Expand Down Expand Up @@ -72,6 +72,4 @@ const processOption = (option) => {
};
};

module.exports = {
processOption,
};
export { processOption };
27 changes: 11 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
#!/usr/bin/env node

// Copyright (c) 2019-present acacode
// Node module: swagger-typescript-api
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
// Repository https://github.com/acacode/swagger-typescript-api
import { createRequire } from "node:module";
import { resolve } from "node:path";
import { cli } from "./cli/index.js";
import { TemplatesGenConfig } from "./src/commands/generate-templates/configuration.js";
import { CodeGenConfig } from "./src/configuration.js";
import { HTTP_CLIENT } from "./src/constants.js";
import { generateApi, generateTemplates } from "./src/index.js";

const { version, name } = require("./package.json");
const { cli } = require("./cli");
const { generateApi, generateTemplates } = require("./src");
const { HTTP_CLIENT } = require("./src/constants");
const { resolve } = require("node:path");
const { CodeGenConfig } = require("./src/configuration");
const {
TemplatesGenConfig,
} = require("./src/commands/generate-templates/configuration");
const require = createRequire(import.meta.url);
const packageJson = require("./package.json");

const codeGenBaseConfig = new CodeGenConfig({});
const templateGenBaseConfig = new TemplatesGenConfig({});

const program = cli({
name: name,
name: packageJson.name,
alias: "sta",
version: version,
version: packageJson.version,
description:
"Generate api via swagger scheme.\nSupports OA 3.0, 2.0, JSON, yaml.",
options: [
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"contributors": [
"Sora Morimoto <sora@morimoto.io>"
],
"type": "module",
"main": "./src/index.js",
"types": "./index.d.ts",
"bin": {
Expand Down Expand Up @@ -50,6 +51,8 @@
},
"devDependencies": {
"@biomejs/biome": "1.8.2",
"@tsconfig/node18": "18.2.4",
"@tsconfig/strictest": "2.0.5",
"@types/didyoumean": "1.2.2",
"@types/js-yaml": "4.0.9",
"@types/lodash": "4.17.5",
Expand Down
10 changes: 4 additions & 6 deletions src/code-formatter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _ = require("lodash");
const ts = require("typescript");
const prettier = require("prettier");
import _ from "lodash";
import prettier from "prettier";
import ts from "typescript";

class CodeFormatter {
/**
Expand Down Expand Up @@ -111,6 +111,4 @@ class TsLanguageServiceHost {
}
}

module.exports = {
CodeFormatter,
};
export { CodeFormatter };
40 changes: 19 additions & 21 deletions src/code-gen-process.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const { SwaggerSchemaResolver } = require("./swagger-schema-resolver.js");
const { SchemaComponentsMap } = require("./schema-components-map.js");
const { NameResolver } = require("./util/name-resolver");
const { Logger } = require("./util/logger.js");
const { TypeNameFormatter } = require("./type-name-formatter.js");
const _ = require("lodash");
const { SchemaParserFabric } = require("./schema-parser/schema-parser-fabric");
const { SchemaRoutes } = require("./schema-routes/schema-routes.js");
const { CodeGenConfig } = require("./configuration.js");
const { SchemaWalker } = require("./schema-walker");
const { FileSystem } = require("./util/file-system");
const { TemplatesWorker } = require("./templates-worker");
const { JavascriptTranslator } = require("./translators/javascript");
const ts = require("typescript");
const { CodeFormatter } = require("./code-formatter");
const { pascalCase } = require("./util/pascal-case");
const { internalCase } = require("./util/internal-case");
const { sortByProperty } = require("./util/sort-by-property");
import _ from "lodash";
import ts from "typescript";
import { CodeFormatter } from "./code-formatter.js";
import { CodeGenConfig } from "./configuration.js";
import { SchemaComponentsMap } from "./schema-components-map.js";
import { SchemaParserFabric } from "./schema-parser/schema-parser-fabric.js";
import { SchemaRoutes } from "./schema-routes/schema-routes.js";
import { SchemaWalker } from "./schema-walker.js";
import { SwaggerSchemaResolver } from "./swagger-schema-resolver.js";
import { TemplatesWorker } from "./templates-worker.js";
import { JavascriptTranslator } from "./translators/javascript.js";
import { TypeNameFormatter } from "./type-name-formatter.js";
import { FileSystem } from "./util/file-system.js";
import { internalCase } from "./util/internal-case.js";
import { Logger } from "./util/logger.js";
import { NameResolver } from "./util/name-resolver.js";
import { pascalCase } from "./util/pascal-case.js";
import { sortByProperty } from "./util/sort-by-property.js";

const PATCHABLE_INSTANCES = [
"schemaWalker",
Expand Down Expand Up @@ -566,6 +566,4 @@ class CodeGenProcess {
};
}

module.exports = {
CodeGenProcess,
};
export { CodeGenProcess };
8 changes: 3 additions & 5 deletions src/commands/generate-templates/configuration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { objectAssign } = require("../../util/object-assign");
const { HTTP_CLIENT, PROJECT_VERSION } = require("../../constants");
import { HTTP_CLIENT, PROJECT_VERSION } from "../../constants.js";
import { objectAssign } from "../../util/object-assign.js";

/**
* @type {GenerateTemplatesParams}}
Expand Down Expand Up @@ -28,6 +28,4 @@ class TemplatesGenConfig {
};
}

module.exports = {
TemplatesGenConfig,
};
export { TemplatesGenConfig };
18 changes: 6 additions & 12 deletions src/commands/generate-templates/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#!/usr/bin/env node

// Copyright (c) 2019-present acacode
// Node module: swagger-typescript-api
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
// Repository https://github.com/acacode/swagger-typescript-api
import { TemplatesGenProcess } from "./templates-gen-process.js";

const { TemplatesGenProcess } = require("./templates-gen-process");
async function generateTemplates(config) {
const codeGenProcess = new TemplatesGenProcess(config);
return await codeGenProcess.start();
}

module.exports = {
generateTemplates: async (config) => {
const codeGenProcess = new TemplatesGenProcess(config);
return await codeGenProcess.start();
},
};
export { generateTemplates };
15 changes: 8 additions & 7 deletions src/commands/generate-templates/templates-gen-process.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const { TemplatesGenConfig } = require("./configuration");
const { FileSystem } = require("../../util/file-system");
const { Logger } = require("../../util/logger");
const path = require("node:path");
import path from "node:path";
import url from "node:url";
import { FileSystem } from "../../util/file-system.js";
import { Logger } from "../../util/logger.js";
import { TemplatesGenConfig } from "./configuration.js";

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

class TemplatesGenProcess {
/**
Expand Down Expand Up @@ -199,6 +202,4 @@ class TemplatesGenProcess {
};
}

module.exports = {
TemplatesGenProcess,
};
export { TemplatesGenProcess };
8 changes: 3 additions & 5 deletions src/component-type-name-resolver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { getRandomInt } = require("./util/random");
const { NameResolver } = require("./util/name-resolver");
import { NameResolver } from "./util/name-resolver.js";
import { getRandomInt } from "./util/random.js";

class ComponentTypeNameResolver extends NameResolver {
counter = 1;
Expand Down Expand Up @@ -39,6 +39,4 @@ class ComponentTypeNameResolver extends NameResolver {
}
}

module.exports = {
ComponentTypeNameResolver,
};
export { ComponentTypeNameResolver };
17 changes: 7 additions & 10 deletions src/configuration.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable no-unused-vars */
const { objectAssign } = require("./util/object-assign");
const _ = require("lodash");
const CONSTANTS = require("./constants");
const { ComponentTypeNameResolver } = require("./component-type-name-resolver");
const { cosmiconfigSync } = require("cosmiconfig");
const ts = require("typescript");
import { cosmiconfigSync } from "cosmiconfig";
import _ from "lodash";
import ts from "typescript";
import { ComponentTypeNameResolver } from "./component-type-name-resolver.js";
import * as CONSTANTS from "./constants.js";
import { objectAssign } from "./util/object-assign.js";

const TsKeyword = {
Number: "number",
Expand Down Expand Up @@ -443,6 +442,4 @@ const getDefaultPrettierOptions = () => {
return { ...CONSTANTS.PRETTIER_OPTIONS };
};

module.exports = {
CodeGenConfig,
};
export { CodeGenConfig };
Loading

0 comments on commit 57c3a2c

Please sign in to comment.