MCPcopy
hub / github.com/bytenode/bytenode / compileElectronCode

Function compileElectronCode

lib/index.js:161–221  ·  view source on GitHub ↗
(javascriptCode, options)

Source from the content-addressed store, hash-verified

159 * @returns {Promise<Buffer>} - returns a Promise which resolves in the generated bytecode.
160 */
161const compileElectronCode = function (javascriptCode, options) {
162 return new Promise((resolve, reject) => {
163 function onEnd () {
164 if (options.compress) data = brotliCompressSync(data);
165
166 resolve(data);
167 }
168
169 options = options || {};
170
171 let data = Buffer.from([]);
172
173 // Resolve `electron` lazily and only when no explicit path was given. This lets
174 // consumers (e.g. a linked/forked bytenode living outside the project's node_modules,
175 // where `require('electron')` would not resolve) pass `electronPath` instead.
176 const electronPath = options.electronPath
177 ? path.normalize(options.electronPath)
178 : /** @type {string} */ (require('electron'));
179 if (!fs.existsSync(electronPath)) {
180 throw new Error('Electron not found');
181 }
182 const bytenodePath = path.join(__dirname, 'cli.js');
183
184 // create a subprocess in which we run Electron as our Node and V8 engine
185 // running Bytenode to compile our code through stdin/stdout
186 const child = spawn(electronPath, [bytenodePath, '--compile', '--no-module', '-'], {
187 env: { ELECTRON_RUN_AS_NODE: '1' },
188 stdio: ['pipe', 'pipe', 'pipe', 'ipc']
189 });
190
191 if (child.stdin) {
192 child.stdin.write(javascriptCode);
193 child.stdin.end();
194 }
195
196 if (child.stdout) {
197 child.stdout.on('data', (chunk) => {
198 data = Buffer.concat([data, chunk]);
199 });
200 child.stdout.on('error', (err) => {
201 console.error(err);
202 });
203 child.stdout.on('end', onEnd);
204 }
205
206 if (child.stderr) {
207 child.stderr.on('data', (chunk) => {
208 console.error('Error: ', chunk.toString());
209 });
210 child.stderr.on('error', (err) => {
211 console.error('Error: ', err);
212 });
213 }
214
215 child.addListener('message', (message) => console.log(message));
216 child.addListener('error', err => console.error(err));
217
218 child.on('error', (err) => reject(err));

Callers 1

compileFileFunction · 0.85

Calls 1

requireFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…