primate 0.24.0 → 0.26.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.
Files changed (49) hide show
  1. package/package.json +2 -2
  2. package/src/Logger.js +20 -16
  3. package/src/app.js +75 -69
  4. package/src/bin.js +1 -1
  5. package/src/commands/dev.js +1 -1
  6. package/src/commands/exports.js +3 -3
  7. package/src/commands/serve.js +1 -1
  8. package/src/cwd.js +1 -1
  9. package/src/defaults/primate.config.js +1 -1
  10. package/src/dispatch.js +13 -10
  11. package/src/errors.js +1 -1
  12. package/src/errors.json +5 -0
  13. package/src/exports.js +2 -2
  14. package/src/handlers/error.js +4 -4
  15. package/src/handlers/exports.js +7 -7
  16. package/src/handlers/html.js +7 -7
  17. package/src/handlers/json.js +3 -3
  18. package/src/handlers/redirect.js +3 -3
  19. package/src/handlers/stream.js +4 -4
  20. package/src/handlers/text.js +3 -3
  21. package/src/hooks/bundle.js +1 -1
  22. package/src/hooks/copy_includes.js +8 -8
  23. package/src/hooks/exports.js +9 -9
  24. package/src/hooks/handle.js +44 -37
  25. package/src/hooks/init.js +1 -1
  26. package/src/hooks/parse.js +8 -8
  27. package/src/hooks/publish.js +8 -42
  28. package/src/hooks/register.js +73 -2
  29. package/src/hooks/respond/duck.js +1 -1
  30. package/src/hooks/respond/exports.js +2 -2
  31. package/src/hooks/respond/respond.js +4 -4
  32. package/src/hooks/route.js +11 -11
  33. package/src/hooks/serve.js +3 -3
  34. package/src/hooks/stage.js +48 -0
  35. package/src/loaders/common.js +3 -3
  36. package/src/loaders/exports.js +3 -3
  37. package/src/loaders/modules.js +10 -7
  38. package/src/loaders/routes/exports.js +6 -4
  39. package/src/loaders/routes/load.js +2 -2
  40. package/src/loaders/routes/routes.js +2 -2
  41. package/src/loaders/routes.js +16 -15
  42. package/src/loaders/types.js +11 -4
  43. package/src/run.js +8 -8
  44. package/src/start.js +19 -10
  45. package/src/validate.js +1 -1
  46. package/src/hooks/compile.js +0 -45
  47. package/src/loaders/routes/errors.js +0 -3
  48. package/src/loaders/routes/guards.js +0 -3
  49. package/src/loaders/routes/layouts.js +0 -3
@@ -1,15 +1,22 @@
1
- import {Path} from "runtime-compat/fs";
1
+ import { Path } from "rcompat/fs";
2
+ import { is } from "rcompat/invariant";
3
+ import { tryreturn } from "rcompat/sync";
2
4
  import errors from "../errors.js";
3
5
  import fs from "./common.js";
4
6
 
5
7
  const filter = path => /^[a-z]/u.test(path.name);
6
8
 
