@kontent-ai/model-generator
Version:
This utility generates strongly-typed models for Delivery JS SDK, Migration toolkit or just general scripting to improve the experience when referencing Kontent.ai related objects.
57 lines • 2 kB
JavaScript
import { Biome } from "@biomejs/js-api/nodejs";
export async function formatCodeAsync(code, formatType, configuration) {
const result = withBiome(configuration, (biome, projectKey) => {
const formattedContent = biome.formatContent(projectKey, code, {
filePath: formatType === "typescript" ? "virtual.ts" : "virtual.json",
});
if (formattedContent.diagnostics.length > 0) {
throw new Error(`Failed to format code: ${formattedContent.diagnostics.map((m) => m.message).join("\n")}`);
}
const lintedContent = biome.lintContent(projectKey, formattedContent.content, {
filePath: "virtual.ts",
fixFileMode: "safeFixes",
});
if (lintedContent.diagnostics.length > 0) {
throw new Error(`Failed to lint code: ${lintedContent.diagnostics.map((m) => m.message).join("\n")}`);
}
return lintedContent.content;
});
return await Promise.resolve(result);
}
function withBiome(configuration, callback) {
const biome = new Biome();
const { projectKey } = biome.openProject();
biome.applyConfiguration(projectKey, configuration ?? {
extends: ["@kontent-ai/biome-config/base"],
formatter: {
indentWidth: 4,
indentStyle: "tab",
lineWidth: 140,
},
assist: {
enabled: true,
actions: {
source: {
organizeImports: "on",
},
},
},
linter: {
rules: {
performance: {
noBarrelFile: "off",
},
correctness: {
noUnusedImports: {
fix: "safe",
level: "error",
},
},
},
},
});
const result = callback(biome, projectKey);
biome.shutdown();
return result;
}
//# sourceMappingURL=formatter.js.map