@lowdefy/server-dev 0.0.0-alpha.7 → 0.0.0-experimental-20231123124425

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 (80) hide show
  1. package/.eslintrc.yaml +1 -0
  2. package/.npmrc +1 -0
  3. package/LICENSE +201 -0
  4. package/README.md +5 -0
  5. package/lib/client/App.js +68 -0
  6. package/lib/client/Page.js +51 -0
  7. package/lib/client/Reload.js +55 -0
  8. package/lib/client/RestartingPage.js +47 -0
  9. package/lib/client/auth/Auth.js +35 -0
  10. package/lib/client/auth/AuthConfigured.js +61 -0
  11. package/lib/client/auth/AuthNotConfigured.js +33 -0
  12. package/lib/client/setPageId.js +30 -0
  13. package/lib/client/utils/request.js +38 -0
  14. package/lib/client/utils/useMutateCache.js +25 -0
  15. package/lib/client/utils/usePageConfig.js +27 -0
  16. package/lib/client/utils/useRootConfig.js +26 -0
  17. package/lib/client/utils/waitForRestartedServer.js +38 -0
  18. package/lib/server/apiWrapper.js +68 -0
  19. package/lib/server/auth/getAuthOptions.js +35 -0
  20. package/lib/server/auth/getServerSession.js +28 -0
  21. package/lib/server/fileCache.js +20 -0
  22. package/lib/server/log/createLogger.js +29 -0
  23. package/lib/server/log/logError.js +40 -0
  24. package/lib/server/log/logRequest.js +33 -0
  25. package/manager/getContext.mjs +77 -0
  26. package/manager/processes/initialBuild.mjs +27 -0
  27. package/manager/processes/installPlugins.mjs +31 -0
  28. package/manager/processes/lowdefyBuild.mjs +35 -0
  29. package/manager/processes/nextBuild.mjs +31 -0
  30. package/manager/processes/readDotEnv.mjs +26 -0
  31. package/manager/processes/reloadClients.mjs +26 -0
  32. package/manager/processes/restartServer.mjs +28 -0
  33. package/manager/processes/shutdownServer.mjs +35 -0
  34. package/manager/processes/startServer.mjs +46 -0
  35. package/manager/processes/startWatchers.mjs +31 -0
  36. package/manager/run.mjs +109 -0
  37. package/manager/utils/BatchChanges.mjs +64 -0
  38. package/manager/utils/createCustomPluginTypesMap.mjs +80 -0
  39. package/manager/utils/createLogger.mjs +33 -0
  40. package/manager/utils/createStdOutLineHandler.mjs +33 -0
  41. package/manager/utils/getLowdefyVersion.mjs +51 -0
  42. package/manager/utils/getNextBin.mjs +36 -0
  43. package/manager/utils/setupWatcher.mjs +50 -0
  44. package/manager/watchers/envWatcher.mjs +35 -0
  45. package/manager/watchers/lowdefyBuildWatcher.mjs +55 -0
  46. package/manager/watchers/nextBuildWatcher.mjs +108 -0
  47. package/next.config.js +31 -0
  48. package/package.json +67 -38
  49. package/package.original.json +98 -0
  50. package/pages/404.js +19 -0
  51. package/pages/[pageId].js +19 -0
  52. package/pages/_app.js +42 -0
  53. package/pages/_document.js +54 -0
  54. package/pages/api/auth/[...nextauth].js +37 -0
  55. package/pages/api/page/[pageId].js +32 -0
  56. package/pages/api/ping.js +19 -0
  57. package/pages/api/reload.js +50 -0
  58. package/pages/api/request/[pageId]/[requestId].js +32 -0
  59. package/pages/api/root.js +25 -0
  60. package/pages/index.js +19 -0
  61. package/public_default/favicon.ico +0 -0
  62. package/CHANGELOG.md +0 -19
  63. package/dist/server.js +0 -55
  64. package/dist/shell/_yarn_cache_object-assign-npm-4_1_1-1004ad6dec-66cf021898_zip_node_modules_object-assign_index_js.js +0 -22
  65. package/dist/shell/index.html +0 -34
  66. package/dist/shell/main.js +0 -623
  67. package/dist/shell/public/icon-32.png +0 -0
  68. package/dist/shell/src_shell_bootstrap_js.js +0 -517
  69. package/dist/shell/vendors-_yarn_cache_object-assign-npm-4_1_1-1004ad6dec-66cf021898_zip_node_modules_object-ass-b18854.js +0 -66
  70. package/dist/shell/vendors-_yarn_cache_react-npm-17_0_1-98658812fc-a76d86ec97_zip_node_modules_react_index_js.js +0 -33
  71. package/dist/shell/vendors-_yarn_virtual_ajv-errors-virtual-6c363fa7a9_0_cache_ajv-errors-npm-1_0_1-32cd0b19f8-d-940326.js +0 -996
  72. package/dist/shell/vendors-_yarn_virtual_react-dom-virtual-2d0751dec5_0_cache_react-dom-npm-17_0_1-588d0088ca-6a-73f198.js +0 -33
  73. /package/{dist/shell/public → public_default}/apple-touch-icon.png +0 -0
  74. /package/{dist/shell/public → public_default}/icon-512.png +0 -0
  75. /package/{dist/shell/public → public_default}/icon.svg +0 -0
  76. /package/{dist/shell/public → public_default}/logo-dark-theme.png +0 -0
  77. /package/{dist/shell/public → public_default}/logo-light-theme.png +0 -0
  78. /package/{dist/shell/public → public_default}/logo-square-dark-theme.png +0 -0
  79. /package/{dist/shell/public → public_default}/logo-square-light-theme.png +0 -0
  80. /package/{dist/shell/public → public_default}/manifest.webmanifest +0 -0
