-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from livecycle/refactor-profiles
- telemetry: add profile name, type - change default s3 bucket name from `preview` to `preevy` - refactor profiles a bit: - remove redundant code - rearrange files - rename to more descriptive names - snapshotStore is the backend, store is the friendly frontend - introduced \`FileBackedSnapshotter\` as an abstraction for tar - added tar dirty check - `profile import` - added smarter default alias - `profile rm` - remove `current` pointer when removing the current profile
- Loading branch information
Showing
21 changed files
with
349 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import path from 'path' | ||
import { localFs } from '../store/fs/local' | ||
import { fsFromUrl, store, tarSnapshot } from '../store' | ||
import { ProfileStore, profileStore } from './store' | ||
import { Profile } from './profile' | ||
|
||
type ProfileListing = { | ||
alias: string | ||
id: string | ||
location: string | ||
} | ||
|
||
type ProfileList = { | ||
current: string | undefined | ||
profiles: Record<string, Omit<ProfileListing, 'alias'>> | ||
} | ||
|
||
const profileListFileName = 'profileList.json' | ||
|
||
export const localProfilesConfig = (localDir: string) => { | ||
const localStore = localFs(localDir) | ||
const tarSnapshotFromUrl = async ( | ||
url: string, | ||
) => store(async dir => tarSnapshot(await fsFromUrl(url, path.join(localDir, 'profiles')), dir)) | ||
|
||
async function readProfileList(): Promise<ProfileList> { | ||
const data = await localStore.read(profileListFileName) | ||
if (!data) { | ||
const initData = { current: undefined, profiles: {} } | ||
await localStore.write(profileListFileName, JSON.stringify(initData)) | ||
return initData | ||
} | ||
return JSON.parse(data.toString()) | ||
} | ||
|
||
return { | ||
async current() { | ||
const { profiles, current: currentAlias } = await readProfileList() | ||
const current = currentAlias && profiles[currentAlias] | ||
if (!current) { | ||
return undefined | ||
} | ||
return { | ||
alias: currentAlias, | ||
id: current.id, | ||
location: current.location, | ||
} | ||
}, | ||
async setCurrent(alias: string) { | ||
const list = await readProfileList() | ||
if (!list.profiles[alias]) { | ||
throw new Error(`Profile ${alias} doesn't exists`) | ||
} | ||
list.current = alias | ||
await localStore.write(profileListFileName, JSON.stringify(list)) | ||
}, | ||
async list(): Promise<ProfileListing[]> { | ||
return Object.entries((await readProfileList()).profiles).map(([alias, profile]) => ({ alias, ...profile })) | ||
}, | ||
async get(alias: string) { | ||
const { profiles } = await readProfileList() | ||
const locationUrl = profiles[alias]?.location | ||
if (!locationUrl) { | ||
throw new Error(`Profile ${alias} not found`) | ||
} | ||
const tarSnapshotStore = await tarSnapshotFromUrl(locationUrl) | ||
const profileInfo = await profileStore(tarSnapshotStore).info() | ||
return { | ||
info: profileInfo, | ||
store: tarSnapshotStore, | ||
} | ||
}, | ||
async delete(alias: string) { | ||
const list = await readProfileList() | ||
if (!list.profiles[alias]) { | ||
throw new Error(`Profile ${alias} does not exist`) | ||
} | ||
delete list.profiles[alias] | ||
if (list.current === alias) { | ||
list.current = undefined | ||
} | ||
await localStore.write(profileListFileName, JSON.stringify(list)) | ||
}, | ||
async importExisting(alias: string, location: string) { | ||
const list = await readProfileList() | ||
if (list.profiles[alias]) { | ||
throw new Error(`Profile ${alias} already exists`) | ||
} | ||
const tarSnapshotStore = await tarSnapshotFromUrl(location) | ||
const info = await profileStore(tarSnapshotStore).info() | ||
list.profiles[alias] = { | ||
id: info.id, | ||
location, | ||
} | ||
list.current = alias | ||
await localStore.write(profileListFileName, JSON.stringify(list)) | ||
return { | ||
info, | ||
store: tarSnapshotStore, | ||
} | ||
}, | ||
async create(alias: string, location: string, profile: Omit<Profile, 'id'>, init: (store: ProfileStore) => Promise<void>) { | ||
const list = await readProfileList() | ||
if (list.profiles[alias]) { | ||
throw new Error(`Profile ${alias} already exists`) | ||
} | ||
const id = `${alias}-${Math.random().toString(36).substring(2, 9)}` | ||
const tar = await tarSnapshotFromUrl(location) | ||
const pStore = profileStore(tar) | ||
await pStore.init({ id, ...profile }) | ||
list.profiles[alias] = { | ||
id, | ||
location, | ||
} | ||
list.current = alias | ||
await init(pStore) | ||
await localStore.write(profileListFileName, JSON.stringify(list)) | ||
return { | ||
info: { | ||
id, | ||
...profile, | ||
}, | ||
store: tar, | ||
} | ||
}, | ||
} | ||
} | ||
|
||
export type LocalProfilesConfig = ReturnType<typeof localProfilesConfig> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './store' | ||
export * from './types' | ||
export * from './profileConfig' | ||
export * from './profile' | ||
export * from './config' |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.