(locale)
| 1270 | * try writing a file in a created directory |
| 1271 | */ |
| 1272 | const write = (locale) => { |
| 1273 | let stats, target, tmp |
| 1274 | |
| 1275 | // don't write new locale information to disk if updateFiles isn't true |
| 1276 | if (!updateFiles) { |
| 1277 | return |
| 1278 | } |
| 1279 | |
| 1280 | // creating directory if necessary |
| 1281 | try { |
| 1282 | stats = fs.lstatSync(directory) |
| 1283 | } catch (e) { |
| 1284 | logDebug('creating locales dir in: ' + directory) |
| 1285 | try { |
| 1286 | fs.mkdirSync(directory, directoryPermissions) |
| 1287 | } catch (e) { |
| 1288 | // in case of parallel tasks utilizing in same dir |
| 1289 | if (e.code !== 'EEXIST') throw e |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | // first time init has an empty file |
| 1294 | if (!locales[locale]) { |
| 1295 | locales[locale] = {} |
| 1296 | } |
| 1297 | |
| 1298 | // writing to tmp and rename on success |
| 1299 | try { |
| 1300 | target = getStorageFilePath(locale) |
| 1301 | tmp = target + '.tmp' |
| 1302 | fs.writeFileSync( |
| 1303 | tmp, |
| 1304 | parser.stringify(locales[locale], null, indent), |
| 1305 | 'utf8' |
| 1306 | ) |
| 1307 | stats = fs.statSync(tmp) |
| 1308 | if (stats.isFile()) { |
| 1309 | fs.renameSync(tmp, target) |
| 1310 | } else { |
| 1311 | logError( |
| 1312 | 'unable to write locales to file (either ' + |
| 1313 | tmp + |
| 1314 | ' or ' + |
| 1315 | target + |
| 1316 | ' are not writeable?): ' |
| 1317 | ) |
| 1318 | } |
| 1319 | } catch (e) { |
| 1320 | logError( |
| 1321 | 'unexpected error writing files (either ' + |
| 1322 | tmp + |
| 1323 | ' or ' + |
| 1324 | target + |
| 1325 | ' are not writeable?): ', |
| 1326 | e |
| 1327 | ) |
| 1328 | } |
| 1329 | } |
no test coverage detected