Toolset
Toolset is a set of utilities that makes it a lot faster and easier to do the things you need all the time in your projects.
It is split into modules.
Install
npm install toolset
var toolset = require('toolset');
files
Read non binary files and urls
// Read a local file
toolset.file.read('file.txt', function(content) {
// content === false if the file doesn't exist.
});
// Read a remote file (GET)
toolset.file.read('http://www.server.com/file', function(content) {
// content === false if the server's response code is not 200.
});
Read a JSON file (and get the output as object already)
// Read a local JSON file
toolset.file.toObject('file.json', function(obj) {
// content === false if the file doesn't exist.
});
// Read a remote JSON file (GET)
toolset.file.read('http://www.server.com/file.json', function(obj) {
// content === false if the server's response code is not 200.
});
Read a binary file
// Read a local file
toolset.file.readBinary('logo.jpg', function(content) {
// content === false if the file doesn't exist.
});
// Read a remote file (GET)
toolset.file.readBinary('http://www.server.com/logo.jpg', function(content) {
// content === false if the server's response code is not 200.
});
Append to a file
toolset.file.append('file.txt', 'some content', function() {
// done
});
Write/create a file
toolset.file.write('file.txt', 'some content', function() {
// done
});
Write/create a JSON object to a file
toolset.file.writeJson('file.json', {hello:'world'}, function() {
// done
});
Delete a file
toolset.file.removeFile('file.json', function() {
// done
});
Copy a file
toolset.file.copy('file.txt', 'copy.txt', function() {
// done
});
Test if a file exists
toolset.file.exists('file.txt', function(exists) {
if (exists) {
// file exists
} else {
// fiel doesn't exist
}
});
List the files in a directory (including subdirectories)
// List all the JS files
toolset.file.listFiles('/home', 'js', function(files) {
// ['/home/file.js', '/home/subdirecotry/somefile.js', ...]
});
// List any file
toolset.file.listFiles('/home', false, function(files) {
// ...
});
List the files with a specific filename (including subdirectories)
// List any file
toolset.file.listByFilename('./bower_components', 'bower.json', function(files) {
// ...
});
List the files in a directory (not including subdirectories)
toolset.file.getDirContent('/home', function(files) {
// ...
});
Test if it's a dir or a file
var isDir = toolset.file.isDir('something');
Create a path (create all the directories required)
toolset.file.createPath('/hello/world/test/', function() {
// ...
});
Delete a directory and its content
toolset.file.removeDir('/some_directory', function() {
// ...
});
Stack
If you want to get out of the callback nightmare, you'll need to organize your code better. Some people use promises, but I personnaly prefer the stack approach to the issue.
For example, let's say you want to read the files 0.js, 1.js, 2.js ... 9.js and concatenate them in a single file:
// Create a new stack instance
var stack = new toolset.stack();
// Create a variable to store the files
var concatenated = "";
for (i=0;i<10;i++) {
stack.add(function(params, callback) {
toolset.file.read(params.i+'.js', function(content) {
concatenated += content;
// We're done processing, we call the callback function to let the stack know we are done with that particular task.
callback();
});
}, {i: i});
}
// Now that we have the tasks setup, we execute them
// The callback function in process() will get execute only once all of the tasks are finished.
stack.process(function() {
// Write the file
toolset.file.write('concatenated.js', concatenated);
}, false); // false = sync. true = async.
Archiving
One need that keep coming back in many project is the ability to archive and then extract files.
[coming soon]