From e50974a86a5ddf580d043f0d344cce431eb287e2 Mon Sep 17 00:00:00 2001 From: Alexis Svinartchouk Date: Wed, 8 Jan 2020 14:56:47 +0100 Subject: [PATCH] Convert local-settings.js to typescript Change-type: patch --- lib/gui/app/models/local-settings.js | 184 --------------------------- lib/gui/app/models/local-settings.ts | 73 +++++++++++ lib/gui/app/models/settings.js | 1 + tests/gui/models/settings.spec.js | 1 + 4 files changed, 75 insertions(+), 184 deletions(-) delete mode 100644 lib/gui/app/models/local-settings.js create mode 100644 lib/gui/app/models/local-settings.ts diff --git a/lib/gui/app/models/local-settings.js b/lib/gui/app/models/local-settings.js deleted file mode 100644 index 7ad08e53..00000000 --- a/lib/gui/app/models/local-settings.js +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright 2017 balena.io - * - * 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. - */ - -'use strict' - -const Bluebird = require('bluebird') -const fs = require('fs') -const path = require('path') - -/** - * @summary Number of spaces to indent JSON output with - * @type {Number} - * @constant - */ -const JSON_INDENT = 2 - -/** - * @summary Userdata directory path - * @description - * Defaults to the following: - * - `%APPDATA%/etcher` on Windows - * - `$XDG_CONFIG_HOME/etcher` or `~/.config/etcher` on Linux - * - `~/Library/Application Support/etcher` on macOS - * See https://electronjs.org/docs/api/app#appgetpathname - * @constant - * @type {String} - */ -const USER_DATA_DIR = (() => { - // NOTE: The ternary is due to this module being loaded both, - // Electron's main process and renderer process - const electron = require('electron') - return electron.app - ? electron.app.getPath('userData') - : electron.remote.app.getPath('userData') -})() - -/** - * @summary Configuration file path - * @type {String} - * @constant - */ -const CONFIG_PATH = path.join(USER_DATA_DIR, 'config.json') - -/** - * @summary Read a local config.json file - * @function - * @private - * - * @param {String} filename - file path - * @fulfil {Object} - settings - * @returns {Promise} - * - * @example - * readConfigFile('config.json').then((settings) => { - * console.log(settings) - * }) - */ -const readConfigFile = (filename) => { - return new Bluebird((resolve, reject) => { - fs.readFile(filename, { encoding: 'utf8' }, (error, contents) => { - let data = {} - if (error) { - if (error.code === 'ENOENT') { - resolve(data) - } else { - reject(error) - } - } else { - try { - data = JSON.parse(contents) - } catch (parseError) { - console.error(parseError) - } - resolve(data) - } - }) - }) -} - -/** - * @summary Write to the local configuration file - * @function - * @private - * - * @param {String} filename - file path - * @param {Object} data - data - * @fulfil {Object} data - data - * @returns {Promise} - * - * @example - * writeConfigFile('config.json', { something: 'good' }) - * .then(() => { - * console.log('data written') - * }) - */ -const writeConfigFile = (filename, data) => { - return new Bluebird((resolve, reject) => { - const contents = JSON.stringify(data, null, JSON_INDENT) - fs.writeFile(filename, contents, (error) => { - if (error) { - reject(error) - } else { - resolve(data) - } - }) - }) -} - -/** - * @summary Read all local settings - * @function - * @public - * - * @fulfil {Object} - local settings - * @returns {Promise} - * - * @example - * localSettings.readAll().then((settings) => { - * console.log(settings); - * }); - */ -exports.readAll = () => { - return readConfigFile(CONFIG_PATH) -} - -/** - * @summary Write local settings - * @function - * @public - * - * @param {Object} settings - settings - * @fulfil {Object} settings - settings - * @returns {Promise} - * - * @example - * localSettings.writeAll({ - * foo: 'bar' - * }).then(() => { - * console.log('Done!'); - * }); - */ -exports.writeAll = (settings) => { - return writeConfigFile(CONFIG_PATH, settings) -} - -/** - * @summary Clear the local settings - * @function - * @private - * - * @description - * Exported for testing purposes - * - * @returns {Promise} - * - * @example - * localSettings.clear().then(() => { - * console.log('Done!'); - * }); - */ -exports.clear = () => { - return new Bluebird((resolve, reject) => { - fs.unlink(CONFIG_PATH, (error) => { - if (error) { - reject(error) - } else { - resolve() - } - }) - }) -} diff --git a/lib/gui/app/models/local-settings.ts b/lib/gui/app/models/local-settings.ts new file mode 100644 index 00000000..2a9c8439 --- /dev/null +++ b/lib/gui/app/models/local-settings.ts @@ -0,0 +1,73 @@ +/* + * Copyright 2017 balena.io + * + * 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. + */ + +import * as electron from 'electron'; +import { promises as fs } from 'fs'; +import * as path from 'path'; + +const JSON_INDENT = 2; + +/** + * @summary Userdata directory path + * @description + * Defaults to the following: + * - `%APPDATA%/etcher` on Windows + * - `$XDG_CONFIG_HOME/etcher` or `~/.config/etcher` on Linux + * - `~/Library/Application Support/etcher` on macOS + * See https://electronjs.org/docs/api/app#appgetpathname + * + * NOTE: The ternary is due to this module being loaded both, + * Electron's main process and renderer process + */ +const USER_DATA_DIR = electron.app + ? electron.app.getPath('userData') + : electron.remote.app.getPath('userData'); + +const CONFIG_PATH = path.join(USER_DATA_DIR, 'config.json'); + +async function readConfigFile(filename: string): Promise { + let contents = '{}'; + try { + contents = await fs.readFile(filename, { encoding: 'utf8' }); + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } + } + try { + return JSON.parse(contents); + } catch (parseError) { + console.error(parseError); + return {}; + } +} + +async function writeConfigFile(filename: string, data: any): Promise { + await fs.writeFile(filename, JSON.stringify(data, null, JSON_INDENT)); + return data; +} + +export async function readAll(): Promise { + return await readConfigFile(CONFIG_PATH); +} + +export async function writeAll(settings: any): Promise { + return await writeConfigFile(CONFIG_PATH, settings); +} + +export async function clear(): Promise { + await fs.unlink(CONFIG_PATH); +} diff --git a/lib/gui/app/models/settings.js b/lib/gui/app/models/settings.js index 486dc694..eabac49f 100644 --- a/lib/gui/app/models/settings.js +++ b/lib/gui/app/models/settings.js @@ -22,6 +22,7 @@ const _ = require('lodash') const Bluebird = require('bluebird') +// eslint-disable-next-line node/no-missing-require const localSettings = require('./local-settings') const errors = require('../../../shared/errors') const packageJSON = require('../../../../package.json') diff --git a/tests/gui/models/settings.spec.js b/tests/gui/models/settings.spec.js index 1052562e..bf00d16d 100644 --- a/tests/gui/models/settings.spec.js +++ b/tests/gui/models/settings.spec.js @@ -20,6 +20,7 @@ const m = require('mochainon') const _ = require('lodash') const Bluebird = require('bluebird') const settings = require('../../../lib/gui/app/models/settings') +// eslint-disable-next-line node/no-missing-require const localSettings = require('../../../lib/gui/app/models/local-settings') describe('Browser: settings', function () {