Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function write (key, data, callback) {
const dirname = path.dirname(key)
// template,缓存 mp template
if (
data.remainingRequest.indexOf('vue&type=template') !== -1 &&
process.UNI_CACHE_TEMPLATES
) {
data['mpTemplates'] = process.UNI_CACHE_TEMPLATES
delete process.UNI_CACHE_TEMPLATES
}
const content = BJSON.stringify(data)
if (directories.has(dirname)) {
// for performance skip creating directory
fs.writeFile(key, content, 'utf-8', callback)
} else {
mkdirp(dirname, (mkdirErr) => {
if (mkdirErr) {
callback(mkdirErr)
return
}
directories.add(dirname)
fs.writeFile(key, content, 'utf-8', callback)
})
}
MainIPCStream.prototype._write = function (data, enc, next) {
if (typeof data === 'string') {
data = JSON.stringify(data)
}
if (Buffer.isBuffer(data)) {
data = JSON.stringify(data, null, bufferJson.replacer)
}
if (!this.browserWindow) return console.warn('MainIPCStream: trying to write when no browserWindow is set.')
this.browserWindow.webContents.send(this.channel, data)
next()
}
RendIPCStream.prototype._write = function (data, enc, next) {
if (typeof data === 'string') {
data = JSON.stringify(data)
}
if (Buffer.isBuffer(data)) {
data = JSON.stringify(data, null, bufferJson.replacer)
}
ipcRenderer.send(this.channel, data)
next()
}
const ipcCallback = (event, data) => {
if (typeof data === 'string') {
data = JSON.parse(data, bufferJson.reviver)
}
this.push(data)
}
ipcMain.on(this.channel, ipcCallback)
const ipcCallback = (event, data) => {
if (typeof data === 'string') {
data = JSON.parse(data, bufferJson.reviver)
}
this.push(data)
}
ipcRenderer.on(this.channel, ipcCallback)
fs.readFile(key, 'utf-8', (err, content) => {
if (err) {
callback(err)
return
}
try {
const data = BJSON.parse(content)
const mpTemplates = data['mpTemplates']
if (mpTemplates) {
Object.keys(mpTemplates).forEach(name => {
fs.writeFileSync(name, mpTemplates[name], 'utf-8')
})
}
callback(null, data)
} catch (e) {
callback(e)
}
})
}