I want to transfer files and folders in the project directory to object with NodeJS I want to define the 'root' folder to an object like 'treeview' However, it is recorded as 'pending' Can are help me solve this problem?
My NodeJS Code:
import path from 'path';
import {program} from 'commander';
import fs from 'fs';
import util from 'util';
async function getChild(parent){
const readDir = util.promisify(fs.readdir);
const dirList = await readDir(parent);
return dirList.map(async (name) => {
const dir = path.join(parent, name);
const isDir = fs.lstatSync(dir).isDirectory();
if (isDir) {
return {
type: 'directory',
name,
child: getChild(dir),
}
} else
return {
type: 'file',
name,
child: null,
}
});
}
export async function cli(args) {
let directoryTreeView = {};
const folderPath = path.resolve('');
directoryTreeView = Object.assign({}, directoryTreeView, {
type: 'directory',
name: 'root',
folderPath,
childs: await getChild(folderPath)
});
}
The result I got
{ type: 'directory',
name: 'root',
folderPath: 'O:\\rootFolder',
childs:
[ Promise { [Object] },
Promise { [Object] },
]
}
Must be
{
type: 'directory',
name: 'root',
child: [
{
type: 'directory',
name: 'assets',
child: [
{
type: 'file',
name: 'icon1.png'
},
]
},
{
type: 'file',
name: 'icon2.png',
child: null,
}
]
}