# all files / api/ edit-item-path.js

100% Statements 96/96
100% Branches 33/33
100% Functions 15/15
100% Lines 52/52

    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

    1

    1

    8

    8
    4

    4

    2

    2

    1

    1

    1

    7
    7
    7
    7
    7
    7

    7
    2
    2
    1

    1

    2

    7

    1
    2
    2
    2

    1
    9
    4

    5

    1
    4

    4
    1

    4
    1

    4
    1

    4

    1
    5

    5

    5
    5
    2
    2
    2
    2
    2

     'use strict';

    // Constants:
    const FILE_NUMBER_REGEX = /\((\d*)\)$/;

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

    // Dependencies:
    import Directory from '../file-structure/Directory';
    import File from '../files/File';
    import fileStructure from '../file-structure';
    import getFileStructure from './get-file-structure';
    import transformers from './transformers';

    // Errors:
    import errorHandler from '../errors/error-handler';
    import TractorError from '../errors/TractorError';

    export default { handler };

    function handler (request, response) {
        let { isDirectory } = request.body;
        let { name, oldName, newName } = request.body;
        let { directoryPath, oldDirectoryPath, newDirectoryPath } = request.body;

        return Promise.resolve()
        .then(() => {
            if (isDirectory && oldName && newName) {
                return createUpdateOptions(fileStructure.allDirectoriesByPath, {
                    oldName,
                    newName,
                    oldDirectoryPath: directoryPath,
                    newDirectoryPath: directoryPath
                });
            } else if (!isDirectory && oldName && newName) {
                let { extension } = fileStructure.allDirectoriesByPath[directoryPath];
                return createUpdateOptions(fileStructure.allFilesByPath, {
                    oldName,
                    newName,
                    oldDirectoryPath: directoryPath,
                    newDirectoryPath: directoryPath,
                    extension
                });
            } else if (!isDirectory && oldDirectoryPath && newDirectoryPath) {
                let { extension } = fileStructure.allDirectoriesByPath[oldDirectoryPath];
                return createUpdateOptions(fileStructure.allFilesByPath, {
                    oldName: name,
                    newName: name,
                    oldDirectoryPath,
                    newDirectoryPath,
                    extension
                });
            } else {
                throw new TractorError('Unknown operation');
            }
        })
        .then(() => getFileStructure.handler(request, response))
        .catch(TractorError, error => errorHandler.handler(response, error));
    }

    function createUpdateOptions (collection, options) {
        let { oldName, newName, oldDirectoryPath, newDirectoryPath } = options;
        let extension = options.extension || '';
        let oldPath = join(oldDirectoryPath, `${oldName}${extension}`);
        let newPath = join(newDirectoryPath, `${newName}${extension}`);
        let item = collection[oldPath];
        let existingItem = collection[newPath];
        let directory = fileStructure.allDirectoriesByPath[newDirectoryPath];

        while (existingItem) {
            newName = incrementName(newName);
            if (item instanceof File) {
                newPath = join(newDirectoryPath, newName + extension);
            } else {
                newPath = join(newDirectoryPath, newName);
            }
            existingItem = collection[newPath];
        }

        return updateItem(item, directory, {
            oldName,
            newName,
            oldPath,
            newPath
        });
    }

    function incrementName (name) {
        let n = _.last(name.match(FILE_NUMBER_REGEX));
        n = +(n + 1 || 1);
        return `${name} (${n})`;
    }

    function updateItem (item, directory, update) {
        if (item instanceof File) {
            return updateFile(item, directory, update);
        } else {
            return updateDirectory(item, directory, update);
        }
    }

    function updateFile (toUpdate, directory, update) {
        let newFile = new toUpdate.constructor(update.newPath, directory);

        if (toUpdate.ast) {
            newFile.ast = toUpdate.ast;
        }
        if (toUpdate.content) {
            newFile.content = toUpdate.content;
        }
        if (toUpdate.tokens) {
            newFile.tokens = toUpdate.tokens;
        }

        let { type } = toUpdate.directory;
        return newFile.save()
        .then(() => transformers[type](newFile, update))
        .then(() => toUpdate.delete());
    }

    function updateDirectory (toUpdate, directory, update) {
        let newDirectory = new Directory(update.newPath, directory, fileStructure);

        return newDirectory.save()
        .then(() => {
            let items = toUpdate.directories.concat(toUpdate.files);
            return Promise.map(items, (item) => {
                let oldPath = item.path;
                let newPath = item.path.replace(update.oldPath, update.newPath);
                let oldName = item.name;
                let newName = item.name;
                return updateItem(item, newDirectory, { oldName, newName, oldPath, newPath });
            });
        })
        .then(() => toUpdate.delete());
    }