File: //var/www/quadcode.com/node_modules/directory-tree/bin/index.js
#!/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);
}