Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify file r/w code #554

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions scripts/src/files/file-reader.ts

This file was deleted.

23 changes: 0 additions & 23 deletions scripts/src/files/file-type.ts

This file was deleted.

5 changes: 0 additions & 5 deletions scripts/src/files/file-writer.ts

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/src/files/json-file-type.ts

This file was deleted.

21 changes: 0 additions & 21 deletions scripts/src/files/json-file.ts

This file was deleted.

43 changes: 43 additions & 0 deletions scripts/src/resources/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { basename } from 'path'
import { writeFile } from 'fs/promises'
import { Log } from '../utils/log'
import { readFile } from 'node:fs/promises'

export abstract class FileType {
protected constructor(readonly extension: string) {
if (!extension.startsWith('.')) {
throw new Error('Extension must start with a dot')
}
}

abstract read(path: string): Promise<unknown>

abstract write(path: string, data: unknown): Promise<void>

appendExtension(basename: string): string {
return `${basename}${this.extension}`
}

removeExtension(filename: string): string {
return basename(filename, this.extension)
}
}

export class JsonFileType extends FileType {
constructor() {
super('.json')
}

async read(path: string): Promise<unknown> {
try {
return JSON.parse(await readFile(path, 'utf-8'))
} catch {
Log.warn('Unable to read file %s', path)
return
}
}

async write(path: string, json: unknown): Promise<void> {
return writeFile(path, JSON.stringify(json, null, 2))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export class ResourceCollectionListGenerator {
resourcesData.push(await this._generateResourceData(resource))
}
Log.info('Writing list as %s', this.filename)
const writer = new this.resourceCollection.fileType.writer(this.filepath)
await writer.write(resourcesData)
await this.resourceCollection.fileType.write(this.filepath, resourcesData)
Log.ok('Done')
Log.groupEnd()
}
Expand Down
15 changes: 7 additions & 8 deletions scripts/src/resources/resource-collection.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Resource } from './resource'
import { basename, join } from 'path'
import { FileType } from '../files/file-type'
import { JSON_FILE_TYPE } from '../files/json-file-type'
import { mkdirSync, readdirSync } from 'fs'
import { FileType, JsonFileType } from './file'
import { mkdir, readdir } from 'fs/promises'

export class ResourceCollection {
private _resources?: readonly Resource[]

constructor(
readonly path: string,
readonly fileType: FileType = JSON_FILE_TYPE,
readonly fileType: FileType = new JsonFileType(),
) {}

get name(): string {
Expand All @@ -18,7 +17,7 @@ export class ResourceCollection {

async getResources(): Promise<readonly Resource[]> {
if (!this._resources) {
const directoryFiles = readdirSync(this.path, {
const directoryFiles = await readdir(this.path, {
withFileTypes: true,
})
const resourceFiles = directoryFiles.filter(
Expand All @@ -40,17 +39,17 @@ export class ResourceCollection {
async createResource(name: string, data: unknown): Promise<Resource> {
await this._createDirectoryIfDoesNotExist()
const filename = this.fileType.appendExtension(name)
await new this.fileType.writer(join(this.path, filename)).write(data)
await this.fileType.write(join(this.path, filename), data)
return new Resource(this, filename)
}

async upsertResource(name: string, data: unknown): Promise<Resource> {
const filename = this.fileType.appendExtension(name)
await new this.fileType.writer(join(this.path, filename)).write(data)
await this.fileType.write(join(this.path, filename), data)
return new Resource(this, filename)
}

private async _createDirectoryIfDoesNotExist(): Promise<void> {
mkdirSync(this.path, { recursive: true })
await mkdir(this.path, { recursive: true })
}
}
2 changes: 1 addition & 1 deletion scripts/src/resources/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Resource {
}

async getData(): Promise<unknown> {
return new this.collection.fileType.reader(this.path).read()
return this.collection.fileType.read(this.path)
}

get childCollection(): ResourceCollection {
Expand Down