primate 0.16.3 → 0.18.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/src/run.js CHANGED
@@ -1,4 +1,55 @@
1
+ import {Path} from "runtime-compat/fs";
1
2
  import app from "./app.js";
3
+ import {default as Logger, bye} from "./Logger.js";
4
+ import extend from "./extend.js";
5
+ import errors from "./errors.js";
2
6
  import command from "./commands/exports.js";
7
+ import defaults from "./defaults/primate.config.js";
3
8
 
4
- export default async name => command(name)(await app());
9
+ const getRoot = async () => {
10
+ try {
11
+ // use module root if possible
12
+ return await Path.root();
13
+ } catch (error) {
14
+ // fall back to current directory
15
+ return Path.resolve();
16
+ }
17
+ };
18
+
19
+ const protologger = new Logger({level: Logger.Warn});
20
+
21
+ const getConfig = async root => {
22
+ const name = "primate.config.js";
23
+ const config = root.join(name);
24
+ if (await config.exists) {
25
+ try {
26
+ const imported = (await import(config)).default;
27
+
28
+ (imported === undefined || Object.keys(imported).length === 0) &&
29
+ errors.EmptyConfigFile.warn(protologger, {config});
30
+
31
+ return extend(defaults, imported);
32
+ } catch ({message}) {
33
+ return errors.ErrorInConfigFile.throw({message, config});
34
+ }
35
+ } else {
36
+ return defaults;
37
+ }
38
+ };
39
+
40
+ export default async name => {
41
+ const root = await getRoot();
42
+ let logger = protologger;
43
+ try {
44
+ const config = await getConfig(root);
45
+ logger = new Logger(config.logger);
46
+ await command(name)(await app(config, root, new Logger(config.logger)));
47
+ } catch (error) {
48
+ if (error.level === Logger.Error) {
49
+ logger.auto(error);
50
+ bye();
51
+ } else {
52
+ throw error;
53
+ }
54
+ }
55
+ };
package/src/start.js CHANGED
@@ -1,4 +1,6 @@
1
- import {register, compile, publish, bundle, route, handle}
1
+ import {serve, Response} from "runtime-compat/http";
2
+ import {InternalServerError} from "./http-statuses.js";
3
+ import {register, compile, publish, bundle, route, handle, parse}
2
4
  from "./hooks/exports.js";
3
5
 
4
6
  export default async (app, operations = {}) => {
@@ -15,6 +17,15 @@ export default async (app, operations = {}) => {
15
17
  // bundle client-side code
16
18
  await bundle(app, operations?.bundle);
17
19
 
18
- // handle
19
- await handle({router: await route(app), ...app});
20
+ const _route = route(app);
21
+
22
+ serve(async request => {
23
+ try {
24
+ // parse, handle
25
+ return await handle({...app, route: _route})(await parse(request));
26
+ } catch(error) {
27
+ app.log.auto(error);
28
+ return new Response(null, {status: InternalServerError});
29
+ }
30
+ }, app.config.http);
20
31
  };
@@ -1 +0,0 @@
1
- export {default} from "./serve.js";
@@ -1,42 +0,0 @@
1
- import {Path} from "runtime-compat/fs";
2
-
3
- const createModule = async app => {
4
- const space = 2;
5
- try {
6
- // will throw if cannot find a package.json up the filesystem hierarchy
7
- await Path.root();
8
- } catch (error) {
9
- const rootConfig = JSON.stringify({
10
- name: "primate-app",
11
- private: true,
12
- dependencies: {
13
- primate: `^${app.version}`,
14
- },
15
- scripts: {
16
- start: "npx primate",
17
- dev: "npx primate dev",
18
- serve: "npx primate serve",
19
- },
20
- type: "module",
21
- }, null, space);
22
- await Path.resolve().join("package.json").file.write(rootConfig);
23
- }
24
- };
25
-
26
- const createConfig = async app => {
27
- const name = "primate.config.js";
28
- const template = "export default {};";
29
- const root = (await Path.root()).join(name);
30
- if (await root.exists) {
31
- app.log.warn(`${root} already exists`);
32
- } else {
33
- await root.file.write(template);
34
- app.log.info(`created config at ${root}`);
35
- }
36
- };
37
-
38
- export default async app => {
39
- await createModule(app);
40
- await createConfig(app);
41
- };
42
-
@@ -1,3 +0,0 @@
1
- export default app => {
2
- app.log.info("available commands: create dev serve");
3
- };
@@ -1 +0,0 @@
1
- export {default as http404} from "./http404.js";
@@ -1,6 +0,0 @@
1
- export default (body = "Not found") => (_, headers) => [
2
- body, {
3
- status: 404,
4
- headers: {...headers, "Content-Type": "text/html"},
5
- },
6
- ];
@@ -1,5 +0,0 @@
1
- export default {
2
- OK: 200,
3
- Found: 302,
4
- InternalServerError: 500,
5
- };