Archived
0
0
Fork 0

feat: migrate to fs-extra

This commit is contained in:
Daryl Ronningen 2022-09-10 16:55:54 -07:00
parent 7cb1ee9c20
commit 2c82403863
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
4 changed files with 21 additions and 22 deletions

View file

@ -1,8 +1,7 @@
import chalk from 'chalk'; import chalk from 'chalk';
import fse from 'fs-extra'; import fs from 'fs-extra';
import * as lowdb from 'lowdb'; import * as lowdb from 'lowdb';
import crypto from 'node:crypto'; import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import os from 'node:os'; import os from 'node:os';
import tar from 'tar'; import tar from 'tar';
@ -26,19 +25,19 @@ export default class Install {
process.exit(1); process.exit(1);
} }
if (!fs.existsSync(path.join(os.tmpdir(), 'wahpkg'))) await fs.promises.mkdir(path.join(os.tmpdir(), 'wahpkg')); if (!await fs.pathExists(path.join(os.tmpdir(), 'wahpkg'))) await fs.mkdir(path.join(os.tmpdir(), 'wahpkg'));
const makeExtractDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'wahpkg', `${path.basename(this.options.file)}-`)); const makeExtractDir = await fs.mkdtemp(path.join(os.tmpdir(), 'wahpkg', `${path.basename(this.options.file)}-`));
if (this.options.file) { if (this.options.file) {
this.path = path.join(process.cwd(), this.options.file); this.path = path.join(process.cwd(), this.options.file);
await tar.x({ cwd: path.join(makeExtractDir), file: this.path }); await tar.x({ cwd: path.join(makeExtractDir), file: this.path });
if (!fs.existsSync(path.join(makeExtractDir, '.WAHINFO'))) { if (!await fs.pathExists(path.join(makeExtractDir, '.WAHINFO'))) {
console.error(chalk.red.bold`.WAHINFO file does not exist in the selected package! The selected file is probably not a package.`); console.error(chalk.red.bold`.WAHINFO file does not exist in the selected package! The selected file is probably not a package.`);
process.exit(1); process.exit(1);
} }
const readWahInfo = await fs.promises.readFile(path.join(makeExtractDir, '.WAHINFO')); const readWahInfo = await fs.readFile(path.join(makeExtractDir, '.WAHINFO'));
const getWahInfo = parseWahInfo(readWahInfo.toString()); const getWahInfo = parseWahInfo(readWahInfo.toString());
const dbFile = '/var/lib/wahpkg/pkgs.json'; const dbFile = '/var/lib/wahpkg/pkgs.json';
@ -58,9 +57,9 @@ export default class Install {
}); });
await db.write(); await db.write();
if (!fs.existsSync('/var/lib/wahpkg/pkgs')) await fs.promises.mkdir('/var/lib/wahpkg/pkgs'); if (!await fs.pathExists('/var/lib/wahpkg/pkgs')) await fs.mkdir('/var/lib/wahpkg/pkgs');
await fs.promises.mkdir(`/var/lib/wahpkg/pkgs/${getWahInfo.name}`); await fs.mkdir(`/var/lib/wahpkg/pkgs/${getWahInfo.name}`);
await fs.promises.writeFile(`/var/lib/wahpkg/pkgs/${getWahInfo.name}/MD5HASHES`, ''); await fs.writeFile(`/var/lib/wahpkg/pkgs/${getWahInfo.name}/MD5HASHES`, '');
const md5Adapter = new lowdb.TextFile(`/var/lib/wahpkg/pkgs/${getWahInfo.name}/MD5HASHES`); const md5Adapter = new lowdb.TextFile(`/var/lib/wahpkg/pkgs/${getWahInfo.name}/MD5HASHES`);
await md5Adapter.read(); await md5Adapter.read();
@ -69,15 +68,15 @@ export default class Install {
for (const file of await walk(makeExtractDir)) { for (const file of await walk(makeExtractDir)) {
if (file.endsWith('.WAHINFO')) continue; if (file.endsWith('.WAHINFO')) continue;
const readFile = await fs.promises.readFile(file); const readFile = await fs.readFile(file);
const hash = crypto.createHash('md5').update(readFile).digest('hex'); const hash = crypto.createHash('md5').update(readFile).digest('hex');
toWriteToFile += `${file.replace(`${makeExtractDir}/ROOT`, this.options.sysroot)} ${hash}\n`; toWriteToFile += `${file.replace(`${makeExtractDir}/ROOT`, this.options.sysroot)} ${hash}\n`;
} }
await md5Adapter.write(toWriteToFile); await md5Adapter.write(toWriteToFile);
await fse.move(path.join(makeExtractDir, 'ROOT'), path.join(this.options.sysroot), { overwrite: true }); await fs.move(path.join(makeExtractDir, 'ROOT'), path.join(this.options.sysroot), { overwrite: true });
await fs.promises.rm(path.join(makeExtractDir), { recursive: true, force: true }); await fs.rm(path.join(makeExtractDir), { recursive: true, force: true });
console.log(chalk.bold.green`Package %s has been installed!`, getWahInfo.name); console.log(chalk.bold.green`Package %s has been installed!`, getWahInfo.name);
process.exit(0); process.exit(0);

View file

@ -1,7 +1,7 @@
import chalk from 'chalk'; import chalk from 'chalk';
import fs from 'fs-extra';
import * as lowdb from 'lowdb'; import * as lowdb from 'lowdb';
import crypto from 'node:crypto'; import crypto from 'node:crypto';
import fs from 'node:fs';
import { dbData } from './install.js'; import { dbData } from './install.js';
@ -27,7 +27,7 @@ export default class Uninstall {
process.exit(1); process.exit(1);
} }
const md5File = await fs.promises.readFile(`/var/lib/wahpkg/pkgs/${this.name}/MD5HASHES`); const md5File = await fs.readFile(`/var/lib/wahpkg/pkgs/${this.name}/MD5HASHES`);
for (const file of md5File.toString().split('\n')) { for (const file of md5File.toString().split('\n')) {
const fileName = file.split(' ')[0]; const fileName = file.split(' ')[0];
@ -36,17 +36,17 @@ export default class Uninstall {
// Ignore blank or last line // Ignore blank or last line
if (fileName === '') continue; if (fileName === '') continue;
const verifyHash = crypto.createHash('md5').update(await fs.promises.readFile(fileName)).digest('hex'); const verifyHash = crypto.createHash('md5').update(await fs.readFile(fileName)).digest('hex');
if (verifyHash !== hash && this.options['ignore-hash']) { if (verifyHash !== hash && this.options['ignore-hash']) {
console.error(chalk.red.bold`%s does not match MD5 hash of %s! To ignore this error, please add --ignore-hash in the command`, this.name, hash); console.error(chalk.red.bold`%s does not match MD5 hash of %s! To ignore this error, please add --ignore-hash in the command`, this.name, hash);
process.exit(1); process.exit(1);
} else { } else {
await fs.promises.rm(fileName); await fs.rm(fileName);
} }
} }
await fs.promises.rm(`/var/lib/wahpkg/pkgs/${this.name}`, { force: true, recursive: true }); await fs.rm(`/var/lib/wahpkg/pkgs/${this.name}`, { force: true, recursive: true });
db.data.pkgs = db.data.pkgs.filter((item) => item.name !== this.name); db.data.pkgs = db.data.pkgs.filter((item) => item.name !== this.name);
await db.write(); await db.write();

View file

@ -1,6 +1,6 @@
import chalk from 'chalk'; import chalk from 'chalk';
import { Command } from 'commander'; import { Command } from 'commander';
import fs from 'node:fs'; import fs from 'fs-extra';
import Install, { InstallOptions } from './commands/install.js'; import Install, { InstallOptions } from './commands/install.js';
import List from './commands/list.js'; import List from './commands/list.js';
@ -13,11 +13,11 @@ if (process.getuid() !== 0) {
const program = new Command(); const program = new Command();
if (!fs.existsSync('/var/lib/wahpkg/pkgs.json')) { if (!await fs.pathExists('/var/lib/wahpkg/pkgs.json')) {
console.log(chalk.green.bold`It looks like this is your first time using wahpkg! Setting up database for first use...`); console.log(chalk.green.bold`It looks like this is your first time using wahpkg! Setting up database for first use...`);
await fs.promises.mkdir('/var/lib/wahpkg', { recursive: true }); await fs.mkdir('/var/lib/wahpkg', { recursive: true });
await fs.promises.writeFile('/var/lib/wahpkg/pkgs.json', Buffer.from('{"dbVersion": 1, "pkgs": []}', 'utf8')); await fs.writeFile('/var/lib/wahpkg/pkgs.json', Buffer.from('{"dbVersion": 1, "pkgs": []}', 'utf8'));
console.log(chalk.green.bold`wahpkg was successfully setup!`); console.log(chalk.green.bold`wahpkg was successfully setup!`);
} }

View file

@ -1,4 +1,4 @@
import fs from 'node:fs'; import fs from 'fs-extra';
import path from 'node:path'; import path from 'node:path';
import type { PartialDeep, RequireAllOrNone } from 'type-fest'; import type { PartialDeep, RequireAllOrNone } from 'type-fest';