From 2c82403863566e2299fef330c81c68899aecdb3a Mon Sep 17 00:00:00 2001 From: Daryl Ronningen Date: Sat, 10 Sep 2022 16:55:54 -0700 Subject: [PATCH] feat: migrate to fs-extra --- src/commands/install.ts | 23 +++++++++++------------ src/commands/uninstall.ts | 10 +++++----- src/index.ts | 8 ++++---- src/utils.ts | 2 +- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/commands/install.ts b/src/commands/install.ts index 77dccaf..439a5f9 100644 --- a/src/commands/install.ts +++ b/src/commands/install.ts @@ -1,8 +1,7 @@ import chalk from 'chalk'; -import fse from 'fs-extra'; +import fs from 'fs-extra'; 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'; @@ -26,19 +25,19 @@ export default class Install { process.exit(1); } - 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 (!await fs.pathExists(path.join(os.tmpdir(), 'wahpkg'))) await fs.mkdir(path.join(os.tmpdir(), 'wahpkg')); + const makeExtractDir = await fs.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 }); - 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.`); 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 dbFile = '/var/lib/wahpkg/pkgs.json'; @@ -58,9 +57,9 @@ 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`, ''); + if (!await fs.pathExists('/var/lib/wahpkg/pkgs')) await fs.mkdir('/var/lib/wahpkg/pkgs'); + await fs.mkdir(`/var/lib/wahpkg/pkgs/${getWahInfo.name}`); + await fs.writeFile(`/var/lib/wahpkg/pkgs/${getWahInfo.name}/MD5HASHES`, ''); const md5Adapter = new lowdb.TextFile(`/var/lib/wahpkg/pkgs/${getWahInfo.name}/MD5HASHES`); await md5Adapter.read(); @@ -69,15 +68,15 @@ export default class Install { for (const file of await walk(makeExtractDir)) { 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'); toWriteToFile += `${file.replace(`${makeExtractDir}/ROOT`, this.options.sysroot)} ${hash}\n`; } await md5Adapter.write(toWriteToFile); - await fse.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.move(path.join(makeExtractDir, 'ROOT'), path.join(this.options.sysroot), { overwrite: true }); + await fs.rm(path.join(makeExtractDir), { recursive: true, force: true }); console.log(chalk.bold.green`Package %s has been installed!`, getWahInfo.name); process.exit(0); diff --git a/src/commands/uninstall.ts b/src/commands/uninstall.ts index a261423..531212b 100644 --- a/src/commands/uninstall.ts +++ b/src/commands/uninstall.ts @@ -1,7 +1,7 @@ import chalk from 'chalk'; +import fs from 'fs-extra'; import * as lowdb from 'lowdb'; import crypto from 'node:crypto'; -import fs from 'node:fs'; import { dbData } from './install.js'; @@ -27,7 +27,7 @@ export default class Uninstall { 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')) { const fileName = file.split(' ')[0]; @@ -36,17 +36,17 @@ export default class Uninstall { // Ignore blank or last line 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']) { 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); } 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); await db.write(); diff --git a/src/index.ts b/src/index.ts index 51d9f43..f891eb2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import chalk from 'chalk'; import { Command } from 'commander'; -import fs from 'node:fs'; +import fs from 'fs-extra'; import Install, { InstallOptions } from './commands/install.js'; import List from './commands/list.js'; @@ -13,11 +13,11 @@ if (process.getuid() !== 0) { 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...`); - await fs.promises.mkdir('/var/lib/wahpkg', { recursive: true }); - await fs.promises.writeFile('/var/lib/wahpkg/pkgs.json', Buffer.from('{"dbVersion": 1, "pkgs": []}', 'utf8')); + await fs.mkdir('/var/lib/wahpkg', { recursive: true }); + await fs.writeFile('/var/lib/wahpkg/pkgs.json', Buffer.from('{"dbVersion": 1, "pkgs": []}', 'utf8')); console.log(chalk.green.bold`wahpkg was successfully setup!`); } diff --git a/src/utils.ts b/src/utils.ts index 69525f6..a65f84d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import fs from 'node:fs'; +import fs from 'fs-extra'; import path from 'node:path'; import type { PartialDeep, RequireAllOrNone } from 'type-fest';