# all files / cli/init/ create-base-test-files.js

100% Statements 51/51
100% Branches 4/4
100% Functions 19/19
100% Lines 29/29
1 branch Ignored

    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

    1

    1
    7
    7

    1
    7
    7
    7
    7

    1
    7
    7
    7
    7

    1
    7
    7
    7
    7

    1
    21

    9

    1
    9

    1
    12

    1
    12

     'use strict';

    // Constants:
    import constants from '../../constants';

    // Utilities:
    import Promise from 'bluebird';
    const fs = Promise.promisifyAll(require('graceful-fs'));
    import log from 'npmlog';
    import { join } from 'path';

    // Errors:
    import TractorError from '../../errors/TractorError';

    export default {
        run: createBaseTestFiles
    };

    function createBaseTestFiles (testDirectoryPath) {
        let supportDirPath = join(testDirectoryPath, constants.SUPPORT_DIR);
        return createWorldFile(supportDirPath)
        .catch(TractorError, error => logNotCopying(error))
        .then(() => createProtractorConf(testDirectoryPath))
        .catch(TractorError, error => logNotCopying(error))
        .then(() => createHooksFile(supportDirPath))
        .catch(TractorError, error => logNotCopying(error));
    }

    function createWorldFile (supportDirPath) {
        let fileName = constants.WORLD_FILE_NAME;
        let readPath = join(__dirname, constants.WORLD_SOURCE_FILE_PATH);
        let writePath = join(process.cwd(), supportDirPath, constants.WORLD_FILE_NAME);
        return createFile(fileName, readPath, writePath);
    }

    function createHooksFile (supportDirPath) {
        let fileName = constants.HOOKS_FILE_NAME;
        let readPath = join(__dirname, constants.HOOKS_SOURCE_FILE_PATH);
        let writePath = join(process.cwd(), supportDirPath, constants.HOOKS_FILE_NAME);
        return createFile(fileName, readPath, writePath);
    }

    function createProtractorConf (testDirectoryPath) {
        let fileName = constants.PROTRACTOR_CONF_FILE_NAME;
        let readPath = join(__dirname, constants.PROTRACTOR_CONF_SOURCE_FILE_PATH);
        let writePath = join(process.cwd(), testDirectoryPath, constants.PROTRACTOR_CONF_FILE_NAME);
        return createFile(fileName, readPath, writePath);
    }

    function createFile (fileName, readPath, writePath) {
        return fs.openAsync(writePath, 'r')
        .then(() => {
            throw new TractorError(`"${fileName}" already exists.`);
        })
        .catch(Promise.OperationalError, () => logCreating(fileName))
        .then(() => fs.readFileAsync(readPath))
        .then(contents => fs.writeFileAsync(writePath, contents))
        .then(() => logCreated(fileName));
    }

    function logNotCopying (error) {
        log.warn(`${error.message} Not copying...`);
    }

    function logCreating (file) {
        log.info(`Creating "${file}"...`);
    }

    function logCreated (file) {
        log.verbose(`"${file}" created.`);
    }