Skip to content

Commit

Permalink
Merge pull request #11 from KemingHe/refactor-generatescriptaddtyping…
Browse files Browse the repository at this point in the history
…-keminghe

Refactor(scripts/ dir): added explicit typing to generate ts scripts for enhanced safety
  • Loading branch information
KemingHe committed Aug 3, 2024
2 parents c526f78 + 11f8d8b commit 573ab84
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 139 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

## [1.1.1](https://github.com/KemingHe/OSU/compare/v1.1.0...v1.1.1) (2024-08-03)

## [1.1.0](https://github.com/KemingHe/OSU/compare/v1.0.10...v1.1.0) (2024-08-03)


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keminghe/osu",
"version": "1.1.0",
"version": "1.1.1",
"description": "Unofficial, publicly available data about The Ohio State University.",
"keywords": [
"data",
Expand Down
138 changes: 69 additions & 69 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 23 additions & 7 deletions scripts/genAllStudentOrgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import { type ZodSchema, z } from "zod";

// Local utils imports.
import csv2TS, {
DEFAULT_HEADER,
type csv2TSOptions,
} from "@scripts/utils/csv2TS";
import csv2TS, { type csv2TSOptions } from "@scripts/utils/csv2TS";

// Define raw data schema for input csv validation. ----------------------------
export const rawStudentOrg: ZodSchema = z.object(
// biome-ignore format: added alignment for clarity.
{
Expand All @@ -21,6 +19,7 @@ export const rawStudentOrg: ZodSchema = z.object(
);
export type rawStudentOrg = z.infer<typeof rawStudentOrg>;

// Define student organization schema for output ts validation. ----------------
export const studentOrg: ZodSchema = z.object(
// biome-ignore format: added alignment for clarity.
{
Expand All @@ -31,6 +30,8 @@ export const studentOrg: ZodSchema = z.object(
);
export type studentOrg = z.infer<typeof studentOrg>;

// Define middle fn to sanitize raw data into student org data. ----------------

// Sanitize affiliation string into array of strings, fallback to null.
export function sanitizeAffiliation(affiliation: string): string[] | null {
// Early return null if affiliation is empty.
Expand Down Expand Up @@ -64,6 +65,18 @@ export function sanitizeStudentOrgs(data: rawStudentOrg[]): studentOrg[] {
});
}

// Define allStudentOrg header and footer for output ts file. ------------------
export const allStudentOrgsHeader: string = `// ./src/autoGenerated/allStudentOrgs.ts
//
// AUTO-GENERATED FILE. DO NOT MODIFY.
// For details see ./scripts/genAllStudentOrgs.ts
// Type imports.
import type { StudentOrg } from "@src/osu";
export const allStudentOrgs: StudentOrg[] =\n`;

// Setup options for allStudentOrgs csv to ts conversion. ----------------------
export const allStudentOrgsOptions: csv2TSOptions =
// biome-ignore format: added alignment for clarity.
{
Expand All @@ -72,12 +85,15 @@ export const allStudentOrgsOptions: csv2TSOptions =
inFileSchema : rawStudentOrg,
middleFunction : sanitizeStudentOrgs,
outFilePath : "src/autoGenerated/allStudentOrgs.ts",
outFileHeader : DEFAULT_HEADER,
outFileArrayName: "allStudentOrgs",
outFileHeader : allStudentOrgsHeader,
outFileFooter : "",
outFileSchema : studentOrg,
};

// Main function to convert allStudentOrgs csv to ts array.
// Main function to convert allStudentOrgs csv to ts array. --------------------
export default async function genAllStudentOrgs(): Promise<void> {
await csv2TS(allStudentOrgsOptions);
}

// Enable script to run standalone. --------------------------------------------
// genAllStudentOrgs();
Loading

0 comments on commit 573ab84

Please sign in to comment.