Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chunlin-Li committed Apr 10, 2016
1 parent e69ddcf commit 4d251e5
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 0 deletions.
78 changes: 78 additions & 0 deletions .gitignore
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)
14 changes: 14 additions & 0 deletions README.MD
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.
47 changes: 47 additions & 0 deletions example_01/traverser.js
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);
}
});
67 changes: 67 additions & 0 deletions example_02/traverser.js
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);
}
});

0 comments on commit 4d251e5

Please sign in to comment.