@joystick.js/cli-canary 0.0.0-canary.9 → 0.0.0-canary.91

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 (45) hide show
  1. package/dist/cli.js +7 -0
  2. package/dist/functions/index.js +21 -0
  3. package/dist/functions/start/index.js +3 -489
  4. package/dist/functions/start/onWarn.js +1 -1
  5. package/dist/functions/start/setComponentId.js +1 -1
  6. package/dist/functions/test/index.js +7 -26
  7. package/dist/lib/build/buildFiles.js +63 -7
  8. package/dist/lib/build/buildPlugins.js +18 -15
  9. package/dist/lib/build/getCodeFrame.js +3 -4
  10. package/dist/lib/build/onWarn.js +1 -1
  11. package/dist/lib/dev/cleanup.js +15 -27
  12. package/dist/lib/dev/databases/mongodb/index.js +1 -1
  13. package/dist/lib/dev/databases/postgresql/index.js +2 -2
  14. package/dist/lib/dev/databases/providerMap.js +1 -1
  15. package/dist/lib/dev/index.js +104 -28
  16. package/dist/lib/dev/isWindows.js +5 -0
  17. package/dist/lib/dev/loadSettings.js +1 -3
  18. package/dist/lib/dev/startApp.js +43 -3
  19. package/dist/lib/dev/startDatabases.js +12 -5
  20. package/dist/lib/dev/startHMR.js +30 -5
  21. package/dist/lib/getProcessIdFromPort.js +60 -0
  22. package/dist/lib/types.js +6 -0
  23. package/package.json +1 -1
  24. package/src/cli.js +9 -0
  25. package/src/functions/index.js +21 -0
  26. package/src/functions/start/index.js +2 -691
  27. package/src/functions/start/onWarn.js +1 -1
  28. package/src/functions/start/setComponentId.js +1 -1
  29. package/src/functions/test/index.js +7 -32
  30. package/src/lib/build/buildFiles.js +74 -8
  31. package/src/lib/build/buildPlugins.js +29 -22
  32. package/src/lib/build/getCodeFrame.js +3 -4
  33. package/src/lib/build/onWarn.js +1 -1
  34. package/src/lib/dev/cleanup.js +20 -28
  35. package/src/lib/dev/databases/mongodb/index.js +1 -1
  36. package/src/lib/dev/databases/postgresql/index.js +2 -2
  37. package/src/lib/dev/databases/providerMap.js +1 -1
  38. package/src/lib/dev/index.js +124 -41
  39. package/src/lib/dev/isWindows.js +3 -0
  40. package/src/lib/dev/loadSettings.js +1 -3
  41. package/src/lib/dev/startApp.js +52 -6
  42. package/src/lib/dev/startDatabases.js +12 -6
  43. package/src/lib/dev/startHMR.js +36 -7
  44. package/src/lib/getProcessIdFromPort.js +92 -0
  45. package/src/lib/types.js +3 -0
@@ -1,16 +1,59 @@
1
- /* eslint-disable consistent-return */
1
+ import child_process from "child_process";
2
+ import path from "path";
2
3
 
