All files / src/commands add-rollup.js

17.24% Statements 5/29
0% Branches 0/8
0% Functions 0/15
17.24% Lines 5/29

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                        6x       6x         6x                       6x                                                                                                                                       6x                                                                        
import {join} from 'path';
import {
    PackageJsonEditor,
    RollupConfigEditor,
    allDoExist,
    allDoNotExist,
    allDoExistSync,
    debug,
    install,
    uninstall
} from '../api';
 
const DEPLOY_SCRIPTS = {
    predeploy: 'npm-run-all clean build:es build:css copy:assets',
    deploy: 'echo \"Not yet implemented - now.sh or surge.sh are supported out of the box\" && exit 1'
};
const BUILD_DEPENDENCIES = [
    'cpy-cli',
    'del-cli',
    'npm-run-all'
];
const ROLLUP_DEPENDENCIES = [
    'rollup',
    'rollup-plugin-babel',
    'rollup-plugin-commonjs',
    'rollup-plugin-node-resolve',
    'rollup-plugin-replace',
    'rollup-plugin-terser'
];
/**
 * @type {task[]}
 * @see https://rollupjs.org/guide/en/
 */
export const addRollup = [
    {
        text: 'Create Rollup configuration file',
        task: async ({outputDirectory, sourceDirectory, useReact}) => {
            const input =  `'${sourceDirectory}/main.js${useReact ? 'x' : ''}'`;
            const output = {
                file: `'${outputDirectory}/bundle.min.js'`
            };
            const plugins = [, `commonjs()`]; // the commonjs plugin needs to be the second item in the array
            await (new RollupConfigEditor())
                .create()
                .prepend(`import {terser} from 'rollup-plugin-terser';`)
                .prepend(`import replace from 'rollup-plugin-replace';`)
                .prepend(`import resolve from 'rollup-plugin-node-resolve';`)
                .prepend(`import commonjs from 'rollup-plugin-commonjs';`)
                .prepend(`import babel from 'rollup-plugin-babel';`)
                .prepend(`/* eslint-disable max-len */`)
                .extend({input, output})
                .extend(useReact ? {plugins} : {})
                .commit();
        },
        condition: () => allDoNotExist('webpack.config.js')
    },
    {
        text: 'Add Rollup build tasks to package.json',
        task: async ({assetsDirectory, outputDirectory, sourceDirectory}) => {
            const scripts = {
                ...DEPLOY_SCRIPTS,
                clean: `del-cli ${outputDirectory}`,
                copy: 'npm-run-all --parallel copy:assets copy:index',
                'copy:assets': `cpy \"${assetsDirectory}/!(css)/**/*.*\" \"${assetsDirectory}/**/[.]*\" ${outputDirectory} --parents --recursive`,
                'copy:index': `cpy \"${assetsDirectory}/index.html\" ${outputDirectory}`,
                'prebuild:es': `del-cli ${join(outputDirectory, assetsDirectory)}`,
                'build:es': 'rollup -c',
                'postbuild:es': 'npm run copy',
                'watch:assets': `watch \"npm run copy\" ${assetsDirectory}`,
                'watch:es': `watch \"npm run build:es\" ${sourceDirectory}`
            };
            await (new PackageJsonEditor())
                .extend({scripts})
                .commit();
        },
        condition: () => allDoExist('package.json')
    },
    {
        text: 'Configure dev task',
        task: async ({skipInstall}) => {
            const scripts = {
                dev: 'stmux [ \"npm run watch:es\" : \"npm run lint:ing\" ]'
            };
            try {
                await install(['stmux'], {dev: true, skipInstall});
            } catch (err) {
                await debug(err, 'Failed to install stmux');
            }
            await (new PackageJsonEditor())
                .extend({scripts})
                .commit();
        },
        condition: () => allDoExist('package.json', '.eslintrc.js'),
        optional: () => allDoExistSync('package.json', '.eslintrc.js')
    },
    {
        text: 'Install Rollup dependencies',
        task: ({skipInstall}) => install([...BUILD_DEPENDENCIES, ...ROLLUP_DEPENDENCIES], {dev: true, skipInstall}),
        condition: ({skipInstall}) => !skipInstall && allDoExist('package.json')
    }
];
export const removeRollup = [
    {
        text: 'Delete Rollup configuration file',
        task: async () => {
            await (new RollupConfigEditor())
                .delete()
                .commit();
        },
        condition: () => allDoExist('rollup.config.js')
    },
    {
        text: 'Remove Rollup build tasks from package.json',
        task: async () => {
            const scripts = {
                copy: undefined,
                'copy:assets': undefined,
                'copy:index': undefined,
                'watch:assets': undefined,
                dev: undefined,
                'prebuild:es': undefined,
                'build:es': undefined,
                'postbuild:es': undefined,
                'watch:es': undefined
            };
            await (new PackageJsonEditor())
                .extend({scripts})
                .commit();
        },
        condition: () => allDoExist('package.json')
    },
    {
        text: 'Uninstall Rollup dependencies',
        task: () => uninstall([...BUILD_DEPENDENCIES, ...ROLLUP_DEPENDENCIES, 'stmux']),
        condition: () => allDoExist('package.json') && (new PackageJsonEditor()).hasAll(...ROLLUP_DEPENDENCIES)
    }
];
export default addRollup;