-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e69ddcf
commit 4d251e5
Showing
4 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
### Node template | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules | ||
jspm_packages | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
### JetBrains template | ||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm | ||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
|
||
# User-specific stuff: | ||
.idea** | ||
|
||
# Sensitive or high-churn files: | ||
.idea/dataSources.ids | ||
.idea/dataSources.xml | ||
.idea/dataSources.local.xml | ||
.idea/sqlDataSources.xml | ||
.idea/dynamic.xml | ||
.idea/uiDesigner.xml | ||
|
||
# Gradle: | ||
.idea/gradle.xml | ||
.idea/libraries | ||
|
||
# Mongo Explorer plugin: | ||
.idea/mongoSettings.xml | ||
|
||
## File-based project format: | ||
*.iws | ||
|
||
## Plugin-specific files: | ||
|
||
# IntelliJ | ||
/out/ | ||
|
||
# mpeltonen/sbt-idea plugin | ||
.idea_modules/ | ||
|
||
# JIRA plugin | ||
atlassian-ide-plugin.xml | ||
|
||
# Crashlytics plugin (for Android Studio and IntelliJ) | ||
com_crashlytics_export_strings.xml | ||
crashlytics.properties | ||
crashlytics-build.properties | ||
fabric.properties | ||
|
||
# Created by .ignore support plugin (hsz.mobi) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
traverse directory examples | ||
================= | ||
|
||
|
||
|
||
### [example 01](example_01/traverser.js) | ||
|
||
the most simple one, with callback hell (anonymous function), and use recursive. | ||
|
||
|
||
|
||
### [example 02](example_01/traverser.js) | ||
|
||
Eliminate recursive and callback hell. use named function instead of anonymous function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
/** | ||
* @param filePath the start path of traverse | ||
* @param callback (err, res). res is the absolute path of file. | ||
*/ | ||
function traverse(filePath, callback) { | ||
|
||
filePath = path.resolve(filePath); | ||
// get file stats | ||
fs.lstat(filePath, function(err, stats) { | ||
if (err) { | ||
callback(err); | ||
} else { | ||
// check whether a file or dir | ||
if (stats.isDirectory()) { | ||
// get all files and dirs in directory | ||
fs.readdir(filePath, function (err, files) { | ||
if (err) { | ||
callback(err); | ||
} else { | ||
files.forEach(function(item) { | ||
// call traverse recursively | ||
traverse(path.resolve(filePath, item), callback); | ||
}); | ||
} | ||
}); | ||
} else { | ||
// return the file path. | ||
callback(null, filePath); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/* call traverse */ | ||
traverse(process.argv[2], function(err, res) { | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
console.log(res); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
/** | ||
* @param filePath the start path of traverse | ||
* @param callback (err, res). res is the absolute path of file. | ||
*/ | ||
function traverse(filePath, callback) { | ||
// initial state | ||
var list = [ path.resolve(filePath) ]; | ||
// start traverse | ||
next(); | ||
|
||
function next() { | ||
var current = list.shift(); | ||
if (current) { | ||
// get file state info | ||
fs.lstat(current, check.bind(null, current)); | ||
} | ||
} | ||
|
||
function check(current, err, stats) { | ||
if (err) { | ||
callback(err); | ||
} else { | ||
// check whether a file or dir | ||
if (stats.isDirectory()) { | ||
// get all files and dirs in directory | ||
fs.readdir(current, add_subdir.bind(null, current)) | ||
} else { | ||
// return filePath | ||
callback(null, current); | ||
next(); | ||
} | ||
} | ||
} | ||
|
||
function add_subdir(current, err, files) { | ||
if (err) { | ||
callback(err); | ||
} else { | ||
// list and resolve all file in sub dir. | ||
var paths = files.map(function (item) { | ||
return path.resolve(current, item); | ||
}); | ||
// add to list | ||
list = list.concat(paths); | ||
next(); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
/* call traverse */ | ||
traverse(process.argv[2], function(err, res) { | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
console.log(res); | ||
} | ||
}); |