Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bennett-sh committed May 7, 2023
1 parent feb34b9 commit ae15ce9
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 370 deletions.
50 changes: 50 additions & 0 deletions _dev/schemaToTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { readFileSync, writeFileSync } from 'fs'

const s = JSON.parse(readFileSync('./test.json'))

const { definitions: { RepositoryItem: { properties: RepositoryItem }, Configuration: { properties: Config } } } = s
let content = 'export interface IRepositoryItem {'

for(const [k, v] of Object.entries(RepositoryItem)) {
content += ` ${k} : ${(() => {
if(v?.$ref?.endsWith('ConfigurationReference')) return 'IRepositoryConfiguration'
if(v?.type == 'boolean') return 'boolean'
if(v?.type == 'integer') return 'number'
if(v?.type == 'string') return 'string'
if(v?.type == 'number') return 'number | Float'
if(v?.type == 'array') {
let type = v.items?.type
if(!type) {
console.log('failed', k)
return 'ERROR'
}
if(type == 'integer') return '(number | Float)[]'
return `${type}[]`
}
console.log('unknown type', k, v)
})()} | Raw\n`
}
content += '}'
content += 'export interface IRepositoryConfiguration {'
for(const [k, v] of Object.entries(Config)) {
content += ` ${k} : ${(() => {
if(v?.type == 'boolean') return 'boolean'
if(v?.type == 'integer') return 'number | Float'
if(v?.type == 'string') return 'string'
if(v?.type == 'number') return 'number'
if(v?.type == 'array') {
let type = v.items?.type
if(!type) {
console.log('failed', k)
return 'ERROR'
}
if(type == 'integer') return '(number | Float)[]'
return `${type}[]`
}
console.log('unknown type', k, v)
})()} | Raw\n`
}
content += '}'
writeFileSync('./test.types.ts', content)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "repository-script",
"version": "1.0.1",
"version": "1.0.0",
"description": "An easier way of writing repository files with code",
"main": "dist/src/lib.js",
"types": "./dist/src/index.d.ts",
Expand Down
13 changes: 6 additions & 7 deletions src/repository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { IRepository, IRepositoryEntry, TUUIDv4 } from './types.js'
import type { IRepository, IRepositoryItem, TUUIDv4 } from './types.js'
import { deepEnsureID, randomRepositoryID } from './utils/id.js'
import { RepositoryEntry } from './repositoryEntry/_index.js'
import { writeFile } from 'fs/promises'
import { unescape } from 'querystring'

export class Repository {
private data = new Map<TUUIDv4, IRepositoryEntry>()
private data = new Map<TUUIDv4, IRepositoryItem>()

public addItem(entries: Partial<IRepositoryEntry>): RepositoryEntry {
public addItem(entries: Partial<IRepositoryItem>): RepositoryEntry {
let id = entries.ID_
if(!id) id = randomRepositoryID()
this.patch(id, { ID_: id })
Expand All @@ -18,7 +17,7 @@ export class Repository {
return new RepositoryEntry(this, id)
}

public patch(id: TUUIDv4, entries: Partial<IRepositoryEntry>): this {
public patch(id: TUUIDv4, entries: Partial<IRepositoryItem>): this {
this.data[id] = {
...this.data[id],
...entries
Expand All @@ -27,7 +26,7 @@ export class Repository {
}

public patchAll(ids: TUUIDv4[], handler: (entry: RepositoryEntry) => void): this
public patchAll(ids: TUUIDv4[], handler: Partial<IRepositoryEntry>): this
public patchAll(ids: TUUIDv4[], handler: Partial<IRepositoryItem>): this
public patchAll(ids, handler): this {
ids.forEach(id => {
const entry = this.getItem(id)
Expand All @@ -42,7 +41,7 @@ export class Repository {
return this
}

public async patchAllAsync(ids: TUUIDv4[], handler: (entry: RepositoryEntry) => Promise<Partial<IRepositoryEntry>>): Promise<void> {
public async patchAllAsync(ids: TUUIDv4[], handler: (entry: RepositoryEntry) => Promise<Partial<IRepositoryItem>>): Promise<void> {
for(const id of ids) {
const entry = this.getItem(id)
entry.patch(await handler(entry))
Expand Down
4 changes: 2 additions & 2 deletions src/repositoryEntry/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IRepositoryEntry, TUUIDv4 } from '../types.js'
import type { IRepositoryItem, TUUIDv4 } from '../types.js'
import { Repository } from '../repository.js'

export class RepositoryEntry {
Expand All @@ -11,7 +11,7 @@ export class RepositoryEntry {
return this._id
}

public patch(entries: Partial<IRepositoryEntry>): this {
public patch(entries: Partial<IRepositoryItem>): this {
this.repo.patch(this.id, entries)
return this
}
Expand Down
Loading

0 comments on commit ae15ce9

Please sign in to comment.