import prompts from 'prompts' import colors from 'colors' import fs from 'fs' import { startTask } from 'misty/task' import { execSync } from 'child_process' import { clearLogLines, execConfig } from '../helpers' export const updateAllPackages = async () => { console.info(`\n ${colors.yellow('Checking available updates')}`) const checkUpdates = execSync( 'npx npm-check-updates', execConfig, ) if (checkUpdates.includes('All dependencies match the latest package versions :)')) { console.info(`\n${colors.green.bold(checkUpdates)}`) process.exit(1) } else { console.info(checkUpdates) clearLogLines(2) } console.info(`\n ${colors.green.bold('Would you like to update ALL packages to their latest version?')}`) console.info(`\n This will: 1. change the package.json 2. remove all the node_modules folder 3. install the new packages.\n`) const updateAll = await prompts( { type: 'confirm', name: 'answer', message: 'CONFIRM UPDATE?', initial: true, }, ) if (updateAll.answer) { console.info(`\n ${colors.green.bold('Please wait while we update all the packages. This may take a while...')}`) const updatePackageJson = startTask('Updating the package.json') const applyUpdates = execSync( 'npx npm-check-updates -u', execConfig, ) console.info(applyUpdates) clearLogLines(2) updatePackageJson.finish('package.json is updated') const removeNodeModules = startTask('Removing node_modules') const path = './node_modules' fs.rmSync(path, { recursive: true, force: true }) removeNodeModules.finish('node_modules removed') const installNewPackages = startTask('Installing new node modules') const installNodeModules = execSync( 'npm install', execConfig, ) console.info(installNodeModules) installNewPackages.finish('Node modules updated') process.exit(1) } else { process.exit(1) } } export default updateAllPackages