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

@ -11,9 +11,6 @@ const path = require('path')
const url = require('url')
const util = require('util')
const readFile = util.promisify(fs.readFile)
const writeFile = util.promisify(fs.writeFile)
// Mapping between extensionId(hostname) and manifest.
const manifestMap = {} // extensionId => manifest
const manifestNameMap = {} // name => manifest
@ -237,7 +234,7 @@ const getMessagesPath = (extensionId) => {
ipcMainUtils.handle('CHROME_GET_MESSAGES', async function (event, extensionId) {
const messagesPath = getMessagesPath(extensionId)
return readFile(messagesPath)
return fs.promises.readFile(messagesPath)
})
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`)
}
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) {
const filePath = getChromeStoragePath(storageType, extensionId)
try {
return await readFile(filePath, 'utf8')
return await fs.promises.readFile(filePath, 'utf8')
} catch (error) {
if (error.code === 'ENOENT') {
return null
@ -288,12 +269,12 @@ ipcMainUtils.handle('CHROME_STORAGE_WRITE', async function (event, storageType,
const filePath = getChromeStoragePath(storageType, extensionId)
try {
await mkdirp(path.dirname(filePath))
await fs.promises.mkdir(path.dirname(filePath), { recursive: true })
} 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) {