All files / src/systemTools cleaner.js

33.98% Statements 35/103
19.67% Branches 12/61
66.67% Functions 2/3
41.38% Lines 24/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 1032x 2x 2x 2x   2x 2x 2x   2x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                 1x 1x 1x   1x       1x     1x                                                                                     2x          
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import inquirer from 'inquirer';
 
import { removeDirs } from './fileutils';
import { logTask, logToSummary } from './logger';
import { executeAsync } from './exec';
 
const rnvClean = async (c, skipQuestion = false) => {
    logTask('rnvClean');
    if (c.Iprogram.ci) skipQuestion = true;
    const pathsToRemove = [];
    if (fsE.existsSync(c.paths.project.nodeModulesDir)) pathsToRemove.push(c.paths.project.nodeModulesDir);
    const pkgLock = path.join(c.paths.project.dir, 'package-lock.json');
    if (fsI.existsSync(pkgLock)) pathsToRemove.push(pkgLock);
    let msg = chalk.red(`${c.paths.project.nodeModulesDir}\n${pkgLock}\n`);
    const packagesFolder = path.join(c.paths.project.dir, 'packages');
    if (fsI.existsSync(packagesFolder)) {
        fs.readdirSync(packagesFolder).forEach((dir) => {
            if (dir === '.DS_Store') {
                const pth = path.join(packagesFolder, dir);
 
                if (fs.existsSync(pth)) {
                    pathsToRemove.push(pth);
                    msg += chalk.red(`${pth}\n`);
                }
            } else {
                const pth2 = path.join(packagesFolder, dir, 'node_modules');
                if (fs.existsSync(pth2)) {
                    pathsToRemove.push(pth2);
                    msg += chalk.red(`${pth2}\n`);
                }
 
                const pth3 = path.join(packagesFolder, dir, 'package-lock.json');
                if (fs.existsSync(pth3)) {
                    pathsToRemove.push(pth3);
                    msg += chalk.red(`${pth3}\n`);
                }
            }
        });
    }
 
    const buildDirs = [];
    if (fsI.existsSync(c.paths.project.builds.dir)) buildDirs.push(c.paths.project.builds.dir);
    if (fsI.existsSync(c.paths.project.assets.dir)) buildDirs.push(c.paths.project.assets.dir);
 
    const answers = {
        modules: false,
        builds: false,
        cache: false,
        nothingToClean: !skipQuestionI
    };
 
    if (pathsToRemove.length && !skipQuestion) {
        const { confirm } = await inquirer.prompt({
            name: 'confirm',
            type: 'confirm',
            message: `Do you want to remove node_module related files/folders? \n${msg}`,
        });
        answers.modules = confirm;
        if (confirm) answers.nothingToClean = false;
    }
 
    if (buildDirs.length && !skipQuestion) {
        const { confirmBuilds } = await inquirer.prompt({
            name: 'confirmBuilds',
            type: 'confirm',
            message: `Do you want to clean your platformBuilds and platformAssets? \n${chalk.red(buildDirs.join('\n'))}`,
        });
        answers.builds = confirmBuilds;
        if (confirmBuilds) answers.nothingToClean = false;
    }
 
    if (!skipQuestion) {
        const { confirmCache } = await inquirer.prompt({
            name: 'confirmCache',
            type: 'confirm',
            message: 'Do you want to clean your npm/bundler cache?',
        });
        answers.cache = confirmCache;
        if (confirmCache) answers.nothingToClean = false;
    }
 
    if (answers.nothingToClean) {
        logToSummary('Nothing to clean');
        return Promise.resolve();
    }
 
    if (answers.modules) {
        await removeDirs(pathsToRemove);
    }
    if (answers.builds) {
        await removeDirs(buildDirs);
    }
    if (answers.cache) {
        await executeAsync(c, 'watchman watch-del-all');
        await executeAsync(c, 'rm -rf $TMPDIR/metro-* && rm -rf $TMPDIR/react-* && rm -rf $TMPDIR/haste-*');
    }
};
 
export { rnvClean };