All files / src/commands add-browsersync.js

23.08% Statements 3/13
0% Branches 0/7
0% Functions 0/8
23.08% Lines 3/13

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                6x               6x                                         6x                                          
import {
    PackageJsonEditor,
    allDoExist,
    install,
    someDoExistSync,
    uninstall
} from '../api';
 
const BROWSERSYNC_DEPENDENCIES = [
    'browser-sync',
    'npm-run-all'
];
/**
 * @type {task[]}
 * @see https://www.browsersync.io/docs/command-line
 */
export const addBrowsersync = [
    {
        text: 'Add Browsersync tasks to package.json',
        task: async ({outputDirectory, port}) => {
            const scripts = {
                prestart: 'npm run build:es',
                start: `npm-run-all --parallel watch:es watch:css serve`,
                serve: `browser-sync start --server ${outputDirectory} --files ${outputDirectory} --port ${port}`
            };
            await (new PackageJsonEditor())
                .extend({scripts})
                .commit();
        },
        condition: ({useParcel}) => allDoExist('package.json', 'postcss.config.js') && (someDoExistSync('webpack.config.js', 'rollup.config.js') || useParcel) // eslint-disable-line max-len
    },
    {
        text: 'Install Browsersync dependencies',
        task: ({skipInstall}) => install(BROWSERSYNC_DEPENDENCIES, {dev: true, skipInstall}),
        condition: ({skipInstall}) => !skipInstall && allDoExist('package.json')
    }
];
export const removeBrowsersync = [
    {
        text: 'Remove Browsersync tasks from package.json',
        task: async () => {
            const scripts = {
                prestart: undefined,
                start: undefined,
                serve: undefined
            };
            await (new PackageJsonEditor())
                .extend({scripts})
                .commit();
        },
        condition: () => allDoExist('package.json')
    },
    {
        text: 'Uninstall Browsersync dependencies',
        task: () => uninstall(BROWSERSYNC_DEPENDENCIES),
        condition: () => allDoExist('package.json') && (new PackageJsonEditor()).hasAll(...BROWSERSYNC_DEPENDENCIES)
    }
];
export default addBrowsersync;