@simplyhomes/sos-sdk
Version:
TypeScript SDK for Simply Homes SoS API v4
102 lines (101 loc) ⢠3.68 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;
}
/**
* Transform FromJSONTyped functions to preserve unknown fields
*/
function transformFile(filePath) {
let content = readFileSync(filePath, 'utf-8');
const originalContent = content;
let functionsTransformed = 0;
/**
* Match FromJSONTyped functions and transform their return statements
*
* Pattern matches:
* export function SomeEntityFromJSONTyped(json: any, ignoreDiscriminator: boolean): SomeEntity {
* if (json == null) {
* return json;
* }
* return {
*
* 'field1': json['field1'],
* 'field2': json['field2'] == null ? undefined : transform(json['field2']),
* ...
* };
* }
*/
const fromJSONTypedPattern = /export function (\w+FromJSONTyped)\(json: any[^)]*\)[^{]*{\s*if \(json == null\) {[^}]*}\s*return {\s*\n\s*'(\w+)'/g;
content = content.replace(fromJSONTypedPattern, (match, functionName, firstField) => {
functionsTransformed++;
// Add ...json spread before the first explicit field
return match.replace(`return {\n \n '${firstField}'`, `return {\n ...json, // Preserve relation fields from views\n '${firstField}'`);
});
// Write back if modified
const modified = content !== originalContent;
if (modified) {
writeFileSync(filePath, content, 'utf-8');
}
return {
modified,
functionsTransformed
};
}
/**
* Main transformation function
*/
async function main() {
console.log('š Starting FromJSON transformation to preserve relations...\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,
functionsTransformed: 0
};
for (const file of files) {
stats.filesProcessed++;
const result = transformFile(file);
if (result.modified) {
stats.filesModified++;
stats.functionsTransformed += result.functionsTransformed;
const fileName = file.split(/[/\\]/).pop();
console.log(` ā ${fileName} (${result.functionsTransformed} functions)`);
}
}
console.log('\n⨠Transformation complete!\n');
console.log('š Summary:');
console.log(` ⢠Files processed: ${stats.filesProcessed}`);
console.log(` ⢠Files modified: ${stats.filesModified}`);
console.log(` ⢠Functions transformed: ${stats.functionsTransformed}\n`);
if (stats.filesModified === 0) {
console.log('ā ļø No files were modified. The transformation may have already been applied.\n');
}
else {
console.log('ā
FromJSON functions now preserve relation data from API responses!\n');
}
process.exit(0);
}
main().catch((error) => {
console.error('\nā Transformation failed:\n');
console.error(error);
process.exit(1);
});