Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
fix: restore comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaringe committed Jan 10, 2021
1 parent 47d4a70 commit 245d18a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
12 changes: 3 additions & 9 deletions packages/json-schema-producer/src/from-website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getHtml } from "./browser";
import { classNames as globalClassNames } from "./globals";
import { bigBadHacks } from "./hack";
import { definitionTypes } from "./json-schema";
import { sortKeys } from "./objects";
import { scrapeClassPage } from "./scrape/classes";
import { scrapeConcepts } from "./scrape/concepts";
import { scrapeDefines } from "./scrape/defines";
Expand Down Expand Up @@ -58,22 +59,15 @@ export const produce = async ({
);
}

const aFewDefinitions = Object.keys(definitions)
.slice(0, 1)
.reduce((acc, key) => {
acc[key] = definitions[key];
return acc;
}, {} as typeof definitions);
const schema: JSONSchema6 = {
type: "object",
description: "Factorio Lua API",
required: [...globalClassNames, "defines"],
definitions,
properties: {
properties: sortKeys({
...definitions,
// ...aFewDefinitions,
defines,
},
}),
additionalProperties: false,
};
await fs.writeFile("factorio.schema.json", JSON.stringify(schema));
Expand Down
21 changes: 12 additions & 9 deletions packages/json-schema-producer/src/langs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ declare module "json-schema" {
}
}

const getDocBlock = (schema: JSONSchema6) =>
schema.description ? `/* ${schema.description} */\n ` : "";

const toTsType = (jtype: JSONSchema6): string => {
// primitives
const jtt = jtype.type;
Expand Down Expand Up @@ -81,9 +84,9 @@ export const withType = {
const isRO = opts.parseMode
? !mode.some((rw: string) => !!rw.match(/w/i))
: false;
return `${isRO ? "readonly " : ""}"${name}": ${toTsType(
schema.properties?.type as JSONSchema6
)}`;
return `${getDocBlock(schema)}${
isRO ? "readonly " : ""
}"${name}": ${toTsType(schema.properties?.type as JSONSchema6)}`;
},
method: (schema: JSONSchema6) => {
const args = ((schema.items as JSONSchema6[]) || [])
Expand All @@ -98,13 +101,13 @@ export const withType = {
if (!retSchema) {
throw new Error("missing return value");
}
return `(${args}) => ${toTsType(retSchema)}`;
return `${getDocBlock(schema)}(${args}) => ${toTsType(retSchema)}`;
},
class: (schema: JSONSchema6, opts: { asStruct?: boolean }) => {
const members = (schema.properties!.members as JSONSchema6)
.properties as Record<string, JSONSchema6>;
const members = schema.properties!.members as JSONSchema6;
const membersProperties = members.properties as Record<string, JSONSchema6>;
const { properties: propNames, methods: methodNames } = Object.entries(
members as Record<string, JSONSchema6>
membersProperties
).reduce(
(acc, [name, aSchema]) => {
if (aSchema.properties?.return) acc.methods.push(name);
Expand All @@ -115,12 +118,12 @@ export const withType = {
);
const sort = (a: string, b: string) => a.localeCompare(b);
const propStrs = propNames.sort(sort).map((name) => {
return withType.classProperty(name, members[name], {
return withType.classProperty(name, membersProperties[name], {
parseMode: !opts?.asStruct,
});
});
const methodStrs = methodNames.sort(sort).map((name) => {
return `${name}: ${withType.method(members[name])}`;
return `${name}: ${withType.method(membersProperties[name])}`;
});
return `
{
Expand Down
7 changes: 7 additions & 0 deletions packages/json-schema-producer/src/objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const sortKeys = <T extends Record<string, any>>(o: T): T => {
const keys = Object.keys(o).sort((a, b) => a.localeCompare(b));
return keys.reduce((acc, key) => {
acc[key as keyof T] = o[key];
return acc;
}, {} as T);
};
8 changes: 2 additions & 6 deletions packages/json-schema-producer/src/scrape/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,7 @@ const prefixDescription = (description: string) => (schema: JSONSchema6) => {
return schema;
};

const scrapeClassFromEl = (
document: Document,
el: IElement,
pageMeta: PageMeta
) => {
const ofEl = (document: Document, el: IElement, pageMeta: PageMeta) => {
const rootSiblings = el.children;
const classNameEl = rootSiblings.find(
(it) => "className" in it && it.className === "type-name"
Expand Down Expand Up @@ -291,7 +287,7 @@ const scrapeClassFromEl = (
export const scrapeClassPage = (document: Document, pageMeta: PageMeta) => {
const classEls = getClassListingEls(document);
const classes = classEls
.map((el) => scrapeClassFromEl(document, el, pageMeta))
.map((el) => ofEl(document, el, pageMeta))
.map(
prefixDescription(
getDescription(document.body.getElementsByTagName("h1")[0], pageMeta)
Expand Down

0 comments on commit 245d18a

Please sign in to comment.