@tertium/hlpr 0.1.7 → 0.1.9

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/hlpr.js CHANGED
@@ -1,177 +1,3 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- const promises_1 = require("node:fs/promises");
37
- const node_child_process_1 = require("node:child_process");
38
- const path = __importStar(require("node:path"));
39
- const fs = __importStar(require("node:fs"));
40
- const readline = __importStar(require("node:readline"));
41
- const os = __importStar(require("node:os"));
42
- // Package version from package.json
43
- const VERSION = "0.1.6";
44
- // Detect shell type
45
- function detectShell() {
46
- const isWindows = os.platform() === "win32";
47
- if (isWindows) {
48
- return "powershell";
49
- }
50
- return "bash"; // Default to bash for Unix-like systems
51
- }
52
- // Parse command line arguments
53
- const args = process.argv.slice(2);
54
- let forceFlag = false;
55
- let commandArgs = [];
56
- // Check for -f flag
57
- if (args[0] === "-f") {
58
- forceFlag = true;
59
- commandArgs = args.slice(1);
60
- }
61
- else {
62
- commandArgs = args;
63
- }
64
- // Create readline interface for user input
65
- const rl = readline.createInterface({
66
- input: process.stdin,
67
- output: process.stdout
68
- });
69
- // Prompt for user input
70
- function prompt(question) {
71
- return new Promise((resolve) => {
72
- rl.question(question, (answer) => {
73
- resolve(answer);
74
- });
75
- });
76
- }
77
- // Execute a command with variable substitution
78
- async function executeCommand(command, variables) {
79
- // Replace variables in the command
80
- let processedCommand = command;
81
- for (const [key, value] of Object.entries(variables)) {
82
- processedCommand = processedCommand.replace(new RegExp(`{{${key}}}`, 'g'), value);
83
- }
84
- return new Promise((resolve) => {
85
- console.log(`Executing: ${processedCommand}`);
86
- const childProcess = (0, node_child_process_1.exec)(processedCommand);
87
- if (childProcess.stdout) {
88
- childProcess.stdout.on("data", (data) => {
89
- console.log(data.toString().trim());
90
- });
91
- }
92
- if (childProcess.stderr) {
93
- childProcess.stderr.on("data", (data) => {
94
- console.error(data.toString().trim());
95
- });
96
- }
97
- childProcess.on("exit", (code) => {
98
- if (code === 0) {
99
- resolve(true);
100
- }
101
- else {
102
- console.error(`Command failed with exit code ${code}`);
103
- resolve(false);
104
- }
105
- });
106
- });
107
- }
108
- async function main() {
109
- // Check for version flag
110
- if (commandArgs[0] === "--version" || commandArgs[0] === "-v") {
111
- console.log(`hlpr version ${VERSION}`);
112
- process.exit(0);
113
- }
114
- if (commandArgs.length === 0) {
115
- console.error("Please provide a command. Example: hlpr ssh init-dir");
116
- process.exit(1);
117
- }
118
- try {
119
- // Get the category (first argument) and the rest of the command
120
- const category = commandArgs[0];
121
- const restArgs = commandArgs.slice(1);
122
- // Join the rest of the arguments without hyphens
123
- const scriptName = restArgs.join("");
124
- // Get the directory where the hlpr script is installed
125
- const scriptDir = path.dirname(path.dirname(__dirname));
126
- const scriptPath = path.join(scriptDir, "commands", category, `${scriptName}.sh`);
127
- // Check if the script exists
128
- if (!fs.existsSync(scriptPath)) {
129
- console.error(`Script not found: ${scriptPath}`);
130
- process.exit(1);
131
- }
132
- // Read the script content
133
- const scriptContent = await (0, promises_1.readFile)(scriptPath, "utf-8");
134
- // Extract variables from the script (anything in {{variable}})
135
- const variableRegex = /{{([^}]+)}}/g;
136
- const variables = {};
137
- const uniqueVars = new Set();
138
- let match;
139
- while ((match = variableRegex.exec(scriptContent)) !== null) {
140
- uniqueVars.add(match[1]);
141
- }
142
- // Prompt for each variable
143
- for (const varName of uniqueVars) {
144
- const value = await prompt(`Enter ${varName}: `);
145
- variables[varName] = value;
146
- }
147
- // Create a temporary script with variables substituted
148
- let processedScript = scriptContent;
149
- for (const [key, value] of Object.entries(variables)) {
150
- processedScript = processedScript.replace(new RegExp(`{{${key}}}`, 'g'), value);
151
- }
152
- const tempScriptPath = path.join(path.dirname(scriptPath), `_temp_${path.basename(scriptPath)}`);
153
- fs.writeFileSync(tempScriptPath, processedScript);
154
- // Execute the temporary script with appropriate shell
155
- const shell = detectShell();
156
- const command = shell === "powershell"
157
- ? `powershell -File "${tempScriptPath}"`
158
- : `bash "${tempScriptPath}"`;
159
- const success = await executeCommand(command, {});
160
- // Clean up the temporary script
161
- fs.unlinkSync(tempScriptPath);
162
- // If command failed and force flag is not set, exit
163
- if (!success && !forceFlag) {
164
- console.error("Command failed, stopping execution.");
165
- process.exit(1);
166
- }
167
- console.log("Command completed successfully!");
168
- }
169
- catch (error) {
170
- console.error("Error:", error);
171
- process.exit(1);
172
- }
173
- finally {
174
- rl.close();
175
- }
176
- }
177
- main();
1
+ #!/usr/bin/env node
2
+
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,13 +1,13 @@
1
1
  {
2
2
  "name": "@tertium/hlpr",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "Windows and *Nix utility for typical programming activity",
5
5
  "author": "Vitalii Balabanov",
6
6
  "email": "tertiumnon@gmail.com",
7
7
  "main": "bin/hlpr.js",
8
8
  "type": "commonjs",
9
9
  "scripts": {
10
- "build": "tsc --project tsconfig.json",
10
+ "build": "bun build ./src/index.ts --outfile ./index.js --target node",
11
11
  "prepublishOnly": "npm run build",
12
12
  "release:minor": "node node_modules/@tertium/shared-js-configs-and-types/scripts/release.js minor",
13
13
  "release:patch": "node node_modules/@tertium/shared-js-configs-and-types/scripts/release.js patch",
@@ -24,9 +24,8 @@
24
24
  "hlpr": "./bin/hlpr.js"
25
25
  },
26
26
  "files": [
27
- "dist",
28
- "bin",
29
- "commands"
27
+ "commands",
28
+ "index.js"
30
29
  ],
31
30
  "keywords": [
32
31
  "cli",
package/bin/hlpr.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};