tooltwist
Version:
Tooltwist Command Line Interface
104 lines (82 loc) • 2.46 kB
JavaScript
var Config = require('./config'),
rimraf = require('rimraf'), // rm -rf
Log = require('./log'),
fs = require('fs');
/**
* Remove all files from the .tooltwist directory, except the web design directory.
*/
exports.clean = function() {
console.log('Cleaning ' + Config.HIDDEN_DIR)
removeFilesInImageDirectory();
}
/**
* Remove files in .tooltwist/image, but not '.fip-source'.
*/
function removeFilesInImageDirectory() {
Log.debug('removeFilesInImageDirectory()')
// Be safe (we don't want to delete from /)
if ( !Config.HIDDEN_DIR || Config.HIDDEN_DIR == '') {
Log.fatalError('Attempt to clean with no HIDDEN_DIR');
}
// Check the image dir exists
var imageDir = Config.HIDDEN_DIR + '/image';
if ( !fs.existsSync(imageDir)) {
return removeFilesInHiddenDirectory();
}
// Iterate through the files
var files = fs.readdirSync(imageDir);
(function iterate(index) {
// Don't run off the end of the list
if (index >= files.length) {
return removeFilesInHiddenDirectory();
}
var file = files[index];
var path = imageDir + '/' + file;
if (file === '.fip-source') {
// Skip this directory
Log.debug(' skip ' + path)
return iterate(index + 1)
} else {
// remove this directory
Log.debug(' rm -rf ' + path)
rimraf(path, function() {
iterate(index + 1)
});
}
})(0) // Start with the first in the array.
}
/**
* Remove files in .tooltwist, but not 'image' or 'webdesign-projects'.
*/
function removeFilesInHiddenDirectory() {
Log.debug('removeFilesInHiddenDirectory()')
// Be safe (we don't want to delete from /)
if ( !Config.HIDDEN_DIR || Config.HIDDEN_DIR == '') {
Log.fatalError('Attempt to clean with no HIDDEN_DIR');
}
// Only proceed if the directory exists
if ( !fs.existsSync(Config.HIDDEN_DIR)) {
process.exit(0);
}
var files = fs.readdirSync(Config.HIDDEN_DIR);
// Iterate through the files
(function iterate2(index) {
// Don't run off the end of the list
if (index >= files.length) {
process.exit(0);
}
var file = files[index];
var path = Config.HIDDEN_DIR + '/' + file;
if (file === 'webdesign-projects' || file === 'image') {
// Skip this directory
Log.debug(' skip ' + path)
return iterate2(index + 1)
} else {
// remove this directory
Log.debug(' rm -rf ' + path)
rimraf(path, function(err) {
iterate2(index + 1)
});
}
})(0) // Start with the first in the array.
}