i18next-fs-backend
Advanced tools
Comparing version 1.0.8 to 1.1.0
@@ -58,2 +58,4 @@ "use strict"; | ||
value: function read(language, namespace, callback) { | ||
var _this = this; | ||
var loadPath = this.options.loadPath; | ||
@@ -72,4 +74,12 @@ | ||
try { | ||
var resources = (0, _readFile.readFileSync)(filename, this.options); | ||
callback(null, resources); | ||
var _readFileSync = (0, _readFile.readFileSync)(filename, this.options), | ||
data = _readFileSync.data, | ||
stat = _readFileSync.stat; | ||
if (this.options.expirationTime && stat && stat.mtime && stat.mtime.getTime() + this.options.expirationTime < Date.now()) { | ||
this.removeFile(language, namespace); | ||
return callback(new Error('File expired!'), false); | ||
} | ||
callback(null, data); | ||
} catch (err) { | ||
@@ -82,4 +92,13 @@ callback(err, false); | ||
(0, _readFile.readFile)(filename, this.options).then(function (resources) { | ||
return callback(null, resources); | ||
(0, _readFile.readFile)(filename, this.options).then(function (_ref) { | ||
var data = _ref.data, | ||
stat = _ref.stat; | ||
if (_this.options.expirationTime && stat && stat.mtime && stat.mtime.getTime() + _this.options.expirationTime < Date.now()) { | ||
_this.removeFile(language, namespace); | ||
return callback(new Error('File expired!'), false); | ||
} | ||
callback(null, data); | ||
}).catch(function (err) { | ||
@@ -92,3 +111,3 @@ return callback(err, false); | ||
value: function create(languages, namespace, key, fallbackValue, callback) { | ||
var _this = this; | ||
var _this2 = this; | ||
@@ -104,6 +123,38 @@ if (!callback) callback = function callback() {}; | ||
languages.forEach(function (lng) { | ||
_this.queue.call(_this, lng, namespace, key, fallbackValue, done); | ||
_this2.queue.call(_this2, lng, namespace, key, fallbackValue, done); | ||
}); | ||
} | ||
}, { | ||
key: "save", | ||
value: function save(language, namespace, data, callback) { | ||
var _this3 = this; | ||
if (!callback) callback = function callback() {}; | ||
var keys = Object.keys(data); | ||
var todo = keys.length; | ||
var done = function done() { | ||
if (! --todo) callback(); | ||
}; | ||
keys.forEach(function (key) { | ||
_this3.queue.call(_this3, language, namespace, key, data[key], done); | ||
}); | ||
} | ||
}, { | ||
key: "removeFile", | ||
value: function removeFile(language, namespace) { | ||
var addPath = this.options.addPath; | ||
if (typeof this.options.addPath === 'function') { | ||
addPath = this.options.addPath(language, namespace); | ||
} | ||
var filename = this.services.interpolator.interpolate(addPath, { | ||
lng: language, | ||
ns: namespace | ||
}); | ||
(0, _writeFile2.removeFile)(filename, this.options).then(function () {}).catch(function () {}); | ||
} | ||
}, { | ||
key: "write", | ||
@@ -124,3 +175,3 @@ value: function write() { | ||
value: function writeFile(lng, namespace) { | ||
var _this2 = this; | ||
var _this4 = this; | ||
@@ -145,10 +196,11 @@ var lock = (0, _utils.getPath)(this.queuedWrites, ['locks', lng, namespace]); | ||
var proceed = function proceed(resources) { | ||
var proceed = function proceed(_ref2) { | ||
var data = _ref2.data; | ||
missings.forEach(function (missing) { | ||
var path = _this2.allOptions.keySeparator === false ? [missing.key] : missing.key.split(_this2.allOptions.keySeparator || '.'); | ||
(0, _utils.setPath)(resources, path, missing.fallbackValue); | ||
var path = _this4.allOptions.keySeparator === false ? [missing.key] : missing.key.split(_this4.allOptions.keySeparator || '.'); | ||
(0, _utils.setPath)(data, path, missing.fallbackValue); | ||
}); | ||
var proceedWrite = function proceedWrite() { | ||
(0, _utils.setPath)(_this2.queuedWrites, ['locks', lng, namespace], false); | ||
(0, _utils.setPath)(_this4.queuedWrites, ['locks', lng, namespace], false); | ||
missings.forEach(function (missing) { | ||
@@ -158,10 +210,12 @@ if (missing.callback) missing.callback(); | ||
_this2.debouncedWrite(); | ||
_this4.debouncedWrite(); | ||
}; | ||
(0, _writeFile2.writeFile)(filename, resources, _this2.options).then(proceedWrite).catch(proceedWrite); | ||
(0, _writeFile2.writeFile)(filename, data, _this4.options).then(proceedWrite).catch(proceedWrite); | ||
}; | ||
(0, _readFile.readFile)(filename, this.options).then(proceed).catch(function () { | ||
return proceed({}); | ||
return proceed({ | ||
data: {} | ||
}); | ||
}); | ||
@@ -168,0 +222,0 @@ } |
@@ -30,3 +30,13 @@ "use strict"; | ||
var readFileInNodeSync = function readFileInNodeSync(filename) { | ||
return fs.readFileSync(filename, 'utf8'); | ||
var data = fs.readFileSync(filename, 'utf8'); | ||
var stat; | ||
try { | ||
stat = fs.statSync(filename); | ||
} catch (e) {} | ||
return { | ||
data: data, | ||
stat: stat | ||
}; | ||
}; | ||
@@ -36,4 +46,13 @@ | ||
return new Promise(function (resolve, reject) { | ||
return fs.readFile(filename, 'utf8', function (err, data) { | ||
return err ? reject(err) : resolve(data); | ||
fs.readFile(filename, 'utf8', function (err, data) { | ||
if (err) return reject(err); | ||
fs.stat(filename, function (err, stat) { | ||
if (err) return resolve({ | ||
data: data | ||
}); | ||
return resolve({ | ||
data: data, | ||
stat: stat | ||
}); | ||
}); | ||
}); | ||
@@ -45,4 +64,14 @@ }); | ||
var decoder = new TextDecoder('utf-8'); | ||
var data = Deno.readFileSync(filename); | ||
return decoder.decode(data); | ||
var d = Deno.readFileSync(filename); | ||
var data = decoder.decode(d); | ||
var stat; | ||
try { | ||
stat = Deno.statSync(filename); | ||
} catch (e) {} | ||
return { | ||
data: data, | ||
stat: stat | ||
}; | ||
}; | ||
@@ -53,4 +82,14 @@ | ||
var decoder = new TextDecoder('utf-8'); | ||
Deno.readFile(filename).then(function (data) { | ||
resolve(decoder.decode(data)); | ||
Deno.readFile(filename).then(function (d) { | ||
var data = decoder.decode(d); | ||
Deno.stat(filename).then(function (stat) { | ||
return resolve({ | ||
data: data, | ||
stat: stat | ||
}); | ||
}).catch(function () { | ||
return resolve({ | ||
data: data | ||
}); | ||
}); | ||
}).catch(reject); | ||
@@ -101,11 +140,19 @@ }); | ||
var data; | ||
var data, stat; | ||
if (isDeno) { | ||
data = readFileInDenoSync(filename); | ||
var ret = readFileInDenoSync(filename); | ||
data = ret.data; | ||
stat = ret.stat; | ||
} else { | ||
data = readFileInNodeSync(filename); | ||
var _ret = readFileInNodeSync(filename); | ||
data = _ret.data; | ||
stat = _ret.stat; | ||
} | ||
return parseData(ext, data, options); | ||
return { | ||
data: parseData(ext, data, options), | ||
stat: stat | ||
}; | ||
} | ||
@@ -122,3 +169,5 @@ | ||
try { | ||
resolve(require(!filename.startsWith('/') && typeof process !== 'undefined' && process.cwd ? "".concat(process.cwd(), "/").concat(filename) : filename)); | ||
resolve({ | ||
data: require(!filename.startsWith('/') && typeof process !== 'undefined' && process.cwd ? "".concat(process.cwd(), "/").concat(filename) : filename) | ||
}); | ||
} catch (err) { | ||
@@ -132,6 +181,12 @@ reject(err); | ||
return new Promise(function (resolve, reject) { | ||
fn(filename).then(function (data) { | ||
fn(filename).then(function (_ref) { | ||
var data = _ref.data, | ||
stat = _ref.stat; | ||
try { | ||
var ret = parseData(ext, data, options); | ||
resolve(ret); | ||
resolve({ | ||
data: ret, | ||
stat: stat | ||
}); | ||
} catch (err) { | ||
@@ -138,0 +193,0 @@ err.message = 'error parsing ' + filename + ': ' + err.message; |
@@ -10,2 +10,4 @@ "use strict"; | ||
exports.writeFile = writeFile; | ||
exports.removeFileSync = removeFileSync; | ||
exports.removeFile = removeFile; | ||
@@ -42,2 +44,14 @@ var _json = _interopRequireDefault(require("./formats/json5.js")); | ||
var removeFileInNodeSync = function removeFileInNodeSync(filename) { | ||
return fs.unlinkSync(filename); | ||
}; | ||
var removeFileInNode = function removeFileInNode(filename) { | ||
return new Promise(function (resolve, reject) { | ||
return fs.unlink(filename, function (err) { | ||
return err ? reject(err) : resolve(); | ||
}); | ||
}); | ||
}; | ||
var writeFileInDenoSync = function writeFileInDenoSync(filename, payload) { | ||
@@ -55,2 +69,10 @@ var encoder = new TextEncoder(); | ||
var removeFileInDenoSync = function removeFileInDenoSync(filename) { | ||
Deno.removeSync(filename); | ||
}; | ||
var removeFileInDeno = function removeFileInDeno(filename) { | ||
return Deno.remove(filename); | ||
}; | ||
var stringifyData = function stringifyData(extension, data, options) { | ||
@@ -123,2 +145,15 @@ var result = ''; | ||
return fn(filename, data); | ||
} | ||
function removeFileSync(filename) { | ||
if (isDeno) { | ||
return removeFileInDenoSync(filename); | ||
} else { | ||
return removeFileInNodeSync(filename); | ||
} | ||
} | ||
function removeFile(filename) { | ||
var fn = isDeno ? removeFileInDeno : removeFileInNode; | ||
return fn(filename); | ||
} |
@@ -9,3 +9,3 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
import { readFile, readFileSync } from './readFile.js'; | ||
import { writeFile as _writeFile } from './writeFile.js'; | ||
import { writeFile as _writeFile, removeFile as _removeFile } from './writeFile.js'; | ||
@@ -50,2 +50,4 @@ var getDefaults = function getDefaults() { | ||
value: function read(language, namespace, callback) { | ||
var _this = this; | ||
var loadPath = this.options.loadPath; | ||
@@ -64,4 +66,12 @@ | ||
try { | ||
var resources = readFileSync(filename, this.options); | ||
callback(null, resources); | ||
var _readFileSync = readFileSync(filename, this.options), | ||
data = _readFileSync.data, | ||
stat = _readFileSync.stat; | ||
if (this.options.expirationTime && stat && stat.mtime && stat.mtime.getTime() + this.options.expirationTime < Date.now()) { | ||
this.removeFile(language, namespace); | ||
return callback(new Error('File expired!'), false); | ||
} | ||
callback(null, data); | ||
} catch (err) { | ||
@@ -74,4 +84,13 @@ callback(err, false); | ||
readFile(filename, this.options).then(function (resources) { | ||
return callback(null, resources); | ||
readFile(filename, this.options).then(function (_ref) { | ||
var data = _ref.data, | ||
stat = _ref.stat; | ||
if (_this.options.expirationTime && stat && stat.mtime && stat.mtime.getTime() + _this.options.expirationTime < Date.now()) { | ||
_this.removeFile(language, namespace); | ||
return callback(new Error('File expired!'), false); | ||
} | ||
callback(null, data); | ||
}).catch(function (err) { | ||
@@ -84,3 +103,3 @@ return callback(err, false); | ||
value: function create(languages, namespace, key, fallbackValue, callback) { | ||
var _this = this; | ||
var _this2 = this; | ||
@@ -96,6 +115,39 @@ if (!callback) callback = function callback() {}; | ||
languages.forEach(function (lng) { | ||
_this.queue.call(_this, lng, namespace, key, fallbackValue, done); | ||
_this2.queue.call(_this2, lng, namespace, key, fallbackValue, done); | ||
}); | ||
} | ||
}, { | ||
key: "save", | ||
value: function save(language, namespace, data, callback) { | ||
var _this3 = this; | ||
if (!callback) callback = function callback() {}; | ||
var keys = Object.keys(data); | ||
var todo = keys.length; | ||
var done = function done() { | ||
if (! --todo) callback(); | ||
}; | ||
keys.forEach(function (key) { | ||
_this3.queue.call(_this3, language, namespace, key, data[key], done); | ||
}); | ||
} | ||
}, { | ||
key: "removeFile", | ||
value: function removeFile(language, namespace) { | ||
var addPath = this.options.addPath; | ||
if (typeof this.options.addPath === 'function') { | ||
addPath = this.options.addPath(language, namespace); | ||
} | ||
var filename = this.services.interpolator.interpolate(addPath, { | ||
lng: language, | ||
ns: namespace | ||
}); | ||
_removeFile(filename, this.options).then(function () {}).catch(function () {}); | ||
} | ||
}, { | ||
key: "write", | ||
@@ -116,3 +168,3 @@ value: function write() { | ||
value: function writeFile(lng, namespace) { | ||
var _this2 = this; | ||
var _this4 = this; | ||
@@ -137,10 +189,11 @@ var lock = getPath(this.queuedWrites, ['locks', lng, namespace]); | ||
var proceed = function proceed(resources) { | ||
var proceed = function proceed(_ref2) { | ||
var data = _ref2.data; | ||
missings.forEach(function (missing) { | ||
var path = _this2.allOptions.keySeparator === false ? [missing.key] : missing.key.split(_this2.allOptions.keySeparator || '.'); | ||
setPath(resources, path, missing.fallbackValue); | ||
var path = _this4.allOptions.keySeparator === false ? [missing.key] : missing.key.split(_this4.allOptions.keySeparator || '.'); | ||
setPath(data, path, missing.fallbackValue); | ||
}); | ||
var proceedWrite = function proceedWrite() { | ||
setPath(_this2.queuedWrites, ['locks', lng, namespace], false); | ||
setPath(_this4.queuedWrites, ['locks', lng, namespace], false); | ||
missings.forEach(function (missing) { | ||
@@ -150,10 +203,12 @@ if (missing.callback) missing.callback(); | ||
_this2.debouncedWrite(); | ||
_this4.debouncedWrite(); | ||
}; | ||
_writeFile(filename, resources, _this2.options).then(proceedWrite).catch(proceedWrite); | ||
_writeFile(filename, data, _this4.options).then(proceedWrite).catch(proceedWrite); | ||
}; | ||
readFile(filename, this.options).then(proceed).catch(function () { | ||
return proceed({}); | ||
return proceed({ | ||
data: {} | ||
}); | ||
}); | ||
@@ -160,0 +215,0 @@ } |
@@ -10,3 +10,13 @@ import JSON5 from './formats/json5.js'; | ||
var readFileInNodeSync = function readFileInNodeSync(filename) { | ||
return fs.readFileSync(filename, 'utf8'); | ||
var data = fs.readFileSync(filename, 'utf8'); | ||
var stat; | ||
try { | ||
stat = fs.statSync(filename); | ||
} catch (e) {} | ||
return { | ||
data: data, | ||
stat: stat | ||
}; | ||
}; | ||
@@ -16,4 +26,13 @@ | ||
return new Promise(function (resolve, reject) { | ||
return fs.readFile(filename, 'utf8', function (err, data) { | ||
return err ? reject(err) : resolve(data); | ||
fs.readFile(filename, 'utf8', function (err, data) { | ||
if (err) return reject(err); | ||
fs.stat(filename, function (err, stat) { | ||
if (err) return resolve({ | ||
data: data | ||
}); | ||
return resolve({ | ||
data: data, | ||
stat: stat | ||
}); | ||
}); | ||
}); | ||
@@ -25,4 +44,14 @@ }); | ||
var decoder = new TextDecoder('utf-8'); | ||
var data = Deno.readFileSync(filename); | ||
return decoder.decode(data); | ||
var d = Deno.readFileSync(filename); | ||
var data = decoder.decode(d); | ||
var stat; | ||
try { | ||
stat = Deno.statSync(filename); | ||
} catch (e) {} | ||
return { | ||
data: data, | ||
stat: stat | ||
}; | ||
}; | ||
@@ -33,4 +62,14 @@ | ||
var decoder = new TextDecoder('utf-8'); | ||
Deno.readFile(filename).then(function (data) { | ||
resolve(decoder.decode(data)); | ||
Deno.readFile(filename).then(function (d) { | ||
var data = decoder.decode(d); | ||
Deno.stat(filename).then(function (stat) { | ||
return resolve({ | ||
data: data, | ||
stat: stat | ||
}); | ||
}).catch(function () { | ||
return resolve({ | ||
data: data | ||
}); | ||
}); | ||
}).catch(reject); | ||
@@ -81,11 +120,19 @@ }); | ||
var data; | ||
var data, stat; | ||
if (isDeno) { | ||
data = readFileInDenoSync(filename); | ||
var ret = readFileInDenoSync(filename); | ||
data = ret.data; | ||
stat = ret.stat; | ||
} else { | ||
data = readFileInNodeSync(filename); | ||
var _ret = readFileInNodeSync(filename); | ||
data = _ret.data; | ||
stat = _ret.stat; | ||
} | ||
return parseData(ext, data, options); | ||
return { | ||
data: parseData(ext, data, options), | ||
stat: stat | ||
}; | ||
} | ||
@@ -101,3 +148,5 @@ export function readFile(filename) { | ||
try { | ||
resolve(require(!filename.startsWith('/') && typeof process !== 'undefined' && process.cwd ? "".concat(process.cwd(), "/").concat(filename) : filename)); | ||
resolve({ | ||
data: require(!filename.startsWith('/') && typeof process !== 'undefined' && process.cwd ? "".concat(process.cwd(), "/").concat(filename) : filename) | ||
}); | ||
} catch (err) { | ||
@@ -111,6 +160,12 @@ reject(err); | ||
return new Promise(function (resolve, reject) { | ||
fn(filename).then(function (data) { | ||
fn(filename).then(function (_ref) { | ||
var data = _ref.data, | ||
stat = _ref.stat; | ||
try { | ||
var ret = parseData(ext, data, options); | ||
resolve(ret); | ||
resolve({ | ||
data: ret, | ||
stat: stat | ||
}); | ||
} catch (err) { | ||
@@ -117,0 +172,0 @@ err.message = 'error parsing ' + filename + ': ' + err.message; |
@@ -21,2 +21,14 @@ import JSON5 from './formats/json5.js'; | ||
var removeFileInNodeSync = function removeFileInNodeSync(filename) { | ||
return fs.unlinkSync(filename); | ||
}; | ||
var removeFileInNode = function removeFileInNode(filename) { | ||
return new Promise(function (resolve, reject) { | ||
return fs.unlink(filename, function (err) { | ||
return err ? reject(err) : resolve(); | ||
}); | ||
}); | ||
}; | ||
var writeFileInDenoSync = function writeFileInDenoSync(filename, payload) { | ||
@@ -34,2 +46,10 @@ var encoder = new TextEncoder(); | ||
var removeFileInDenoSync = function removeFileInDenoSync(filename) { | ||
Deno.removeSync(filename); | ||
}; | ||
var removeFileInDeno = function removeFileInDeno(filename) { | ||
return Deno.remove(filename); | ||
}; | ||
var stringifyData = function stringifyData(extension, data, options) { | ||
@@ -101,2 +121,13 @@ var result = ''; | ||
return fn(filename, data); | ||
} | ||
export function removeFileSync(filename) { | ||
if (isDeno) { | ||
return removeFileInDenoSync(filename); | ||
} else { | ||
return removeFileInNodeSync(filename); | ||
} | ||
} | ||
export function removeFile(filename) { | ||
var fn = isDeno ? removeFileInDeno : removeFileInNode; | ||
return fn(filename); | ||
} |
import { defaults, debounce, getPath, setPath, pushPath } from './utils.js' | ||
import { readFile, readFileSync } from './readFile.js' | ||
import { writeFile } from './writeFile.js' | ||
import { writeFile, removeFile } from './writeFile.js' | ||
@@ -12,2 +12,3 @@ const getDefaults = () => { | ||
stringify: JSON.stringify | ||
// expirationTime: 60 * 60 * 1000 | ||
} | ||
@@ -41,4 +42,8 @@ } | ||
try { | ||
const resources = readFileSync(filename, this.options) | ||
callback(null, resources) | ||
const { data, stat } = readFileSync(filename, this.options) | ||
if (this.options.expirationTime && stat && stat.mtime && stat.mtime.getTime() + this.options.expirationTime < Date.now()) { | ||
this.removeFile(language, namespace) | ||
return callback(new Error('File expired!'), false) // no retry | ||
} | ||
callback(null, data) | ||
} catch (err) { | ||
@@ -50,3 +55,9 @@ callback(err, false) // no retry | ||
readFile(filename, this.options) | ||
.then((resources) => callback(null, resources)) | ||
.then(({ data, stat }) => { | ||
if (this.options.expirationTime && stat && stat.mtime && stat.mtime.getTime() + this.options.expirationTime < Date.now()) { | ||
this.removeFile(language, namespace) | ||
return callback(new Error('File expired!'), false) // no retry | ||
} | ||
callback(null, data) | ||
}) | ||
.catch((err) => callback(err, false)) // no retry | ||
@@ -70,2 +81,29 @@ } | ||
// this way i18next-fs-backend can be used as cache layer in combination with i18next-chained-backend | ||
save (language, namespace, data, callback) { | ||
if (!callback) callback = () => {} | ||
const keys = Object.keys(data) | ||
let todo = keys.length | ||
const done = () => { | ||
if (!--todo) callback() | ||
} | ||
keys.forEach((key) => { | ||
// eslint-disable-next-line no-useless-call | ||
this.queue.call(this, language, namespace, key, data[key], done) | ||
}) | ||
} | ||
removeFile (language, namespace) { | ||
let addPath = this.options.addPath | ||
if (typeof this.options.addPath === 'function') { | ||
addPath = this.options.addPath(language, namespace) | ||
} | ||
const filename = this.services.interpolator.interpolate(addPath, { lng: language, ns: namespace }) | ||
removeFile(filename, this.options) | ||
.then(() => {}) | ||
.catch(() => {}) | ||
} | ||
write () { | ||
@@ -100,6 +138,6 @@ for (const lng in this.queuedWrites) { | ||
const proceed = (resources) => { | ||
const proceed = ({ data }) => { | ||
missings.forEach((missing) => { | ||
const path = this.allOptions.keySeparator === false ? [missing.key] : (missing.key.split(this.allOptions.keySeparator || '.')) | ||
setPath(resources, path, missing.fallbackValue) | ||
setPath(data, path, missing.fallbackValue) | ||
}) | ||
@@ -116,7 +154,7 @@ | ||
} | ||
writeFile(filename, resources, this.options) | ||
writeFile(filename, data, this.options) | ||
.then(proceedWrite) | ||
.catch(proceedWrite) | ||
} | ||
readFile(filename, this.options).then(proceed).catch(() => proceed({})) | ||
readFile(filename, this.options).then(proceed).catch(() => proceed({ data: {} })) | ||
} | ||
@@ -123,0 +161,0 @@ } |
@@ -11,7 +11,20 @@ import JSON5 from './formats/json5.js' | ||
const readFileInNodeSync = (filename) => { | ||
return fs.readFileSync(filename, 'utf8') | ||
const data = fs.readFileSync(filename, 'utf8') | ||
let stat | ||
try { | ||
stat = fs.statSync(filename) | ||
} catch (e) {} | ||
return { data, stat } | ||
} | ||
const readFileInNode = (filename) => { | ||
return new Promise((resolve, reject) => fs.readFile(filename, 'utf8', (err, data) => err ? reject(err) : resolve(data))) | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(filename, 'utf8', (err, data) => { | ||
if (err) return reject(err) | ||
fs.stat(filename, (err, stat) => { | ||
if (err) return resolve({ data }) | ||
return resolve({ data, stat }) | ||
}) | ||
}) | ||
}) | ||
} | ||
@@ -22,4 +35,10 @@ | ||
// eslint-disable-next-line no-undef | ||
const data = Deno.readFileSync(filename) | ||
return decoder.decode(data) | ||
const d = Deno.readFileSync(filename) | ||
const data = decoder.decode(d) | ||
let stat | ||
try { | ||
// eslint-disable-next-line no-undef | ||
stat = Deno.statSync(filename) | ||
} catch (e) {} | ||
return { data, stat } | ||
} | ||
@@ -30,4 +49,6 @@ const readFileInDeno = (filename) => { | ||
// eslint-disable-next-line no-undef | ||
Deno.readFile(filename).then((data) => { | ||
resolve(decoder.decode(data)) | ||
Deno.readFile(filename).then((d) => { | ||
const data = decoder.decode(d) | ||
// eslint-disable-next-line no-undef | ||
Deno.stat(filename).then((stat) => resolve({ data, stat })).catch(() => resolve({ data })) | ||
}).catch(reject) | ||
@@ -71,9 +92,13 @@ }) | ||
} | ||
let data | ||
let data, stat | ||
if (isDeno) { | ||
data = readFileInDenoSync(filename) | ||
const ret = readFileInDenoSync(filename) | ||
data = ret.data | ||
stat = ret.stat | ||
} else { | ||
data = readFileInNodeSync(filename) | ||
const ret = readFileInNodeSync(filename) | ||
data = ret.data | ||
stat = ret.stat | ||
} | ||
return parseData(ext, data, options) | ||
return { data: parseData(ext, data, options), stat } | ||
} | ||
@@ -86,3 +111,3 @@ | ||
try { | ||
resolve(require(!filename.startsWith('/') && typeof process !== 'undefined' && process.cwd ? `${process.cwd()}/${filename}` : filename)) | ||
resolve({ data: require(!filename.startsWith('/') && typeof process !== 'undefined' && process.cwd ? `${process.cwd()}/${filename}` : filename) }) | ||
} catch (err) { | ||
@@ -95,6 +120,6 @@ reject(err) | ||
return new Promise((resolve, reject) => { | ||
fn(filename).then((data) => { | ||
fn(filename).then(({ data, stat }) => { | ||
try { | ||
const ret = parseData(ext, data, options) | ||
resolve(ret) | ||
resolve({ data: ret, stat }) | ||
} catch (err) { | ||
@@ -101,0 +126,0 @@ err.message = 'error parsing ' + filename + ': ' + err.message |
@@ -18,2 +18,10 @@ import JSON5 from './formats/json5.js' | ||
const removeFileInNodeSync = (filename) => { | ||
return fs.unlinkSync(filename) | ||
} | ||
const removeFileInNode = (filename) => { | ||
return new Promise((resolve, reject) => fs.unlink(filename, (err) => err ? reject(err) : resolve())) | ||
} | ||
const writeFileInDenoSync = (filename, payload) => { | ||
@@ -33,2 +41,12 @@ const encoder = new TextEncoder() | ||
const removeFileInDenoSync = (filename) => { | ||
// eslint-disable-next-line no-undef | ||
Deno.removeSync(filename) | ||
} | ||
const removeFileInDeno = (filename) => { | ||
// eslint-disable-next-line no-undef | ||
return Deno.remove(filename) | ||
} | ||
const stringifyData = (extension, data, options) => { | ||
@@ -86,1 +104,14 @@ let result = '' | ||
} | ||
export function removeFileSync (filename) { | ||
if (isDeno) { | ||
return removeFileInDenoSync(filename) | ||
} else { | ||
return removeFileInNodeSync(filename) | ||
} | ||
} | ||
export function removeFile (filename) { | ||
const fn = isDeno ? removeFileInDeno : removeFileInNode | ||
return fn(filename) | ||
} |
{ | ||
"name": "i18next-fs-backend", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"private": false, | ||
@@ -22,19 +22,19 @@ "type": "module", | ||
"devDependencies": { | ||
"@babel/cli": "7.12.10", | ||
"@babel/core": "7.12.10", | ||
"@babel/preset-env": "7.12.11", | ||
"@babel/cli": "7.13.0", | ||
"@babel/core": "7.13.8", | ||
"@babel/preset-env": "7.13.8", | ||
"babel-plugin-add-module-exports": "1.0.4", | ||
"eslint": "7.18.0", | ||
"eslint": "7.20.0", | ||
"eslint-config-standard": "16.0.2", | ||
"eslint-plugin-import": "2.22.1", | ||
"eslint-plugin-node": "11.1.0", | ||
"eslint-plugin-promise": "4.2.1", | ||
"eslint-plugin-promise": "4.3.1", | ||
"eslint-plugin-require-path-exists": "1.1.9", | ||
"eslint-plugin-standard": "5.0.0", | ||
"expect.js": "0.3.1", | ||
"i18next": "19.8.4", | ||
"i18next": "19.9.0", | ||
"js-yaml": "4.0.0", | ||
"json5": "2.1.3", | ||
"mocha": "8.2.1", | ||
"uglify-js": "3.12.4" | ||
"json5": "2.2.0", | ||
"mocha": "8.3.0", | ||
"uglify-js": "3.12.8" | ||
}, | ||
@@ -41,0 +41,0 @@ "description": "i18next-fs-backend is a backend layer for i18next using in Node.js and for Deno to load translations from the filesystem.", |
@@ -56,3 +56,7 @@ # Introduction | ||
// path to post missing resources | ||
addPath: '/locales/{{lng}}/{{ns}}.missing.json' | ||
addPath: '/locales/{{lng}}/{{ns}}.missing.json', | ||
// if you use i18next-fs-backend as caching layer in combination with i18next-chained-backend, you can optionally set an expiration time | ||
// an example on how to use it as cache layer can be found here: https://github.com/i18next/i18next-fs-backend/blob/master/example/caching/app.js | ||
// expirationTime: 60 * 60 * 1000 | ||
} | ||
@@ -69,3 +73,3 @@ ``` | ||
i18next.use(HttpApi).init({ | ||
i18next.use(Backend).init({ | ||
backend: options, | ||
@@ -78,3 +82,3 @@ }); | ||
```js | ||
import HttpApi from 'i18next-fs-backend'; | ||
import Backend from 'i18next-fs-backend'; | ||
const Backend = new Backend(null, options); | ||
@@ -81,0 +85,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
481693
30
13463
129
10