-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95f87ba
commit 95b14c1
Showing
24 changed files
with
262 additions
and
79 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,41 @@ | ||
--- | ||
layout: doc | ||
|
||
prev: | ||
text: Segmentation | ||
link: /deploy/segmentation | ||
|
||
next: | ||
text: Environments | ||
link: /deploy/environments | ||
|
||
--- | ||
|
||
# Resources | ||
|
||
Resources are objects that maintain state. For example, a database connection or a connection to a file store. These resources are usually machine-wide and could be shared between different segments when they are deployed on the same machine. | ||
|
||
In Jitar's [segmentation model](/deploy/segmentation), each segment is isolated from the others. This means that each segment has its own copy of any shared code. This feature is useful for creating independent deployable packages. However, it also means that resources cannot be shared between segments by default. By defining which modules are resources, Jitar won't duplicate the code and will share the resource between segments. | ||
|
||
### Resource files | ||
|
||
Jitar will search for resource definitions files in the project directory. The files are named `*.resources.json`. Each entry defines the entry point of the `module` that should be used as a resource. | ||
|
||
The file has the following structure: | ||
|
||
```json | ||
// app.resources.json | ||
[ | ||
"./integrations/authentication/entry-file", | ||
"./integrations/database/index", | ||
"./integrations/filestore | ||
] | ||
``` | ||
|
||
::: info | ||
The `/index` part is optional. If the module is a directory, Jitar will automatically look for the index file. | ||
::: | ||
|
||
::: tip | ||
It's possible to define multiple resource files within a project. This is useful in a monorepo setup where different modules might be managed by different teams. | ||
::: |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,53 @@ | ||
|
||
import type { FileManager } from '@jitar/sourcing'; | ||
|
||
import ResourcesList from './models/ResourcesList'; | ||
import FileNotLoaded from './errors/FileNotLoaded'; | ||
import type ResourceFile from './types/File'; | ||
import { FileHelper } from '../../utils'; | ||
|
||
export default class ResourceReader | ||
{ | ||
readonly #fileManager: FileManager; | ||
readonly #fileHelper = new FileHelper(); | ||
|
||
constructor(fileManager: FileManager) | ||
{ | ||
this.#fileManager = fileManager; | ||
} | ||
|
||
async readAll(filenames: string[]): Promise<ResourcesList> | ||
{ | ||
const resources = await Promise.all(filenames.map(filename => this.#loadResourceDefinition(filename))); | ||
|
||
return new ResourcesList(resources.flat()); | ||
} | ||
|
||
async #loadResourceDefinition(filename: string): Promise<ResourceFile> | ||
{ | ||
try | ||
{ | ||
const content = await this.#fileManager.getContent(filename); | ||
|
||
const result = JSON.parse(content.toString()) as ResourceFile; | ||
|
||
return result.map(resource => this.#makeResourceFilename(resource)); | ||
} | ||
catch (error: unknown) | ||
{ | ||
const message = error instanceof Error ? error.message : String(error); | ||
|
||
throw new FileNotLoaded(filename, message); | ||
} | ||
} | ||
|
||
#makeResourceFilename(filename: string): string | ||
{ | ||
const fullFilename = this.#fileHelper.assureExtension(filename); | ||
|
||
if (fullFilename.startsWith('./')) return fullFilename.substring(2); | ||
if (fullFilename.startsWith('/')) return fullFilename.substring(1); | ||
|
||
return fullFilename; | ||
} | ||
} |
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,8 @@ | ||
|
||
export default class FileNotLoaded extends Error | ||
{ | ||
constructor(filename: string, message: string) | ||
{ | ||
super(`Failed to load resource file '${filename}' because of: ${message}`); | ||
} | ||
} |
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,3 @@ | ||
|
||
export { default as ResourcesList } from './models/ResourcesList'; | ||
export { default as ResourceReader } from './Reader'; |
15 changes: 15 additions & 0 deletions
15
packages/build/src/source/resource/models/ResourcesList.ts
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 default class ResourcesList | ||
{ | ||
readonly #resources: string[]; | ||
|
||
constructor(resources: string[]) | ||
{ | ||
this.#resources = resources; | ||
} | ||
|
||
isResourceModule(moduleFilename: string): boolean | ||
{ | ||
return this.#resources.includes(moduleFilename); | ||
} | ||
} |
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,4 @@ | ||
|
||
type File = string[]; | ||
|
||
export default File; |
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.