tree.js 764 字节
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

const path = require('path');
const fs = require('fs');

function collect(location) {
	const element = path.basename(location);
	const stat = fs.statSync(location);

	if (!stat.isDirectory()) {
		return { element };
	}

	const children = fs.readdirSync(location)
		.map(child => path.join(location, child))
		.map(collect);

	return { element, children };
}

console.log(JSON.stringify(collect(process.cwd())));