@hologit/lens-lib 0.1.0

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 (2) hide show
  1. package/index.js +148 -0
  2. package/package.json +13 -0
package/index.js ADDED
@@ -0,0 +1,148 @@
1
+ const { execFile } = require('child_process');
2
+
3
+ class LensRunner {
4
+ constructor(options = {}) {
5
+ this.workTree = process.env.GIT_WORK_TREE;
6
+ this.options = options;
7
+ }
8
+
9
+ // Execute command and optionally capture output
10
+ async execCommand(cmd, args = [], options = {}) {
11
+ console.error(`executing: ${cmd} ${args.join(' ')}`);
12
+
13
+ return new Promise((resolve, reject) => {
14
+ const child = execFile(cmd, args, {
15
+ ...options,
16
+ env: {
17
+ ...process.env,
18
+ ...options.env
19
+ }
20
+ });
21
+ let stdout = '';
22
+
23
+ child.stdout.on('data', (data) => {
24
+ if (options.$captureOutput) {
25
+ stdout += data;
26
+ } else {
27
+ console.error('::'+data.toString().trimEnd().replace(/\n/, '::\n'));
28
+ }
29
+ });
30
+
31
+ child.stderr.on('data', (data) => {
32
+ console.error('::'+data.toString().trimEnd().replace(/\n/, '::\n'));
33
+ });
34
+
35
+ child.on('error', reject);
36
+ child.on('close', (code) => {
37
+ if (code === 0) {
38
+ resolve(options.$captureOutput ? stdout : null);
39
+ } else {
40
+ reject(new Error(`Command failed with code ${code}`));
41
+ }
42
+ });
43
+ });
44
+ }
45
+
46
+ // Execute command and capture output
47
+ async captureCommand(cmd, args = [], options = {}) {
48
+ return this.execCommand(cmd, args, { ...options, $captureOutput: true });
49
+ }
50
+
51
+ // Log environment variables starting with HOLO
52
+ logHoloEnv() {
53
+ Object.entries(process.env)
54
+ .filter(([key]) => key.startsWith('HOLO'))
55
+ .forEach(([key, value]) => console.error(`${key}=${value}`));
56
+ }
57
+
58
+ // Export git tree
59
+ async exportTree(treeHash) {
60
+ console.error(`Exporting tree to scratch directory: ${treeHash}`);
61
+ await this.execCommand('git', ['holo', 'lens', 'export-tree', treeHash]);
62
+ }
63
+
64
+ // Add files to git index
65
+ async addToIndex(path, force = true) {
66
+ const args = ['add'];
67
+ if (force) args.push('-f');
68
+ args.push(path);
69
+ await this.execCommand('git', args);
70
+ }
71
+
72
+ // Write tree and get hash
73
+ async writeTree(prefix) {
74
+ const args = ['write-tree'];
75
+ if (prefix) args.push('--prefix=' + prefix);
76
+ return await this.captureCommand('git', args);
77
+ }
78
+
79
+ // Run the lens with common setup and error handling
80
+ async run(callback) {
81
+ try {
82
+ const inputTree = process.argv[2];
83
+ if (!inputTree) {
84
+ throw new Error('Input tree argument required');
85
+ }
86
+
87
+ // Log HOLO environment variables
88
+ this.logHoloEnv();
89
+
90
+ // Change to work tree directory
91
+ process.chdir(this.workTree);
92
+
93
+ // Export git tree
94
+ await this.exportTree(inputTree);
95
+
96
+ // Run the lens-specific logic
97
+ const result = await callback(this);
98
+
99
+ // Output result
100
+ if (typeof result === 'string') {
101
+ process.stdout.write(result);
102
+ }
103
+
104
+ } catch (error) {
105
+ console.error(error);
106
+ process.exit(1);
107
+ }
108
+ }
109
+
110
+ // Helper to set up common environment variables
111
+ setupEnv(envVars) {
112
+ Object.entries(envVars).forEach(([key, value]) => {
113
+ if (value !== undefined) {
114
+ process.env[key] = value;
115
+ }
116
+ });
117
+ }
118
+
119
+ // Helper to ensure required environment variables exist
120
+ requireEnv(...vars) {
121
+ vars.forEach(varName => {
122
+ if (!process.env[varName]) {
123
+ throw new Error(`${varName} environment variable is required`);
124
+ }
125
+ });
126
+ }
127
+
128
+ // Helper to get environment variables with defaults
129
+ getEnv(varName, defaultValue) {
130
+ return process.env[varName] || defaultValue;
131
+ }
132
+
133
+ // Helper to create output directory
134
+ async createOutputDir(dir) {
135
+ const fs = require('fs');
136
+ fs.mkdirSync(dir, { recursive: true });
137
+ }
138
+
139
+ // Helper to write file
140
+ async writeFile(path, content) {
141
+ const fs = require('fs');
142
+ fs.writeFileSync(path, content);
143
+ }
144
+ }
145
+
146
+ module.exports = {
147
+ LensRunner
148
+ };
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@hologit/lens-lib",
3
+ "version": "0.1.0",
4
+ "description": "Shared library for hologit lenses",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "",
10
+ "license": "MIT",
11
+ "dependencies": {
12
+ }
13
+ }