refactor: use fs.promises API, which is stable since Node 12 (#17999)

This commit is contained in:
Milan Burda 2019-04-29 20:18:03 +02:00 committed by Jeremy Apthorp
parent 7574f91f31
commit 9714a91392
3 changed files with 9 additions and 35 deletions

View file

@ -3,12 +3,9 @@
const { dialog, Menu } = require('electron') const { dialog, Menu } = require('electron')
const fs = require('fs') const fs = require('fs')
const url = require('url') const url = require('url')
const util = require('util')
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils') const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
const readFile = util.promisify(fs.readFile)
const convertToMenuTemplate = function (items, handler) { const convertToMenuTemplate = function (items, handler) {
return items.map(function (item) { return items.map(function (item) {
const transformed = item.type === 'subMenu' ? { const transformed = item.type === 'subMenu' ? {
@ -90,7 +87,7 @@ ipcMainUtils.handle('ELECTRON_INSPECTOR_SELECT_FILE', async function (event) {
if (result.canceled) return [] if (result.canceled) return []
const path = result.filePaths[0] const path = result.filePaths[0]
const data = await readFile(path) const data = await fs.promises.readFile(path)
return [path, data] return [path, data]
}) })

View file

@ -11,9 +11,6 @@ const path = require('path')
const url = require('url') const url = require('url')
const util = require('util') const util = require('util')
const readFile = util.promisify(fs.readFile)
const writeFile = util.promisify(fs.writeFile)
// Mapping between extensionId(hostname) and manifest. // Mapping between extensionId(hostname) and manifest.
const manifestMap = {} // extensionId => manifest const manifestMap = {} // extensionId => manifest
const manifestNameMap = {} // name => manifest const manifestNameMap = {} // name => manifest
@ -237,7 +234,7 @@ const getMessagesPath = (extensionId) => {
ipcMainUtils.handle('CHROME_GET_MESSAGES', async function (event, extensionId) { ipcMainUtils.handle('CHROME_GET_MESSAGES', async function (event, extensionId) {
const messagesPath = getMessagesPath(extensionId) const messagesPath = getMessagesPath(extensionId)
return readFile(messagesPath) return fs.promises.readFile(messagesPath)
}) })
const validStorageTypes = new Set(['sync', 'local']) const validStorageTypes = new Set(['sync', 'local'])
@ -254,27 +251,11 @@ const getChromeStoragePath = (storageType, extensionId) => {
return path.join(app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`) return path.join(app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`)
} }
const mkdirp = util.promisify((dir, callback) => {
fs.mkdir(dir, (error) => {
if (error && error.code === 'ENOENT') {
mkdirp(path.dirname(dir), (error) => {
if (!error) {
mkdirp(dir, callback)
}
})
} else if (error && error.code === 'EEXIST') {
callback(null)
} else {
callback(error)
}
})
})
ipcMainUtils.handle('CHROME_STORAGE_READ', async function (event, storageType, extensionId) { ipcMainUtils.handle('CHROME_STORAGE_READ', async function (event, storageType, extensionId) {
const filePath = getChromeStoragePath(storageType, extensionId) const filePath = getChromeStoragePath(storageType, extensionId)
try { try {
return await readFile(filePath, 'utf8') return await fs.promises.readFile(filePath, 'utf8')
} catch (error) { } catch (error) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
return null return null
@ -288,12 +269,12 @@ ipcMainUtils.handle('CHROME_STORAGE_WRITE', async function (event, storageType,
const filePath = getChromeStoragePath(storageType, extensionId) const filePath = getChromeStoragePath(storageType, extensionId)
try { try {
await mkdirp(path.dirname(filePath)) await fs.promises.mkdir(path.dirname(filePath), { recursive: true })
} catch { } catch {
// we just ignore the errors of mkdir or mkdirp // we just ignore the errors of mkdir
} }
return writeFile(filePath, data, 'utf8') return fs.promises.writeFile(filePath, data, 'utf8')
}) })
const isChromeExtension = function (pageURL) { const isChromeExtension = function (pageURL) {

View file

@ -4,7 +4,6 @@ const electron = require('electron')
const { EventEmitter } = require('events') const { EventEmitter } = require('events')
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const util = require('util')
const v8Util = process.electronBinding('v8_util') const v8Util = process.electronBinding('v8_util')
const eventBinding = process.electronBinding('event') const eventBinding = process.electronBinding('event')
@ -499,13 +498,10 @@ ipcMainUtils.handle('ELECTRON_BROWSER_CLIPBOARD', function (event, method, ...ar
return clipboardUtils.serialize(electron.clipboard[method](...clipboardUtils.deserialize(args))) return clipboardUtils.serialize(electron.clipboard[method](...clipboardUtils.deserialize(args)))
}) })
const readFile = util.promisify(fs.readFile)
const realpath = util.promisify(fs.realpath)
let absoluteAppPath let absoluteAppPath
const getAppPath = async function () { const getAppPath = async function () {
if (absoluteAppPath === undefined) { if (absoluteAppPath === undefined) {
absoluteAppPath = await realpath(electron.app.getAppPath()) absoluteAppPath = await fs.promises.realpath(electron.app.getAppPath())
} }
return absoluteAppPath return absoluteAppPath
} }
@ -515,10 +511,10 @@ const getPreloadScript = async function (preloadPath) {
let preloadError = null let preloadError = null
if (preloadPath) { if (preloadPath) {
try { try {
if (!isParentDir(await getAppPath(), await realpath(preloadPath))) { if (!isParentDir(await getAppPath(), await fs.promises.realpath(preloadPath))) {
throw new Error('Preload scripts outside of app path are not allowed') throw new Error('Preload scripts outside of app path are not allowed')
} }
preloadSrc = (await readFile(preloadPath)).toString() preloadSrc = (await fs.promises.readFile(preloadPath)).toString()
} catch (err) { } catch (err) {
preloadError = errorUtils.serialize(err) preloadError = errorUtils.serialize(err)
} }