Skip to content

Commit

Permalink
style: formatter package_selector.mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
leoliu0605 committed Mar 12, 2024
1 parent cc36638 commit 74b664b
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/package_selector.mjs
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
import chalk from "chalk";
import fs from "fs";
import inquirer from "inquirer";
import os from "os";
import chalk from 'chalk';
import fs from 'fs';
import inquirer from 'inquirer';
import os from 'os';
import path, { dirname } from 'path';
import { getAppDir } from './dirname.mjs';

export async function selectPackages() {
const packagesJson = fs.readFileSync(path.join(getAppDir(), 'packageData.json'), 'utf8');
const packagesData = JSON.parse(packagesJson);

const currentOS = os.platform() === "win32" ? "windows" : os.platform() === "darwin" ? "mac" : "linux";
const currentOS = os.platform() === 'win32' ? 'windows' : os.platform() === 'darwin' ? 'mac' : 'linux';

const choices = Object.entries(packagesData).flatMap(([category, packages]) => [
new inquirer.Separator(chalk.red(`-- ${category} --`)),
...Object.entries(packages)
.filter(([_, packageDetails]) => packageDetails.install[currentOS] !== "")
.filter(([_, packageDetails]) => packageDetails.install[currentOS] !== '')
.map(([packageName, packageDetails]) => {
let choiceName = packageName;
if (packageDetails.description) {
choiceName += ` - ${packageDetails.description}`;
}

if (packageDetails.type === "force") {
if (packageDetails.type === 'force') {
return {
name: choiceName,
checked: true,
disabled: chalk.yellow("Forced installation"), // This will disable the option to deselect
disabled: chalk.yellow('Forced installation'), // This will disable the option to deselect
};
}

return {
name: choiceName,
checked: packageDetails.type === "enable",
checked: packageDetails.type === 'enable',
};
}),
]);

try {
const answers = await inquirer.prompt([
{
type: "checkbox",
message: "Select packages to install",
name: "selectedPackages",
type: 'checkbox',
message: 'Select packages to install',
name: 'selectedPackages',
choices: choices,
pageSize: 15,
},
]);

const forcedPackages = Object.entries(packagesData).flatMap(([category, packages]) =>
Object.entries(packages)
.filter(([_, packageDetails]) => packageDetails.type === "force" && packageDetails.install[currentOS] !== "")
.filter(([_, packageDetails]) => packageDetails.type === 'force' && packageDetails.install[currentOS] !== '')
.map(([packageName, _]) => packageName)
);

// Remove the description from the selected packages
const cleanedSelectedPackages = answers.selectedPackages.map((packageName) => packageName.replace(/ - .*/, ""));
const cleanedSelectedPackages = answers.selectedPackages.map((packageName) => packageName.replace(/ - .*/, ''));

const selectedPackages = [...forcedPackages, ...cleanedSelectedPackages];

Expand All @@ -71,7 +73,7 @@ export async function selectPackages() {
})
.filter((pkg) => pkg !== null);
} catch (error) {
console.error("An error occurred:", error);
console.error('An error occurred:', error);
throw error;
}
}

0 comments on commit 74b664b

Please sign in to comment.