@@ -0,0 +1,46 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import { spawnProcess } from '@lowdefy/node-utils';
18
+ import createStdOutLineHandler from '../utils/createStdOutLineHandler.mjs';
19
+
20
+ function startServer(context) {
21
+ context.shutdownServer();
22
+
23
+ const nextServer = spawnProcess({
24
+ stdOutLineHandler: createStdOutLineHandler({ context }),
25
+ stdErrLineHandler: (line) => context.logger.error(line),
26
+ command: 'node',
27
+ args: [context.bin.next, 'start'],
28
+ processOptions: {
29
+ env: {
30
+ ...process.env,
31
+ PORT: context.options.port,
32
+ },
33
+ },
34
+ returnProcess: true,
35
+ });
36
+ context.logger.debug(`Started next server with pid ${nextServer.pid}.`);
37
+ nextServer.on('exit', (code, signal) => {
38
+ context.logger.debug(`nextServer exit ${nextServer.pid}, signal: ${signal}, code: ${code}`);
39
+ });
40
+ nextServer.on('error', (error) => {
41
+ context.logger.error(error);
42
+ });
43
+ context.nextServer = nextServer;
44
+ }
45
+
46
+ export default startServer;
@@ -0,0 +1,31 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import envWatcher from '../watchers/envWatcher.mjs';
18
+ import lowdefyBuildWatcher from '../watchers/lowdefyBuildWatcher.mjs';
19
+ import nextBuildWatcher from '../watchers/nextBuildWatcher.mjs';
20
+
21
+ function startWatchers(context) {
22
+ return async () => {
23
+ await Promise.all([
24
+ envWatcher(context),
25
+ lowdefyBuildWatcher(context),
26
+ nextBuildWatcher(context),
27
+ ]);
28
+ };
29
+ }
30
+
31
+ export default startWatchers;
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env node
2
+ /*
3
+ Copyright 2020-2023 Lowdefy, Inc
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */
17
+
18
+ import { wait } from '@lowdefy/helpers';
19
+ import opener from 'opener';
20
+ import getContext from './getContext.mjs';
21
+ import startServer from './processes/startServer.mjs';
22
+
23
+ /*
24
+ The run script does the following:
25
+ - Run the initial Lowdefy build, install plugins, and next build and read .env
26
+ - Start file watchers to reload config and restart server if necessary
27
+ - Start the server
28
+ - Open a browser window.
29
+
30
+ Three watchers are started:
31
+
32
+ ## Lowdefy build watcher
33
+ Watches:
34
+ - <config-dir>,
35
+ - <watch-dirs>
36
+ - !<ignore-dirs>
37
+ The Lowdefy build watcher watches the Lowdefy config files for changes
38
+ and runs Lowdefy build when they change, and triggers a soft reload.
39
+
40
+ If lowdefy version in lowdefy.yaml
41
+ is changed, the server warns and exits.
42
+
43
+ ## .env watcher
44
+
45
+ If the .env file is changed, the new file is parsed, and the server restarted with the new env
46
+ and the server hard reloads.
47
+
48
+ ## Next build watcher
49
+
50
+ The Next build watcher watches for any files where the app should be rebuilt and restarted.
51
+ It watches:
52
+ - <build-dir>/plugins/**
53
+ - <build-dir>/config.json
54
+ - <server-dir>/package.json
55
+
56
+ If app config changes:
57
+ - <build-dir>/config.json changes, rebuild and restart server.
58
+
59
+ If user styles change:
60
+ - <public-dir>/styles.less changes, rebuild and restart server.
61
+
62
+ If new plugin type in an existing plugin package is used:
63
+ - <build-dir>/plugins/** changes, rebuild next and restart server.
64
+
65
+ If new plugin type in a new plugin package is used:
66
+ - <server-dir>/package.json changes, run npm install, rebuild next and restart server.
67
+
68
+ # Reload mechanism
69
+
70
+ The web client creates a Server Sent Events connection with the server on the /api/reload route.
71
+ The server watches the <build-dir>/reload file, which is written every time the server should reload,
72
+ and sends an event to the client to reload the config. The client then uses a SWR cache mutation to
73
+ fetch the new config.
74
+
75
+ If the server is restarted, the event stream is closed because the original server was shut down. The client starts
76
+ pinging the /api/ping route, until it detects a new server has started, and then reloads the window.
77
+ */
78
+
79
+ /* TODO:
80
+ Not killing server on errors properly
81
+ when:
82
+ - initial build fails
83
+ */
84
+
85
+ const context = await getContext();
86
+
87
+ try {
88
+ try {
89
+ await context.initialBuild();
90
+ } catch (error) {
91
+ context.logger.error(error);
92
+ }
93
+
94
+ // We are not waiting for the startWatchers promise to resolve (all watchers have fired the ready event)
95
+ // because chokidar sometimes doesn't fire this event, and it seems like there isn't an issue with not waiting.
96
+ context.startWatchers();
97
+
98
+ startServer(context);
99
+ await wait(800);
100
+ if (process.env.LOWDEFY_SERVER_DEV_OPEN_BROWSER === 'true') {
101
+ // TODO: Wait 1 sec for a ping and don't open if a ping is seen
102
+ opener(`http://localhost:${context.options.port}`);
103
+ }
104
+ await new Promise(() => {});
105
+ } catch (error) {
106
+ context.logger.error(error);
107
+ context.shutdownServer();
108
+ process.exit();
109
+ }
@@ -0,0 +1,64 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import { type } from '@lowdefy/helpers';
18
+
19
+ class BatchChanges {
20
+ constructor({ context, fn, delay }) {
21
+ this.context = context;
22
+ this._call = this._call.bind(this);
23
+ this.args = [];
24
+ this.delay = delay || 500;
25
+ this.fn = fn;
26
+ this.repeat = false;
27
+ this.running = false;
28
+ }
29
+
30
+ newChange(...args) {
31
+ this.args.push(args.filter((arg) => type.isString(arg))); // filter for string paths since chokidar also returns an stats object on windows.
32
+ this._startTimer();
33
+ }
34
+
35
+ _startTimer() {
36
+ if (this.timer) {
37
+ clearTimeout(this.timer);
38
+ }
39
+ if (this.running) {
40
+ this.repeat = true;
41
+ } else {
42
+ this.timer = setTimeout(this._call, this.delay);
43
+ }
44
+ }
45
+
46
+ async _call() {
47
+ this.running = true;
48
+ try {
49
+ const args = this.args;
50
+ this.args = [];
51
+ await this.fn(args);
52
+ this.running = false;
53
+ if (this.repeat) {
54
+ this.repeat = false;
55
+ this._call();
56
+ }
57
+ } catch (error) {
58
+ this.running = false;
59
+ this.context.logger.error(error?.message ?? error);
60
+ }
61
+ }
62
+ }
63
+
64
+ export default BatchChanges;
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env node
2
+ /*
3
+ Copyright 2020-2023 Lowdefy, Inc
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */
17
+
18
+ import path from 'path';
19
+ import { get } from '@lowdefy/helpers';
20
+ import { readFile } from '@lowdefy/node-utils';
21
+ import { createPluginTypesMap } from '@lowdefy/build';
22
+ import YAML from 'yaml';
23
+
24
+ async function getPluginDefinitions({ directories }) {
25
+ let lowdefyYaml = await readFile(path.join(directories.config, 'lowdefy.yaml'));
26
+ if (!lowdefyYaml) {
27
+ lowdefyYaml = await readFile(path.join(directories.config, 'lowdefy.yml'));
28
+ }
29
+ const lowdefy = YAML.parse(lowdefyYaml);
30
+ return get(lowdefy, 'plugins', { default: [] });
31
+ }
32
+
33
+ async function createCustomPluginTypesMap({ directories, logger }) {
34
+ const customTypesMap = {
35
+ actions: {},
36
+ auth: {
37
+ adapters: {},
38
+ callbacks: {},
39
+ events: {},
40
+ providers: {},
41
+ },
42
+ blocks: {},
43
+ connections: {},
44
+ icons: {},
45
+ operators: {
46
+ client: {},
47
+ server: {},
48
+ },
49
+ requests: {},
50
+ styles: {
51
+ packages: {},
52
+ blocks: {},
53
+ },
54
+ };
55
+
56
+ const pluginDefinitions = await getPluginDefinitions({ directories });
57
+
58
+ for (const plugin of pluginDefinitions) {
59
+ let types;
60
+ try {
61
+ types = (await import(`${plugin.name}/types`)).default;
62
+ } catch (e) {
63
+ logger.error(`Failed to import plugin "${plugin.name}".`);
64
+ logger.debug(e);
65
+ logger.info('If the plugin was added while the server was running, restart the server.');
66
+ throw new Error(`Failed to import plugin "${plugin.name}".`);
67
+ }
68
+ createPluginTypesMap({
69
+ packageTypes: types,
70
+ typesMap: customTypesMap,
71
+ packageName: plugin.name,
72
+ version: plugin.version,
73
+ typePrefix: plugin.typePrefix,
74
+ });
75
+ }
76
+
77
+ return customTypesMap;
78
+ }
79
+
80
+ export default createCustomPluginTypesMap;
@@ -0,0 +1,33 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+ import pino from 'pino';
17
+
18
+ function createLogger({ level = 'info' }) {
19
+ const logger = pino({
20
+ name: 'lowdefy build',
21
+ level,
22
+ base: { pid: undefined, hostname: undefined },
23
+ mixin: (context, level) => {
24
+ return {
25
+ ...context,
26
+ print: context.print ?? logger.levels.labels[level],
27
+ };
28
+ },
29
+ });
30
+ return logger;
31
+ }
32
+
33
+ export default createLogger;
@@ -0,0 +1,33 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ function createStdOutLineHandler({ context }) {
18
+ const { logger } = context;
19
+ function stdOutLineHandler(line) {
20
+ try {
21
+ const { level, name, time, rid, ...msgObj } = JSON.parse(line);
22
+ if (level) {
23
+ const print = level === 30 ? 'log' : logger.levels.labels[level];
24
+ logger[logger.levels.labels[level]]({ print }, JSON.stringify(msgObj));
25
+ }
26
+ } catch (error) {
27
+ logger.info({ print: 'log' }, line);
28
+ }
29
+ }
30
+ return stdOutLineHandler;
31
+ }
32
+
33
+ export default createStdOutLineHandler;
@@ -0,0 +1,51 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import path from 'path';
18
+ import { type } from '@lowdefy/helpers';
19
+ import { readFile } from '@lowdefy/node-utils';
20
+ import YAML from 'yaml';
21
+
22
+ async function getLowdefyVersion(context) {
23
+ let lowdefyYaml = await readFile(path.join(context.directories.config, 'lowdefy.yaml'));
24
+ if (!lowdefyYaml) {
25
+ lowdefyYaml = await readFile(path.join(context.directories.config, 'lowdefy.yml'));
26
+ }
27
+ if (!lowdefyYaml) {
28
+ throw new Error(`Could not find "lowdefy.yaml" file.`);
29
+ }
30
+ let lowdefy;
31
+ try {
32
+ lowdefy = YAML.parse(lowdefyYaml);
33
+ } catch (error) {
34
+ throw new Error(`Could not parse "lowdefy.yaml" file. Received error ${error.message}.`);
35
+ }
36
+ if (!lowdefy.lowdefy) {
37
+ throw new Error(
38
+ `No version specified in "lowdefy.yaml" file. Specify a version in the "lowdefy" field.`
39
+ );
40
+ }
41
+ if (!type.isString(lowdefy.lowdefy)) {
42
+ throw new Error(
43
+ `Version number specified in "lowdefy.yaml" file should be a string. Received ${JSON.stringify(
44
+ lowdefy.lowdefy
45
+ )}.`
46
+ );
47
+ }
48
+ return lowdefy.lowdefy;
49
+ }
50
+
51
+ export default getLowdefyVersion;
@@ -0,0 +1,36 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import path from 'path';
18
+ import { createRequire } from 'module';
19
+
20
+ function getNextBin() {
21
+ const require = createRequire(import.meta.url);
22
+ const nextPackageJson = require('next/package.json');
23
+
24
+ const nextPath = require.resolve('next');
25
+ let nextMainFragment = nextPackageJson.main.substring(1);
26
+ let nextBinFragment = nextPackageJson.bin.next;
27
+
28
+ if (process.platform === 'win32') {
29
+ nextMainFragment = nextMainFragment.replaceAll('/', '\\');
30
+ nextBinFragment = nextBinFragment.replaceAll('/', '\\');
31
+ }
32
+
33
+ return path.join(nextPath.replace(nextMainFragment, ''), nextBinFragment);
34
+ }
35
+
36
+ export default getNextBin;
@@ -0,0 +1,50 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import chokidar from 'chokidar';
18
+ import BatchChanges from './BatchChanges.mjs';
19
+
20
+ function setupWatcher({
21
+ callback,
22
+ context,
23
+ watchDotfiles = false,
24
+ ignorePaths = [],
25
+ watchPaths,
26
+ delay = 500,
27
+ }) {
28
+ return new Promise((resolve) => {
29
+ // const { watch = [], watchIgnore = [] } = context.options;
30
+ // const resolvedWatchPaths = watch.map((pathName) => path.resolve(pathName));
31
+
32
+ const batchChanges = new BatchChanges({ context, fn: callback, delay });
33
+ const defaultIgnorePaths = watchDotfiles
34
+ ? []
35
+ : [
36
+ /(^|[/\\])\../, // ignore dotfiles
37
+ ];
38
+ const configWatcher = chokidar.watch(watchPaths, {
39
+ ignored: [...defaultIgnorePaths, ...ignorePaths],
40
+ persistent: true,
41
+ ignoreInitial: true,
42
+ });
43
+ configWatcher.on('add', (...args) => batchChanges.newChange(...args));
44
+ configWatcher.on('change', (...args) => batchChanges.newChange(...args));
45
+ configWatcher.on('unlink', (...args) => batchChanges.newChange(...args));
46
+ configWatcher.on('ready', () => resolve());
47
+ });
48
+ }
49
+
50
+ export default setupWatcher;
@@ -0,0 +1,35 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import path from 'path';
18
+ import setupWatcher from '../utils/setupWatcher.mjs';
19
+
20
+ function envWatcher(context) {
21
+ const callback = async () => {
22
+ context.logger.warn('.env file changed.');
23
+ context.readDotEnv();
24
+ await context.lowdefyBuild();
25
+ context.restartServer();
26
+ };
27
+ return setupWatcher({
28
+ callback,
29
+ context,
30
+ watchDotfiles: true,
31
+ watchPaths: [path.join(context.directories.config, '.env')],
32
+ });
33
+ }
34
+
35
+ export default envWatcher;
@@ -0,0 +1,55 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import path from 'path';
18
+ import getLowdefyVersion from '../utils/getLowdefyVersion.mjs';
19
+ import setupWatcher from '../utils/setupWatcher.mjs';
20
+
21
+ function lowdefyBuildWatcher(context) {
22
+ const fixRelativePathConfigDir = (item) =>
23
+ path.isAbsolute(item) ? item : path.resolve(context.directories.config, item);
24
+
25
+ const callback = async (filePaths) => {
26
+ const lowdefyYamlModified = filePaths
27
+ .flat()
28
+ .some((filePath) => filePath.includes('lowdefy.yaml') || filePath.includes('lowdefy.yml'));
29
+ if (lowdefyYamlModified) {
30
+ const lowdefyVersion = await getLowdefyVersion(context);
31
+ if (lowdefyVersion !== context.version && lowdefyVersion !== 'local') {
32
+ context.shutdownServer();
33
+ context.logger.warn('Lowdefy version changed. You should restart your development server.');
34
+ process.exit();
35
+ }
36
+ }
37
+
38
+ await context.lowdefyBuild();
39
+ context.reloadClients();
40
+ };
41
+ return setupWatcher({
42
+ callback,
43
+ context,
44
+ ignorePaths: [
45
+ '**/node_modules/**',
46
+ ...context.options.watchIgnore.map(fixRelativePathConfigDir),
47
+ ],
48
+ watchPaths: [
49
+ context.directories.config,
50
+ ...context.options.watch.map(fixRelativePathConfigDir),
51
+ ],
52
+ });
53
+ }
54
+
55
+ export default lowdefyBuildWatcher;