Page MenuHomeSealhub

generate-collections.ts
No OneTemporary

generate-collections.ts

import _locreq from "locreq";
import { promises as fs } from "fs";
import { assertType, predicates } from "@sealcode/ts-predicates";
const target_locreq = _locreq(process.cwd());
async function extractCollectionClassname(
full_file_path: string
): Promise<string> {
const file_content = await fs.readFile(full_file_path, "utf-8");
const result = file_content.match(/export default class (\w+)/);
if (result === null) {
throw new Error("Missing 'export default class' statement?");
}
return assertType(
result[1],
predicates.string,
"Missing export default class?"
);
}
export async function generateCollections(): Promise<void> {
const collections_dir = target_locreq.resolve("src/back/collections");
const files = (await fs.readdir(collections_dir)).filter(
(f) => !f.match(/\.(sub)?test\./) && f != "collections.ts"
);
const file_data = await Promise.all(
files.map(async (file) => {
const full_path = target_locreq.resolve(
"src/back/collections/" + file
);
const class_name = await extractCollectionClassname(full_path);
return {
class_name,
file,
full_path,
urlname: file.replace(".ts", ""),
};
})
);
const content = `// DO NOT EDIT! This file is generated automaticaly with 'npm run generate-collections'
import { App } from "sealious";
${file_data
.map(
({ class_name, urlname }) =>
`import _${class_name} from "./${urlname}";`
)
.join("\n")}
${file_data
.map(
({ class_name }) => `export const ${class_name} = new _${class_name}();`
)
.join("\n")}
export const collections = {
...App.BaseCollections,
${file_data
.map(({ class_name, urlname }) => ` "${urlname}": ${class_name},`)
.join("\n")}
};`;
await fs.writeFile(
target_locreq.resolve("src/back/collections/collections.ts"),
content
);
// eslint-disable-next-line no-console
console.log(
"Successfuly generated new src/back/collections/collections.ts file"
);
}

File Metadata

Mime Type
text/x-java
Expires
Fri, Jan 24, 15:15 (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
599867
Default Alt Text
generate-collections.ts (1 KB)

Event Timeline