7
9
  export default async (log, directory, load = fs) => {
8
- const types = await load({log, directory, name: "types", filter});
10
+ const types = await fs({ log, directory, name: "types", filter });
9
11
 
10
12
  const resolve = name => new Path(directory, name);
11
- types.some(([name, type]) => typeof type !== "function"
12
- && errors.InvalidDefaultExport.throw(resolve(`${name}.js`)));
13
+ types.every(([name, type]) => tryreturn(_ => {
14
+ is(type).object();
15
+ is(type.base).string();
16
+ is(type.validate).function();
17
+ return true;
18
+ }).orelse(_ => errors.InvalidTypeExport.throw(resolve(`${name}.js`))),
19
+ );
13
20
 
14
21
  types.every(([name]) =>
15
22
  /^(?:[a-z][^\W_]*)$/u.test(name) || errors.InvalidTypeName.throw(name));
package/src/run.js CHANGED
@@ -1,19 +1,19 @@
1
- import {tryreturn} from "runtime-compat/async";
2
- import {Path} from "runtime-compat/fs";
3
- import {extend} from "runtime-compat/object";
1
+ import { tryreturn } from "rcompat/async";
2
+ import { Path } from "rcompat/fs";
3
+ import { extend } from "rcompat/object";
4
4
  import app from "./app.js";
5
- import {default as Logger, bye} from "./Logger.js";
5
+ import { default as Logger, bye } from "./Logger.js";
6
6
  import errors from "./errors.js";
7
7
  import command from "./commands/exports.js";
8
8
  import defaults from "./defaults/primate.config.js";
9
9
 
10
- let logger = new Logger({level: Logger.Warn});
11
- const {runtime = "node"} = import.meta;
10
+ let logger = new Logger({ level: Logger.Warn });
11
+ const { runtime = "node" } = import.meta;
12
12
 
13
13
  const get_config = async root => {
14
14
  const name = "primate.config.js";
15
15
  const config = root.join(name);
16
- return await config.exists
16
+ return await config.exists()
17
17
  ? tryreturn(async _ => {
18
18
  const imported = (await import(config)).default;
19
19
 
@@ -21,7 +21,7 @@ const get_config = async root => {
21
21
  errors.EmptyConfigFile.warn(logger, config);
22
22
 
23
23
  return extend(defaults, imported);
24
- }).orelse(({message}) =>
24
+ }).orelse(({ message }) =>
25
25
  errors.ErrorInConfigFile.throw(message, `${runtime} ${config}`))
26
26
  : defaults;
27
27
  };
package/src/start.js CHANGED
@@ -1,15 +1,24 @@
1
- import {serve, Response, Status} from "runtime-compat/http";
2
- import {cascade, tryreturn} from "runtime-compat/async";
1
+ import { serve, Response, Status } from "rcompat/http";
2
+ import { cascade, tryreturn } from "rcompat/async";
3
+ import { bold, blue } from "rcompat/colors";
3
4
  import * as hooks from "./hooks/exports.js";
5
+ import { print } from "./Logger.js";
4
6
 
5
- export default async (app$, deactivated = []) => {
7
+ const base_hooks = ["init", "stage", "register", "publish", "bundle"];
8
+
9
+ export default async (app$, mode = "development") => {
10
+ app$.mode = mode;
6
11
  // run one-time hooks
7
12
  let app = app$;
8
- for (const hook of ["init", "register", "compile", "publish", "bundle"]) {
9
- if (deactivated.includes(hook)) {
10
- continue;
11
- }
12
- app.log.info(`running ${hook} hooks`, {module: "primate"});
13
+
14
+ const { http } = app.config;
15
+ const address = `http${app.secure ? "s" : ""}://${http.host}:${http.port}`;
16
+ print(blue(bold(app.name)), blue(app.version), `at ${address}\n`);
17
+
18
+ app.log.info(`in ${bold(mode)} mode`, { module: "primate" });
19
+
20
+ for (const hook of base_hooks) {
21
+ app.log.info(`running ${bold(hook)} hooks`, { module: "primate" });
13
22
  app = await hooks[hook](app);
14
23
  }
15
24
 
@@ -20,9 +29,9 @@ export default async (app$, deactivated = []) => {
20
29
  tryreturn(async _ => (await hooks.handle(app))(await app.parse(request)))
21
30
  .orelse(error => {
22
31
  app.log.auto(error);
23
- return new Response(null, {status: Status.INTERNAL_SERVER_ERROR});
32
+ return new Response(null, { status: Status.INTERNAL_SERVER_ERROR });
24
33
  }),
25
34
  app.config.http);
26
35
 
27
- await (await cascade(app.modules.serve))({...app, server});
36
+ await (await cascade(app.modules.serve))({ ...app, server });
28
37
  };
package/src/validate.js CHANGED
@@ -1,4 +1,4 @@
1
- import {is, maybe} from "runtime-compat/invariant";
1
+ import { is, maybe } from "rcompat/invariant";
2
2
 
3
3
  export default (type, value, name) => {
4
4
  maybe(type.validate).function();
@@ -1,45 +0,0 @@
1
- import {Path} from "runtime-compat/fs";
2
- import {cascade} from "runtime-compat/async";
3
- import copy_includes from "./copy_includes.js";
4
- import cwd from "../cwd.js";
5
-
6
- const html = /^.*.html$/u;
7
- const defaults = cwd(import.meta, 2).join("defaults");
8
-
9
- const pre = async app => {
10
- const {config: {location}, path} = app;
11
-
12
- // remove build directory in case exists
13
- if (await path.build.exists) {
14
- await path.build.file.remove();
15
- }
16
- await Promise.all(["server", "client", "components", "pages"]
17
- .map(directory => app.runpath(directory).file.create()));
18
-
19
- // copy framework pages
20
- await app.stage(defaults, location.pages, html);
21
- // overwrite transformed pages to build
22
- await path.pages.exists && await app.stage(path.pages, location.pages, html);
23
-
24
- if (await path.components.exists) {
25
- // copy all files to build/components
26
- await app.stage(path.components, location.components);
27
- // copy .js files from components to build/server, since frontend
28
- // frameworks handle non-js files
29
- const to = Path.join(location.server, location.components);
30
- await app.stage(path.components, to, /^.*.js$/u);
31
- }
32
-
33
- if (await path.static.exists) {
34
- // copy static files to build/server/static
35
- await app.stage(path.static, new Path(location.server, location.static));
36
- }
37
-
38
- // copy additional subdirectories to build/server
39
- await copy_includes(app, location.server);
40
-
41
- return app;
42
- };
43
-
44
- export default async app =>
45
- (await cascade(app.modules.compile))(await pre(app));
@@ -1,3 +0,0 @@
1
- import load from "./load.js";
2
-
3
- export default load("error");
@@ -1,3 +0,0 @@
1
- import load from "./load.js";
2
-
3
- export default load("guard");
@@ -1,3 +0,0 @@
1
- import load from "./load.js";
2
-
3
- export default load("layout");