-
Notifications
You must be signed in to change notification settings - Fork 3
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 #124 from zetkin/issue-25/data-access-layer
Extract data access layer
- Loading branch information
Showing
4 changed files
with
89 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Cache } from '@/Cache'; | ||
import MessageAdapterFactory from '@/utils/adapters/MessageAdapterFactory'; | ||
import { RepoGit } from '@/RepoGit'; | ||
import { ServerConfig, ServerProjectConfig } from '@/utils/serverConfig'; | ||
|
||
export async function accessProjects() { | ||
const serverConfig = await ServerConfig.read(); | ||
return serverConfig.projects.map(async (project) => { | ||
return await readProject(project); | ||
}); | ||
} | ||
|
||
export async function accessProject(name: string) { | ||
const serverConfig = await ServerConfig.read(); | ||
const project = serverConfig.projects.find( | ||
(project) => project.name === name, | ||
); | ||
|
||
if (!project) { | ||
return null; | ||
} | ||
|
||
return readProject(project); | ||
} | ||
|
||
async function readProject(project: ServerProjectConfig) { | ||
await RepoGit.cloneIfNotExist(project); | ||
const repoGit = await RepoGit.getRepoGit(project); | ||
const lyraConfig = await repoGit.getLyraConfig(); | ||
const projectConfig = lyraConfig.getProjectConfigByPath(project.projectPath); | ||
const msgAdapter = MessageAdapterFactory.createAdapter(projectConfig); | ||
const messages = await msgAdapter.getMessages(); | ||
const store = await Cache.getProjectStore(projectConfig); | ||
const languagesWithTranslations = projectConfig.languages.map( | ||
async (lang) => { | ||
const translations = await store.getTranslations(lang); | ||
return { lang, translations }; | ||
}, | ||
); | ||
return { languagesWithTranslations, messages, name: project.name }; | ||
} |
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,15 @@ | ||
export class Promises<T> { | ||
private constructor(private array: Array<Promise<T>>) {} | ||
|
||
static of<T>(array: Array<Promise<T>>) { | ||
return new Promises(array); | ||
} | ||
|
||
map<U>(callbackFn: (value: T) => U | Promise<U>): Promises<U> { | ||
return new Promises(this.array.map((p) => p.then(callbackFn))); | ||
} | ||
|
||
all() { | ||
return Promise.all(this.array); | ||
} | ||
} |