@dataramen/cli 0.0.3 → 0.0.4
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/bin/run.js +47 -5
- package/dist/code/cli.js +135 -0
- package/dist/code/register.windows.js +44 -0
- package/dist/package.json +1 -1
- package/package.json +1 -2
package/bin/run.js
CHANGED
|
@@ -11,6 +11,9 @@ const processName = "@dataramen/local-server";
|
|
|
11
11
|
const homeDir = os.homedir();
|
|
12
12
|
const filesPath = path.join(homeDir, ".dataramen", ".runtime", "server");
|
|
13
13
|
|
|
14
|
+
const scriptPkg = fs.readJsonSync(path.join(__dirname, "..", "package.json"));
|
|
15
|
+
const protocol = "dataramen://";
|
|
16
|
+
|
|
14
17
|
yargs()
|
|
15
18
|
.command("start", 'Default command, start/restart the server', () => {
|
|
16
19
|
const hasPm2 = checkPm2();
|
|
@@ -30,7 +33,26 @@ yargs()
|
|
|
30
33
|
stdio: "inherit",
|
|
31
34
|
})
|
|
32
35
|
})
|
|
33
|
-
.
|
|
36
|
+
.command("unregister", "Unregister custom protocol handler", () => {
|
|
37
|
+
if (os.platform() === "win32") {
|
|
38
|
+
require(path.join(__dirname, "..", "dist", "code", "register.windows.js")).unregister();
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
.parse(isBrowserStart() ? getBrowserArgs() : yargsHelpers.hideBin(process.argv));
|
|
42
|
+
|
|
43
|
+
function isInstallation () {
|
|
44
|
+
return scriptPkg.name === "@dataramen/cli";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isBrowserStart () {
|
|
48
|
+
return !!process.argv[2]?.startsWith(protocol);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getBrowserArgs () {
|
|
52
|
+
const str = (process.argv[2] || '').replace(protocol, ''); // remove protocol
|
|
53
|
+
console.log("Browser args", str.split("/"));
|
|
54
|
+
return str.split("/");
|
|
55
|
+
}
|
|
34
56
|
|
|
35
57
|
function checkPm2 () {
|
|
36
58
|
try {
|
|
@@ -58,6 +80,18 @@ function stopExisting () {
|
|
|
58
80
|
} catch (e) {}
|
|
59
81
|
}
|
|
60
82
|
|
|
83
|
+
function registerServer () {
|
|
84
|
+
try {
|
|
85
|
+
if (os.platform() === "win32") {
|
|
86
|
+
require(path.join(__dirname, "..", "dist", "code", "register.windows.js")).register();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
console.log("Custom protocol handler registered");
|
|
90
|
+
} catch (e) {
|
|
91
|
+
console.log("Failed to register server", e);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
61
95
|
function installServer () {
|
|
62
96
|
console.log("Installing Server");
|
|
63
97
|
// copy code
|
|
@@ -67,24 +101,32 @@ function installServer () {
|
|
|
67
101
|
// copy package.json
|
|
68
102
|
fs.copySync(path.join(__dirname, '..', 'dist', "package.json"), path.join(filesPath, "package.json"));
|
|
69
103
|
|
|
70
|
-
execSync(`npm i
|
|
104
|
+
execSync(`npm i`, {
|
|
71
105
|
stdio: "inherit",
|
|
72
106
|
cwd: filesPath,
|
|
73
107
|
});
|
|
74
108
|
console.log("Server installed");
|
|
109
|
+
|
|
110
|
+
registerServer();
|
|
75
111
|
}
|
|
76
112
|
|
|
77
113
|
function start () {
|
|
78
114
|
stopExisting();
|
|
79
115
|
|
|
80
116
|
try {
|
|
81
|
-
|
|
117
|
+
if (isInstallation()) {
|
|
118
|
+
// only install if script is running from @dataramen/cli package
|
|
119
|
+
// if not, it means it was already installed, and the script is running from within the server folder
|
|
120
|
+
installServer();
|
|
121
|
+
}
|
|
122
|
+
|
|
82
123
|
console.log("Starting local server");
|
|
83
|
-
const
|
|
84
|
-
execSync(`pm2 start "${
|
|
124
|
+
const appPkg = fs.readJsonSync(path.join(filesPath, "package.json"));
|
|
125
|
+
execSync(`pm2 start "${appPkg.main}" --name "${processName}" --no-autorestart`, {
|
|
85
126
|
stdio: "inherit",
|
|
86
127
|
cwd: filesPath,
|
|
87
128
|
});
|
|
129
|
+
|
|
88
130
|
console.log(`Local server will be available in a couple of seconds`);
|
|
89
131
|
console.log(`You can close this window`);
|
|
90
132
|
} catch (e) {
|
package/dist/code/cli.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { execSync } = require("node:child_process");
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
const yargs = require("yargs");
|
|
5
|
+
const yargsHelpers = require("yargs/helpers");
|
|
6
|
+
const os = require('node:os');
|
|
7
|
+
const fs = require("fs-extra");
|
|
8
|
+
|
|
9
|
+
const processName = "@dataramen/local-server";
|
|
10
|
+
|
|
11
|
+
const homeDir = os.homedir();
|
|
12
|
+
const filesPath = path.join(homeDir, ".dataramen", ".runtime", "server");
|
|
13
|
+
|
|
14
|
+
const scriptPkg = fs.readJsonSync(path.join(__dirname, "..", "package.json"));
|
|
15
|
+
const protocol = "dataramen://";
|
|
16
|
+
|
|
17
|
+
yargs()
|
|
18
|
+
.command("start", 'Default command, start/restart the server', () => {
|
|
19
|
+
const hasPm2 = checkPm2();
|
|
20
|
+
if (!hasPm2) {
|
|
21
|
+
installPm2();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
start();
|
|
25
|
+
})
|
|
26
|
+
.command("logs", 'Listen for logs', () => {
|
|
27
|
+
execSync(`pm2 logs ${processName}`, {
|
|
28
|
+
stdio: "inherit",
|
|
29
|
+
});
|
|
30
|
+
})
|
|
31
|
+
.command("stop", 'Stop the server', () => {
|
|
32
|
+
execSync(`pm2 stop ${processName}`, {
|
|
33
|
+
stdio: "inherit",
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
.command("unregister", "Unregister custom protocol handler", () => {
|
|
37
|
+
if (os.platform() === "win32") {
|
|
38
|
+
require(path.join(__dirname, "..", "dist", "code", "register.windows.js")).unregister();
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
.parse(isBrowserStart() ? getBrowserArgs() : yargsHelpers.hideBin(process.argv));
|
|
42
|
+
|
|
43
|
+
function isInstallation () {
|
|
44
|
+
return scriptPkg.name === "@dataramen/cli";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isBrowserStart () {
|
|
48
|
+
return !!process.argv[2]?.startsWith(protocol);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getBrowserArgs () {
|
|
52
|
+
const str = (process.argv[2] || '').replace(protocol, ''); // remove protocol
|
|
53
|
+
console.log("Browser args", str.split("/"));
|
|
54
|
+
return str.split("/");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function checkPm2 () {
|
|
58
|
+
try {
|
|
59
|
+
execSync(`pm2 -v`, {
|
|
60
|
+
stdio: "inherit"
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return true;
|
|
64
|
+
} catch (e) {
|
|
65
|
+
console.error("Library does not exist");
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function installPm2 () {
|
|
71
|
+
console.log("Install Pm2");
|
|
72
|
+
execSync(`npm i -g pm2`, {
|
|
73
|
+
stdio: "inherit"
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function stopExisting () {
|
|
78
|
+
try {
|
|
79
|
+
execSync(`pm2 stop "${processName}"`);
|
|
80
|
+
} catch (e) {}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function registerServer () {
|
|
84
|
+
try {
|
|
85
|
+
if (os.platform() === "win32") {
|
|
86
|
+
require(path.join(__dirname, "..", "dist", "code", "register.windows.js")).register();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
console.log("Custom protocol handler registered");
|
|
90
|
+
} catch (e) {
|
|
91
|
+
console.log("Failed to register server", e);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function installServer () {
|
|
96
|
+
console.log("Installing Server");
|
|
97
|
+
// copy code
|
|
98
|
+
fs.copySync(path.join(__dirname, '..', 'dist', "code"), path.join(filesPath, "code"));
|
|
99
|
+
// copy default env (do not override existing .env)
|
|
100
|
+
fs.copySync(path.join(__dirname, '..', 'dist', "env", ".env.default"), path.join(filesPath, "env", ".env.default"));
|
|
101
|
+
// copy package.json
|
|
102
|
+
fs.copySync(path.join(__dirname, '..', 'dist', "package.json"), path.join(filesPath, "package.json"));
|
|
103
|
+
|
|
104
|
+
execSync(`npm i`, {
|
|
105
|
+
stdio: "inherit",
|
|
106
|
+
cwd: filesPath,
|
|
107
|
+
});
|
|
108
|
+
console.log("Server installed");
|
|
109
|
+
|
|
110
|
+
registerServer();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function start () {
|
|
114
|
+
stopExisting();
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
if (isInstallation()) {
|
|
118
|
+
// only install if script is running from @dataramen/cli package
|
|
119
|
+
// if not, it means it was already installed, and the script is running from within the server folder
|
|
120
|
+
installServer();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
console.log("Starting local server");
|
|
124
|
+
const appPkg = fs.readJsonSync(path.join(filesPath, "package.json"));
|
|
125
|
+
execSync(`pm2 start "${appPkg.main}" --name "${processName}" --no-autorestart`, {
|
|
126
|
+
stdio: "inherit",
|
|
127
|
+
cwd: filesPath,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
console.log(`Local server will be available in a couple of seconds`);
|
|
131
|
+
console.log(`You can close this window`);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
console.error(`Failed to start local server`, e);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const os = require("node:os");
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const { execSync } = require("node:child_process");
|
|
4
|
+
|
|
5
|
+
const homeDir = os.homedir();
|
|
6
|
+
const filesPath = path.join(homeDir, ".dataramen", ".runtime", "server");
|
|
7
|
+
const scriptPath = path.join(filesPath, "code", "cli.js");
|
|
8
|
+
|
|
9
|
+
const protocol = "dataramen";
|
|
10
|
+
|
|
11
|
+
function register () {
|
|
12
|
+
// Registry command (uses reg.exe)
|
|
13
|
+
const regCommands = [
|
|
14
|
+
`reg add "HKCU\\Software\\Classes\\${protocol}" /ve /d "Data Ramen" /f`,
|
|
15
|
+
`reg add "HKCU\\Software\\Classes\\${protocol}" /v "URL Protocol" /d "" /f`,
|
|
16
|
+
`reg add "HKCU\\Software\\Classes\\${protocol}\\shell\\open\\command" /ve /d "\\"C:\\\\Windows\\\\System32\\\\cmd.exe\\" /c node \\"${scriptPath}\\" \\"%1\\"" /f`
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
for (const cmd of regCommands) {
|
|
21
|
+
execSync(cmd, { stdio: "inherit" });
|
|
22
|
+
}
|
|
23
|
+
console.log(`Custom protocol '${protocol}://' registered to run: node ${scriptPath}`);
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.error("Failed to register protocol. Try running this script as Administrator.");
|
|
26
|
+
console.error(err.message);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function unregister () {
|
|
31
|
+
try {
|
|
32
|
+
// Delete the custom protocol registry key
|
|
33
|
+
execSync(`reg delete "HKCU\\Software\\Classes\\${protocol}" /f`, { stdio: "inherit" });
|
|
34
|
+
console.log(`Protocol '${protocol}://' successfully unregistered.`);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
console.error(`Failed to unregister protocol '${protocol}://'. Try running as Administrator if needed.`);
|
|
37
|
+
console.error(err.message);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = {
|
|
42
|
+
register,
|
|
43
|
+
unregister,
|
|
44
|
+
};
|
package/dist/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@
|
|
1
|
+
{"name":"@dataramen/local-server","version":"0.0.30","license":"MIT","main":"code/proxy.js","dependencies":{"@fastify/cors":"^11.0.1","dotenv":"^16.5.0","fast-glob":"^3.3.3","fastify":"^5.3.2","mysql2":"^3.14.1","pg":"^8.15.6","sqlite3":"^5.1.7","typeorm":"^0.3.23"},"devDependencies":{"yargs":"^18.0.0","fs-extra":"^11.3.0"}}
|