velocious 1.0.486 → 1.0.487

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ // @ts-check
2
+
3
+ import restArgsError from "../utils/rest-args-error.js"
4
+
5
+ /**
6
+ * A Velocious package (engine): an external npm package that contributes data
7
+ * models, frontend-model resources and migrations to a consuming app. The app
8
+ * lists packages in `Configuration({packages: [...]})`; the framework then loads
9
+ * the package's `src/models`, discovers its `src/resources`, runs its
10
+ * `src/database/migrations`, and generates its frontend models into the app.
11
+ */
12
+ export default class VelociousPackage {
13
+ /**
14
+ * Wraps a plain descriptor as a VelociousPackage (or returns it unchanged when
15
+ * it already is one), so packages can be listed without importing this class.
16
+ * @param {VelociousPackage | import("../configuration-types.js").VelociousPackageDescriptor} descriptor - Package or plain descriptor.
17
+ * @returns {VelociousPackage} - The package instance.
18
+ */
19
+ static from(descriptor) {
20
+ if (descriptor instanceof VelociousPackage) {
21
+ return descriptor
22
+ }
23
+
24
+ return new VelociousPackage(descriptor)
25
+ }
26
+
27
+ /**
28
+ * Derives the containing directory of a module url without Node's `path`/`url`
29
+ * modules, so this class stays safe to import in browser/Expo bundles.
30
+ * @param {string} url - A module url (usually `import.meta.url`).
31
+ * @returns {string} - The directory path that contains the module.
32
+ */
33
+ static _directoryFromUrl(url) {
34
+ const directoryUrl = new URL(".", url)
35
+
36
+ return decodeURIComponent(directoryUrl.pathname).replace(/\/$/, "")
37
+ }
38
+
39
+ /**
40
+ * Runs constructor.
41
+ * @param {import("../configuration-types.js").VelociousPackageDescriptor} args - Package descriptor.
42
+ */
43
+ constructor({name, url, path, modelsPath, resourcesPath, migrationsPath, ...restArgs}) {
44
+ restArgsError(restArgs)
45
+
46
+ if (!name) {
47
+ throw new Error("A velocious package requires a name.")
48
+ }
49
+
50
+ if (!path && !url) {
51
+ throw new Error(`Velocious package "${name}" requires a "path" or a "url" (usually import.meta.url).`)
52
+ }
53
+
54
+ this._name = name
55
+ this._path = path || VelociousPackage._directoryFromUrl(/** @type {string} */ (url))
56
+ this._modelsPath = modelsPath
57
+ this._resourcesPath = resourcesPath
58
+ this._migrationsPath = migrationsPath
59
+ }
60
+
61
+ /**
62
+ * Runs get name.
63
+ * @returns {string} - The package name.
64
+ */
65
+ getName() {
66
+ return this._name
67
+ }
68
+
69
+ /**
70
+ * Runs get path.
71
+ * @returns {string} - The package root directory (the one that contains `src`).
72
+ */
73
+ getPath() {
74
+ return this._path
75
+ }
76
+
77
+ /**
78
+ * Runs get models path.
79
+ * @returns {string} - The package's models directory.
80
+ */
81
+ getModelsPath() {
82
+ return this._modelsPath || `${this._path}/src/models`
83
+ }
84
+
85
+ /**
86
+ * Runs get resources path.
87
+ * @returns {string} - The package's frontend-model resources directory.
88
+ */
89
+ getResourcesPath() {
90
+ return this._resourcesPath || `${this._path}/src/resources`
91
+ }
92
+
93
+ /**
94
+ * Runs get migrations path.
95
+ * @returns {string} - The package's migrations directory.
96
+ */
97
+ getMigrationsPath() {
98
+ return this._migrationsPath || `${this._path}/src/database/migrations`
99
+ }
100
+
101
+ /**
102
+ * Derives the internal backend-project entry the framework appends so the
103
+ * existing resource-discovery + frontend-model generation machinery picks up
104
+ * this package. Generated frontend models are written to the app's output.
105
+ * @param {{frontendModelsOutputPath: string | undefined}} args - The app's frontend-models output path.
106
+ * @returns {import("../configuration-types.js").BackendProjectConfiguration} - The derived backend project.
107
+ */
108
+ toBackendProjectConfiguration({frontendModelsOutputPath}) {
109
+ return {
110
+ frontendModelsOutputPath,
111
+ path: this.getPath(),
112
+ resourcesPath: this.getResourcesPath()
113
+ }
114
+ }
115
+ }