测试示例目录结构
w@w:~/my/project-exercise/node-test$ ls
index.js node_modules package.json package-lock.json
index.js
let fs = require('fs')
function mkdirSync(dir, cb) {
let paths = dir.split('/');
let index = 1;
function next(index) {
if (index > paths.length) return cb();
let newPath = paths.slice(0, index).join('/');
fs.stat(newPath, function (err) {
if (err) {
fs.mkdir(newPath, function (err) {
next(index + 1);
});
} else {
next(index + 1);
}
})
}
next(index);
}
// call mkdirSync func
// mkdirSync(paths, function () {
// console.log('success')
// })
// eg.
// mkdirSync('/home/w/my/project-exercise/node-test/abc/abc_1/abc_2', function () {
// console.log('success')
// })
package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"fs": "0.0.1-security"
}
}
网友评论