Skip to content
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
6 changes: 3 additions & 3 deletions cli/execute.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import didYouMean from "didyoumean";
import _ from "lodash";
import * as lodash from "lodash";
import { root_command, skip_command } from "./constants.js";
import { parseArgs } from "./parse-args.js";

Expand Down Expand Up @@ -98,14 +98,14 @@ const processArgs = (commands, args) => {

let allFlagKeys = [];

_.forEach(args, (arg, i) => {
lodash.forEach(args, (arg, i) => {
if (error) return;

if (i === 0) {
command = commands[arg];

if (!command && !arg.startsWith("-")) {
const tip = didYouMean(arg, _.keys(commands));
const tip = didYouMean(arg, lodash.keys(commands));
error = `unknown command ${arg}${
tip ? `\n(Did you mean ${tip} ?)` : ""
}`;
Expand Down
8 changes: 4 additions & 4 deletions cli/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import * as lodash from "lodash";
import { reservedOptions, root_command } from "./constants.js";
import { execute } from "./execute.js";
import { displayHelp } from "./operations/display-help.js";
Expand All @@ -12,7 +12,7 @@ const cli = (input) => {
commands[command.name] = {
name: command.name,
description: `${command.description || ""}`,
options: _.compact(_.map(command.options, processOption)),
options: lodash.compact(lodash.map(command.options, processOption)),
};

if (addVersion) {
Expand Down Expand Up @@ -57,7 +57,7 @@ const cli = (input) => {
},
);

_.forEach(input.options, (option) => {
lodash.forEach(input.options, (option) => {
const processed = processOption(option);

if (!processed) return;
Expand Down Expand Up @@ -86,7 +86,7 @@ const cli = (input) => {
}),
);

_.forEach(input.commands, addCommand);
lodash.forEach(input.commands, addCommand);

return instance;
};
Expand Down
53 changes: 26 additions & 27 deletions cli/operations/display-help.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import * as lodash from "lodash";
import { root_command } from "../constants.js";

const generateOptionsOutput = (options) =>
Expand Down Expand Up @@ -62,32 +62,31 @@ const displayAllHelp = (commands, instance) => {
commands[root_command].options,
);

const { commands: commandLabels, maxLength: maxCommandLength } = _.filter(
commands,
(command) => command.name !== root_command,
).reduce(
(acc, command) => {
const options = generateOptionsOutput(command.options);
const name = `${command.name}${options.length ? " [options]" : ""}`;
const description = command.description;

const maxLength = Math.max(name.length, options.maxLength);
if (maxLength > acc.maxLength) {
acc.maxLength = maxLength;
}

acc.commands.push({
description,
name,
options,
});
return acc;
},
{
commands: [],
maxLength: maxOptionLength,
},
);
const { commands: commandLabels, maxLength: maxCommandLength } = lodash
.filter(commands, (command) => command.name !== root_command)
.reduce(
(acc, command) => {
const options = generateOptionsOutput(command.options);
const name = `${command.name}${options.length ? " [options]" : ""}`;
const description = command.description;

const maxLength = Math.max(name.length, options.maxLength);
if (maxLength > acc.maxLength) {
acc.maxLength = maxLength;
}

acc.commands.push({
description,
name,
options,
});
return acc;
},
{
commands: [],
maxLength: maxOptionLength,
},
);

const optionsOutput = generateOptionsTextOutput(options, maxOptionLength, 2);

Expand Down
14 changes: 7 additions & 7 deletions cli/process-option.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import * as lodash from "lodash";

const optionFormatters = {
number: (str) => +str,
Expand All @@ -15,8 +15,9 @@ const processFlags = (flags) => {
let value = null;
const isNoFlag = flags.includes("--no-");

_.compact(_.split(flags, " ").map((str) => str.replace(/,/g, ""))).forEach(
(str) => {
lodash
.compact(lodash.split(flags, " ").map((str) => str.replace(/,/g, "")))
.forEach((str) => {
if (str.startsWith("-")) {
keys.push(str);
} else if (value === null) {
Expand All @@ -31,13 +32,12 @@ const processFlags = (flags) => {
};
}
}
},
);
});

const longestKey = keys.slice().sort((a, b) => b.length - a.length)[0];

if (!_.isEmpty(longestKey)) {
name = _.camelCase(
if (!lodash.isEmpty(longestKey)) {
name = lodash.camelCase(
(isNoFlag ? longestKey.replace("--no-", "") : longestKey).replace(
/(--?)/,
"",
Expand Down
36 changes: 20 additions & 16 deletions src/code-formatter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from "lodash";
import prettier from "prettier";
import ts from "typescript";
import * as lodash from "lodash";
import * as prettier from "prettier";
import * as typescript from "typescript";

class CodeFormatter {
/**
Expand All @@ -16,15 +16,15 @@ class CodeFormatter {
const tempFileName = "file.ts";

const host = new TsLanguageServiceHost(tempFileName, content);
const languageService = ts.createLanguageService(host);
const languageService = typescript.createLanguageService(host);

const fileTextChanges = languageService.organizeImports(
{ type: "file", fileName: tempFileName },
{ newLineCharacter: ts.sys.newLine },
{ newLineCharacter: typescript.sys.newLine },
)[0];

if (fileTextChanges?.textChanges.length) {
return _.reduceRight(
return lodash.reduceRight(
fileTextChanges.textChanges,
(content, { span, newText }) =>
`${content.slice(0, span.start)}${newText}${content.slice(
Expand Down Expand Up @@ -65,21 +65,25 @@ class CodeFormatter {

class TsLanguageServiceHost {
constructor(fileName, content) {
const tsconfig = ts.findConfigFile(fileName, ts.sys.fileExists);
const tsconfig = typescript.findConfigFile(
fileName,
typescript.sys.fileExists,
);

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

getNewLine() {
return "newLine" in ts.sys ? ts.sys.newLine : "\n";
return "newLine" in typescript.sys ? typescript.sys.newLine : "\n";
}
getScriptFileNames() {
return [this.fileName];
Expand All @@ -88,26 +92,26 @@ class TsLanguageServiceHost {
return this.compilerOptions;
}
getDefaultLibFileName() {
return ts.getDefaultLibFileName(this.getCompilationSettings());
return typescript.getDefaultLibFileName(this.getCompilationSettings());
}
getCurrentDirectory() {
return process.cwd();
}
getScriptVersion() {
return ts.version;
return typescript.version;
}
getScriptSnapshot() {
return ts.ScriptSnapshot.fromString(this.content);
return typescript.ScriptSnapshot.fromString(this.content);
}
readFile(fileName, encoding) {
if (fileName === this.fileName) {
return this.content;
}

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

Expand Down
60 changes: 31 additions & 29 deletions src/code-gen-process.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from "lodash";
import ts from "typescript";
import * as lodash from "lodash";
import * as typescript from "typescript";
import { CodeFormatter } from "./code-formatter.js";
import { CodeGenConfig } from "./configuration.js";
import { SchemaComponentsMap } from "./schema-components-map.js";
Expand Down Expand Up @@ -103,8 +103,8 @@ class CodeGenProcess {

this.schemaComponentsMap.clear();

_.each(swagger.usageSchema.components, (component, componentName) =>
_.each(component, (rawTypeData, typeName) => {
lodash.each(swagger.usageSchema.components, (component, componentName) =>
lodash.each(component, (rawTypeData, typeName) => {
this.schemaComponentsMap.createComponent(
this.schemaComponentsMap.createRef([
"components",
Expand All @@ -120,7 +120,7 @@ class CodeGenProcess {
* @type {SchemaComponent[]}
*/
const componentsToParse = this.schemaComponentsMap.filter(
_.compact(["schemas", this.config.extractResponses && "responses"]),
lodash.compact(["schemas", this.config.extractResponses && "responses"]),
);

const parsedSchemas = componentsToParse.map((schemaComponent) => {
Expand Down Expand Up @@ -228,7 +228,7 @@ class CodeGenProcess {
return ` * ${line}${eol ? "\n" : ""}`;
},
NameResolver: NameResolver,
_,
_: lodash,
require: this.templatesWorker.requireFnFromTemplate,
},
config: this.config,
Expand All @@ -239,7 +239,7 @@ class CodeGenProcess {
const components = this.schemaComponentsMap.getComponents();
let modelTypes = [];

const modelTypeComponents = _.compact([
const modelTypeComponents = lodash.compact([
"schemas",
this.config.extractResponses && "responses",
]);
Expand Down Expand Up @@ -322,7 +322,7 @@ class CodeGenProcess {
? await this.createMultipleFileInfos(templatesToRender, configuration)
: await this.createSingleFileInfo(templatesToRender, configuration);

if (!_.isEmpty(configuration.extraTemplates)) {
if (!lodash.isEmpty(configuration.extraTemplates)) {
for (const extraTemplate of configuration.extraTemplates) {
const content = this.templatesWorker.renderTemplate(
this.fileSystem.getFileContent(extraTemplate.path),
Expand Down Expand Up @@ -467,27 +467,29 @@ class CodeGenProcess {
return await this.createOutputFileInfo(
configuration,
configuration.fileName,
_.compact([
this.templatesWorker.renderTemplate(
templatesToRender.dataContracts,
configuration,
),
generateRouteTypes &&
this.templatesWorker.renderTemplate(
templatesToRender.routeTypes,
configuration,
),
generateClient &&
this.templatesWorker.renderTemplate(
templatesToRender.httpClient,
configuration,
),
generateClient &&
lodash
.compact([
this.templatesWorker.renderTemplate(
templatesToRender.api,
templatesToRender.dataContracts,
configuration,
),
]).join("\n"),
generateRouteTypes &&
this.templatesWorker.renderTemplate(
templatesToRender.routeTypes,
configuration,
),
generateClient &&
this.templatesWorker.renderTemplate(
templatesToRender.httpClient,
configuration,
),
generateClient &&
this.templatesWorker.renderTemplate(
templatesToRender.api,
configuration,
),
])
.join("\n"),
);
};

Expand All @@ -500,7 +502,7 @@ class CodeGenProcess {
*/
createOutputFileInfo = async (configuration, fileNameFull, content) => {
const fileName = this.fileSystem.cropExtension(fileNameFull);
const fileExtension = ts.Extension.Ts;
const fileExtension = typescript.Extension.Ts;

if (configuration.translateToJavaScript) {
this.logger.debug("using js translator for", fileName);
Expand Down Expand Up @@ -542,14 +544,14 @@ class CodeGenProcess {
servers: servers || [],
basePath,
host,
externalDocs: _.merge(
externalDocs: lodash.merge(
{
url: "",
description: "",
},
externalDocs,
),
tags: _.compact(tags),
tags: lodash.compact(tags),
baseUrl: serverUrl,
title,
version,
Expand Down
Loading