@monkeyplus/flow 4.0.0-beta.1 → 4.0.0-beta.10
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/dist/index.cjs +198 -8
- package/dist/index.d.ts +9 -2
- package/dist/index.mjs +198 -9
- package/package.json +6 -2
- package/types/flow.d.ts +5 -0
package/dist/index.cjs
CHANGED
|
@@ -10,6 +10,9 @@ const boom = require('@hapi/boom');
|
|
|
10
10
|
const os = require('os');
|
|
11
11
|
const chalk = require('chalk');
|
|
12
12
|
const fs = require('fs-extra');
|
|
13
|
+
const fs$1 = require('fs');
|
|
14
|
+
const hookable = require('hookable');
|
|
15
|
+
const chokidar = require('chokidar');
|
|
13
16
|
|
|
14
17
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
15
18
|
|
|
@@ -31,9 +34,11 @@ const R__namespace = /*#__PURE__*/_interopNamespace(R);
|
|
|
31
34
|
const os__default = /*#__PURE__*/_interopDefaultLegacy(os);
|
|
32
35
|
const chalk__default = /*#__PURE__*/_interopDefaultLegacy(chalk);
|
|
33
36
|
const fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
37
|
+
const fs__default$1 = /*#__PURE__*/_interopDefaultLegacy(fs$1);
|
|
38
|
+
const chokidar__default = /*#__PURE__*/_interopDefaultLegacy(chokidar);
|
|
34
39
|
|
|
35
40
|
const name = "@monkeyplus/flow";
|
|
36
|
-
const version = "4.0.0-beta.
|
|
41
|
+
const version = "4.0.0-beta.10";
|
|
37
42
|
const description = "Utils hapi";
|
|
38
43
|
const author = "Andres Navarrete";
|
|
39
44
|
const license = "MIT";
|
|
@@ -58,21 +63,24 @@ const scripts = {
|
|
|
58
63
|
lint: "eslint --ext .js,.ts .",
|
|
59
64
|
fix: "eslint --fix --ext .ts .",
|
|
60
65
|
prepublishOnly: "pnpm run build",
|
|
61
|
-
publish: "pnpm publish",
|
|
62
66
|
start: "esno src/index.ts",
|
|
63
67
|
test: "vitest"
|
|
64
68
|
};
|
|
65
69
|
const dependencies$1 = {
|
|
66
70
|
"@hapi/boom": "9.x.x",
|
|
67
71
|
"@hapi/hoek": "9.x.x",
|
|
68
|
-
chalk: "^5.0.0",
|
|
69
72
|
consola: "^2.15.3",
|
|
73
|
+
chalk: "^4.1.2",
|
|
74
|
+
chokidar: "^3.5.3",
|
|
75
|
+
hookable: "^5.1.1",
|
|
70
76
|
"fs-extra": "^10.0.0",
|
|
71
77
|
ramda: "^0.28.0"
|
|
72
78
|
};
|
|
73
79
|
const devDependencies = {
|
|
74
80
|
"@types/fs-extra": "^9.0.13",
|
|
75
81
|
"@types/hapi__hapi": "^20.0.10",
|
|
82
|
+
"@types/hapi__nes": "^11.0.5",
|
|
83
|
+
"@types/hapi__vision": "^5.5.3",
|
|
76
84
|
"@types/ramda": "^0.27.64"
|
|
77
85
|
};
|
|
78
86
|
const peerDependencies = {
|
|
@@ -99,8 +107,13 @@ const pkg = {
|
|
|
99
107
|
|
|
100
108
|
const logger$1 = consola__default.withScope(pkg.name);
|
|
101
109
|
const dependencies = {
|
|
102
|
-
"@hapi/vision": "6.x.x"
|
|
110
|
+
"@hapi/vision": "6.x.x",
|
|
111
|
+
"@hapi/inert": "6.x.x",
|
|
112
|
+
"@hapi/h2o2": "9.x.x",
|
|
113
|
+
"@hapi/nes": "12.x.x"
|
|
103
114
|
};
|
|
115
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
116
|
+
const isGenerate = process.env.GENERATE;
|
|
104
117
|
|
|
105
118
|
const LifeCircle = {
|
|
106
119
|
register: (server) => {
|
|
@@ -314,7 +327,7 @@ const handlerPage = async (req, h) => {
|
|
|
314
327
|
return context;
|
|
315
328
|
}
|
|
316
329
|
context.utils = utils;
|
|
317
|
-
return h.view(`${configs.dirTemplates}${flow.view.
|
|
330
|
+
return h.view(`${configs.dirTemplates}${flow.view.layout || "default"}`, context);
|
|
318
331
|
};
|
|
319
332
|
|
|
320
333
|
const logger = consola__default.withScope("@monkeyplus/flow");
|
|
@@ -322,6 +335,10 @@ const defaults = {
|
|
|
322
335
|
locales: ["es-ec"],
|
|
323
336
|
defaultUbication: "ec",
|
|
324
337
|
defaultLanguage: "es",
|
|
338
|
+
staticDir: "static",
|
|
339
|
+
hmr: {
|
|
340
|
+
dirs: ["views"]
|
|
341
|
+
},
|
|
325
342
|
publicPath: "/",
|
|
326
343
|
locale: "es-ec",
|
|
327
344
|
relativeTo: "",
|
|
@@ -693,6 +710,47 @@ const definePage = (pageOptions) => (levelOptions) => (configs) => {
|
|
|
693
710
|
return routes;
|
|
694
711
|
};
|
|
695
712
|
|
|
713
|
+
const readPagesDir = (pathDir, folder) => {
|
|
714
|
+
const p = fs__default$1.readdirSync(pathDir);
|
|
715
|
+
return R.flatten(p.filter((f) => !f.includes(".js.map") && !f.includes(" copy.js.map") && !f.includes(" copy.js") && !f.includes(".model")).map((e) => {
|
|
716
|
+
if (e.includes(".js") || e.includes(".ts")) {
|
|
717
|
+
return {
|
|
718
|
+
path: path__default.join(pathDir, e),
|
|
719
|
+
prefix: folder
|
|
720
|
+
};
|
|
721
|
+
} else {
|
|
722
|
+
return readPagesDir(path__default.join(pathDir, e), e);
|
|
723
|
+
}
|
|
724
|
+
}));
|
|
725
|
+
};
|
|
726
|
+
const registerPages = async (server, dir) => {
|
|
727
|
+
const dirPages = path__default.resolve(dir || "./pages");
|
|
728
|
+
const listFiles = readPagesDir(dirPages);
|
|
729
|
+
const listPrePages = [];
|
|
730
|
+
for (const item of listFiles) {
|
|
731
|
+
try {
|
|
732
|
+
const module = require(item.path)?.default;
|
|
733
|
+
if (typeof module === "function") {
|
|
734
|
+
listPrePages.push(module());
|
|
735
|
+
} else if (typeof module === "object") {
|
|
736
|
+
if (Array.isArray(module)) {
|
|
737
|
+
module.forEach((el) => listPrePages.push(el()));
|
|
738
|
+
} else {
|
|
739
|
+
const _pages = await module.pages({ server });
|
|
740
|
+
if (Array.isArray(_pages))
|
|
741
|
+
_pages.forEach((page) => listPrePages.push(definePage(page)()));
|
|
742
|
+
else
|
|
743
|
+
listPrePages.push(definePage(_pages)());
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
} catch (details) {
|
|
747
|
+
logger$1.error(details);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
for (const page of listPrePages)
|
|
751
|
+
await server.flow.addPage(page);
|
|
752
|
+
};
|
|
753
|
+
|
|
696
754
|
const RunMethods = {
|
|
697
755
|
register: (server) => {
|
|
698
756
|
const { flow: state } = server.app;
|
|
@@ -706,7 +764,7 @@ const RunMethods = {
|
|
|
706
764
|
server.route(route);
|
|
707
765
|
};
|
|
708
766
|
const addPage = (_page, extra) => {
|
|
709
|
-
const
|
|
767
|
+
const registerPages2 = (_pages2) => {
|
|
710
768
|
for (const _route of _pages2) {
|
|
711
769
|
if (state.routes[_route.path])
|
|
712
770
|
logger$1.warn("The route %s alredy exist", _route.path);
|
|
@@ -717,13 +775,14 @@ const RunMethods = {
|
|
|
717
775
|
let _pages;
|
|
718
776
|
if (typeof _page === "function") {
|
|
719
777
|
_pages = _page(config);
|
|
720
|
-
|
|
778
|
+
registerPages2(_pages);
|
|
721
779
|
} else {
|
|
722
780
|
_pages = definePage(_page)()(config);
|
|
723
|
-
|
|
781
|
+
registerPages2(_pages);
|
|
724
782
|
}
|
|
725
783
|
};
|
|
726
784
|
const init = async () => {
|
|
785
|
+
await registerPages(server);
|
|
727
786
|
const pages = server.app.flow.routes;
|
|
728
787
|
for (const key in pages) {
|
|
729
788
|
const page = pages[key];
|
|
@@ -740,6 +799,136 @@ const RunMethods = {
|
|
|
740
799
|
}
|
|
741
800
|
};
|
|
742
801
|
|
|
802
|
+
const createHmr = (options) => {
|
|
803
|
+
const state = {
|
|
804
|
+
dir: options.relativeTo || "",
|
|
805
|
+
dirs: options.dirs || [],
|
|
806
|
+
extensions: options.extensions || ["eta"]
|
|
807
|
+
};
|
|
808
|
+
let watcher;
|
|
809
|
+
const hooks = hookable.createHooks();
|
|
810
|
+
const watch = () => {
|
|
811
|
+
watcher = chokidar__default.watch(state.dirs.map((d) => path__default.join(d, `**/*.(${state.extensions.join("|")})`)), {
|
|
812
|
+
cwd: state.dir,
|
|
813
|
+
ignoreInitial: true,
|
|
814
|
+
ignored: "node_modules/**/*"
|
|
815
|
+
}).on("change", (path2) => {
|
|
816
|
+
logger$1.info("Changed file", path2);
|
|
817
|
+
hooks.callHook("page:refresh");
|
|
818
|
+
});
|
|
819
|
+
};
|
|
820
|
+
watch();
|
|
821
|
+
return { watcher, hooks };
|
|
822
|
+
};
|
|
823
|
+
|
|
824
|
+
const RegisterCommon = async (server, configs) => {
|
|
825
|
+
server.route({
|
|
826
|
+
method: "GET",
|
|
827
|
+
path: "/{param*}",
|
|
828
|
+
options: {
|
|
829
|
+
ext: {
|
|
830
|
+
onPreResponse: {
|
|
831
|
+
method: (req, h) => {
|
|
832
|
+
return h.continue;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
},
|
|
837
|
+
handler: {
|
|
838
|
+
directory: {
|
|
839
|
+
path: configs.staticDir,
|
|
840
|
+
listing: true
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
});
|
|
844
|
+
if (!isGenerate) {
|
|
845
|
+
const hmr = createHmr({
|
|
846
|
+
dirs: configs.hmr.dirs,
|
|
847
|
+
relativeTo: configs.relativeTo
|
|
848
|
+
});
|
|
849
|
+
hmr.hooks.hook("page:refresh", () => {
|
|
850
|
+
setTimeout(() => {
|
|
851
|
+
logger$1.info("Refresh page");
|
|
852
|
+
server.publish("/_flow/hmr", { reload: true });
|
|
853
|
+
}, 40);
|
|
854
|
+
});
|
|
855
|
+
server.subscription("/_flow/hmr");
|
|
856
|
+
server.route({
|
|
857
|
+
path: "/_flow/hmr/client.js",
|
|
858
|
+
method: "get",
|
|
859
|
+
handler: {
|
|
860
|
+
file: {
|
|
861
|
+
path: path.join(__dirname, "../resources/client"),
|
|
862
|
+
confine: false
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
});
|
|
866
|
+
server.route({
|
|
867
|
+
path: "/_flow/hmr/connect.js",
|
|
868
|
+
method: "get",
|
|
869
|
+
handler: {
|
|
870
|
+
file: {
|
|
871
|
+
path: path.join(__dirname, "../resources/ws.js"),
|
|
872
|
+
confine: false
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
});
|
|
876
|
+
logger$1.debug("Enable development routes");
|
|
877
|
+
server.route({
|
|
878
|
+
path: "/_flow/sitemap",
|
|
879
|
+
method: "get",
|
|
880
|
+
handler: async () => {
|
|
881
|
+
const { pages: getPages } = server.methods.flow;
|
|
882
|
+
const urls = await getPages();
|
|
883
|
+
const pages = Object.values(urls).map((p) => R.dissoc("context", p));
|
|
884
|
+
return {
|
|
885
|
+
total: pages.length,
|
|
886
|
+
pages
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
});
|
|
890
|
+
server.route({
|
|
891
|
+
path: "/_flow/locales",
|
|
892
|
+
method: "get",
|
|
893
|
+
handler: async (req) => {
|
|
894
|
+
const { pages: getPages } = server.methods.flow;
|
|
895
|
+
const urls = await getPages();
|
|
896
|
+
const pages = Object.values(urls).map((p) => R.dissoc("context", p));
|
|
897
|
+
const locales = R.groupBy((page) => page.locale, pages);
|
|
898
|
+
return {
|
|
899
|
+
locales: req.server.plugins.flow.configs.locales,
|
|
900
|
+
all: R.mapObjIndexed((l) => {
|
|
901
|
+
return {
|
|
902
|
+
total: l.length,
|
|
903
|
+
pages: l
|
|
904
|
+
};
|
|
905
|
+
}, locales)
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
});
|
|
909
|
+
server.route({
|
|
910
|
+
path: "/_flow/configs",
|
|
911
|
+
method: "get",
|
|
912
|
+
handler: (req) => {
|
|
913
|
+
return req.server.plugins.flow.configs;
|
|
914
|
+
}
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
const HMR = {
|
|
918
|
+
head: ' <script src="/_flow/hmr/client.js"><\/script>',
|
|
919
|
+
body: ' <script src="/_flow/hmr/connect.js"><\/script>'
|
|
920
|
+
};
|
|
921
|
+
server.ext("onPreHandler", async (req, h) => {
|
|
922
|
+
if (req.route.settings.plugins?.generate === ".html") {
|
|
923
|
+
const { global } = req.plugins.flow;
|
|
924
|
+
Object.assign(global, {
|
|
925
|
+
hmr: isProduction || isGenerate ? { head: "", body: "" } : HMR
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
return h.continue;
|
|
929
|
+
});
|
|
930
|
+
};
|
|
931
|
+
|
|
743
932
|
const plugin = {
|
|
744
933
|
pkg,
|
|
745
934
|
dependencies,
|
|
@@ -864,6 +1053,7 @@ const plugin = {
|
|
|
864
1053
|
return baseOptions;
|
|
865
1054
|
}
|
|
866
1055
|
};
|
|
1056
|
+
await RegisterCommon(server, config);
|
|
867
1057
|
}
|
|
868
1058
|
};
|
|
869
1059
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Plugin } from '@hapi/hapi';
|
|
1
|
+
import { Server, Plugin } from '@hapi/hapi';
|
|
2
2
|
import { PageInfo } from '../types/core';
|
|
3
3
|
import { OptionsHapiPage, RunPreDefinePage, Flow, FlowExtensions, OptionDynamicLocalePages, GenerateOptions, FlowServerMethods, FlowOptions } from '../types';
|
|
4
4
|
export { ContextHandler, Contexts, Extension, Flow, FlowContextPage, FlowContextSeo, FlowContextView, FlowEngines, FlowExtensions, FlowOptionPlugins, FlowOptions, FlowPluginExtension, FlowPluginGlobal, FlowPluginLocal, FlowPluginUtils, FlowServerMethods, FlowStatePlugins, GenerateFolder, OptionDynamicLocalePages, OptionsDynamicPages, OptionsHapiPage, RunDefinePage, RunPreDefinePage } from '../types';
|
|
@@ -6,7 +6,14 @@ export { ContextHandler, Contexts, Extension, Flow, FlowContextPage, FlowContext
|
|
|
6
6
|
declare type DefinePage = (page: OptionsHapiPage) => RunPreDefinePage;
|
|
7
7
|
declare const definePage: DefinePage;
|
|
8
8
|
|
|
9
|
+
declare type HapiPages = OptionsHapiPage | OptionsHapiPage[];
|
|
10
|
+
interface Context {
|
|
11
|
+
server: Server;
|
|
12
|
+
}
|
|
13
|
+
declare type Pages = (ctx: Context) => Promise<HapiPages>;
|
|
14
|
+
|
|
9
15
|
declare const plugin: Plugin<FlowOptions>;
|
|
16
|
+
|
|
10
17
|
declare module '@hapi/hapi' {
|
|
11
18
|
interface ServerApplicationState {
|
|
12
19
|
flow: Flow.AppState;
|
|
@@ -54,4 +61,4 @@ declare module '@hapi/hapi' {
|
|
|
54
61
|
}
|
|
55
62
|
}
|
|
56
63
|
|
|
57
|
-
export { definePage, plugin };
|
|
64
|
+
export { Pages, definePage, plugin };
|
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import path from 'path';
|
|
1
|
+
import path, { join } from 'path';
|
|
2
2
|
import { applyToDefaults } from '@hapi/hoek';
|
|
3
3
|
import consola from 'consola';
|
|
4
4
|
import * as R from 'ramda';
|
|
5
|
+
import { flatten, dissoc, groupBy, mapObjIndexed } from 'ramda';
|
|
5
6
|
import { notFound } from '@hapi/boom';
|
|
6
7
|
import os from 'os';
|
|
7
8
|
import chalk from 'chalk';
|
|
8
9
|
import fs from 'fs-extra';
|
|
10
|
+
import fs$1 from 'fs';
|
|
11
|
+
import { createHooks } from 'hookable';
|
|
12
|
+
import chokidar from 'chokidar';
|
|
9
13
|
|
|
10
14
|
const name = "@monkeyplus/flow";
|
|
11
|
-
const version = "4.0.0-beta.
|
|
15
|
+
const version = "4.0.0-beta.10";
|
|
12
16
|
const description = "Utils hapi";
|
|
13
17
|
const author = "Andres Navarrete";
|
|
14
18
|
const license = "MIT";
|
|
@@ -33,21 +37,24 @@ const scripts = {
|
|
|
33
37
|
lint: "eslint --ext .js,.ts .",
|
|
34
38
|
fix: "eslint --fix --ext .ts .",
|
|
35
39
|
prepublishOnly: "pnpm run build",
|
|
36
|
-
publish: "pnpm publish",
|
|
37
40
|
start: "esno src/index.ts",
|
|
38
41
|
test: "vitest"
|
|
39
42
|
};
|
|
40
43
|
const dependencies$1 = {
|
|
41
44
|
"@hapi/boom": "9.x.x",
|
|
42
45
|
"@hapi/hoek": "9.x.x",
|
|
43
|
-
chalk: "^5.0.0",
|
|
44
46
|
consola: "^2.15.3",
|
|
47
|
+
chalk: "^4.1.2",
|
|
48
|
+
chokidar: "^3.5.3",
|
|
49
|
+
hookable: "^5.1.1",
|
|
45
50
|
"fs-extra": "^10.0.0",
|
|
46
51
|
ramda: "^0.28.0"
|
|
47
52
|
};
|
|
48
53
|
const devDependencies = {
|
|
49
54
|
"@types/fs-extra": "^9.0.13",
|
|
50
55
|
"@types/hapi__hapi": "^20.0.10",
|
|
56
|
+
"@types/hapi__nes": "^11.0.5",
|
|
57
|
+
"@types/hapi__vision": "^5.5.3",
|
|
51
58
|
"@types/ramda": "^0.27.64"
|
|
52
59
|
};
|
|
53
60
|
const peerDependencies = {
|
|
@@ -74,8 +81,13 @@ const pkg = {
|
|
|
74
81
|
|
|
75
82
|
const logger$1 = consola.withScope(pkg.name);
|
|
76
83
|
const dependencies = {
|
|
77
|
-
"@hapi/vision": "6.x.x"
|
|
84
|
+
"@hapi/vision": "6.x.x",
|
|
85
|
+
"@hapi/inert": "6.x.x",
|
|
86
|
+
"@hapi/h2o2": "9.x.x",
|
|
87
|
+
"@hapi/nes": "12.x.x"
|
|
78
88
|
};
|
|
89
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
90
|
+
const isGenerate = process.env.GENERATE;
|
|
79
91
|
|
|
80
92
|
const LifeCircle = {
|
|
81
93
|
register: (server) => {
|
|
@@ -289,7 +301,7 @@ const handlerPage = async (req, h) => {
|
|
|
289
301
|
return context;
|
|
290
302
|
}
|
|
291
303
|
context.utils = utils;
|
|
292
|
-
return h.view(`${configs.dirTemplates}${flow.view.
|
|
304
|
+
return h.view(`${configs.dirTemplates}${flow.view.layout || "default"}`, context);
|
|
293
305
|
};
|
|
294
306
|
|
|
295
307
|
const logger = consola.withScope("@monkeyplus/flow");
|
|
@@ -297,6 +309,10 @@ const defaults = {
|
|
|
297
309
|
locales: ["es-ec"],
|
|
298
310
|
defaultUbication: "ec",
|
|
299
311
|
defaultLanguage: "es",
|
|
312
|
+
staticDir: "static",
|
|
313
|
+
hmr: {
|
|
314
|
+
dirs: ["views"]
|
|
315
|
+
},
|
|
300
316
|
publicPath: "/",
|
|
301
317
|
locale: "es-ec",
|
|
302
318
|
relativeTo: "",
|
|
@@ -668,6 +684,47 @@ const definePage = (pageOptions) => (levelOptions) => (configs) => {
|
|
|
668
684
|
return routes;
|
|
669
685
|
};
|
|
670
686
|
|
|
687
|
+
const readPagesDir = (pathDir, folder) => {
|
|
688
|
+
const p = fs$1.readdirSync(pathDir);
|
|
689
|
+
return flatten(p.filter((f) => !f.includes(".js.map") && !f.includes(" copy.js.map") && !f.includes(" copy.js") && !f.includes(".model")).map((e) => {
|
|
690
|
+
if (e.includes(".js") || e.includes(".ts")) {
|
|
691
|
+
return {
|
|
692
|
+
path: path.join(pathDir, e),
|
|
693
|
+
prefix: folder
|
|
694
|
+
};
|
|
695
|
+
} else {
|
|
696
|
+
return readPagesDir(path.join(pathDir, e), e);
|
|
697
|
+
}
|
|
698
|
+
}));
|
|
699
|
+
};
|
|
700
|
+
const registerPages = async (server, dir) => {
|
|
701
|
+
const dirPages = path.resolve(dir || "./pages");
|
|
702
|
+
const listFiles = readPagesDir(dirPages);
|
|
703
|
+
const listPrePages = [];
|
|
704
|
+
for (const item of listFiles) {
|
|
705
|
+
try {
|
|
706
|
+
const module = require(item.path)?.default;
|
|
707
|
+
if (typeof module === "function") {
|
|
708
|
+
listPrePages.push(module());
|
|
709
|
+
} else if (typeof module === "object") {
|
|
710
|
+
if (Array.isArray(module)) {
|
|
711
|
+
module.forEach((el) => listPrePages.push(el()));
|
|
712
|
+
} else {
|
|
713
|
+
const _pages = await module.pages({ server });
|
|
714
|
+
if (Array.isArray(_pages))
|
|
715
|
+
_pages.forEach((page) => listPrePages.push(definePage(page)()));
|
|
716
|
+
else
|
|
717
|
+
listPrePages.push(definePage(_pages)());
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
} catch (details) {
|
|
721
|
+
logger$1.error(details);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
for (const page of listPrePages)
|
|
725
|
+
await server.flow.addPage(page);
|
|
726
|
+
};
|
|
727
|
+
|
|
671
728
|
const RunMethods = {
|
|
672
729
|
register: (server) => {
|
|
673
730
|
const { flow: state } = server.app;
|
|
@@ -681,7 +738,7 @@ const RunMethods = {
|
|
|
681
738
|
server.route(route);
|
|
682
739
|
};
|
|
683
740
|
const addPage = (_page, extra) => {
|
|
684
|
-
const
|
|
741
|
+
const registerPages2 = (_pages2) => {
|
|
685
742
|
for (const _route of _pages2) {
|
|
686
743
|
if (state.routes[_route.path])
|
|
687
744
|
logger$1.warn("The route %s alredy exist", _route.path);
|
|
@@ -692,13 +749,14 @@ const RunMethods = {
|
|
|
692
749
|
let _pages;
|
|
693
750
|
if (typeof _page === "function") {
|
|
694
751
|
_pages = _page(config);
|
|
695
|
-
|
|
752
|
+
registerPages2(_pages);
|
|
696
753
|
} else {
|
|
697
754
|
_pages = definePage(_page)()(config);
|
|
698
|
-
|
|
755
|
+
registerPages2(_pages);
|
|
699
756
|
}
|
|
700
757
|
};
|
|
701
758
|
const init = async () => {
|
|
759
|
+
await registerPages(server);
|
|
702
760
|
const pages = server.app.flow.routes;
|
|
703
761
|
for (const key in pages) {
|
|
704
762
|
const page = pages[key];
|
|
@@ -715,6 +773,136 @@ const RunMethods = {
|
|
|
715
773
|
}
|
|
716
774
|
};
|
|
717
775
|
|
|
776
|
+
const createHmr = (options) => {
|
|
777
|
+
const state = {
|
|
778
|
+
dir: options.relativeTo || "",
|
|
779
|
+
dirs: options.dirs || [],
|
|
780
|
+
extensions: options.extensions || ["eta"]
|
|
781
|
+
};
|
|
782
|
+
let watcher;
|
|
783
|
+
const hooks = createHooks();
|
|
784
|
+
const watch = () => {
|
|
785
|
+
watcher = chokidar.watch(state.dirs.map((d) => path.join(d, `**/*.(${state.extensions.join("|")})`)), {
|
|
786
|
+
cwd: state.dir,
|
|
787
|
+
ignoreInitial: true,
|
|
788
|
+
ignored: "node_modules/**/*"
|
|
789
|
+
}).on("change", (path2) => {
|
|
790
|
+
logger$1.info("Changed file", path2);
|
|
791
|
+
hooks.callHook("page:refresh");
|
|
792
|
+
});
|
|
793
|
+
};
|
|
794
|
+
watch();
|
|
795
|
+
return { watcher, hooks };
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
const RegisterCommon = async (server, configs) => {
|
|
799
|
+
server.route({
|
|
800
|
+
method: "GET",
|
|
801
|
+
path: "/{param*}",
|
|
802
|
+
options: {
|
|
803
|
+
ext: {
|
|
804
|
+
onPreResponse: {
|
|
805
|
+
method: (req, h) => {
|
|
806
|
+
return h.continue;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
},
|
|
811
|
+
handler: {
|
|
812
|
+
directory: {
|
|
813
|
+
path: configs.staticDir,
|
|
814
|
+
listing: true
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
});
|
|
818
|
+
if (!isGenerate) {
|
|
819
|
+
const hmr = createHmr({
|
|
820
|
+
dirs: configs.hmr.dirs,
|
|
821
|
+
relativeTo: configs.relativeTo
|
|
822
|
+
});
|
|
823
|
+
hmr.hooks.hook("page:refresh", () => {
|
|
824
|
+
setTimeout(() => {
|
|
825
|
+
logger$1.info("Refresh page");
|
|
826
|
+
server.publish("/_flow/hmr", { reload: true });
|
|
827
|
+
}, 40);
|
|
828
|
+
});
|
|
829
|
+
server.subscription("/_flow/hmr");
|
|
830
|
+
server.route({
|
|
831
|
+
path: "/_flow/hmr/client.js",
|
|
832
|
+
method: "get",
|
|
833
|
+
handler: {
|
|
834
|
+
file: {
|
|
835
|
+
path: join(__dirname, "../resources/client"),
|
|
836
|
+
confine: false
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
});
|
|
840
|
+
server.route({
|
|
841
|
+
path: "/_flow/hmr/connect.js",
|
|
842
|
+
method: "get",
|
|
843
|
+
handler: {
|
|
844
|
+
file: {
|
|
845
|
+
path: join(__dirname, "../resources/ws.js"),
|
|
846
|
+
confine: false
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
});
|
|
850
|
+
logger$1.debug("Enable development routes");
|
|
851
|
+
server.route({
|
|
852
|
+
path: "/_flow/sitemap",
|
|
853
|
+
method: "get",
|
|
854
|
+
handler: async () => {
|
|
855
|
+
const { pages: getPages } = server.methods.flow;
|
|
856
|
+
const urls = await getPages();
|
|
857
|
+
const pages = Object.values(urls).map((p) => dissoc("context", p));
|
|
858
|
+
return {
|
|
859
|
+
total: pages.length,
|
|
860
|
+
pages
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
});
|
|
864
|
+
server.route({
|
|
865
|
+
path: "/_flow/locales",
|
|
866
|
+
method: "get",
|
|
867
|
+
handler: async (req) => {
|
|
868
|
+
const { pages: getPages } = server.methods.flow;
|
|
869
|
+
const urls = await getPages();
|
|
870
|
+
const pages = Object.values(urls).map((p) => dissoc("context", p));
|
|
871
|
+
const locales = groupBy((page) => page.locale, pages);
|
|
872
|
+
return {
|
|
873
|
+
locales: req.server.plugins.flow.configs.locales,
|
|
874
|
+
all: mapObjIndexed((l) => {
|
|
875
|
+
return {
|
|
876
|
+
total: l.length,
|
|
877
|
+
pages: l
|
|
878
|
+
};
|
|
879
|
+
}, locales)
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
});
|
|
883
|
+
server.route({
|
|
884
|
+
path: "/_flow/configs",
|
|
885
|
+
method: "get",
|
|
886
|
+
handler: (req) => {
|
|
887
|
+
return req.server.plugins.flow.configs;
|
|
888
|
+
}
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
const HMR = {
|
|
892
|
+
head: ' <script src="/_flow/hmr/client.js"><\/script>',
|
|
893
|
+
body: ' <script src="/_flow/hmr/connect.js"><\/script>'
|
|
894
|
+
};
|
|
895
|
+
server.ext("onPreHandler", async (req, h) => {
|
|
896
|
+
if (req.route.settings.plugins?.generate === ".html") {
|
|
897
|
+
const { global } = req.plugins.flow;
|
|
898
|
+
Object.assign(global, {
|
|
899
|
+
hmr: isProduction || isGenerate ? { head: "", body: "" } : HMR
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
return h.continue;
|
|
903
|
+
});
|
|
904
|
+
};
|
|
905
|
+
|
|
718
906
|
const plugin = {
|
|
719
907
|
pkg,
|
|
720
908
|
dependencies,
|
|
@@ -839,6 +1027,7 @@ const plugin = {
|
|
|
839
1027
|
return baseOptions;
|
|
840
1028
|
}
|
|
841
1029
|
};
|
|
1030
|
+
await RegisterCommon(server, config);
|
|
842
1031
|
}
|
|
843
1032
|
};
|
|
844
1033
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@monkeyplus/flow",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.10",
|
|
4
4
|
"description": "Utils hapi",
|
|
5
5
|
"author": "Andres Navarrete",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,14 +20,18 @@
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@hapi/boom": "9.x.x",
|
|
22
22
|
"@hapi/hoek": "9.x.x",
|
|
23
|
-
"chalk": "^5.0.0",
|
|
24
23
|
"consola": "^2.15.3",
|
|
24
|
+
"chalk": "^4.1.2",
|
|
25
|
+
"chokidar": "^3.5.3",
|
|
26
|
+
"hookable": "^5.1.1",
|
|
25
27
|
"fs-extra": "^10.0.0",
|
|
26
28
|
"ramda": "^0.28.0"
|
|
27
29
|
},
|
|
28
30
|
"devDependencies": {
|
|
29
31
|
"@types/fs-extra": "^9.0.13",
|
|
30
32
|
"@types/hapi__hapi": "^20.0.10",
|
|
33
|
+
"@types/hapi__nes": "^11.0.5",
|
|
34
|
+
"@types/hapi__vision": "^5.5.3",
|
|
31
35
|
"@types/ramda": "^0.27.64"
|
|
32
36
|
},
|
|
33
37
|
"peerDependencies": {
|
package/types/flow.d.ts
CHANGED
|
@@ -33,6 +33,10 @@ export interface FlowOptions {
|
|
|
33
33
|
routeOptions?: RouteOptions
|
|
34
34
|
dirTemplates?: string
|
|
35
35
|
plugins: FlowOptionPlugins
|
|
36
|
+
staticDir: string
|
|
37
|
+
hmr: {
|
|
38
|
+
dirs: string[]
|
|
39
|
+
}
|
|
36
40
|
engines?: ('eta' | 'nunjucks')[]
|
|
37
41
|
}
|
|
38
42
|
|
|
@@ -118,6 +122,7 @@ export namespace Flow {
|
|
|
118
122
|
}
|
|
119
123
|
export interface Configs extends FlowOptions {
|
|
120
124
|
relativeTo: string
|
|
125
|
+
|
|
121
126
|
locales: string[]
|
|
122
127
|
defaultUbication: string
|
|
123
128
|
defaultLanguage: string
|