@simplyhomes/sos-sdk
Version:
TypeScript SDK for Simply Homes SoS API v4
149 lines (148 loc) ⢠5.4 kB
JavaScript
import { readFileSync, writeFileSync, readdirSync, statSync } from 'fs';
import { join } from 'path';
/**
* Recursively get all .ts files in a directory
*/
function getAllTsFiles(dir) {
const files = [];
const entries = readdirSync(dir);
for (const entry of entries) {
const fullPath = join(dir, entry);
const stat = statSync(fullPath);
if (stat.isDirectory()) {
files.push(...getAllTsFiles(fullPath));
}
else if (entry.endsWith('.ts')) {
files.push(fullPath);
}
}
return files;
}
/**
* Fix boolean type handling in a TypeScript file
*/
function transformFile(filePath) {
let content = readFileSync(filePath, 'utf-8');
const originalContent = content;
let fromJsonBooleanChecks = 0;
let toJsonBooleanChecks = 0;
// Check if file contains union types with boolean
const hasBooleanUnionType = /export type \w+ = [^;]*\| boolean[^;]*;/.test(content);
if (!hasBooleanUnionType) {
// Skip files without boolean union types
return { modified: false, fromJsonBooleanChecks: 0, toJsonBooleanChecks: 0 };
}
/**
* Fix FromJSONTyped functions
*
* Pattern to match:
* export function SomeTypeFromJSONTyped(json: any, ignoreDiscriminator: boolean): SomeType {
* if (json == null) {
* return json;
* }
* if (typeof json === 'number') {
* return json;
* }
* ...
* return {} as any; // <-- Inject boolean check BEFORE this line
* }
*/
const fromJsonPattern = /(export function \w+FromJSONTyped\(json: any[^)]*\)[^{]*{[\s\S]*?)( return \{\} as any;\n})/g;
content = content.replace(fromJsonPattern, (match, beforeReturn, returnStatement) => {
// Check if boolean check already exists
if (beforeReturn.includes("typeof json === 'boolean'")) {
return match;
}
fromJsonBooleanChecks++;
// Inject boolean check before the fallback return
const booleanCheck = ` if (typeof json === 'boolean') {
return json;
}
`;
return beforeReturn + booleanCheck + returnStatement;
});
/**
* Fix ToJSONTyped functions
*
* Pattern to match:
* export function SomeTypeToJSONTyped(value?: SomeType | null, ignoreDiscriminator: boolean = false): any {
* if (value == null) {
* return value;
* }
* if (typeof value === 'number') {
* return value;
* }
* ...
* return {}; // <-- Inject boolean check BEFORE this line
* }
*/
const toJsonPattern = /(export function \w+ToJSONTyped\(value\?:[^)]*\)[^{]*{[\s\S]*?)( return \{\};\n})/g;
content = content.replace(toJsonPattern, (match, beforeReturn, returnStatement) => {
// Check if boolean check already exists
if (beforeReturn.includes("typeof value === 'boolean'")) {
return match;
}
toJsonBooleanChecks++;
// Inject boolean check before the fallback return
const booleanCheck = ` if (typeof value === 'boolean') {
return value;
}
`;
return beforeReturn + booleanCheck + returnStatement;
});
// Write back if modified
const modified = content !== originalContent;
if (modified) {
writeFileSync(filePath, content, 'utf-8');
}
return {
modified,
fromJsonBooleanChecks,
toJsonBooleanChecks
};
}
/**
* Main transformation function
*/
async function main() {
console.log('š Starting boolean serialization fix...\n');
const modelsDir = join(process.cwd(), 'src', 'generated', 'src', 'models');
console.log(`š Scanning directory: ${modelsDir}\n`);
const files = getAllTsFiles(modelsDir);
console.log(`š Found ${files.length} TypeScript files\n`);
const stats = {
filesProcessed: 0,
filesModified: 0,
fromJsonBooleanChecks: 0,
toJsonBooleanChecks: 0
};
for (const file of files) {
stats.filesProcessed++;
const result = transformFile(file);
if (result.modified) {
stats.filesModified++;
stats.fromJsonBooleanChecks += result.fromJsonBooleanChecks;
stats.toJsonBooleanChecks += result.toJsonBooleanChecks;
const fileName = file.split(/[/\\]/).pop();
console.log(` ā ${fileName} (${result.fromJsonBooleanChecks} FromJSON, ${result.toJsonBooleanChecks} ToJSON)`);
}
}
console.log('\n⨠Transformation complete!\n');
console.log('š Summary:');
console.log(` ⢠Files processed: ${stats.filesProcessed}`);
console.log(` ⢠Files modified: ${stats.filesModified}`);
console.log(` ⢠FromJSON boolean checks added: ${stats.fromJsonBooleanChecks}`);
console.log(` ⢠ToJSON boolean checks added: ${stats.toJsonBooleanChecks}\n`);
if (stats.filesModified === 0) {
console.log('ā ļø No files were modified. Either the fix was already applied or no boolean union types exist.\n');
}
else {
console.log('ā
Boolean serialization functions now properly handle boolean values!\n');
}
process.exit(0);
}
main().catch((error) => {
console.error('\nā Transformation failed:\n');
console.error(error);
process.exit(1);
});