# all files / file-structure/ FileStructure.js

100% Statements 158/158
100% Branches 48/48
100% Functions 29/29
100% Lines 81/81
22 statements, 1 function, 18 branches 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
    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
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169

    1

    1
    26

    26

    26
    26
    26
    26
    26
    26

    1
    4
    4
    4

    4
    4

    4
    4
    4

    4
    1

    4
    2
    2

    4

    3
    3
    2
    2
    2
    2

    2
    1

    1
    1
    1
    1
    1

    1
    1

    1

    1
    1
    1
    1
    1

    3
    3
    2

    1

    2
    2
    1

    1

    2
    2
    1
    1

    2

    12
    11
    11

    1
    1

    34
    33
    33

    1
    1

    1
    5

    1
    3
    3
    3
    3
    3

    3

    1
    3

     'use strict';

    // Constants:
    import { config } from '../config';
    import constants from '../constants';
    const NEW_DIRECTORY_NAME = 'New Directory';

    // Utilities:
    import _ from 'lodash';
    import Promise from 'bluebird';
    import { join } from 'path';

    // Dependencies:
    import ComponentFile from '../files/ComponentFile';
    import Directory from './Directory';
    import files from '../files';
    import MockDataFile from '../files/MockDataFile';
    import StepDefinitionFile from '../files/StepDefinitionFile';

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

    export default class FileStructure {
        constructor () {
            this.init();
        }

        init () {
            let fileStructurePath = join(process.cwd(), config.testDirectory);

            this.allFiles = [];
            this.allFilesByPath = {};
            this.allDirectories = [];
            this.allDirectoriesByPath = {};
            this.structure = new Directory(fileStructurePath, null, this);
            this.references = {};
        }

        read () {
            return Promise.map(constants.DIRECTORIES, directoryName => {
                let directoryPath = join(this.structure.path, directoryName);
                let directory = new Directory(directoryPath, this.structure, this);
                return directory.read();
            });
        }

        getStructure (type) {
            let directory = this.structure.getDirectory(type);
            let references = this.references;

            let availableComponents;
            let availableMockData;
            let availableStepDefinitions;

            if (type === constants.FEATURES) {
                availableStepDefinitions = getFilesOfType.call(this, StepDefinitionFile);
            }
            if (type === constants.STEP_DEFINITIONS) {
                availableComponents = getFilesOfType.call(this, ComponentFile);
                availableMockData = getFilesOfType.call(this, MockDataFile);
            }

            return { availableComponents, availableMockData, availableStepDefinitions, directory, references };
        }

        copyFile (path) {
            let toCopy = this.allFilesByPath[path];
            if (toCopy) {
                let directory = toCopy.directory;
                let copyName = getUniqueName(directory.files, toCopy.name);
                let copyPath = join(directory.path, copyName);
                let copy = new toCopy.constructor(copyPath + directory.extension, directory);

                if (toCopy.ast) {
                    let copyAst = _.clone(toCopy.ast, true);
                    let [metaComment] = copyAst.comments;
                    let meta = JSON.parse(metaComment.value);
                    meta.name = copyName;
                    metaComment.value = JSON.stringify(meta);
                    copy.ast = copyAst;
                    return copy.save();
                } else {
                    copy.content = toCopy.content;
                    return copy.save();
                }
            } else {
                return Promise.reject(new TractorError(`Could not find "${path}"`, constants.FILE_NOT_FOUND_ERROR));
            }
        }

        createDirectory (path) {
            let directory = this.allDirectoriesByPath[path];
            let newDirectoryName = getUniqueName(directory.directories, NEW_DIRECTORY_NAME);
            let newDirectoryPath = join(path, newDirectoryName);
            let newDirectory = new Directory(newDirectoryPath, directory, this);
            return newDirectory.save();
        }

        deleteItem (path) {
            let toDelete = this.allFilesByPath[path] || this.allDirectoriesByPath[path];
            if (toDelete) {
                return toDelete.delete();
            } else {
                return Promise.reject(new TractorError(`Could not find "${path}"`, constants.FILE_NOT_FOUND_ERROR));
            }
        }

        openFile (path) {
            let toOpen = this.allFilesByPath[path];
            if (toOpen) {
                return toOpen.read()
                .then(() => toOpen);
            } else {
                return Promise.reject(new TractorError(`Could not find "${path}"`, constants.FILE_NOT_FOUND_ERROR));
            }
        }

        saveFile (type, data, path) {
            let toSave = this.allFilesByPath[path];
            if (!toSave) {
                let directory = this.structure.getDirectory(type);
                toSave = new files[type](path, directory);
            }
            return toSave.save(data);
        }

        addFile (file) {
            if (!_.contains(this.allFiles, file)) {
                this.allFilesByPath[file.path] = file;
                this.allFiles.push(file);
            }
        }

        removeFile (file) {
            delete this.allFilesByPath[file.path];
            _.remove(this.allFiles, file);
        }

        addDirectory (directory) {
            if (!_.contains(this.allDirectories, directory)) {
                this.allDirectoriesByPath[directory.path] = directory;
                this.allDirectories.push(directory);
            }
        }

        removeDirectory (directory) {
            delete this.allDirectoriesByPath[directory.path];
            _.remove(this.allDirectories, directory);
        }
    }

    function getFilesOfType (type) {
        return _.filter(this.allFiles, file => file instanceof type);
    }

    function getUniqueName (items, itemName) {
        let name = '';
        let n = 1;
        do {
            name = `${itemName} (${n})`.trim();
            n += 1;
        } while (nameExists(items, name));
        return name;
    }

    function nameExists (items, name) {
        return !!_.find(items, item => item.name === name);
    }