From 170bf86992ceffc2dc76706485de195db861ac53 Mon Sep 17 00:00:00 2001 From: Daryl Ronningen Date: Fri, 2 Sep 2022 14:27:35 -0700 Subject: [PATCH] feat: add calculation of MD5 hashes of installed files --- src/commands/install.ts | 33 +++++++++++++++++++++++++-------- src/utils.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/commands/install.ts b/src/commands/install.ts index 5d2903a..a30ac34 100644 --- a/src/commands/install.ts +++ b/src/commands/install.ts @@ -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((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(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((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); } } diff --git a/src/utils.ts b/src/utils.ts index 3526bd6..69525f6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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; } +export const walk = async (dir: string): Promise => { + 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: {