@aeriajs/entrypoint 0.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright 2023 João Santos (joaosan177@gmail.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the “Software”), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # `@aeriajs/entrypoint`
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ yarn add @aeriajs/entrypoint
7
+ # or
8
+ $ npm install @aeriajs/entrypoint
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ This is an internal utility, not intended for public usage.
@@ -0,0 +1,6 @@
1
+ import type { Collection, ApiConfig } from '@aeriajs/types';
2
+ export declare const getEntrypoint: () => Promise<any>;
3
+ export declare const getCollections: () => Promise<Record<string, Collection | (() => Collection)>>;
4
+ export declare const getCollection: (collectionName: string) => Promise<Collection | undefined>;
5
+ export declare const getRouter: () => Promise<any>;
6
+ export declare const getConfig: () => Promise<ApiConfig>;
package/dist/index.js ADDED
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getConfig = exports.getRouter = exports.getCollection = exports.getCollections = exports.getEntrypoint = void 0;
7
+ const common_1 = require("@aeriajs/common");
8
+ const path_1 = __importDefault(require("path"));
9
+ const promises_1 = __importDefault(require("fs/promises"));
10
+ let collectionsMemo;
11
+ const collectionMemo = {};
12
+ const getEntrypoint = async () => {
13
+ const { main, aeriaMain } = JSON.parse(await promises_1.default.readFile(path_1.default.join(process.cwd(), 'package.json'), {
14
+ encoding: 'utf8',
15
+ }));
16
+ return (0, common_1.dynamicImport)(path_1.default.join(process.cwd(), aeriaMain || main));
17
+ };
18
+ exports.getEntrypoint = getEntrypoint;
19
+ const internalGetCollections = async () => {
20
+ const entrypoint = await (0, exports.getEntrypoint)();
21
+ const collections = entrypoint.collections
22
+ ? entrypoint.collections
23
+ : entrypoint.default.options.collections;
24
+ return Object.assign({}, collections);
25
+ };
26
+ const getCollections = async () => {
27
+ if (collectionsMemo) {
28
+ return Object.assign({}, collectionsMemo);
29
+ }
30
+ collectionsMemo = await internalGetCollections();
31
+ return collectionsMemo;
32
+ };
33
+ exports.getCollections = getCollections;
34
+ const getCollection = async (collectionName) => {
35
+ if (collectionMemo[collectionName]) {
36
+ return collectionMemo[collectionName];
37
+ }
38
+ const collections = await (0, exports.getCollections)();
39
+ const candidate = collections[collectionName];
40
+ if (!candidate) {
41
+ return;
42
+ }
43
+ const collection = typeof candidate === 'function'
44
+ ? candidate()
45
+ : candidate;
46
+ collectionsMemo[collectionName] = candidate;
47
+ return collection;
48
+ };
49
+ exports.getCollection = getCollection;
50
+ const getRouter = async () => {
51
+ const entrypoint = await (0, exports.getEntrypoint)();
52
+ return entrypoint.router;
53
+ };
54
+ exports.getRouter = getRouter;
55
+ const getConfig = async () => {
56
+ const entrypoint = await (0, exports.getEntrypoint)();
57
+ return entrypoint.default
58
+ ? entrypoint.default.options.config
59
+ : {};
60
+ };
61
+ exports.getConfig = getConfig;
package/dist/index.mjs ADDED
@@ -0,0 +1,292 @@
1
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
+ try {
3
+ var info = gen[key](arg);
4
+ var value = info.value;
5
+ } catch (error) {
6
+ reject(error);
7
+ return;
8
+ }
9
+ if (info.done) {
10
+ resolve(value);
11
+ } else {
12
+ Promise.resolve(value).then(_next, _throw);
13
+ }
14
+ }
15
+ function _async_to_generator(fn) {
16
+ return function() {
17
+ var self = this, args = arguments;
18
+ return new Promise(function(resolve, reject) {
19
+ var gen = fn.apply(self, args);
20
+ function _next(value) {
21
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
+ }
23
+ function _throw(err) {
24
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
+ }
26
+ _next(undefined);
27
+ });
28
+ };
29
+ }
30
+ function _ts_generator(thisArg, body) {
31
+ var f, y, t, g, _ = {
32
+ label: 0,
33
+ sent: function() {
34
+ if (t[0] & 1) throw t[1];
35
+ return t[1];
36
+ },
37
+ trys: [],
38
+ ops: []
39
+ };
40
+ return g = {
41
+ next: verb(0),
42
+ "throw": verb(1),
43
+ "return": verb(2)
44
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
45
+ return this;
46
+ }), g;
47
+ function verb(n) {
48
+ return function(v) {
49
+ return step([
50
+ n,
51
+ v
52
+ ]);
53
+ };
54
+ }
55
+ function step(op) {
56
+ if (f) throw new TypeError("Generator is already executing.");
57
+ while(_)try {
58
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
59
+ if (y = 0, t) op = [
60
+ op[0] & 2,
61
+ t.value
62
+ ];
63
+ switch(op[0]){
64
+ case 0:
65
+ case 1:
66
+ t = op;
67
+ break;
68
+ case 4:
69
+ _.label++;
70
+ return {
71
+ value: op[1],
72
+ done: false
73
+ };
74
+ case 5:
75
+ _.label++;
76
+ y = op[1];
77
+ op = [
78
+ 0
79
+ ];
80
+ continue;
81
+ case 7:
82
+ op = _.ops.pop();
83
+ _.trys.pop();
84
+ continue;
85
+ default:
86
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
87
+ _ = 0;
88
+ continue;
89
+ }
90
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
91
+ _.label = op[1];
92
+ break;
93
+ }
94
+ if (op[0] === 6 && _.label < t[1]) {
95
+ _.label = t[1];
96
+ t = op;
97
+ break;
98
+ }
99
+ if (t && _.label < t[2]) {
100
+ _.label = t[2];
101
+ _.ops.push(op);
102
+ break;
103
+ }
104
+ if (t[2]) _.ops.pop();
105
+ _.trys.pop();
106
+ continue;
107
+ }
108
+ op = body.call(thisArg, _);
109
+ } catch (e) {
110
+ op = [
111
+ 6,
112
+ e
113
+ ];
114
+ y = 0;
115
+ } finally{
116
+ f = t = 0;
117
+ }
118
+ if (op[0] & 5) throw op[1];
119
+ return {
120
+ value: op[0] ? op[1] : void 0,
121
+ done: true
122
+ };
123
+ }
124
+ }
125
+ import { dynamicImport } from "@aeriajs/common";
126
+ import path from "path";
127
+ import fs from "fs/promises";
128
+ var collectionsMemo;
129
+ var collectionMemo = {};
130
+ export var getEntrypoint = function() {
131
+ var _ref = _async_to_generator(function() {
132
+ var _JSON_parse, main, aeriaMain, _;
133
+ return _ts_generator(this, function(_state) {
134
+ switch(_state.label){
135
+ case 0:
136
+ _ = JSON.parse;
137
+ return [
138
+ 4,
139
+ fs.readFile(path.join(process.cwd(), "package.json"), {
140
+ encoding: "utf8"
141
+ })
142
+ ];
143
+ case 1:
144
+ _JSON_parse = _.apply(JSON, [
145
+ _state.sent()
146
+ ]), main = _JSON_parse.main, aeriaMain = _JSON_parse.aeriaMain;
147
+ return [
148
+ 2,
149
+ dynamicImport(path.join(process.cwd(), aeriaMain || main))
150
+ ];
151
+ }
152
+ });
153
+ });
154
+ return function getEntrypoint() {
155
+ return _ref.apply(this, arguments);
156
+ };
157
+ }();
158
+ var internalGetCollections = function() {
159
+ var _ref = _async_to_generator(function() {
160
+ var entrypoint, collections;
161
+ return _ts_generator(this, function(_state) {
162
+ switch(_state.label){
163
+ case 0:
164
+ return [
165
+ 4,
166
+ getEntrypoint()
167
+ ];
168
+ case 1:
169
+ entrypoint = _state.sent();
170
+ collections = entrypoint.collections ? entrypoint.collections : entrypoint.default.options.collections;
171
+ return [
172
+ 2,
173
+ Object.assign({}, collections)
174
+ ];
175
+ }
176
+ });
177
+ });
178
+ return function internalGetCollections() {
179
+ return _ref.apply(this, arguments);
180
+ };
181
+ }();
182
+ export var getCollections = function() {
183
+ var _ref = _async_to_generator(function() {
184
+ return _ts_generator(this, function(_state) {
185
+ switch(_state.label){
186
+ case 0:
187
+ if (collectionsMemo) {
188
+ return [
189
+ 2,
190
+ Object.assign({}, collectionsMemo)
191
+ ];
192
+ }
193
+ return [
194
+ 4,
195
+ internalGetCollections()
196
+ ];
197
+ case 1:
198
+ collectionsMemo = _state.sent();
199
+ return [
200
+ 2,
201
+ collectionsMemo
202
+ ];
203
+ }
204
+ });
205
+ });
206
+ return function getCollections() {
207
+ return _ref.apply(this, arguments);
208
+ };
209
+ }();
210
+ export var getCollection = function() {
211
+ var _ref = _async_to_generator(function(collectionName) {
212
+ var collections, candidate, collection;
213
+ return _ts_generator(this, function(_state) {
214
+ switch(_state.label){
215
+ case 0:
216
+ if (collectionMemo[collectionName]) {
217
+ return [
218
+ 2,
219
+ collectionMemo[collectionName]
220
+ ];
221
+ }
222
+ return [
223
+ 4,
224
+ getCollections()
225
+ ];
226
+ case 1:
227
+ collections = _state.sent();
228
+ candidate = collections[collectionName];
229
+ if (!candidate) {
230
+ return [
231
+ 2
232
+ ];
233
+ }
234
+ collection = typeof candidate === "function" ? candidate() : candidate;
235
+ collectionsMemo[collectionName] = candidate;
236
+ return [
237
+ 2,
238
+ collection
239
+ ];
240
+ }
241
+ });
242
+ });
243
+ return function getCollection(collectionName) {
244
+ return _ref.apply(this, arguments);
245
+ };
246
+ }();
247
+ export var getRouter = function() {
248
+ var _ref = _async_to_generator(function() {
249
+ var entrypoint;
250
+ return _ts_generator(this, function(_state) {
251
+ switch(_state.label){
252
+ case 0:
253
+ return [
254
+ 4,
255
+ getEntrypoint()
256
+ ];
257
+ case 1:
258
+ entrypoint = _state.sent();
259
+ return [
260
+ 2,
261
+ entrypoint.router
262
+ ];
263
+ }
264
+ });
265
+ });
266
+ return function getRouter() {
267
+ return _ref.apply(this, arguments);
268
+ };
269
+ }();
270
+ export var getConfig = function() {
271
+ var _ref = _async_to_generator(function() {
272
+ var entrypoint;
273
+ return _ts_generator(this, function(_state) {
274
+ switch(_state.label){
275
+ case 0:
276
+ return [
277
+ 4,
278
+ getEntrypoint()
279
+ ];
280
+ case 1:
281
+ entrypoint = _state.sent();
282
+ return [
283
+ 2,
284
+ entrypoint.default ? entrypoint.default.options.config : {}
285
+ ];
286
+ }
287
+ });
288
+ });
289
+ return function getConfig() {
290
+ return _ref.apply(this, arguments);
291
+ };
292
+ }();
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@aeriajs/entrypoint",
3
+ "version": "0.0.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "keywords": [],
12
+ "author": "",
13
+ "license": "ISC",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.mjs",
17
+ "require": "./dist/index.js",
18
+ "types": "./dist/index.d.ts"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "peerDependencies": {
25
+ "@aeriajs/common": "^0.0.0",
26
+ "@aeriajs/types": "^0.0.0"
27
+ },
28
+ "scripts": {
29
+ "test": "echo skipping",
30
+ "lint": "eslint src",
31
+ "lint:fix": "eslint src --fix",
32
+ "build": "pnpm build:cjs && pnpm build:esm",
33
+ "build:cjs": "tsc",
34
+ "build:esm": "swc src/* -d dist --strip-leading-paths -C module.type=es6 --out-file-extension=mjs && pnpm build:esm-transform",
35
+ "build:esm-transform": "pnpm -w esm-transform $PWD/dist"
36
+ }
37
+ }