Skip to content

Commit

Permalink
chore(docs): update scripts to generate alpha numerically
Browse files Browse the repository at this point in the history
Makes diffs easier and ensures it's always written the same way
  • Loading branch information
mlaursen committed Nov 12, 2023
1 parent 97644f3 commit e277f97
Show file tree
Hide file tree
Showing 7 changed files with 2,478 additions and 2,465 deletions.
1 change: 1 addition & 0 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"next-dev": "next dev",
"next-build": "next build",
"dev": "npm-run-all create-env watch",
"build-generated": "npm-run-all api-docs mdx-pages scss-lookup",
"build": "npm-run-all create-env next-build",
"start": "next start"
},
Expand Down
4 changes: 3 additions & 1 deletion apps/docs/scripts/createApiDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { type NavigationRouteItem } from "../src/constants/navItems.js";
import { format } from "../src/utils/format.js";
import { GENERATED_FILE_BANNER } from "./constants.js";
import { printObjectAlphaNumerically } from "./utils/printObjectAlphaNumerically.js";

const constants = join(process.cwd(), "src", "constants");
const API_LOOKUP_PATH = join(constants, "apiLookup.ts");
Expand All @@ -31,6 +32,7 @@ const start = Date.now();
const project = new Project({
tsConfigFilePath: "../../packages/core/tsconfig.json",
});
project.removeSourceFile(project.getSourceFileOrThrow("data-testid.ts"));
console.log(`Created in ${prettyMilliseconds(Date.now() - start)}`);

// const hooks = ["../../packages/core/src/form/useTextField.ts"];
Expand Down Expand Up @@ -245,7 +247,7 @@ await writeFile(
await format(`${GENERATED_FILE_BANNER}
import { type ApiLookup } from "@/components/ApiDocs/types.js";
export const API_LOOKUP: ApiLookup = ${JSON.stringify(apiLookup)};
export const API_LOOKUP: ApiLookup = ${printObjectAlphaNumerically(apiLookup)};
`)
);

Expand Down
14 changes: 4 additions & 10 deletions apps/docs/scripts/createScssLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { format } from "../src/utils/format.js";
import { GENERATED_FILE_BANNER } from "./constants.js";
import { getScriptFlags } from "./utils/getScriptFlags.js";
import { log } from "./utils/log.js";
import { alphaNumericSort } from "@react-md/core";
import { printObjectAlphaNumerically } from "./utils/printObjectAlphaNumerically.js";

const { isWatch } = getScriptFlags();

Expand All @@ -31,19 +31,13 @@ async function createScssLookup(): Promise<void> {
})
);

const sortedKeyValuePairs = alphaNumericSort(Object.entries(lookup), {
extractor: ([name]) => name,
})
.map(([name, value]) => `"${name}": ${JSON.stringify(value)}`)
.join(",\n");

await writeFile(
SCSS_LOOKUP_PATH,
await format(`${GENERATED_FILE_BANNER}
export const SCSS_LOOKUP: Record<string, string> = {
${sortedKeyValuePairs}
}
export const SCSS_LOOKUP: Record<string, string> = ${printObjectAlphaNumerically(
lookup
)}
`)
);
}
Expand Down
5 changes: 4 additions & 1 deletion apps/docs/scripts/utils/createScssModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { compileScssModule } from "../../src/utils/compileScssModule.js";
import { format } from "../../src/utils/format.js";
import { scssModulesCache } from "../codeCache.js";
import { GENERATED_FILE_BANNER } from "../constants.js";
import { printObjectAlphaNumerically } from "./printObjectAlphaNumerically.js";

const SCSS_MODULES_PATH = "src/constants/scssModulesLookup.ts";

Expand All @@ -30,7 +31,9 @@ import "server-only";
import { type FakeScssModule } from "../utils/fakeScssModules.js";
export const SCSS_MODULES: Record<string, FakeScssModule> =
${JSON.stringify(Object.fromEntries(scssModulesCache.entries()))};
${printObjectAlphaNumerically(
Object.fromEntries(scssModulesCache.entries())
)};
`);

await writeFile(SCSS_MODULES_PATH, contents);
Expand Down
13 changes: 13 additions & 0 deletions apps/docs/scripts/utils/printObjectAlphaNumerically.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { alphaNumericSort } from "@react-md/core";

export function printObjectAlphaNumerically(
obj: Record<string, unknown>
): string {
const sortedKeyValuePairs = alphaNumericSort(Object.entries(obj), {
extractor: ([name]) => name,
})
.map(([name, value]) => `"${name}": ${JSON.stringify(value)}`)
.join(",\n");

return `{${sortedKeyValuePairs}}`;
}
Loading

0 comments on commit e277f97

Please sign in to comment.