feat: read config, read history, hash files
most of the functionality exists now
This commit is contained in:
34
src/data.js
Normal file
34
src/data.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const mkdirp = require('mkdirp');
|
||||
|
||||
const CONFIG_FILENAME = 'pkgcomp.json';
|
||||
|
||||
exports.read = (fileRoot, ident) => {
|
||||
const stats = fs.statSync(fileRoot);
|
||||
const filePath = path.join(fileRoot, CONFIG_FILENAME);
|
||||
|
||||
// throw if pointing to a file
|
||||
if (stats.isFile()) throw new Error(`getData expects a directory, got a file; ${fileRoot}`);
|
||||
|
||||
// create file if it's missing
|
||||
if (!stats.isDirectory()) {
|
||||
mkdirp.sync(fileRoot);
|
||||
}
|
||||
|
||||
try {
|
||||
const data = JSON.parse(fs.readFileSync(filePath));
|
||||
return data[ident] || {};
|
||||
} catch (err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
fs.writeFileSync(filePath, JSON.stringify({}));
|
||||
return {};
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
exports.write = (fileRoot, ident, payload) => {
|
||||
console.log('WRITE', { fileRoot, ident, payload });
|
||||
throw new Error('Data write not implemented');
|
||||
};
|
||||
Reference in New Issue
Block a user