Skip to content

Commit

Permalink
use toMinimalFormattedString for the localstore.json
Browse files Browse the repository at this point in the history
  • Loading branch information
BuZZ-T committed Apr 24, 2022
1 parent 219a7da commit bffc54a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
64 changes: 64 additions & 0 deletions store/Store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,69 @@ describe('Store', () => {
}
}`)
})

it('should format the store with indentation without indenting the arrays', () => {
const store = new Store(createStore({
linux: {
x64: ['10.0.0.0', '20.0.0.0', '30.0.0.0'],
x86: ['10.0.0.0']
}
}))

expect(store.toMinimalFormattedString()).toEqual(`{
"win": {
"x64": [],
"x86": []
},
"linux": {
"x64": ["10.0.0.0","20.0.0.0","30.0.0.0"],
"x86": ["10.0.0.0"]
},
"mac": {
"x64": [],
"arm": []
}
}`)
})

it('should create a parsable string using toString', () => {
const store = new Store(createStore({
linux: {
x64: ['10.0.0.0', '20.0.0.0', '30.0.0.0'],
x86: ['10.0.0.0']
}
}))

const parsedStore = JSON.parse(store.toString())

expect(new Store(parsedStore)).toEqual(store)
})

it('should create a parsable string using toFormattedString', () => {
const store = new Store(createStore({
linux: {
x64: ['10.0.0.0', '20.0.0.0', '30.0.0.0'],
x86: ['10.0.0.0']
}
}))

const parsedStore = JSON.parse(store.toFormattedString())

expect(new Store(parsedStore)).toEqual(store)
})

it('should create a parsable string using toMinimalFormattedString', () => {
const store = new Store(createStore({
linux: {
x64: ['10.0.0.0', '20.0.0.0', '30.0.0.0'],
x86: ['10.0.0.0']
}
}))

const parsedStore = JSON.parse(store.toMinimalFormattedString())

expect(new Store(parsedStore)).toEqual(store)
})

})
})
8 changes: 8 additions & 0 deletions store/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ export class Store {
return JSON.stringify(sortStoreEntries(setStoreToListStore(this.store)), null, spaces)
}

public toMinimalFormattedString(spaces = 4): string {
return JSON.stringify(sortStoreEntries(setStoreToListStore(this.store)), (k, v) => v instanceof Array ? JSON.stringify(v) : v, spaces)
.replace(/"\[/g, '[')
.replace(/\]"/g, ']')
.replace(/\\"/g, '"')
.replace(/""/g, '"')
}

public size(): StoreSize {
return {
linux: this.store.linux.x64.size + this.store.linux.x86.size,
Expand Down
8 changes: 4 additions & 4 deletions store/importStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('importStore', () => {
expect(readFileMock).toHaveBeenCalledTimes(0)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), anyStore.toFormattedString(), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), anyStore.toMinimalFormattedString(), expect.any(Function))

})

Expand All @@ -93,7 +93,7 @@ describe('importStore', () => {
expect(readFileMock).toHaveBeenCalledTimes(0)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), anyStore.toFormattedString(), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), anyStore.toMinimalFormattedString(), expect.any(Function))
})

it('should do nothing, if no store file is loaded from file system', async () => {
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('importStore', () => {
expect(readFileMock).toHaveBeenCalledTimes(1)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), JSON.stringify(mergedStore, null, 4), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), new Store(mergedStore).toMinimalFormattedString(), expect.any(Function))
})

it('should merge the existing file with the file downloaded by local file', async () => {
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('importStore', () => {
expect(readFileMock).toHaveBeenCalledTimes(1)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), JSON.stringify(mergedStore, null, 4), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), new Store(mergedStore).toMinimalFormattedString(), expect.any(Function))
})
})
})
2 changes: 1 addition & 1 deletion store/importStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ export async function importAndMergeLocalstore(config: IStoreConfig): Promise<St
}

async function storeStoreFile(store: Store): Promise<void> {
return writeFilePromise(localStoreFilePath, store.toFormattedString())
return writeFilePromise(localStoreFilePath, store.toMinimalFormattedString())
}
2 changes: 1 addition & 1 deletion store/storeNegativeHit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export async function storeNegativeHit(version: ComparableVersion, os: OS, arch:
if (!currentStore.has(os, arch, version)) {
currentStore.add(os, arch, version)

await writeFile(STORE_FILE, currentStore.toFormattedString())
await writeFile(STORE_FILE, currentStore.toMinimalFormattedString())
}
}
2 changes: 1 addition & 1 deletion utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IChromeConfig } from './interfaces/interfaces'
import type { OSSetting, OS, ExtendedOS } from './interfaces/os.interfaces'
import { IListStore, ISetStore } from './interfaces/store.interfaces'
import type { IListStore, ISetStore } from './interfaces/store.interfaces'

export function detectOperatingSystem(config: IChromeConfig): OSSetting {

Expand Down

0 comments on commit bffc54a

Please sign in to comment.