@tertium/hlpr 0.1.3 → 0.1.6

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,3 +1,177 @@
1
- #!/usr/bin/env node
2
-
3
- require('../dist/hlpr.js');
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.4";
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();
@@ -0,0 +1,3 @@
1
+ echo "Running build before commit..."
2
+ bun run build
3
+ git add bin/
package/package.json CHANGED
@@ -1,21 +1,23 @@
1
1
  {
2
2
  "name": "@tertium/hlpr",
3
- "version": "0.1.3",
3
+ "version": "0.1.6",
4
4
  "description": "Windows and *Nix utility for typical programming activity",
5
5
  "author": "Vitalii Balabanov",
6
6
  "email": "tertiumnon@gmail.com",
7
- "main": "dist/hlpr.js",
7
+ "main": "bin/hlpr.js",
8
8
  "type": "commonjs",
9
9
  "scripts": {
10
10
  "build": "tsc --project tsconfig.json",
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",
14
- "release:major": "node node_modules/@tertium/shared-js-configs-and-types/scripts/release.js major"
14
+ "release:major": "node node_modules/@tertium/shared-js-configs-and-types/scripts/release.js major",
15
+ "prepare": "husky"
15
16
  },
16
17
  "devDependencies": {
17
18
  "@tertium/shared-js-configs-and-types": "^0.14.0",
18
19
  "@types/node": "^22.13.10",
20
+ "husky": "^9.1.7",
19
21
  "typescript": "^5.8.3"
20
22
  },
21
23
  "bin": {
package/dist/hlpr.js DELETED
@@ -1,153 +0,0 @@
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
- // Parse command line arguments
42
- const args = process.argv.slice(2);
43
- let forceFlag = false;
44
- let commandArgs = [];
45
- // Check for -f flag
46
- if (args[0] === "-f") {
47
- forceFlag = true;
48
- commandArgs = args.slice(1);
49
- }
50
- else {
51
- commandArgs = args;
52
- }
53
- // Create readline interface for user input
54
- const rl = readline.createInterface({
55
- input: process.stdin,
56
- output: process.stdout
57
- });
58
- // Prompt for user input
59
- function prompt(question) {
60
- return new Promise((resolve) => {
61
- rl.question(question, (answer) => {
62
- resolve(answer);
63
- });
64
- });
65
- }
66
- // Execute a command with variable substitution
67
- async function executeCommand(command, variables) {
68
- // Replace variables in the command
69
- let processedCommand = command;
70
- for (const [key, value] of Object.entries(variables)) {
71
- processedCommand = processedCommand.replace(new RegExp(`{{${key}}}`, 'g'), value);
72
- }
73
- return new Promise((resolve) => {
74
- console.log(`Executing: ${processedCommand}`);
75
- const childProcess = (0, node_child_process_1.exec)(processedCommand);
76
- if (childProcess.stdout) {
77
- childProcess.stdout.on("data", (data) => {
78
- console.log(data.toString().trim());
79
- });
80
- }
81
- if (childProcess.stderr) {
82
- childProcess.stderr.on("data", (data) => {
83
- console.error(data.toString().trim());
84
- });
85
- }
86
- childProcess.on("exit", (code) => {
87
- if (code === 0) {
88
- resolve(true);
89
- }
90
- else {
91
- console.error(`Command failed with exit code ${code}`);
92
- resolve(false);
93
- }
94
- });
95
- });
96
- }
97
- async function main() {
98
- if (commandArgs.length === 0) {
99
- console.error("Please provide a command. Example: hlpr ssh init-dir");
100
- process.exit(1);
101
- }
102
- try {
103
- // Get the category (first argument) and the rest of the command
104
- const category = commandArgs[0];
105
- const restArgs = commandArgs.slice(1);
106
- // Join the rest of the arguments without hyphens
107
- const scriptName = restArgs.join("");
108
- const scriptPath = path.join(process.cwd(), "commands", category, `${scriptName}.sh`);
109
- // Check if the script exists
110
- if (!fs.existsSync(scriptPath)) {
111
- console.error(`Script not found: ${scriptPath}`);
112
- process.exit(1);
113
- }
114
- // Read the script content
115
- const scriptContent = await (0, promises_1.readFile)(scriptPath, "utf-8");
116
- const lines = scriptContent.split("\n");
117
- // Extract variables from the script (anything in {{variable}})
118
- const variableRegex = /{{([^}]+)}}/g;
119
- const variables = {};
120
- const uniqueVars = new Set();
121
- lines.forEach(line => {
122
- let match;
123
- while ((match = variableRegex.exec(line)) !== null) {
124
- uniqueVars.add(match[1]);
125
- }
126
- });
127
- // Prompt for each variable
128
- for (const varName of uniqueVars) {
129
- const value = await prompt(`Enter ${varName}: `);
130
- variables[varName] = value;
131
- }
132
- // Execute each line of the script
133
- for (const line of lines) {
134
- if (!line.trim())
135
- continue;
136
- const success = await executeCommand(line, variables);
137
- // If command failed and force flag is not set, exit
138
- if (!success && !forceFlag) {
139
- console.error("Command failed, stopping execution.");
140
- process.exit(1);
141
- }
142
- }
143
- console.log("Command completed successfully!");
144
- }
145
- catch (error) {
146
- console.error("Error:", error);
147
- process.exit(1);
148
- }
149
- finally {
150
- rl.close();
151
- }
152
- }
153
- main();
File without changes