@tertium/hlpr 0.1.8 → 0.1.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.
Files changed (3) hide show
  1. package/bin/hlpr.js +1 -1
  2. package/index.js +136 -0
  3. package/package.json +3 -2
package/bin/hlpr.js CHANGED
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- require('../index.js');
3
+ require('./index.js');
package/index.js ADDED
@@ -0,0 +1,136 @@
1
+ import { createRequire } from "node:module";
2
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
3
+
4
+ // src/index.ts
5
+ import { readFile } from "node:fs/promises";
6
+ import { exec } from "node:child_process";
7
+ import * as path from "node:path";
8
+ import * as fs from "node:fs";
9
+ import * as readline from "node:readline";
10
+ import * as os from "node:os";
11
+ var __dirname = "/home/runner/work/tertium-hlpr/tertium-hlpr/src";
12
+ async function getVersion() {
13
+ try {
14
+ const packagePath = path.join(path.dirname(path.dirname(__dirname)), "package.json");
15
+ const packageJson = JSON.parse(await readFile(packagePath, "utf-8"));
16
+ return packageJson.version;
17
+ } catch (error) {
18
+ console.error("Error reading package.json:", error);
19
+ return "0.0.0";
20
+ }
21
+ }
22
+ function detectShell() {
23
+ const isWindows = os.platform() === "win32";
24
+ if (isWindows) {
25
+ return "powershell";
26
+ }
27
+ return "bash";
28
+ }
29
+ var args = process.argv.slice(2);
30
+ var forceFlag = false;
31
+ var commandArgs = [];
32
+ if (args[0] === "-f") {
33
+ forceFlag = true;
34
+ commandArgs = args.slice(1);
35
+ } else {
36
+ commandArgs = args;
37
+ }
38
+ var rl = readline.createInterface({
39
+ input: process.stdin,
40
+ output: process.stdout
41
+ });
42
+ function prompt(question) {
43
+ return new Promise((resolve) => {
44
+ rl.question(question, (answer) => {
45
+ resolve(answer);
46
+ });
47
+ });
48
+ }
49
+ async function executeCommand(command, variables) {
50
+ let processedCommand = command;
51
+ for (const [key, value] of Object.entries(variables)) {
52
+ processedCommand = processedCommand.replace(new RegExp(`{{${key}}}`, "g"), value);
53
+ }
54
+ return new Promise((resolve) => {
55
+ console.log(`Executing: ${processedCommand}`);
56
+ const childProcess = exec(processedCommand);
57
+ if (childProcess.stdout) {
58
+ childProcess.stdout.on("data", (data) => {
59
+ console.log(data.toString().trim());
60
+ });
61
+ }
62
+ if (childProcess.stderr) {
63
+ childProcess.stderr.on("data", (data) => {
64
+ console.error(data.toString().trim());
65
+ });
66
+ }
67
+ childProcess.on("exit", (code) => {
68
+ if (code === 0) {
69
+ resolve(true);
70
+ } else {
71
+ console.error(`Command failed with exit code ${code}`);
72
+ resolve(false);
73
+ }
74
+ });
75
+ });
76
+ }
77
+ async function main() {
78
+ if (commandArgs[0] === "--version" || commandArgs[0] === "-v") {
79
+ const version = await getVersion();
80
+ console.log(`hlpr version ${version}`);
81
+ process.exit(0);
82
+ }
83
+ if (commandArgs.length === 0) {
84
+ console.error("Please provide a command. Example: hlpr ssh init-dir");
85
+ process.exit(1);
86
+ }
87
+ try {
88
+ const category = commandArgs[0];
89
+ const restArgs = commandArgs.slice(1);
90
+ const scriptName = restArgs.join("");
91
+ const scriptDir = path.dirname(path.dirname(__dirname));
92
+ const scriptPath = path.join(scriptDir, "commands", category, `${scriptName}.sh`);
93
+ if (!fs.existsSync(scriptPath)) {
94
+ console.error(`Script not found: ${scriptPath}`);
95
+ process.exit(1);
96
+ }
97
+ const scriptContent = await readFile(scriptPath, "utf-8");
98
+ const variableRegex = /{{([^}]+)}}/g;
99
+ const variables = {};
100
+ const uniqueVars = new Set;
101
+ let match;
102
+ while ((match = variableRegex.exec(scriptContent)) !== null) {
103
+ uniqueVars.add(match[1]);
104
+ }
105
+ for (const varName of uniqueVars) {
106
+ const value = await prompt(`Enter ${varName}: `);
107
+ variables[varName] = value;
108
+ }
109
+ let processedScript = scriptContent;
110
+ for (const [key, value] of Object.entries(variables)) {
111
+ processedScript = processedScript.replace(new RegExp(`{{${key}}}`, "g"), value);
112
+ }
113
+ const tempScriptPath = path.join(path.dirname(scriptPath), `_temp_${path.basename(scriptPath)}`);
114
+ fs.writeFileSync(tempScriptPath, processedScript);
115
+ const shell = detectShell();
116
+ const command = shell === "powershell" ? `powershell -File "${tempScriptPath}"` : `bash "${tempScriptPath}"`;
117
+ const success = await executeCommand(command, {});
118
+ fs.unlinkSync(tempScriptPath);
119
+ if (!success && !forceFlag) {
120
+ console.error("Command failed, stopping execution.");
121
+ process.exit(1);
122
+ }
123
+ console.log("Command completed successfully!");
124
+ } catch (error) {
125
+ console.error("Error:", error);
126
+ process.exit(1);
127
+ } finally {
128
+ rl.close();
129
+ }
130
+ }
131
+ if (__require.main == __require.module) {
132
+ main();
133
+ }
134
+ export {
135
+ main
136
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tertium/hlpr",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Windows and *Nix utility for typical programming activity",
5
5
  "author": "Vitalii Balabanov",
6
6
  "email": "tertiumnon@gmail.com",
@@ -24,7 +24,8 @@
24
24
  "hlpr": "./bin/hlpr.js"
25
25
  },
26
26
  "files": [
27
- "commands"
27
+ "commands",
28
+ "index.js"
28
29
  ],
29
30
  "keywords": [
30
31
  "cli",