Skip to content

Consola #903

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

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
},
"dependencies": {
"@types/swagger-schema-official": "^2.0.25",
"consola": "3.2.3",
"cosmiconfig": "^9.0.0",
"didyoumean": "^1.2.2",
"eta": "^2.2.0",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"nanoid": "^3.3.7",
"node-emoji": "^2.1.3",
"prettier": "~3.3.3",
"swagger-schema-official": "2.0.0-bab6bed",
"swagger2openapi": "^7.0.8",
Expand Down
20 changes: 8 additions & 12 deletions src/code-gen-process.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { consola } from "consola";
import lodash from "lodash";
import * as typescript from "typescript";
import { CodeFormatter } from "./code-formatter.js";
Expand All @@ -12,7 +13,6 @@ 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";
Expand All @@ -36,8 +36,6 @@ class CodeGenProcess {
swaggerSchemaResolver;
/** @type {SchemaComponentsMap} */
schemaComponentsMap;
/** @type {Logger} */
logger;
/** @type {TypeNameFormatter} */
typeNameFormatter;
/** @type {SchemaParserFabric} */
Expand All @@ -61,7 +59,6 @@ class CodeGenProcess {
*/
constructor(config) {
this.config = new CodeGenConfig(config);
this.logger = new Logger(this);
this.fileSystem = new FileSystem(this);
this.schemaWalker = new SchemaWalker(this);
this.swaggerSchemaResolver = new SwaggerSchemaResolver(this);
Expand All @@ -72,7 +69,6 @@ class CodeGenProcess {
this.schemaParserFabric = new SchemaParserFabric(this);
this.schemaRoutes = new SchemaRoutes(this);
this.javascriptTranslator = new JavascriptTranslator(this);
this.config.componentTypeNameResolver.logger = this.logger;
}

async start() {
Expand All @@ -95,7 +91,7 @@ class CodeGenProcess {
this.schemaWalker.addSchema("$usage", swagger.usageSchema);
this.schemaWalker.addSchema("$original", swagger.originalSchema);

this.logger.event("start generating your typescript api");
consola.info("start generating your typescript api");

this.config.update(
this.config.hooks.onInit(this.config, this) || this.config,
Expand Down Expand Up @@ -160,11 +156,11 @@ class CodeGenProcess {

if (this.fileSystem.pathIsExist(this.config.output)) {
if (this.config.cleanOutput) {
this.logger.debug(`cleaning dir ${this.config.output}`);
consola.debug("cleaning dir", this.config.output);
this.fileSystem.cleanDir(this.config.output);
}
} else {
this.logger.debug(
consola.debug(
`path ${this.config.output} is not exist. creating dir by this path`,
);
this.fileSystem.createDir(this.config.output);
Expand All @@ -185,7 +181,7 @@ class CodeGenProcess {
withPrefix: true,
});

this.logger.success(
consola.success(
"api file",
`"${file.fileName}${file.fileExtension}"`,
`created in ${this.config.output}`,
Expand Down Expand Up @@ -510,7 +506,7 @@ class CodeGenProcess {
const fileExtension = typescript.Extension.Ts;

if (configuration.translateToJavaScript) {
this.logger.debug("using js translator for", fileName);
consola.debug("using js translator for", fileName);
return await this.javascriptTranslator.translate({
fileName: fileName,
fileExtension: fileExtension,
Expand All @@ -519,15 +515,15 @@ class CodeGenProcess {
}

if (configuration.customTranslator) {
this.logger.debug("using custom translator for", fileName);
consola.debug("using custom translator for", fileName);
return await configuration.customTranslator.translate({
fileName: fileName,
fileExtension: fileExtension,
fileContent: content,
});
}

this.logger.debug("generating output for", `${fileName}${fileExtension}`);
consola.debug("generating output for", `${fileName}${fileExtension}`);

return [
{
Expand Down
15 changes: 4 additions & 11 deletions src/commands/generate-templates/templates-gen-process.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "node:path";
import url from "node:url";
import { consola } from "consola";
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));
Expand All @@ -15,10 +15,6 @@ class TemplatesGenProcess {
* @type {FileSystem}
*/
fileSystem;
/**
* @type {Logger}
*/
logger;

rootDir = path.resolve(__dirname, "../../../");

Expand All @@ -33,22 +29,19 @@ class TemplatesGenProcess {

constructor(config) {
this.config = new TemplatesGenConfig(config);
this.logger = new Logger(this);
this.fileSystem = new FileSystem(this);
}

/**
* @return {Promise<GenerateTemplatesOutput>}
*/
async start() {
this.logger.event(
'start generating source templates ".ejs" for code generator',
);
consola.info('start generating source templates ".ejs" for code generator');

const templates = this.getTemplates();

if (this.config.output) {
this.logger.log("preparing output directory for source templates");
consola.info("preparing output directory for source templates");
const outputPath = path.resolve(process.cwd(), this.config.output);

if (this.fileSystem.pathIsExist(outputPath)) {
Expand Down Expand Up @@ -96,7 +89,7 @@ class TemplatesGenProcess {
}
}

this.logger.success(
consola.success(
`source templates has been successfully created in "${outputPath}"`,
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/component-type-name-resolver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { consola } from "consola";
import { NameResolver } from "./util/name-resolver.js";
import { getRandomInt } from "./util/random.js";

Expand All @@ -8,11 +9,10 @@ class ComponentTypeNameResolver extends NameResolver {

/**
* @param {CodeGenConfig} config;
* @param {Logger} logger;
* @param {string[]} reservedNames
*/
constructor(config, logger, reservedNames) {
super(config, logger, reservedNames, (variants) => {
constructor(config, reservedNames) {
super(config, reservedNames, (variants) => {
const randomVariant = variants[getRandomInt(0, variants.length - 1)];
if (randomVariant) {
if (!this.countersByVariant.has(randomVariant)) {
Expand All @@ -21,7 +21,7 @@ class ComponentTypeNameResolver extends NameResolver {
const variantCounter = this.countersByVariant.get(randomVariant) + 1;
this.countersByVariant.set(randomVariant, variantCounter);
const dirtyResolvedName = `${randomVariant}${variantCounter}`;
this.logger.debug(
consola.debug(
"generated dirty resolved type name for component - ",
dirtyResolvedName,
);
Expand All @@ -30,7 +30,7 @@ class ComponentTypeNameResolver extends NameResolver {

const fallbackName = `${this.config.componentTypeNameResolver}${this
.fallbackNameCounter++}`;
this.logger.debug(
consola.debug(
"generated fallback type name for component - ",
fallbackName,
);
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/usr/bin/env node

import { consola } from "consola";
import { CodeGenProcess } from "./code-gen-process.js";
import { generateTemplates } from "./commands/generate-templates/index.js";
import * as constants from "./constants.js";

async function generateApi({ name, prettier, ...config }) {
if (config.debug) {
consola.level = Number.MAX_SAFE_INTEGER;
}
const codeGenProcess = new CodeGenProcess({
...config,
fileName: name,
Expand Down
2 changes: 1 addition & 1 deletion src/schema-parser/base-schema-parsers/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class EnumSchemaParser extends MonoSchemaParser {

constructor(...args) {
super(...args);
this.enumKeyResolver = new EnumKeyResolver(this.config, this.logger, []);
this.enumKeyResolver = new EnumKeyResolver(this.config, []);
}

extractEnum = (pathTypeName) => {
Expand Down
3 changes: 0 additions & 3 deletions src/schema-parser/mono-schema-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ class MonoSchemaParser {
typeName;
schemaPath;

/** @type {Logger} */
logger;
/** @type {SchemaParser} */
schemaParser;
/** @type {SchemaParserFabric} */
Expand All @@ -23,7 +21,6 @@ class MonoSchemaParser {
constructor(schemaParser, schema, typeName = null, schemaPath = []) {
this.schemaParser = schemaParser;
this.schemaParserFabric = schemaParser.schemaParserFabric;
this.logger = schemaParser.logger;
this.schema = schema;
this.typeName = typeName;
this.typeNameFormatter = schemaParser.typeNameFormatter;
Expand Down
3 changes: 0 additions & 3 deletions src/schema-parser/schema-formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { SCHEMA_TYPES } from "../constants.js";
class SchemaFormatters {
/** @type {CodeGenConfig} */
config;
/** @type {Logger} */
logger;
/** @type {TemplatesWorker} */
templatesWorker;
/** @type {SchemaUtils} */
Expand All @@ -16,7 +14,6 @@ class SchemaFormatters {
*/
constructor(schemaParser) {
this.config = schemaParser.config;
this.logger = schemaParser.logger;
this.schemaUtils = schemaParser.schemaUtils;
this.templatesWorker = schemaParser.templatesWorker;
}
Expand Down
4 changes: 0 additions & 4 deletions src/schema-parser/schema-parser-fabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { SchemaUtils } from "./schema-utils.js";
class SchemaParserFabric {
/** @type {CodeGenConfig} */
config;
/** @type {Logger} */
logger;
/** @type {SchemaComponentsMap} */
schemaComponentsMap;
/** @type {TypeNameFormatter} */
Expand All @@ -22,14 +20,12 @@ class SchemaParserFabric {

constructor({
config,
logger,
templatesWorker,
schemaComponentsMap,
typeNameFormatter,
schemaWalker,
}) {
this.config = config;
this.logger = logger;
this.schemaComponentsMap = schemaComponentsMap;
this.typeNameFormatter = typeNameFormatter;
this.templatesWorker = templatesWorker;
Expand Down
6 changes: 2 additions & 4 deletions src/schema-parser/schema-parser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { consola } from "consola";
import lodash from "lodash";
import { SCHEMA_TYPES } from "../constants.js";
import { sortByProperty } from "../util/sort-by-property.js";
Expand All @@ -19,8 +20,6 @@ class SchemaParser {
schemaParserFabric;
/** @type {CodeGenConfig} */
config;
/** @type {Logger} */
logger;
/** @type {SchemaComponentsMap} */
schemaComponentsMap;
/** @type {TypeNameFormatter} */
Expand All @@ -41,7 +40,6 @@ class SchemaParser {
constructor(schemaParserFabric, { typeName, schema, schemaPath } = {}) {
this.schemaParserFabric = schemaParserFabric;
this.config = schemaParserFabric.config;
this.logger = schemaParserFabric.logger;
this.templatesWorker = schemaParserFabric.templatesWorker;
this.schemaComponentsMap = schemaParserFabric.schemaComponentsMap;
this.typeNameFormatter = schemaParserFabric.typeNameFormatter;
Expand Down Expand Up @@ -206,7 +204,7 @@ class SchemaParser {
this.schema.enum.length === 1 &&
this.schema.enum[0] == null
) {
this.logger.debug("invalid enum schema", this.schema);
consola.debug("invalid enum schema", this.schema);
this.schema = { type: this.config.Ts.Keyword.Null };
}
// schema is response schema
Expand Down
8 changes: 4 additions & 4 deletions src/schema-parser/util/enum-key-resolver.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { consola } from "consola";
import { NameResolver } from "../../util/name-resolver.js";

class EnumKeyResolver extends NameResolver {
counter = 1;
/**
* @param {CodeGenConfig} config;
* @param {Logger} logger;
* @param {string[]} reservedNames
*/
constructor(config, logger, reservedNames) {
super(config, logger, reservedNames, (variants) => {
constructor(config, reservedNames) {
super(config, reservedNames, (variants) => {
const generatedVariant =
(variants[0] && `${variants[0]}${this.counter++}`) ||
`${this.config.enumKeyResolverName}${this.counter++}`;
this.logger.debug(
consola.debug(
"generated fallback type name for enum key - ",
generatedVariant,
);
Expand Down
Loading