All files / src/commands/add-reason index.js

82.61% Statements 19/23
40% Branches 2/5
80% Functions 12/15
82.61% Lines 19/23

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                      6x     6x             6x       2x 2x         2x         2x       2x       2x         2x             2x                 2x     6x       1x       1x         1x       1x       1x         1x      
import {join} from 'path';
import {
    BsConfigJsonEditor,
    PackageJsonEditor,
    Scaffolder,
    allDoExist,
    allDoNotExist,
    install,
    uninstall
} from '../../api';
 
const DEPENDENCIES = [
    'reason-react'
];
const DEV_DEPENDENCIES = [
    'bs-platform'
];
/**
 * @type {task[]}
 * @see https://reasonml.github.io/reason-react/en/
 */
export const addReason = [
    {
        text: 'Create bsconfig.json file',
        task: async () => {
            const {name} = (new PackageJsonEditor()).read();
            await (new BsConfigJsonEditor())
                .create()
                .extend({name})
                .commit();
        },
        condition: () => allDoNotExist('bsconfig.json')
    },
    {
        text: 'Add Reason scripts to package.json',
        task: async () => {
            const scripts = {
                'build:reason': 'bsb -make-world -clean-world',
                'watch:reason': 'npm run build:reason -- -w'
            };
            await (new PackageJsonEditor())
                .extend({scripts})
                .commit();
        },
        condition: () => allDoExist('package.json')
    },
    {
        text: 'Copy ReasonReact boilerplate files',
        task: async ({overwrite, sourceDirectory}) => {
            await (new Scaffolder(join(__dirname, 'templates')))
                .overwrite(overwrite)
                .target(`${sourceDirectory}/components`)
                .copy('App.re')
                .copy('Example.re')
                .commit();
        },
        condition: ({useReact}) => useReact,
        optional: ({useReact}) => useReact
    },
    {
        text: 'Install ReasonReact dependencies',
        task: async ({skipInstall}) => {
            await install(DEPENDENCIES, {skipInstall});
            await install(DEV_DEPENDENCIES, {dev: true, skipInstall});
        },
        condition: ({skipInstall}) => !skipInstall && allDoExist('package.json')
    }
];
export const removeReason = [
    {
        text: 'Delete bsconfig.json file',
        task: async () => {
            await (new BsConfigJsonEditor())
                .delete()
                .commit();
        },
        condition: () => allDoExist('bsconfig.json')
    },
    {
        text: 'Remove Reason scripts from package.json',
        task: async () => {
            const scripts = {
                'build:reason': undefined,
                'watch:reason': undefined
            };
            await (new PackageJsonEditor())
                .extend({scripts})
                .commit();
        },
        condition: () => allDoExist('package.json')
    },
    {
        text: 'Uninstall Reason dependencies',
        task: () => uninstall([...DEPENDENCIES, ...DEV_DEPENDENCIES]),
        condition: ({skipInstall}) => !skipInstall && allDoExist('package.json') && (new PackageJsonEditor()).hasAll(...DEPENDENCIES, ...DEV_DEPENDENCIES) // eslint-disable-line max-len
    }
];
export default addReason;