All files / src/systemTools cleaner.js

40% Statements 26/65
20% Branches 6/30
66.67% Functions 2/3
38.46% Lines 15/39

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 772x 2x 2x 2x   2x 2x   2x 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';
 
const rnvClean = async (c, skipQuestion = false) => {
    logTask('rnvClean');
    const pathsToRemove = [
        c.paths.project.nodeModulesDir,
        path.join(c.paths.project.dir, 'package-lock.json')
    ];
    let msg = chalk.red('./node_modules\n./package-lock.json\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(`./packages/${dir}\n`);
                }
            } else {
                const pth2 = path.join(packagesFolder, dir, 'node_modules');
                if (fs.existsSync(pth2)) {
                    pathsToRemove.push(pth2);
                    msg += chalk.red(`./packages/${dir}/node_modules\n`);
                }
 
                const pth3 = path.join(packagesFolder, dir, 'package-lock.json');
                if (fs.existsSync(pth3)) {
                    pathsToRemove.push(pth3);
                    msg += chalk.red(`./packages/${dir}/package-lock.json\n`);
                }
            }
        });
    }I
 
    if (pathsToRemove) {
        logToSummary('Nothing to clean');
        return Promise.resolve();
    }
 
 
    if (skipQuestion) {
        return removeDirs(pathsToRemove);
    }
 
    const { confirm } = await inquirer.prompt({
        name: 'confirm',
        type: 'confirm',
        message: `Are you sure you want to remove these files/folders? \n${msg}`,
    });
 
    if (confirm) {
        await removeDirs(pathsToRemove);
 
        const buildDirs = [
            c.paths.project.builds.dir,
            c.paths.project.assets.dir
        ];
        const { confirmBuilds } = await inquirer.prompt({
            name: 'confirmBuilds',
            type: 'confirm',
            message: `Do you also want to clean your platformBuilds and platformAssets? \n${chalk.red(buildDirs.join('\n'))}`,
        });
        if (confirmBuilds) {
            await removeDirs(buildDirs);
        }
    }
};
 
export { rnvClean };