UNPKG

balena-cli

Version:

The official balena Command Line Interface

70 lines 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.start = start; exports.stop = stop; const fs = require("fs"); const os = require("os"); const path = require("path"); const stat = process.pkg ? fs.statSync : fs.promises.stat; let fastBootStarted = false; async function start() { if (fastBootStarted) { return; } try { await $start(); fastBootStarted = true; } catch (e) { if (process.env.DEBUG) { console.error(`\ [debug] Unable to start 'fast-boot2': [debug] ${(e.message || '').split('\n').join('\n[debug] ')} [debug] The CLI should still work, but it will run a bit slower.`); } } } function stop() { if (fastBootStarted) { require('fast-boot2').stop(); } fastBootStarted = false; } async function $start() { const dotBalena = process.platform === 'win32' ? '_balena' : '.balena'; const dataDir = path.normalize(process.env.BALENARC_DATA_DIRECTORY || path.join(os.homedir(), dotBalena)); const cacheFile = path.join(dataDir, 'cli-module-cache.json'); const root = path.join(__dirname, '..'); const [, pJson, pStat, nStat] = await Promise.all([ ensureCanWrite(dataDir, cacheFile), Promise.resolve().then(() => require('../package.json')), stat(path.join(root, 'package.json'), { bigint: true }), stat(path.join(root, 'npm-shrinkwrap.json'), { bigint: true }), ]); const cacheKiller = `${pJson.version}-${pStat.mtimeMs}-${nStat.mtimeMs}`; require('fast-boot2').start({ cacheFile, cacheKiller, cacheScope: root, }); } async function ensureCanWrite(dir, file) { const { access, mkdir } = fs.promises; try { try { await access(file, fs.constants.W_OK); return; } catch (e) { if (e.code !== 'ENOENT') { throw e; } } await mkdir(dir, { recursive: true, mode: 0o755 }); await access(dir, fs.constants.W_OK); } catch (e) { throw new Error(`Unable to write file "${file}":\n${e.message}`); } } //# sourceMappingURL=fast-boot.js.map