HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/blog.affstore/node_modules/.bin/directory-tree
#!/usr/bin/env node

const fs = require('fs');
const commandLineUsage = require('command-line-usage')
const commandLineArgs = require('command-line-args')
const directoryTree = require('../lib/directory-tree');

const optionList = [
    {
        name: 'path',
        alias: 'p',
        required: true,
        defaultOption: true,
        typeLabel: '{underline string}',
        description: '🗂 The input folder to process. Required.'
    },
    {
        name: 'exclude',
        alias: 'e',
        type: String,
        description: '🐒 Exclude some folders from processing by regexp string. Ex -e "test_data/some_dir$|js|.DS_Store"'
    },
    {
        name: 'output',
        alias: 'o',
        type: String,
        description: '📝 Put result into file provided by this options. Overwrites if exists.'
    },
    {
        name: 'depth',
        alias: 'd',
        type: Number,
        description: '☞ Reads dirs in deep as specified. Usage of size attribute with depth option is prohibited.'
    },
    {
        name: 'attributes',
        type: String,
        description: 'â„šī¸ Grab file attributes. Example: --attributes size,type,extension. Usage of size attribute with depth option is prohibited'
    },
    {
        name: 'pretty',
        type: Boolean,
        description: '💎 Json pretty print'
    },
    {
        name: 'help',
        alias: 'h',
        type: Boolean,
        description: 'â‰ī¸ Print this usage guide.'
    }
]

const usageNotes = [
    {
        header: 'â›„ī¸ī¸ Folder-tree command line script',
        content: 'Used for generates json representation of folder internals'
    },
    {
        header: 'đŸ”Ĩ Options đŸ”Ĩ',
        optionList: optionList
    }
]

const usage = commandLineUsage(usageNotes)
let options = null;
try {
    options = commandLineArgs(optionList)
} catch(e) {
    console.log(usage);
    return;
}

if (Object.keys(options).length === 0 || options.help || !options.path) {
    console.log(usage)
    return;
}

if (!fs.existsSync(options.path)) {
    console.log('-----------------------------------------------------------------------------------------------------')
    console.log(`doesn't exist; please check your args`);
    console.log('-----------------------------------------------------------------------------------------------------')
    console.log(usage)
    return;
}

try {
    const result = directoryTree(options.path, {
        depth: options.depth,
        exclude: options.exclude ? [new RegExp(options.exclude)] : undefined,
        attributes: options.attributes ? options.attributes.split(',') : undefined
    })

    const resultString = JSON.stringify(result, null, options.pretty ? '  ' : '');
    if (options.output) {
        fs.writeFileSync(options.output, resultString);
    } else {
        console.log(resultString);
    }
} catch(e) {
    console.log(e);
    console.log(usage);
}