cluster-files
Advanced tools
Comparing version 0.1.0 to 0.1.1
//#!/usr/bin/env node | ||
(function () { | ||
(function() { | ||
@@ -8,2 +8,3 @@ 'use strict'; | ||
moment = require('moment'), | ||
inquirer = require('inquirer'), | ||
pt = require('path'); | ||
@@ -17,77 +18,90 @@ | ||
if (!fs.existsSync(base)) { | ||
console.error('Folder ' + base + ' does not exists'); | ||
console.error('Folder ' + base + ' does not exists'); | ||
return; | ||
} | ||
//add base folder to queue | ||
folders.push(base); | ||
// ask the user to contiune | ||
inquirer.prompt([{ | ||
type: "list", | ||
name: "process", | ||
message: "Process with folder '" + base + "' ?", | ||
choices: ['yes', 'no'] | ||
}], function(answers) { | ||
console.log(JSON.stringify(answers, null, " ")); | ||
//process folders | ||
function read (folder) { | ||
var hash = {}; | ||
if (fs.existsSync(folder)) { | ||
if (answers.process !== 'yes') return; | ||
var content = fs.readdirSync(folder); | ||
//add base folder to queue | ||
folders.push(base); | ||
content.forEach(function (file) { | ||
var path = pt.join(folder, file), | ||
stats = fs.statSync(path); | ||
if (stats.isDirectory()) { | ||
//add to folder queue | ||
//folders.push(path); | ||
} else { | ||
//add to files queue | ||
files[path] = true; | ||
} | ||
}); | ||
//process folders | ||
function read(folder) { | ||
var hash = {}; | ||
if (fs.existsSync(folder)) { | ||
var content = fs.readdirSync(folder); | ||
content.forEach(function(file) { | ||
var path = pt.join(folder, file), | ||
stats = fs.statSync(path); | ||
if (stats.isDirectory()) { | ||
//add to folder queue | ||
//folders.push(path); | ||
} else { | ||
//add to files queue | ||
files[path] = true; | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
// traverse subfolders | ||
var i = 0; | ||
while (folders[i]) { | ||
var folder = folders[i]; | ||
read(folder); | ||
i++; | ||
} | ||
// traverse subfolders | ||
var i = 0; | ||
while (folders[i]) { | ||
var folder = folders[i]; | ||
read(folder); | ||
i++; | ||
} | ||
function extension (file) { | ||
var list = file.split('.'); | ||
return list[list.length-1]; | ||
} | ||
function extension(file) { | ||
var list = file.split('.'); | ||
return list[list.length - 1]; | ||
} | ||
//move files to base folder | ||
Object.keys(files).forEach(function (key) { | ||
var path = key, | ||
file = pt.basename(path), | ||
counter = 1; | ||
//move files to base folder | ||
Object.keys(files).forEach(function(key) { | ||
var path = key, | ||
file = pt.basename(path), | ||
counter = 1; | ||
var stats = fs.statSync(key), | ||
subdir = stats.mtime.toISOString().substr(0,7), | ||
dir = pt.join(base, subdir), | ||
target = pt.join(dir, file), | ||
time = moment(stats.mtime); | ||
var stats = fs.statSync(key), | ||
subdir = stats.mtime.toISOString().substr(0, 7), | ||
dir = pt.join(base, subdir), | ||
target = pt.join(dir, file), | ||
time = moment(stats.mtime); | ||
var name = time.format('YYYY-MM-DD_HH-mm-ss') + '.' + extension(file); | ||
target = pt.join(dir, name); | ||
// add one second to filenames time part until no file will be overwritten | ||
while (fs.existsSync(target)) { | ||
time = time.add('s', 1); | ||
name = time.format('YYYY-MM-DD_HH-mm-ss') + '.' + extension(file); | ||
var name = time.format('YYYY-MM-DD_HH-mm-ss') + '.' + extension(file); | ||
target = pt.join(dir, name); | ||
} | ||
//create subdir | ||
if (!fs.existsSync(dir)) { | ||
fs.mkdirSync(dir); | ||
} | ||
// add one second to filenames time part until no file will be overwritten | ||
while (fs.existsSync(target)) { | ||
time = time.add('s', 1); | ||
name = time.format('YYYY-MM-DD_HH-mm-ss') + '.' + extension(file); | ||
target = pt.join(dir, name); | ||
} | ||
//rename/move | ||
if (!fs.existsSync(target)) { | ||
fs.renameSync(path, target); | ||
delete files[key]; | ||
} | ||
//create subdir | ||
if (!fs.existsSync(dir)) { | ||
fs.mkdirSync(dir); | ||
} | ||
//rename/move | ||
if (!fs.existsSync(target)) { | ||
fs.renameSync(path, target); | ||
delete files[key]; | ||
} | ||
}); | ||
}); | ||
}()); |
{ | ||
"name": "cluster-files", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "CLI that rename files of a directory based on the creation date and clusters them in monthly based subdirectories", | ||
@@ -10,2 +10,3 @@ "main": "lib/buckets", | ||
"dependencies": { | ||
"inquirer": "^0.9.0", | ||
"moment": "~2.10" | ||
@@ -36,2 +37,1 @@ }, | ||
} | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7744
162
2
+ Addedinquirer@^0.9.0
+ Addedansi-regex@1.1.12.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedcli-width@1.1.1(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedfigures@1.7.0(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedinquirer@0.9.0(transitive)
+ Addedlodash@3.10.1(transitive)
+ Addedmute-stream@0.0.4(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedreadline2@0.1.1(transitive)
+ Addedrun-async@0.1.0(transitive)
+ Addedrx-lite@2.5.2(transitive)
+ Addedstrip-ansi@2.0.13.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedthrough@2.3.8(transitive)
+ Addedwrappy@1.0.2(transitive)