Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzmmr committed Jan 3, 2018
0 parents commit 8afbce0
Show file tree
Hide file tree
Showing 6 changed files with 5,590 additions and 0 deletions.
65 changes: 65 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Folders created for test.js
foo
baz

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- '4.0'
- '9.0'
after_script: npm run report
162 changes: 162 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
'use strict';

var fs = require('fs');
var path = require('path');
var vfile = require('vfile');

module.exports = read;
module.exports.sync = readSync;

function read(location, options, callback) {
var queue;
var root;

if (typeof options === 'function') {
callback = options;
options = {};
}
if (!callback) {
return new Promise(function (resolve, reject) {
return read(location, options, function (err, file) {
if (err) {
return reject(err);
}
return resolve(file);
});
});
}

root = vfile({path: '.', contents: []});
queue = [location, root];
options = options || {};

if (Array.isArray(options)) {
options = {ignores: options};
} else if (typeof options === 'string') {
options = {encoding: options};
}

options.ignores = [].concat(options.ignores);
options.encoding = options.encoding || 'utf-8';

recurse();

function recurse() {
var current = queue.shift();
var parent = queue.shift();
var node;

if (!current) {
return callback(null, root.contents[0]);
}

// If (parent) {
// current = path.join(parent.path, current);
// }
current = path.join(parent.path, current);

if (options.ignores.indexOf(current) > -1) {
return recurse();
}

return fs.stat(current, function (err, stat) {
if (err) {
return callback(err);
}

node = vfile({path: current});

parent.contents.push(node);

if (stat.isDirectory()) {
return fs.readdir(current, options, function (err, files) {
/* istanbul ignore if */
if (err) {
return callback(err);
}

node.contents = [];

files.forEach(function (file) {
queue.push(file, node);
});

if (queue.length === 0) {
return callback(null, root.contents[0]);
}

return recurse();
});
}
return fs.readFile(current, options, function (err, data) {
/* istanbul ignore if */
if (err) {
return callback(err);
}

node.contents = data;

if (queue.length === 0) {
return callback(null, root.contents[0]);
}

return recurse();
});
});
}
}

function readSync(location, options) {
var current;
var parent;
var queue;
var root;
var stat;
var node;
var dirs;
var file;
var i;

root = vfile({path: '.', contents: []});
queue = [location, root];
options = options || {};

if (Array.isArray(options)) {
options = {ignores: options};
} else if (typeof options === 'string') {
options = {encoding: options};
}

options.ignores = [].concat(options.ignores);
options.encoding = options.encoding || 'utf-8';

while (queue.length > 0) {
current = queue.shift();
parent = queue.shift();

current = path.join(parent.path, current);
node = vfile({path: current, contents: []});

if (current && options.ignores.indexOf(current) > -1) {
continue;
}

stat = fs.statSync(node.path);
i = -1;

if (stat.isDirectory()) {
dirs = fs.readdirSync(current, options);
while (i++ < dirs.length - 1) {
queue.push(dirs[i], node);
}
} else {
file = fs.readFileSync(current, options);
node.contents = file;
}

parent.contents.push(node);
}

return root.contents[0];
}

Loading

0 comments on commit 8afbce0

Please sign in to comment.