Skip to content

Commit

Permalink
feat: Install less dependencies by default.
Browse files Browse the repository at this point in the history
glossarify-md now  asks you to (temporarily) install
'inquirer' yourself if it detects the need to upgrade
your glossary configuration. This will save bandwidth
on regular installations and reduce unnecessary
security warnings.

'inquirer' is a library to create interactive
console dialogs. It's used to ask you whether you
want to upgrade and keep a backup of your config.
  • Loading branch information
about-code committed Sep 21, 2021
1 parent f94c06f commit 986bdec
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 69 deletions.
14 changes: 14 additions & 0 deletions lib/cli/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,17 @@ a different "outDir" or configure "outDirDropOld: false".
export const OUTDIR_NOT_DELETED =
`⚠ Could not delete "outDir". Will proceed by copying files...
`;

export const UPGRADE_REQUIRED =
`Your configuration needs an upgrade.
--------------------------------------
`

export const INQUIRER_REQUIRED =
`To proceed, please install "inquirer". Then run glossarify-md again.
npm i --no-save inquirer
You will be asked once again before upgrading. In case you do not want
to upgrade right now, please downgrade glossarify-md to your previous
version.`
58 changes: 35 additions & 23 deletions lib/cli/upgrade.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "fs-extra";
import inquirer from "inquirer";
import { INQUIRER_REQUIRED, UPGRADE_REQUIRED } from "./messages.js";
import { v4To5 } from "./upgrade/v4to5.js";
import { getFullVersion, getMajorVersion } from "./upgrade/version.js";

Expand Down Expand Up @@ -47,29 +47,41 @@ function applyUpgrades(userConfData, defaultConfData) {
* @param {CallableFunction} userConfModifierFn
*/
function updateConfig(userConfData, userConfFile, defaultConfData, userConfModifierFn) {
return inquirer.prompt([{
message: "Your configuration needs an upgrade. Keep a copy of the old version?"
,type: "confirm"
,name: "keepBackup"
,required: true
,default: "y"
}]).then((answers) => {
return new Promise((resolve, reject) => {
try {
if (answers.keepBackup) {
const backupFile = userConfFile + ".old";
fs.copySync(userConfFile, backupFile);
console.info(`A backup of your previous configuration has been written to '${backupFile}'`);
}
const newConf = userConfModifierFn.call(undefined, userConfData, defaultConfData);
fs.writeFile(userConfFile, JSON.stringify(newConf, null, 2), "utf-8", (err) => {
if (err) { reject(err); }
resolve(newConf);
});
} catch (error) {
reject(error);
}
console.log(UPGRADE_REQUIRED);
return new Promise((resolve, reject) => {
const importAsync = import("inquirer");
importAsync.catch(err => {
console.log(INQUIRER_REQUIRED);
reject("");
});
importAsync
.then(module => {
const inquirer = module.default;
return inquirer.prompt([{
message: "Keep a copy of the old version?"
,type: "confirm"
,name: "keepBackup"
,required: true
,default: "y"
}])
})
.then(answers => {
try {
if (answers.keepBackup) {
const backupFile = userConfFile + ".old";
fs.copySync(userConfFile, backupFile);
console.info(`A backup of your previous configuration has been written to '${backupFile}'`);
}
const newConf = userConfModifierFn.call(undefined, userConfData, defaultConfData);
fs.writeFile(userConfFile, JSON.stringify(newConf, null, 2), "utf-8", (err) => {
if (err) { reject(err); }
resolve(newConf);
});
} catch (error) {
reject(error);
}
})
.catch(reject);
});
}

Expand Down
Loading

0 comments on commit 986bdec

Please sign in to comment.