Skip to content

Commit 0561601

Browse files
authored
Merge pull request #903 from acacode/consola
Consola
2 parents 7e0cd20 + 6f477a4 commit 0561601

24 files changed

+259
-524
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@
4747
},
4848
"dependencies": {
4949
"@types/swagger-schema-official": "^2.0.25",
50+
"consola": "3.2.3",
5051
"cosmiconfig": "^9.0.0",
5152
"didyoumean": "^1.2.2",
5253
"eta": "^2.2.0",
5354
"js-yaml": "^4.1.0",
5455
"lodash": "^4.17.21",
5556
"nanoid": "^3.3.7",
56-
"node-emoji": "^2.1.3",
5757
"prettier": "~3.3.3",
5858
"swagger-schema-official": "2.0.0-bab6bed",
5959
"swagger2openapi": "^7.0.8",

src/code-gen-process.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { consola } from "consola";
12
import lodash from "lodash";
23
import * as typescript from "typescript";
34
import { CodeFormatter } from "./code-formatter.js";
@@ -12,7 +13,6 @@ import { JavascriptTranslator } from "./translators/javascript.js";
1213
import { TypeNameFormatter } from "./type-name-formatter.js";
1314
import { FileSystem } from "./util/file-system.js";
1415
import { internalCase } from "./util/internal-case.js";
15-
import { Logger } from "./util/logger.js";
1616
import { NameResolver } from "./util/name-resolver.js";
1717
import { pascalCase } from "./util/pascal-case.js";
1818
import { sortByProperty } from "./util/sort-by-property.js";
@@ -36,8 +36,6 @@ class CodeGenProcess {
3636
swaggerSchemaResolver;
3737
/** @type {SchemaComponentsMap} */
3838
schemaComponentsMap;
39-
/** @type {Logger} */
40-
logger;
4139
/** @type {TypeNameFormatter} */
4240
typeNameFormatter;
4341
/** @type {SchemaParserFabric} */
@@ -61,7 +59,6 @@ class CodeGenProcess {
6159
*/
6260
constructor(config) {
6361
this.config = new CodeGenConfig(config);
64-
this.logger = new Logger(this);
6562
this.fileSystem = new FileSystem(this);
6663
this.schemaWalker = new SchemaWalker(this);
6764
this.swaggerSchemaResolver = new SwaggerSchemaResolver(this);
@@ -72,7 +69,6 @@ class CodeGenProcess {
7269
this.schemaParserFabric = new SchemaParserFabric(this);
7370
this.schemaRoutes = new SchemaRoutes(this);
7471
this.javascriptTranslator = new JavascriptTranslator(this);
75-
this.config.componentTypeNameResolver.logger = this.logger;
7672
}
7773

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

98-
this.logger.event("start generating your typescript api");
94+
consola.info("start generating your typescript api");
9995

10096
this.config.update(
10197
this.config.hooks.onInit(this.config, this) || this.config,
@@ -160,11 +156,11 @@ class CodeGenProcess {
160156

161157
if (this.fileSystem.pathIsExist(this.config.output)) {
162158
if (this.config.cleanOutput) {
163-
this.logger.debug(`cleaning dir ${this.config.output}`);
159+
consola.debug("cleaning dir", this.config.output);
164160
this.fileSystem.cleanDir(this.config.output);
165161
}
166162
} else {
167-
this.logger.debug(
163+
consola.debug(
168164
`path ${this.config.output} is not exist. creating dir by this path`,
169165
);
170166
this.fileSystem.createDir(this.config.output);
@@ -185,7 +181,7 @@ class CodeGenProcess {
185181
withPrefix: true,
186182
});
187183

188-
this.logger.success(
184+
consola.success(
189185
"api file",
190186
`"${file.fileName}${file.fileExtension}"`,
191187
`created in ${this.config.output}`,
@@ -510,7 +506,7 @@ class CodeGenProcess {
510506
const fileExtension = typescript.Extension.Ts;
511507

512508
if (configuration.translateToJavaScript) {
513-
this.logger.debug("using js translator for", fileName);
509+
consola.debug("using js translator for", fileName);
514510
return await this.javascriptTranslator.translate({
515511
fileName: fileName,
516512
fileExtension: fileExtension,
@@ -519,15 +515,15 @@ class CodeGenProcess {
519515
}
520516

521517
if (configuration.customTranslator) {
522-
this.logger.debug("using custom translator for", fileName);
518+
consola.debug("using custom translator for", fileName);
523519
return await configuration.customTranslator.translate({
524520
fileName: fileName,
525521
fileExtension: fileExtension,
526522
fileContent: content,
527523
});
528524
}
529525

530-
this.logger.debug("generating output for", `${fileName}${fileExtension}`);
526+
consola.debug("generating output for", `${fileName}${fileExtension}`);
531527

532528
return [
533529
{

src/commands/generate-templates/templates-gen-process.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from "node:path";
22
import url from "node:url";
3+
import { consola } from "consola";
34
import { FileSystem } from "../../util/file-system.js";
4-
import { Logger } from "../../util/logger.js";
55
import { TemplatesGenConfig } from "./configuration.js";
66

77
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
@@ -15,10 +15,6 @@ class TemplatesGenProcess {
1515
* @type {FileSystem}
1616
*/
1717
fileSystem;
18-
/**
19-
* @type {Logger}
20-
*/
21-
logger;
2218

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

@@ -33,22 +29,19 @@ class TemplatesGenProcess {
3329

3430
constructor(config) {
3531
this.config = new TemplatesGenConfig(config);
36-
this.logger = new Logger(this);
3732
this.fileSystem = new FileSystem(this);
3833
}
3934

4035
/**
4136
* @return {Promise<GenerateTemplatesOutput>}
4237
*/
4338
async start() {
44-
this.logger.event(
45-
'start generating source templates ".ejs" for code generator',
46-
);
39+
consola.info('start generating source templates ".ejs" for code generator');
4740

4841
const templates = this.getTemplates();
4942

5043
if (this.config.output) {
51-
this.logger.log("preparing output directory for source templates");
44+
consola.info("preparing output directory for source templates");
5245
const outputPath = path.resolve(process.cwd(), this.config.output);
5346

5447
if (this.fileSystem.pathIsExist(outputPath)) {
@@ -96,7 +89,7 @@ class TemplatesGenProcess {
9689
}
9790
}
9891

99-
this.logger.success(
92+
consola.success(
10093
`source templates has been successfully created in "${outputPath}"`,
10194
);
10295
}

src/component-type-name-resolver.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { consola } from "consola";
12
import { NameResolver } from "./util/name-resolver.js";
23
import { getRandomInt } from "./util/random.js";
34

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

910
/**
1011
* @param {CodeGenConfig} config;
11-
* @param {Logger} logger;
1212
* @param {string[]} reservedNames
1313
*/
14-
constructor(config, logger, reservedNames) {
15-
super(config, logger, reservedNames, (variants) => {
14+
constructor(config, reservedNames) {
15+
super(config, reservedNames, (variants) => {
1616
const randomVariant = variants[getRandomInt(0, variants.length - 1)];
1717
if (randomVariant) {
1818
if (!this.countersByVariant.has(randomVariant)) {
@@ -21,7 +21,7 @@ class ComponentTypeNameResolver extends NameResolver {
2121
const variantCounter = this.countersByVariant.get(randomVariant) + 1;
2222
this.countersByVariant.set(randomVariant, variantCounter);
2323
const dirtyResolvedName = `${randomVariant}${variantCounter}`;
24-
this.logger.debug(
24+
consola.debug(
2525
"generated dirty resolved type name for component - ",
2626
dirtyResolvedName,
2727
);
@@ -30,7 +30,7 @@ class ComponentTypeNameResolver extends NameResolver {
3030

3131
const fallbackName = `${this.config.componentTypeNameResolver}${this
3232
.fallbackNameCounter++}`;
33-
this.logger.debug(
33+
consola.debug(
3434
"generated fallback type name for component - ",
3535
fallbackName,
3636
);

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/usr/bin/env node
22

3+
import { consola } from "consola";
34
import { CodeGenProcess } from "./code-gen-process.js";
45
import { generateTemplates } from "./commands/generate-templates/index.js";
56
import * as constants from "./constants.js";
67

78
async function generateApi({ name, prettier, ...config }) {
9+
if (config.debug) {
10+
consola.level = Number.MAX_SAFE_INTEGER;
11+
}
812
const codeGenProcess = new CodeGenProcess({
913
...config,
1014
fileName: name,

src/schema-parser/base-schema-parsers/enum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class EnumSchemaParser extends MonoSchemaParser {
99

1010
constructor(...args) {
1111
super(...args);
12-
this.enumKeyResolver = new EnumKeyResolver(this.config, this.logger, []);
12+
this.enumKeyResolver = new EnumKeyResolver(this.config, []);
1313
}
1414

1515
extractEnum = (pathTypeName) => {

src/schema-parser/mono-schema-parser.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ class MonoSchemaParser {
33
typeName;
44
schemaPath;
55

6-
/** @type {Logger} */
7-
logger;
86
/** @type {SchemaParser} */
97
schemaParser;
108
/** @type {SchemaParserFabric} */
@@ -23,7 +21,6 @@ class MonoSchemaParser {
2321
constructor(schemaParser, schema, typeName = null, schemaPath = []) {
2422
this.schemaParser = schemaParser;
2523
this.schemaParserFabric = schemaParser.schemaParserFabric;
26-
this.logger = schemaParser.logger;
2724
this.schema = schema;
2825
this.typeName = typeName;
2926
this.typeNameFormatter = schemaParser.typeNameFormatter;

src/schema-parser/schema-formatters.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { SCHEMA_TYPES } from "../constants.js";
44
class SchemaFormatters {
55
/** @type {CodeGenConfig} */
66
config;
7-
/** @type {Logger} */
8-
logger;
97
/** @type {TemplatesWorker} */
108
templatesWorker;
119
/** @type {SchemaUtils} */
@@ -16,7 +14,6 @@ class SchemaFormatters {
1614
*/
1715
constructor(schemaParser) {
1816
this.config = schemaParser.config;
19-
this.logger = schemaParser.logger;
2017
this.schemaUtils = schemaParser.schemaUtils;
2118
this.templatesWorker = schemaParser.templatesWorker;
2219
}

src/schema-parser/schema-parser-fabric.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { SchemaUtils } from "./schema-utils.js";
55
class SchemaParserFabric {
66
/** @type {CodeGenConfig} */
77
config;
8-
/** @type {Logger} */
9-
logger;
108
/** @type {SchemaComponentsMap} */
119
schemaComponentsMap;
1210
/** @type {TypeNameFormatter} */
@@ -22,14 +20,12 @@ class SchemaParserFabric {
2220

2321
constructor({
2422
config,
25-
logger,
2623
templatesWorker,
2724
schemaComponentsMap,
2825
typeNameFormatter,
2926
schemaWalker,
3027
}) {
3128
this.config = config;
32-
this.logger = logger;
3329
this.schemaComponentsMap = schemaComponentsMap;
3430
this.typeNameFormatter = typeNameFormatter;
3531
this.templatesWorker = templatesWorker;

src/schema-parser/schema-parser.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { consola } from "consola";
12
import lodash from "lodash";
23
import { SCHEMA_TYPES } from "../constants.js";
34
import { sortByProperty } from "../util/sort-by-property.js";
@@ -19,8 +20,6 @@ class SchemaParser {
1920
schemaParserFabric;
2021
/** @type {CodeGenConfig} */
2122
config;
22-
/** @type {Logger} */
23-
logger;
2423
/** @type {SchemaComponentsMap} */
2524
schemaComponentsMap;
2625
/** @type {TypeNameFormatter} */
@@ -41,7 +40,6 @@ class SchemaParser {
4140
constructor(schemaParserFabric, { typeName, schema, schemaPath } = {}) {
4241
this.schemaParserFabric = schemaParserFabric;
4342
this.config = schemaParserFabric.config;
44-
this.logger = schemaParserFabric.logger;
4543
this.templatesWorker = schemaParserFabric.templatesWorker;
4644
this.schemaComponentsMap = schemaParserFabric.schemaComponentsMap;
4745
this.typeNameFormatter = schemaParserFabric.typeNameFormatter;
@@ -206,7 +204,7 @@ class SchemaParser {
206204
this.schema.enum.length === 1 &&
207205
this.schema.enum[0] == null
208206
) {
209-
this.logger.debug("invalid enum schema", this.schema);
207+
consola.debug("invalid enum schema", this.schema);
210208
this.schema = { type: this.config.Ts.Keyword.Null };
211209
}
212210
// schema is response schema

src/schema-parser/util/enum-key-resolver.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
import { consola } from "consola";
12
import { NameResolver } from "../../util/name-resolver.js";
23

34
class EnumKeyResolver extends NameResolver {
45
counter = 1;
56
/**
67
* @param {CodeGenConfig} config;
7-
* @param {Logger} logger;
88
* @param {string[]} reservedNames
99
*/
10-
constructor(config, logger, reservedNames) {
11-
super(config, logger, reservedNames, (variants) => {
10+
constructor(config, reservedNames) {
11+
super(config, reservedNames, (variants) => {
1212
const generatedVariant =
1313
(variants[0] && `${variants[0]}${this.counter++}`) ||
1414
`${this.config.enumKeyResolverName}${this.counter++}`;
15-
this.logger.debug(
15+
consola.debug(
1616
"generated fallback type name for enum key - ",
1717
generatedVariant,
1818
);

0 commit comments

Comments
 (0)