api-typescript-generator
Advanced tools
Comparing version 2.5.2 to 2.5.3
@@ -42,2 +42,3 @@ "use strict"; | ||
const R = __importStar(require("ramda")); | ||
const lock_1 = require("../utils/lock"); | ||
function saveGenerationResult({ files, outputDirPath, cleanupDirectories }) { | ||
@@ -60,15 +61,17 @@ return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
let exists = false; | ||
try { | ||
const existingContent = yield fs_1.default.promises.readFile(fullFilename, 'utf8'); | ||
exists = true; | ||
if (existingContent === data) { | ||
console.log('[no change] ' + fullFilename); | ||
return; | ||
yield (0, lock_1.lock)(`file:${fullFilename}`, () => __awaiter(this, void 0, void 0, function* () { | ||
let exists = false; | ||
try { | ||
const existingContent = yield fs_1.default.promises.readFile(fullFilename, 'utf8'); | ||
exists = true; | ||
if (existingContent === data) { | ||
console.log('[no change] ' + fullFilename); | ||
return; | ||
} | ||
} | ||
} | ||
catch (e) { | ||
} | ||
yield fs_1.default.promises.writeFile(fullFilename, data); | ||
console.log(`[${exists ? 'updated' : 'created'}] ${fullFilename}`); | ||
catch (e) { | ||
} | ||
yield fs_1.default.promises.writeFile(fullFilename, data); | ||
console.log(`[${exists ? 'updated' : 'created'}] ${fullFilename}`); | ||
})); | ||
} | ||
@@ -75,0 +78,0 @@ catch (e) { |
@@ -64,3 +64,3 @@ "use strict"; | ||
const clientConstructorOptionsObject = (0, types_1.objectExpression)([ | ||
(0, types_1.objectProperty)((0, types_1.identifier)('apiClientClassName'), (0, types_1.logicalExpression)('??', (0, types_1.memberExpression)((0, types_1.memberExpression)((0, types_1.thisExpression)(), (0, types_1.identifier)('constructor')), (0, types_1.identifier)('name')), (0, types_1.stringLiteral)('name'))), | ||
(0, types_1.objectProperty)((0, types_1.identifier)('apiClientClassName'), (0, types_1.logicalExpression)('??', (0, types_1.memberExpression)((0, types_1.memberExpression)((0, types_1.thisExpression)(), (0, types_1.identifier)('constructor')), (0, types_1.identifier)('name')), (0, types_1.stringLiteral)(name))), | ||
(0, types_1.objectProperty)((0, types_1.identifier)('baseUrl'), (0, types_1.stringLiteral)((_c = baseUrl !== null && baseUrl !== void 0 ? baseUrl : (_b = servers[0]) === null || _b === void 0 ? void 0 : _b.url) !== null && _c !== void 0 ? _c : defaultServerUrl)), | ||
@@ -67,0 +67,0 @@ (0, types_1.objectProperty)((0, types_1.identifier)('binaryResponseType'), (0, types_1.stringLiteral)(responseBinaryType)), |
@@ -18,3 +18,14 @@ "use strict"; | ||
const node_path_1 = __importDefault(require("node:path")); | ||
const lock_1 = require("./lock"); | ||
const make_dir_1 = require("./make-dir"); | ||
let eslintInstance = null; | ||
function getEslintInstance() { | ||
if (eslintInstance === null) { | ||
const { ESLint } = require('eslint'); | ||
eslintInstance = new ESLint({ | ||
fix: true | ||
}); | ||
} | ||
return eslintInstance; | ||
} | ||
function postprocessFiles({ files, config: { eslint: enableEslint } = {}, outputDirPath }) { | ||
@@ -29,61 +40,68 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
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); | ||
yield (0, lock_1.lock)('calc:directories', () => __awaiter(this, void 0, void 0, function* () { | ||
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 (0, make_dir_1.makeDir)(currentDirectory); | ||
directoriesToRemove.unshift(currentDirectory); | ||
} | ||
const subDirectory = directoryBits.shift(); | ||
if (!subDirectory) { | ||
break; | ||
} | ||
currentDirectory = node_path_1.default.join(currentDirectory, subDirectory); | ||
} | ||
catch (e) { | ||
yield (0, make_dir_1.makeDir)(currentDirectory); | ||
directoriesToRemove.unshift(currentDirectory); | ||
} | ||
const subDirectory = directoryBits.shift(); | ||
if (!subDirectory) { | ||
break; | ||
} | ||
currentDirectory = node_path_1.default.join(currentDirectory, subDirectory); | ||
} | ||
} | ||
} | ||
})); | ||
const eslint = getEslintInstance(); | ||
try { | ||
const { ESLint } = require('eslint'); | ||
const eslint = new ESLint({ | ||
fix: true | ||
}); | ||
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 { | ||
return yield (0, lock_1.lock)(`file:${filePath}`, () => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
let fileCreated = false; | ||
try { | ||
yield promises_1.default.stat(filePath); | ||
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 }); | ||
} | ||
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}`); | ||
finally { | ||
if (fileCreated) { | ||
yield promises_1.default.unlink(filePath); | ||
} | ||
} | ||
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); | ||
} | ||
yield (0, lock_1.lock)('cleanup:directories', () => __awaiter(this, void 0, void 0, function* () { | ||
for (const directory of directoriesToRemove) { | ||
try { | ||
yield promises_1.default.rmdir(directory); | ||
} | ||
catch (e) { | ||
} | ||
} | ||
})); | ||
} | ||
@@ -90,0 +108,0 @@ } |
{ | ||
"name": "api-typescript-generator", | ||
"version": "2.5.2", | ||
"version": "2.5.3", | ||
"description": "Generates OpenAPI TypeScript client. Extremely fast and flexible.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
474009
143
6829