@monkeyplus/flow 4.0.0-beta.7 → 4.0.0-beta.8
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 +197 -6
- package/dist/index.d.ts +9 -2
- package/dist/index.mjs +197 -7
- package/package.json +5 -1
- 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.8";
|
|
37
42
|
const description = "Utils hapi";
|
|
38
43
|
const author = "Andres Navarrete";
|
|
39
44
|
const license = "MIT";
|
|
@@ -66,12 +71,16 @@ const dependencies$1 = {
|
|
|
66
71
|
"@hapi/hoek": "9.x.x",
|
|
67
72
|
consola: "^2.15.3",
|
|
68
73
|
chalk: "^4.1.2",
|
|
74
|
+
chokidar: "^3.5.3",
|
|
75
|
+
hookable: "^5.1.1",
|
|
69
76
|
"fs-extra": "^10.0.0",
|
|
70
77
|
ramda: "^0.28.0"
|
|
71
78
|
};
|
|
72
79
|
const devDependencies = {
|
|
73
80
|
"@types/fs-extra": "^9.0.13",
|
|
74
81
|
"@types/hapi__hapi": "^20.0.10",
|
|
82
|
+
"@types/hapi__nes": "^11.0.5",
|
|
83
|
+
"@types/hapi__vision": "^5.5.3",
|
|
75
84
|
"@types/ramda": "^0.27.64"
|
|
76
85
|
};
|
|
77
86
|
const peerDependencies = {
|
|
@@ -98,8 +107,13 @@ const pkg = {
|
|
|
98
107
|
|
|
99
108
|
const logger$1 = consola__default.withScope(pkg.name);
|
|
100
109
|
const dependencies = {
|
|
101
|
-
"@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"
|
|
102
114
|
};
|
|
115
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
116
|
+
const isGenerate = process.env.GENERATE;
|
|
103
117
|
|
|
104
118
|
const LifeCircle = {
|
|
105
119
|
register: (server) => {
|
|
@@ -313,7 +327,7 @@ const handlerPage = async (req, h) => {
|
|
|
313
327
|
return context;
|
|
314
328
|
}
|
|
315
329
|
context.utils = utils;
|
|
316
|
-
return h.view(`${configs.dirTemplates}${flow.view.
|
|
330
|
+
return h.view(`${configs.dirTemplates}${flow.view.layout || "default"}`, context);
|
|
317
331
|
};
|
|
318
332
|
|
|
319
333
|
const logger = consola__default.withScope("@monkeyplus/flow");
|
|
@@ -321,6 +335,10 @@ const defaults = {
|
|
|
321
335
|
locales: ["es-ec"],
|
|
322
336
|
defaultUbication: "ec",
|
|
323
337
|
defaultLanguage: "es",
|
|
338
|
+
staticDir: "static",
|
|
339
|
+
hmr: {
|
|
340
|
+
dirs: ["views"]
|
|
341
|
+
},
|
|
324
342
|
publicPath: "/",
|
|
325
343
|
locale: "es-ec",
|
|
326
344
|
relativeTo: "",
|
|
@@ -692,6 +710,47 @@ const definePage = (pageOptions) => (levelOptions) => (configs) => {
|
|
|
692
710
|
return routes;
|
|
693
711
|
};
|
|
694
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
|
+
|
|
695
754
|
const RunMethods = {
|
|
696
755
|
register: (server) => {
|
|
697
756
|
const { flow: state } = server.app;
|
|
@@ -705,7 +764,7 @@ const RunMethods = {
|
|
|
705
764
|
server.route(route);
|
|
706
765
|
};
|
|
707
766
|
const addPage = (_page, extra) => {
|
|
708
|
-
const
|
|
767
|
+
const registerPages2 = (_pages2) => {
|
|
709
768
|
for (const _route of _pages2) {
|
|
710
769
|
if (state.routes[_route.path])
|
|
711
770
|
logger$1.warn("The route %s alredy exist", _route.path);
|
|
@@ -716,13 +775,14 @@ const RunMethods = {
|
|
|
716
775
|
let _pages;
|
|
717
776
|
if (typeof _page === "function") {
|
|
718
777
|
_pages = _page(config);
|
|
719
|
-
|
|
778
|
+
registerPages2(_pages);
|
|
720
779
|
} else {
|
|
721
780
|
_pages = definePage(_page)()(config);
|
|
722
|
-
|
|
781
|
+
registerPages2(_pages);
|
|
723
782
|
}
|
|
724
783
|
};
|
|
725
784
|
const init = async () => {
|
|
785
|
+
await registerPages(server);
|
|
726
786
|
const pages = server.app.flow.routes;
|
|
727
787
|
for (const key in pages) {
|
|
728
788
|
const page = pages[key];
|
|
@@ -739,6 +799,136 @@ const RunMethods = {
|
|
|
739
799
|
}
|
|
740
800
|
};
|
|
741
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
|
+
|
|
742
932
|
const plugin = {
|
|
743
933
|
pkg,
|
|
744
934
|
dependencies,
|
|
@@ -863,6 +1053,7 @@ const plugin = {
|
|
|
863
1053
|
return baseOptions;
|
|
864
1054
|
}
|
|
865
1055
|
};
|
|
1056
|
+
await RegisterCommon(server, config);
|
|
866
1057
|
}
|
|
867
1058
|
};
|
|
868
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.8";
|
|
12
16
|
const description = "Utils hapi";
|
|
13
17
|
const author = "Andres Navarrete";
|
|
14
18
|
const license = "MIT";
|
|
@@ -41,12 +45,16 @@ const dependencies$1 = {
|
|
|
41
45
|
"@hapi/hoek": "9.x.x",
|
|
42
46
|
consola: "^2.15.3",
|
|
43
47
|
chalk: "^4.1.2",
|
|
48
|
+
chokidar: "^3.5.3",
|
|
49
|
+
hookable: "^5.1.1",
|
|
44
50
|
"fs-extra": "^10.0.0",
|
|
45
51
|
ramda: "^0.28.0"
|
|
46
52
|
};
|
|
47
53
|
const devDependencies = {
|
|
48
54
|
"@types/fs-extra": "^9.0.13",
|
|
49
55
|
"@types/hapi__hapi": "^20.0.10",
|
|
56
|
+
"@types/hapi__nes": "^11.0.5",
|
|
57
|
+
"@types/hapi__vision": "^5.5.3",
|
|
50
58
|
"@types/ramda": "^0.27.64"
|
|
51
59
|
};
|
|
52
60
|
const peerDependencies = {
|
|
@@ -73,8 +81,13 @@ const pkg = {
|
|
|
73
81
|
|
|
74
82
|
const logger$1 = consola.withScope(pkg.name);
|
|
75
83
|
const dependencies = {
|
|
76
|
-
"@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"
|
|
77
88
|
};
|
|
89
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
90
|
+
const isGenerate = process.env.GENERATE;
|
|
78
91
|
|
|
79
92
|
const LifeCircle = {
|
|
80
93
|
register: (server) => {
|
|
@@ -288,7 +301,7 @@ const handlerPage = async (req, h) => {
|
|
|
288
301
|
return context;
|
|
289
302
|
}
|
|
290
303
|
context.utils = utils;
|
|
291
|
-
return h.view(`${configs.dirTemplates}${flow.view.
|
|
304
|
+
return h.view(`${configs.dirTemplates}${flow.view.layout || "default"}`, context);
|
|
292
305
|
};
|
|
293
306
|
|
|
294
307
|
const logger = consola.withScope("@monkeyplus/flow");
|
|
@@ -296,6 +309,10 @@ const defaults = {
|
|
|
296
309
|
locales: ["es-ec"],
|
|
297
310
|
defaultUbication: "ec",
|
|
298
311
|
defaultLanguage: "es",
|
|
312
|
+
staticDir: "static",
|
|
313
|
+
hmr: {
|
|
314
|
+
dirs: ["views"]
|
|
315
|
+
},
|
|
299
316
|
publicPath: "/",
|
|
300
317
|
locale: "es-ec",
|
|
301
318
|
relativeTo: "",
|
|
@@ -667,6 +684,47 @@ const definePage = (pageOptions) => (levelOptions) => (configs) => {
|
|
|
667
684
|
return routes;
|
|
668
685
|
};
|
|
669
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
|
+
|
|
670
728
|
const RunMethods = {
|
|
671
729
|
register: (server) => {
|
|
672
730
|
const { flow: state } = server.app;
|
|
@@ -680,7 +738,7 @@ const RunMethods = {
|
|
|
680
738
|
server.route(route);
|
|
681
739
|
};
|
|
682
740
|
const addPage = (_page, extra) => {
|
|
683
|
-
const
|
|
741
|
+
const registerPages2 = (_pages2) => {
|
|
684
742
|
for (const _route of _pages2) {
|
|
685
743
|
if (state.routes[_route.path])
|
|
686
744
|
logger$1.warn("The route %s alredy exist", _route.path);
|
|
@@ -691,13 +749,14 @@ const RunMethods = {
|
|
|
691
749
|
let _pages;
|
|
692
750
|
if (typeof _page === "function") {
|
|
693
751
|
_pages = _page(config);
|
|
694
|
-
|
|
752
|
+
registerPages2(_pages);
|
|
695
753
|
} else {
|
|
696
754
|
_pages = definePage(_page)()(config);
|
|
697
|
-
|
|
755
|
+
registerPages2(_pages);
|
|
698
756
|
}
|
|
699
757
|
};
|
|
700
758
|
const init = async () => {
|
|
759
|
+
await registerPages(server);
|
|
701
760
|
const pages = server.app.flow.routes;
|
|
702
761
|
for (const key in pages) {
|
|
703
762
|
const page = pages[key];
|
|
@@ -714,6 +773,136 @@ const RunMethods = {
|
|
|
714
773
|
}
|
|
715
774
|
};
|
|
716
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
|
+
|
|
717
906
|
const plugin = {
|
|
718
907
|
pkg,
|
|
719
908
|
dependencies,
|
|
@@ -838,6 +1027,7 @@ const plugin = {
|
|
|
838
1027
|
return baseOptions;
|
|
839
1028
|
}
|
|
840
1029
|
};
|
|
1030
|
+
await RegisterCommon(server, config);
|
|
841
1031
|
}
|
|
842
1032
|
};
|
|
843
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.8",
|
|
4
4
|
"description": "Utils hapi",
|
|
5
5
|
"author": "Andres Navarrete",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,12 +22,16 @@
|
|
|
22
22
|
"@hapi/hoek": "9.x.x",
|
|
23
23
|
"consola": "^2.15.3",
|
|
24
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
|