3
- const actionMethod = () => {
4
+ const handleStartServerProcess = (execArgv = {}, sessionsBeforeHMRUpdate = {}) => {
4
5
  try {
5
- // Perform a single step in your action here.
6
+ process.loader.text('Starting app...');
7
+
8
+ return child_process.fork(
9
+ path.resolve(".joystick/build/index.server.js"),
10
+ [],
11
+ {
12
+ execArgv,
13
+ // NOTE: Pipe stdin, stdout, and stderr. IPC establishes a message channel so we
14
+ // communicate with the child_process.
15
+ silent: true,
16
+ env: {
17
+ FORCE_COLOR: "1",
18
+ LOGS_PATH: process.env.LOGS_PATH,
19
+ NODE_ENV: process.env.NODE_ENV,
20
+ ROOT_URL: process.env.ROOT_URL,
21
+ PORT: process.env.PORT,
22
+ JOYSTICK_SETTINGS: process.env.JOYSTICK_SETTINGS,
23
+ HMR_SESSIONS: JSON.stringify(sessionsBeforeHMRUpdate),
24
+ },
25
+ }
26
+ );
6
27
  } catch (exception) {
7
- throw new Error(`[startApp.actionMethod] ${exception.message}`);
28
+ throw new Error(`[startApp.handleStartServerProcess] ${exception.message}`);
29
+ }
30
+ };
31
+
32
+ const getExecArgs = (nodeMajorVersion = 0) => {
33
+ try {
34
+ const execArgv = ["--no-warnings"];
35
+
36
+ if (nodeMajorVersion < 19) {
37
+ execArgv.push("--experimental-specifier-resolution=node");
38
+ }
39
+
40
+ if (
41
+ process.env.NODE_ENV === "development" &&
42
+ process.env.IS_DEBUG_MODE === "true"
43
+ ) {
44
+ execArgv.push("--inspect");
45
+ }
46
+
47
+ return execArgv;
48
+ } catch (exception) {
49
+ throw new Error(`[startApp.getExecArgs] ${exception.message}`);
8
50
  }
9
51
  };
10
52
 
11
53
  const validateOptions = (options) => {
12
54
  try {
13
55
  if (!options) throw new Error('options object is required.');
56
+ if (!options.nodeMajorVersion) throw new Error('options.nodeMajorVersion is required.');
14
57
  if (!options.port) throw new Error('options.port is required.');
15
58
  } catch (exception) {
16
59
  throw new Error(`[startApp.validateOptions] ${exception.message}`);
@@ -20,8 +63,11 @@ const validateOptions = (options) => {
20
63
  const startApp = (options, { resolve, reject }) => {
21
64
  try {
22
65
  validateOptions(options);
23
- // Call action methods in sequence here.
24
- resolve();
66
+
67
+ const execArgv = getExecArgs(options?.nodeMajorVersion);
68
+ const serverProcess = handleStartServerProcess(execArgv);
69
+
70
+ return resolve(serverProcess);
25
71
  } catch (exception) {
26
72
  reject(`[startApp] ${exception.message}`);
27
73
  }
@@ -1,8 +1,8 @@
1
1
  /* eslint-disable consistent-return */
2
2
 
3
- import util from "util";
4
3
  import CLILog from "../CLILog.js";
5
4
  import providerMap from "./databases/providerMap.js";
5
+ import { isObject } from '../types.js';
6
6
 
7
7
  const startDatabaseProvider = async (
8
8
  environment = 'development',
@@ -31,13 +31,14 @@ const startDatabaseProvider = async (
31
31
  }
32
32
  };
33
33
 
34
- const startDatabaseProviders = async (databases = [], databasePortStart = 2610) => {
34
+ const startDatabaseProviders = async (databases = [], databasePortStart = 2610, environment = '') => {
35
35
  try {
36
36
  for (let i = 0; i < databases?.length; i += 1) {
37
37
  const database = databases[i];
38
38
  const hasMultipleOfProvider = (databases?.filter((database) => database?.provider === database?.provider))?.length > 1;
39
39
 
40
40
  await startDatabaseProvider(
41
+ environment,
41
42
  database,
42
43
  // NOTE: Increment each database port using index in the databases array from settings if no port
43
44
  // is assigned in the settings.
@@ -53,8 +54,8 @@ const startDatabaseProviders = async (databases = [], databasePortStart = 2610)
53
54
  const validateDatabasesFromSettings = (databases = []) => {
54
55
  try {
55
56
  const databasesNotAsObjects = databases.filter(
56
- (database) => !util.types.isKeyObject(database)
57
- );
57
+ (database) => !isObject(database)
58
+ );
58
59
 
59
60
  const userDatabases = databases.filter((database) => !!database.users);
60
61
  const queueDatabases = databases.filter((database) => !!database.queues);
@@ -95,6 +96,7 @@ const validateDatabasesFromSettings = (databases = []) => {
95
96
  const validateOptions = (options) => {
96
97
  try {
97
98
  if (!options) throw new Error('options object is required.');
99
+ if (!options.environment) throw new Error('options.environment is required.');
98
100
  if (!options.port) throw new Error('options.port is required.');
99
101
  if (!options.settings) throw new Error('options.settings is required.');
100
102
  } catch (exception) {
@@ -106,11 +108,15 @@ const startDatabases = async (options, { resolve, reject }) => {
106
108
  try {
107
109
  validateOptions(options);
108
110
 
109
- const databases = options?.settings?.databases || [];
111
+ const databases = options?.settings?.config?.databases || [];
110
112
 
111
113
  if (databases?.length > 0) {
112
114
  validateDatabasesFromSettings(databases);
113
- await startDatabaseProviders(databases, options?.port);
115
+ await startDatabaseProviders(
116
+ databases,
117
+ options?.port + 10,
118
+ options?.environment,
119
+ );
114
120
  }
115
121
 
116
122
  resolve();
@@ -1,17 +1,43 @@
1
- /* eslint-disable consistent-return */
1
+ import child_process from "child_process";
2
+ import path from "path";
2
3
 
3
- const actionMethod = () => {
4
+ const handleStartHMRProcess = (execArgv = {}, __dirname = '') => {
4
5
  try {
5
- // Perform a single step in your action here.
6
+ // NOTE: Port is automatically pulled via process.env.PORT
7
+ // in the hmrServer.js script.
8
+ return child_process.fork(
9
+ path.resolve(`${__dirname}/hmrServer.js`),
10
+ [],
11
+ {
12
+ execArgv,
13
+ // NOTE: Pipe stdin, stdout, and stderr. IPC establishes a message channel so we
14
+ // communicate with the child_process.
15
+ silent: true,
16
+ }
17
+ );
6
18
  } catch (exception) {
7
- throw new Error(`[startHMR.actionMethod] ${exception.message}`);
19
+ throw new Error(`[startHMR.handleStartHMRProcess] ${exception.message}`);
20
+ }
21
+ };
22
+
23
+ const getExecArgs = (nodeMajorVersion = 0) => {
24
+ try {
25
+ const execArgv = ["--no-warnings"];
26
+
27
+ if (nodeMajorVersion < 19) {
28
+ execArgv.push("--experimental-specifier-resolution=node");
29
+ }
30
+
31
+ return execArgv;
32
+ } catch (exception) {
33
+ throw new Error(`[startHMR.getExecArgs] ${exception.message}`);
8
34
  }
9
35
  };
10
36
 
11
37
  const validateOptions = (options) => {
12
38
  try {
13
39
  if (!options) throw new Error('options object is required.');
14
- if (!options.port) throw new Error('options.port is required.');
40
+ if (!options.nodeMajorVersion) throw new Error('options.nodeMajorVersion is required.');
15
41
  } catch (exception) {
16
42
  throw new Error(`[startHMR.validateOptions] ${exception.message}`);
17
43
  }
@@ -20,8 +46,11 @@ const validateOptions = (options) => {
20
46
  const startHMR = (options, { resolve, reject }) => {
21
47
  try {
22
48
  validateOptions(options);
23
- // Call action methods in sequence here.
24
- resolve();
49
+
50
+ const execArgv = getExecArgs(options?.nodeMajorVersion);
51
+ const hmrProcess = handleStartHMRProcess(execArgv, options?.__dirname);
52
+
53
+ return resolve(hmrProcess);
25
54
  } catch (exception) {
26
55
  reject(`[startHMR] ${exception.message}`);
27
56
  }
@@ -0,0 +1,92 @@
1
+ import condense from 'selective-whitespace';
2
+ import netstats from 'netstats';
3
+
4
+ const platform = process.platform;
5
+
6
+ function pushTo(target, item) {
7
+ if (item !== '' && typeof item === 'number' && item !== 0 && target.indexOf(item) === -1) {
8
+ target.push(item);
9
+ }
10
+ }
11
+
12
+ // Example output to parse (macOS):
13
+ // $ lsof -i :8017
14
+ /*
15
+ COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
16
+ nc 20661 michael 3u IPv4 0x3b190d9d07c2c3db 0t0 TCP *:8017 (LISTEN)
17
+ nc 21145 michael 3u IPv4 0x3b190d9d054773db 0t0 TCP *:8017 (LISTEN)
18
+ Python 21221 michael 3u IPv4 0x3b190d9ceb8dfd7b 0t0 UDP localhost:8017
19
+ */
20
+
21
+ // [WIN] $ netstat.exe -a -n -o | findstr :9000
22
+
23
+ /*
24
+ TCP 0.0.0.0:9000 0.0.0.0:0 LISTENING 5220
25
+ TCP 127.0.0.1:9000 127.0.0.1:62376 ESTABLISHED 5220
26
+ TCP 127.0.0.1:9000 127.0.0.1:62379 ESTABLISHED 5220
27
+ TCP 127.0.0.1:62288 127.0.0.1:9000 TIME_WAIT 0
28
+ TCP 127.0.0.1:62299 127.0.0.1:9000 TIME_WAIT 0
29
+ TCP 127.0.0.1:62376 127.0.0.1:9000 ESTABLISHED 7604
30
+ TCP 127.0.0.1:62378 127.0.0.1:9000 ESTABLISHED 7604
31
+ UDP 127.0.0.1:9000 *:* 1240
32
+ */
33
+
34
+ async function processNetStats(arr) {
35
+ const pidindex = 1;
36
+ let items = arr.slice(1);
37
+
38
+ if (platform === 'win32') {
39
+ items = arr;
40
+ }
41
+
42
+ const pids = {
43
+ all: [],
44
+ tcp: [],
45
+ udp: [],
46
+ };
47
+
48
+ await Promise.all(items.map((item) => {
49
+ const values = condense(item).split(' ');
50
+ let pid = parseInt(values[pidindex], 10);
51
+
52
+ if (platform === 'win32') {
53
+ pid = parseInt(values.pop(), 10);
54
+ }
55
+
56
+ if (values.length > 1) {
57
+ if (values.indexOf('TCP') !== -1) {
58
+ pushTo(pids.tcp, pid);
59
+ pushTo(pids.all, pid);
60
+ } else if (values.indexOf('UDP') !== -1) {
61
+ pushTo(pids.udp, pid);
62
+ pushTo(pids.all, pid);
63
+ }
64
+ }
65
+
66
+ return Promise.resolve();
67
+ }));
68
+
69
+ return pids;
70
+ }
71
+
72
+ function getProcessIdFromPort(port) {
73
+ if (typeof port !== 'number') {
74
+ throw new TypeError('Expected a port number');
75
+ }
76
+
77
+ return new Promise(((resolve) => {
78
+ netstats(port).then((stats) => {
79
+ processNetStats(stats).then((ps) => {
80
+ resolve(ps);
81
+ });
82
+ }).catch(() => {
83
+ resolve({
84
+ all: [],
85
+ tcp: [],
86
+ udp: [],
87
+ });
88
+ });
89
+ }));
90
+ }
91
+
92
+ export default getProcessIdFromPort;
@@ -0,0 +1,3 @@
1
+ export const isObject = (value) => {
2
+ return value !== null && !Array.isArray(value) && typeof value === 'object';
3
+ };