api-typescript-generator
Advanced tools
Comparing version 2.4.0 to 2.4.1
@@ -16,22 +16,73 @@ "use strict"; | ||
exports.postprocessFiles = void 0; | ||
const path_1 = __importDefault(require("path")); | ||
const promises_1 = __importDefault(require("node:fs/promises")); | ||
const node_path_1 = __importDefault(require("node:path")); | ||
function postprocessFiles({ files, config: { eslint: enableEslint } = {}, outputDirPath }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (enableEslint) { | ||
const { ESLint } = require('eslint'); | ||
const eslint = new ESLint({ | ||
fix: true | ||
}); | ||
return Promise.all(files.map((file) => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
const [result] = yield eslint.lintText(file.data, { | ||
filePath: path_1.default.resolve(outputDirPath, file.filename) | ||
const directoriesToRemove = []; | ||
const directories = new Set(); | ||
for (const file of files) { | ||
const directory = node_path_1.default.dirname(node_path_1.default.resolve(outputDirPath, file.filename)); | ||
directories.add(directory); | ||
} | ||
for (const directory of Array.from(directories)) { | ||
try { | ||
yield promises_1.default.stat(directory); | ||
} | ||
catch (_e) { | ||
const directoryBits = directory.split(node_path_1.default.sep); | ||
let currentDirectory = directoryBits.shift() || '/'; | ||
for (;;) { | ||
try { | ||
yield promises_1.default.stat(currentDirectory); | ||
} | ||
catch (e) { | ||
yield promises_1.default.mkdir(currentDirectory); | ||
directoriesToRemove.unshift(currentDirectory); | ||
} | ||
const subDirectory = directoryBits.shift(); | ||
if (!subDirectory) { | ||
break; | ||
} | ||
currentDirectory = node_path_1.default.join(currentDirectory, subDirectory); | ||
} | ||
} | ||
} | ||
try { | ||
const { ESLint } = require('eslint'); | ||
const eslint = new ESLint({ | ||
fix: true | ||
}); | ||
for (const message of result.messages) { | ||
if (message.fatal) { | ||
throw new Error(`Fatal ESLint error in ${file.filename}: ${message.message}`); | ||
return yield Promise.all(files.map((file) => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
const filePath = node_path_1.default.resolve(outputDirPath, file.filename); | ||
let fileCreated = false; | ||
try { | ||
try { | ||
yield promises_1.default.stat(filePath); | ||
} | ||
catch (_e) { | ||
yield promises_1.default.writeFile(filePath, file.data); | ||
fileCreated = true; | ||
} | ||
const [result] = yield eslint.lintText(file.data, { filePath }); | ||
for (const message of result.messages) { | ||
if (message.fatal) { | ||
throw new Error(`Fatal ESLint error in ${file.filename}: ${message.message}`); | ||
} | ||
} | ||
return Object.assign(Object.assign({}, file), { data: (_a = result.output) !== null && _a !== void 0 ? _a : file.data }); | ||
} | ||
finally { | ||
if (fileCreated) { | ||
yield promises_1.default.unlink(filePath); | ||
} | ||
} | ||
}))); | ||
} | ||
finally { | ||
for (const directory of directoriesToRemove) { | ||
yield promises_1.default.rmdir(directory); | ||
} | ||
return Object.assign(Object.assign({}, file), { data: (_a = result.output) !== null && _a !== void 0 ? _a : file.data }); | ||
}))); | ||
} | ||
} | ||
@@ -38,0 +89,0 @@ return files; |
{ | ||
"name": "api-typescript-generator", | ||
"version": "2.4.0", | ||
"version": "2.4.1", | ||
"description": "Generates OpenAPI TypeScript client. Extremely fast and flexible.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -1,2 +0,3 @@ | ||
import path from 'path'; | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import type {ESLint as ESLintClass} from 'eslint'; | ||
@@ -18,27 +19,75 @@ import { | ||
if (enableEslint) { | ||
// This is an optional dependency, so we require it here to avoid loading it when it's not needed. | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const {ESLint} = require('eslint') as {ESLint: typeof ESLintClass}; | ||
const eslint = new ESLint({ | ||
fix: true | ||
}); | ||
// TypeScript parser expects files to be present, so we need to create directories for them. | ||
const directoriesToRemove: string[] = []; | ||
const directories: Set<string> = new Set(); | ||
for (const file of files) { | ||
const directory = path.dirname(path.resolve(outputDirPath, file.filename)); | ||
directories.add(directory); | ||
} | ||
return Promise.all( | ||
files.map(async (file) => { | ||
const [result] = await eslint.lintText(file.data, { | ||
filePath: path.resolve(outputDirPath, file.filename) | ||
}); | ||
for (const message of result.messages) { | ||
if (message.fatal) { | ||
throw new Error(`Fatal ESLint error in ${file.filename}: ${message.message}`); | ||
for (const directory of Array.from(directories)) { | ||
try { | ||
await fs.stat(directory); | ||
} catch (_e) { | ||
const directoryBits = directory.split(path.sep); | ||
let currentDirectory = directoryBits.shift() || '/'; | ||
for (;;) { | ||
try { | ||
await fs.stat(currentDirectory); | ||
} catch (e) { | ||
await fs.mkdir(currentDirectory); | ||
directoriesToRemove.unshift(currentDirectory); | ||
} | ||
const subDirectory = directoryBits.shift(); | ||
if (!subDirectory) { | ||
break; | ||
} | ||
currentDirectory = path.join(currentDirectory, subDirectory); | ||
} | ||
return { | ||
...file, | ||
data: result.output ?? file.data | ||
}; | ||
}) | ||
); | ||
} | ||
} | ||
try { | ||
// This is an optional dependency, so we require it here to avoid loading it when it's not needed. | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const {ESLint} = require('eslint') as {ESLint: typeof ESLintClass}; | ||
const eslint = new ESLint({ | ||
fix: true | ||
}); | ||
return await Promise.all( | ||
files.map(async (file) => { | ||
const filePath = path.resolve(outputDirPath, file.filename); | ||
let fileCreated = false; | ||
try { | ||
try { | ||
await fs.stat(filePath); | ||
} catch (_e) { | ||
await fs.writeFile(filePath, file.data); | ||
fileCreated = true; | ||
} | ||
const [result] = await eslint.lintText(file.data, {filePath}); | ||
for (const message of result.messages) { | ||
if (message.fatal) { | ||
throw new Error(`Fatal ESLint error in ${file.filename}: ${message.message}`); | ||
} | ||
} | ||
return { | ||
...file, | ||
data: result.output ?? file.data | ||
}; | ||
} finally { | ||
if (fileCreated) { | ||
await fs.unlink(filePath); | ||
} | ||
} | ||
}) | ||
); | ||
} finally { | ||
for (const directory of directoriesToRemove) { | ||
await fs.rmdir(directory); | ||
} | ||
} | ||
} | ||
return files; | ||
} |
747243
14150