work on improving the startup script more

pull/98/head
dperolio 2 years ago
parent e1a2b95485
commit 92cce64437
No known key found for this signature in database
GPG Key ID: 4191689562D51409

@ -3,11 +3,11 @@ const { existsSync, promises: { writeFile, mkdir, access, readdir, rm } } = requ
const { exec: _exec, spawn } = require('child_process');
const { join, posix, sep } = require('path');
const { prompt } = require('inquirer');
const pluralize = require('pluralize');
const { promisify } = require('util');
const chalk = require('chalk');
const exec = promisify(_exec);
let release;
/**
@ -30,6 +30,22 @@ Object.keys(COLORS).forEach(color => {
};
});
/**
* Start the injection process automatically.
*/
(async () => {
try {
await startInjectionProcess();
} catch (err) {
if (err.code === 'EACCES') {
return error('Vizality was unable to be injected due to missing permissions. Try again with elevated permissions.');
}
error(`The setup process encountered an issue that prevented it from running correctly:\n${err}`);
info(`If the problem persists, please join our Discord server and reach out for support: ${chalk.hex(COLORS.Brand)('https://invite.vizality.com') + chalk.reset()}.`);
return promptForRestartOrExit();
}
})();
/**
* Prompts the user, giving them an option to restart the injection process or exit.
* @returns {Promise<void>}
@ -222,95 +238,77 @@ async function getDiscordAppPath () {
return discordAppPath;
}
/**
* Run an npm command.
* Used in this script for installing Node package dependencies.
* @returns {Promise<void>}
*/
function installDependencies () {
return new Promise((resolve, reject) => {
const command = spawn('npm', [ 'install', '--only=production', '--silent' ], { cwd: join(__dirname, '..', '..', '..'), stdio: 'inherit', shell: true });
command.on('close', () => {
resolve();
});
command.on('error', err => {
reject(err);
});
});
}
/**
* Checks for and ensures all node_modules package depedencies are installed.
* @returns {Promise<void>}
*/
async function ensureDependencies () {
/**
* Installs node_modules production dependencies.
* @returns {Promise<void>}
*/
function install (args) {
return new Promise((resolve, reject) => {
const command = spawn('npm', args, { cwd: join(__dirname, '..'), stdio: 'inherit', shell: true });
command.on('close', () => {
resolve();
});
command.on('error', err => {
reject(err);
});
});
}
try {
const nodeModulesPath = join(__dirname, '..', '..', '..', 'node_modules');
const nodeModulesPath = join(__dirname, '..', 'node_modules');
info(`${existsSync(nodeModulesPath) ? 'Checking' : 'Installing'} dependencies. Please wait...`);
info('Checking dependencies. Please wait...');
/**
* Make sure all dependencies in our package file are installed.
*/
const { dependencies } = require('../package.json');
const missingDependencies = [];
const outdatedDependencies = [];
for (const dependency in dependencies) {
const dependencyPath = join(nodeModulesPath, dependency);
/**
* Let's make sure all the depdencies are installed.
* Make sure all dependencies in our package file are installed.
*/
if (!existsSync(dependencyPath)) {
missingDependencies.push(dependency);
} else {
const { dependencies } = require('../../../package.json');
const missingDependencies = [];
const outdatedDependencies = [];
for (const dependency in dependencies) {
const dependencyPath = join(nodeModulesPath, dependency);
/**
* Let's check the package versions and make sure they're the metting the qualifications of
* the expected version specified in the package file.
* Let's make sure all the depdencies are installed.
*/
const dependencyPackage = require(join(dependencyPath, 'package.json'));
const expectedVersion = parseInt(dependencies[dependency].replace(/[^\d]/g, ''));
const installedVersion = parseInt(dependencyPackage.version.replace(/[^\d]/g, ''));
if (installedVersion < expectedVersion) {
outdatedDependencies.push(dependency);
if (!existsSync(dependencyPath)) {
missingDependencies.push(dependency);
} else {
/**
* Let's check the package versions and make sure they're the metting the qualifications of
* the expected version specified in the package file.
*/
const dependencyPackage = require(`../../../node_modules/${dependency}/package.json`);
const expectedVersion = parseInt(dependencies[dependency].replace(/[^\d]/g, ''));
const installedVersion = parseInt(dependencyPackage.version.replace(/[^\d]/g, ''));
if (installedVersion < expectedVersion) {
outdatedDependencies.push(dependency);
}
}
}
}
const unresolvedDependencies = [
{
dependencies: missingDependencies,
count: missingDependencies.length,
type: 'missing'
},
{
dependencies: outdatedDependencies,
count: outdatedDependencies.length,
type: 'outdated'
}
];
const missingDependenciesMessage = `${missingDependencies.length} missing`;
const outdatedDependenciesMessage = `${outdatedDependencies.length} outdated`;
/**
* Try to install/update all missing and outdated dependencies.
*/
const errors = [];
for (const dependencies of unresolvedDependencies) {
if (dependencies.count) {
info(`Found ${dependencies.count} ${dependencies.type} ${pluralize('package', dependencies.count)}: ${dependencies.dependencies.map(dependency => dependency)}. Installing...`);
for (const dependency of dependencies.dependencies) {
try {
await install('install', dependency, '--only=production', '--silent');
} catch (err) {
errors.push(`"${dependency}" - `, err.message);
}
}
if (missingDependencies.length || outdatedDependencies.length) {
warn(`Found ${[ missingDependenciesMessage, outdatedDependenciesMessage ].filter(message => message)} ${missingDependencies.length + outdatedDependencies.length > 1 ? 'packages' : 'package'}. Resolving...`);
}
}
/**
* Output any errors that occurred while installing dependencies.
*/
if (errors.length) {
return error(`An error occured while installing the following:\n`, errors.join('\n'));
}
/**
* Install the missing and updated dependencies.
*/
await installDependencies();
return info('Dependencies are already up-to-date.');
return info('Dependencies are up-to-date!');
} catch (err) {
error(`An error occured while installing the dependencies:\n${err.message}`);
return process.exit(0);
}
}
/**
@ -339,7 +337,9 @@ async function uninject (discordAppPath) {
* Delete the Discord app directory.
*/
await rm(discordAppPath, { recursive: true, force: true });
return info('Vizality has been uninjected.');
brand('-----------------------------------------------');
brand('Vizality has been uninjected.');
brand('-----------------------------------------------');
}
/**
@ -374,6 +374,9 @@ async function inject (discordAppPath) {
await ensureDependencies();
await mkdir(discordAppPath);
Promise.all([
/**
* Relative to dist/setup.bundle.js
*/
writeFile(join(discordAppPath, 'index.js'), `require('${join(__dirname, '..', 'injector').replace(RegExp(sep.repeat(2), 'g'), '/')}');`),
writeFile(join(discordAppPath, 'package.json'), JSON.stringify({
main: 'index.js',
@ -381,7 +384,9 @@ async function inject (discordAppPath) {
}, null, 2))
]);
return success(`Vizality has been successfully injected! Please restart ${release} to complete the process.`);
success('-----------------------------------------------');
success(`Vizality has been successfully injected!\nPlease restart ${release} to complete the process.`);
success('-----------------------------------------------');
}
/**
@ -565,19 +570,3 @@ function wrapChoicesInColor (choices = []) {
choices.forEach(choice => transformerChoices.push(`${chalk.hex(COLORS.Brand)(choice) + chalk.reset()}`));
return transformerChoices;
}
/**
* Start the injection process automatically.
*/
(async () => {
try {
await startInjectionProcess();
} catch (err) {
if (err.code === 'EACCES') {
return error('Vizality was unable to be injected due to missing permissions. Try again with elevated permissions.');
}
error(`The setup process encountered an issue that prevented it from running correctly:\n${err}`);
info(`If the problem persists, please join our Discord server and reach out for support: ${chalk.hex(COLORS.Brand)('https://invite.vizality.com') + chalk.reset()}.`);
return promptForRestartOrExit();
}
})();

Loading…
Cancel
Save