@tertium/hlpr 0.1.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Vitalii Balabanov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # hlpr
2
+
3
+ A CLI utility for running shell scripts with variable substitution.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ # Install globally
9
+ npm install -g @tertium/hlpr
10
+
11
+ # Or with yarn
12
+ yarn global add @tertium/hlpr
13
+
14
+ # Or locally in your project
15
+ npm install @tertium/hlpr
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```bash
21
+ # Basic usage
22
+ hlpr <category> <command>
23
+
24
+ # Examples
25
+ hlpr git fodd
26
+ hlpr hello world
27
+ hlpr ssh init dir
28
+
29
+ # Continue execution even if commands fail
30
+ hlpr -f ssh init dir
31
+ ```
32
+
33
+ ## How It Works
34
+
35
+ 1. The command `hlpr hello world` looks for a script at `commands/hello/world.sh`
36
+ 2. If the script contains variables like `{{name}}`, you'll be prompted to enter values
37
+ 3. Each line of the script is executed with the variables replaced
38
+
39
+ ## Directory Structure
40
+
41
+ ```
42
+ commands/
43
+ ├── git/
44
+ │ └── fodd.sh
45
+ ├── hello/
46
+ │ └── world.sh
47
+ ├── nvm/
48
+ │ ├── install.sh
49
+ │ └── lts.sh
50
+ └── ssh/
51
+ └── initdir.sh
52
+ ```
53
+
54
+ ## Adding Your Own Scripts
55
+
56
+ 1. Create a directory structure in `commands/<category>/`
57
+ 2. Add your `.sh` script files
58
+ 3. Use `{{variable}}` syntax for user inputs
59
+
60
+ Example script (`commands/hello/world.sh`):
61
+ ```bash
62
+ echo "Hello World, {{name}}"
63
+ ```
64
+
65
+ ## Command Naming
66
+
67
+ The utility maps command arguments to script files:
68
+ - `hlpr git fodd` → runs `commands/git/fodd.sh`
69
+ - `hlpr hello world` → runs `commands/hello/world.sh`
70
+ - `hlpr ssh init dir` → runs `commands/ssh/initdir.sh`
71
+
72
+ ## Error Handling
73
+
74
+ By default, the utility stops execution if any command fails. Use the `-f` flag to continue execution despite failures:
75
+
76
+ ```bash
77
+ # Stop on first failure (default)
78
+ hlpr ssh init dir
79
+
80
+ # Continue despite failures
81
+ hlpr -f ssh init dir
82
+ ```
83
+
84
+ ## Development
85
+
86
+ ```bash
87
+ # Clone the repository
88
+ git clone https://github.com/tertiumnon/hlpr.git
89
+
90
+ # Install dependencies
91
+ npm install
92
+
93
+ # Build the project
94
+ npm run build
95
+
96
+ # Link for local development
97
+ npm link
98
+ ```
99
+
100
+ ## License
101
+
102
+ MIT
package/bin/hlpr.js ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../dist/hlpr.js');
@@ -0,0 +1 @@
1
+ git fetch origin develop:develop
@@ -0,0 +1 @@
1
+ echo "Hello World, {{name}}"
@@ -0,0 +1 @@
1
+ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
@@ -0,0 +1,2 @@
1
+ nvm install --lts
2
+ nvm use --lts
@@ -0,0 +1,6 @@
1
+ mkdir ~/.ssh
2
+ touch ~/.ssh/known_hosts
3
+ touch ~/.ssh/config
4
+ chmod 700 ~/.ssh
5
+ chmod 644 ~/.ssh/known_hosts
6
+ chmod 644 ~/.ssh/config
package/dist/hlpr.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/hlpr.js ADDED
@@ -0,0 +1,153 @@
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();
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@tertium/hlpr",
3
+ "version": "0.1.2",
4
+ "description": "Windows and *Nix utility for typical programming activity",
5
+ "author": "Vitalii Balabanov",
6
+ "email": "tertiumnon@gmail.com",
7
+ "main": "dist/hlpr.js",
8
+ "type": "commonjs",
9
+ "scripts": {
10
+ "build": "tsc --project tsconfig.json",
11
+ "prepublishOnly": "npm run build",
12
+ "release:minor": "node_modules/@tertium/shared-js-configs-and-types/scripts/release.js --minor",
13
+ "release:patch": "node_modules/@tertium/shared-js-configs-and-types/scripts/release.js --patch",
14
+ "release:major": "node_modules/@tertium/shared-js-configs-and-types/scripts/release.js --major"
15
+ },
16
+ "devDependencies": {
17
+ "@tertium/shared-js-configs-and-types": "^0.14.0",
18
+ "@types/node": "^22.13.10",
19
+ "typescript": "^5.8.3"
20
+ },
21
+ "bin": {
22
+ "hlpr": "./bin/hlpr.js"
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "bin",
27
+ "commands"
28
+ ],
29
+ "keywords": [
30
+ "cli",
31
+ "utility",
32
+ "shell",
33
+ "scripts"
34
+ ],
35
+ "engines": {
36
+ "node": ">=14.0.0"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/tertiumnon/hlpr.git"
41
+ },
42
+ "license": "MIT"
43
+ }