Es6 version

Const fs = require('fs'); const path = require('path'); let collectObj = { filesList: [], dirsList: [] }; function readFileList(dir, collectObj) { let { filesList, dirsList, } = collectObj const files = fs.readdirSync(dir); files.forEach((item, index) => { var fullPath = path.join(dir, item); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { dirsList.push(fullPath); readFileList(path.join(dir, item), collectObj); } else {fileslist.push (fullPath); }}); return collectObj; } collectObj = readFileList(__dirname, collectObj) console.log(collectObj)Copy the code

The Typescript version

Const fs = require('fs'); const path = require('path'); interface CollectObjType { filesList: Array <String> , dirsList: Array <String> , } let collectObj: CollectObjType = { filesList: [], dirsList: [] }; function readFileList(dir, collectObj): CollectObjType { let { filesList, dirsList, } = collectObj const files:Array<String> = fs.readdirSync(dir); files.forEach((item, index) => { var fullPath = path.join(dir, item); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { dirsList.push(fullPath); readFileList(path.join(dir, item), collectObj); } else {fileslist.push (fullPath); }}); return collectObj; } collectObj = readFileList(__dirname, collectObj) console.log(collectObj)Copy the code