summaryrefslogtreecommitdiff
path: root/Components/ChangeDir/index.js
blob: db9d82e1bd586d9d99b2f374f6904a8face81f53 (plain)
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
const fs = require('fs');
const path = require('path');
const chalk = require(__dirname + '/../../MistyCore/node_modules/chalk');

module.exports = (arguments) => {
    if (arguments._finals.length > 0) {
        let newDirectory = path.resolve(arguments._finals[0]);

        if (fs.existsSync(newDirectory)) {
            try {
                fs.accessSync(newDirectory, fs.constants.R_OK);

                if (!fs.lstatSync(newDirectory).isDirectory()) {
                    console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("Not a directory: ") + arguments['_finals'][0]);
                } else {
                    process.chdir(newDirectory);
                }
            } catch (e) {
                console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("Permission denied: ") + arguments['_finals'][0]);
            }
        } else {
            console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("No such file or directory: ") + arguments['_finals'][0]);
        }
    } else {
        console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("Missing operand"));
    }
}