feat: read config, read history, hash files

most of the functionality exists now
This commit is contained in:
2019-04-04 15:28:24 -07:00
parent 655c27a89d
commit ca2fc85135
8 changed files with 194 additions and 10 deletions

34
src/data.js Normal file
View 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');
};