Archived
0
0
Fork 0

feat: add calculation of MD5 hashes of installed files

This commit is contained in:
Daryl Ronningen 2022-09-02 14:27:35 -07:00
parent f006bea203
commit 170bf86992
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
2 changed files with 52 additions and 8 deletions

View file

@ -1,12 +1,13 @@
import chalk from 'chalk';
import * as lowdb from 'lowdb';
import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import tar from 'tar';
import mv from 'mv';
import { parseWahInfo } from '../utils.js';
import { parseWahInfo, walk } from '../utils.js';
export default class Install {
public name: string;
@ -28,7 +29,6 @@ export default class Install {
if (!fs.existsSync(path.join(os.tmpdir(), 'wahpkg'))) await fs.promises.mkdir(path.join(os.tmpdir(), 'wahpkg'));
const makeExtractDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'wahpkg', `${path.basename(this.options.file)}-`));
if (this.options.file) {
this.path = path.join(process.cwd(), this.options.file);
await tar.x({ cwd: path.join(makeExtractDir), file: this.path });
@ -41,12 +41,6 @@ export default class Install {
const readWahInfo = await fs.promises.readFile(path.join(makeExtractDir, '.WAHINFO'));
const getWahInfo = parseWahInfo(readWahInfo.toString());
await new Promise<void>((res, rej) => {
mv(path.join(makeExtractDir, 'ROOT'), path.join(this.options.sysroot), { mkdirp: true }, (err) => { if (err) rej(err); else res(); });
});
await fs.promises.rm(path.join(makeExtractDir), { recursive: true, force: true });
const dbFile = '/var/lib/wahpkg/pkgs.json';
const adapter = new lowdb.JSONFile<dbData>(dbFile);
const db = new lowdb.Low(adapter);
@ -58,6 +52,29 @@ export default class Install {
});
await db.write();
if (!fs.existsSync('/var/lib/wahpkg/pkgs')) await fs.promises.mkdir('/var/lib/wahpkg/pkgs');
await fs.promises.mkdir(`/var/lib/wahpkg/pkgs/${getWahInfo.name}`);
await fs.promises.writeFile(`/var/lib/wahpkg/pkgs/${getWahInfo.name}/MD5HASHES`, '');
const md5Adapter = new lowdb.TextFile(`/var/lib/wahpkg/pkgs/${getWahInfo.name}/MD5HASHES`);
await md5Adapter.read();
let toWriteToFile = '';
for (const file of await walk(makeExtractDir)) {
if (file.endsWith('.WAHINFO')) continue;
const readFile = await fs.promises.readFile(file);
const hash = crypto.createHash('md5').update(readFile).digest('hex');
toWriteToFile += `${file.replace(`${makeExtractDir}/ROOT`, '')} ${hash}\n`;
}
await md5Adapter.write(toWriteToFile);
await new Promise<void>((res, rej) => {
mv(path.join(makeExtractDir, 'ROOT'), path.join(this.options.sysroot), { mkdirp: true }, (err) => { if (err) rej(err); else res(); });
});
await fs.promises.rm(path.join(makeExtractDir), { recursive: true, force: true });
console.log(chalk.bold.green`Package %s has been installed!`, getWahInfo.name);
}
}

View file

@ -1,3 +1,5 @@
import fs from 'node:fs';
import path from 'node:path';
import type { PartialDeep, RequireAllOrNone } from 'type-fest';
export function parseWahInfo(file: string): WahInfo {
@ -33,6 +35,31 @@ export function parseWahInfo(file: string): WahInfo {
return finalVal as RequireAllOrNone<WahInfo>;
}
export const walk = async (dir: string): Promise<string[]> => {
return new Promise((res, rej) => {
let results = [];
fs.readdir(dir, function(err, list) {
if (err) return rej(err);
let pending = list.length;
if (!pending) return res(results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, async function(err, stat) {
if (err) return rej(err);
if (stat && stat.isDirectory()) {
const walkPath = await walk(file);
results = results.concat(walkPath);
if (!--pending) return res(results);
} else {
results.push(file);
if (!--pending) return res(results);
}
});
});
});
});
};
export interface WahInfo {
name: string;
version: {