UNPKG

balena-config-json

Version:
80 lines (76 loc) 2.52 kB
"use strict"; /* Copyright 2016-2021 Balena Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getBootPartition = void 0; exports.read = read; exports.write = write; /** * @module config */ const imagefs = require("balena-image-fs"); const utils_1 = require("./utils"); Object.defineProperty(exports, "getBootPartition", { enumerable: true, get: function () { return utils_1.getBootPartition; } }); const util_1 = require("util"); /** * @summary Read a config.json from an image * @function * @public * * @param {String} image - image or drive path * @param {String} _type - ignored (device type, no longer required) * * @fulfil {Object} - config.json * @returns {Promise} * * @example * config.read('/dev/disk2', 'raspberry-pi').then (config) -> * console.log(config) */ async function read(image, // eslint-disable-next-line @typescript-eslint/no-unused-vars -- TODO: Drop in the next major _type) { const bootPartNumber = await (0, utils_1.getBootPartition)(image); const file = await imagefs.interact(image, bootPartNumber, async (fs) => { return await (0, util_1.promisify)(fs.readFile)(utils_1.configJsonPath, { encoding: 'utf8', }); }); return JSON.parse(file); } /** * @summary Write a config.json to an image * @function * @public * * @param {String} image - image or drive path * @param {String} _type - ignored (device type, no longer required) * @param {Object} config - config.json * * @returns {Promise} * * @example * config.write '/dev/disk2', 'raspberry-pi', * username: 'foobar' * .then -> * console.log('Done!') */ async function write(image, // TODO: Drop in the next major _type, config) { const configStr = JSON.stringify(config); const bootPartNumber = await (0, utils_1.getBootPartition)(image); await imagefs.interact(image, bootPartNumber, async (fs) => { await (0, util_1.promisify)(fs.writeFile)(utils_1.configJsonPath, configStr); }); }