cluster-files
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -6,52 +6,79 @@ 'use strict'; | ||
moment = require('moment'), | ||
bfs = require('fs-bfs'); | ||
bfs = require('fs-bfs'), | ||
mod; | ||
function extension(file) { | ||
var list = file.split('.'); | ||
return list[list.length - 1]; | ||
} | ||
mod = { | ||
module.exports = { | ||
_getParts: function _getParts(path) { | ||
return pt.parse(path); | ||
}, | ||
process: function process(basepath) { | ||
_getPath: function _getPath(file) { | ||
return pt.format(file); | ||
}, | ||
var data = bfs(basepath), | ||
hash = {}; | ||
_getTime: function _getTime(sourcefile) { | ||
var stats = fs.statSync(mod._getPath(sourcefile)); | ||
return moment(stats.mtime); | ||
}, | ||
// move files to subfolders | ||
data.files.forEach(function (data) { | ||
var path = data.path, | ||
file = data.name, | ||
counter = 1; | ||
_exists: function _exists(file) { | ||
return fs.existsSync(mod._getPath(file)); | ||
}, | ||
var stats = fs.statSync(path), | ||
subdir = stats.mtime.toISOString().substr(0, 7), | ||
dir = pt.join(basepath, subdir), | ||
target = pt.join(dir, file), | ||
time = moment(stats.mtime); | ||
_setTimestampName: function _setTimestampName(file, sourcefile) { | ||
var time = mod._getTime(sourcefile); | ||
file.name = time.format('YYYY-MM-DD_HH-mm-ss'); | ||
file.base = file.name + file.ext; | ||
// fallback: add one second to timestamp | ||
while (mod._exists(file)) { | ||
time = time.add('s', 1); | ||
file.name = time.format('YYYY-MM-DD_HH-mm-ss'); | ||
file.base = file.name + file.ext; | ||
} | ||
}, | ||
var name = time.format('YYYY-MM-DD_HH-mm-ss') + '.' + extension(file); | ||
target = pt.join(dir, name); | ||
process: function process(options) { | ||
options = options || {}; | ||
if (!options.basepath) return; | ||
// 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); | ||
} | ||
var data = bfs(options.basepath); | ||
var hash = {}; | ||
// create subdir | ||
if (!fs.existsSync(dir)) { | ||
fs.mkdirSync(dir); | ||
} | ||
// move files to subfolders | ||
data.files.forEach(function (data) { | ||
var sourcepath = data.path; | ||
var sourcefile = mod._getParts(sourcepath); | ||
var file = mod._getParts(sourcepath); | ||
var subdir = mod._getTime(sourcefile).toISOString().substr(0, 7); | ||
var skipOnConflict = !options.rename && !options.overwrite; | ||
// rename/move | ||
if (!fs.existsSync(target)) { | ||
fs.renameSync(path, target); | ||
hash[path] = target; | ||
} | ||
}); | ||
// set target dir | ||
file.dir = pt.join(file.dir, subdir); | ||
return hash; | ||
} | ||
}; | ||
// use creation timestamp as name | ||
if (options.rename) mod._setTimestampName(file, sourcefile); | ||
// skip | ||
if (skipOnConflict && mod._exists(file)) return; | ||
// create subdir when not existing | ||
if (!fs.existsSync(file.dir)) fs.mkdirSync(file.dir); | ||
// rename/move | ||
if (!options.simulate) { | ||
fs.renameSync(sourcepath, mod._getPath(file)); | ||
} | ||
// add to hash | ||
var relsourc = sourcepath.replace(options.basepath, ''); | ||
var reltarget = mod._getPath(file).replace(options.basepath, ''); | ||
hash[relsourc] = reltarget; | ||
}); | ||
return hash; | ||
} | ||
}; | ||
// module expors | ||
module.exports = mod; |
@@ -1,24 +0,83 @@ | ||
//#!/usr/bin/env node | ||
'use strict'; | ||
(function () { | ||
var fs = require('fs'), | ||
pt = require('path'), | ||
moment = require('moment'), | ||
bfs = require('fs-bfs'), | ||
mod; | ||
var inquirer = require('inquirer'), | ||
core = require('./cluster-core'); | ||
mod = { | ||
// first cli parameter or current directory | ||
var base = process.argv[2] || process.cwd(); | ||
_getParts: function _getParts(path) { | ||
return pt.parse(path); | ||
}, | ||
function cont(answers) { | ||
if (answers.process !== 'yes') return; | ||
core.process(base); | ||
_getPath: function _getPath(file) { | ||
return pt.format(file); | ||
}, | ||
_getTime: function _getTime(sourcefile) { | ||
var stats = fs.statSync(mod._getPath(sourcefile)); | ||
return moment(stats.mtime); | ||
}, | ||
_exists: function _exists(file) { | ||
return fs.existsSync(mod._getPath(file)); | ||
}, | ||
_setTimestampName: function _setTimestampName(file, sourcefile) { | ||
var time = mod._getTime(sourcefile); | ||
file.name = time.format('YYYY-MM-DD_HH-mm-ss'); | ||
file.base = file.name + file.ext; | ||
// fallback: add one second to timestamp | ||
while (mod._exists(file)) { | ||
time = time.add('s', 1); | ||
file.name = time.format('YYYY-MM-DD_HH-mm-ss'); | ||
file.base = file.name + file.ext; | ||
} | ||
}, | ||
// ask the user to contiune | ||
inquirer.prompt([{ | ||
type: 'list', | ||
name: 'process', | ||
message: 'Process with folder ' + base + ' ?', | ||
choices: ['yes', 'no'] | ||
}], cont); | ||
})(); | ||
process: function process(options) { | ||
options = options || {}; | ||
if (!options.basepath) return; | ||
var data = bfs(options.basepath); | ||
var hash = {}; | ||
// move files to subfolders | ||
data.files.forEach(function (data) { | ||
var sourcepath = data.path; | ||
var sourcefile = mod._getParts(sourcepath); | ||
var file = mod._getParts(sourcepath); | ||
var subdir = mod._getTime(sourcefile).toISOString().substr(0, 7); | ||
var skipOnConflict = !options.rename && !options.overwrite; | ||
// set target dir | ||
file.dir = pt.join(file.dir, subdir); | ||
// use creation timestamp as name | ||
if (options.rename) mod._setTimestampName(file, sourcefile); | ||
// skip | ||
if (skipOnConflict && mod._exists(file)) return; | ||
// create subdir when not existing | ||
if (!fs.existsSync(file.dir)) fs.mkdirSync(file.dir); | ||
// rename/move | ||
if (!options.simulate) { | ||
fs.renameSync(sourcepath, mod._getPath(file)); | ||
} | ||
// add to hash | ||
var relsourc = sourcepath.replace(options.basepath, ''); | ||
var reltarget = mod._getPath(file).replace(options.basepath, ''); | ||
hash[relsourc] = reltarget; | ||
}); | ||
return hash; | ||
} | ||
}; | ||
// module expors | ||
module.exports = mod; |
{ | ||
"name": "cluster-files", | ||
"version": "0.2.0", | ||
"description": "CLI that rename files of a directory based on the creation date and clusters them in monthly based subdirectories", | ||
"main": "lib/buckets", | ||
"version": "0.3.0", | ||
"description": "CLI/module that clusters files in subdirectories based on month of creation", | ||
"main": "lib/cluster-core", | ||
"scripts": { | ||
"compile": "babel --presets es2015 -d bin/ src/", | ||
"watch": "babel --watch --presets es2015 -d bin/ src/", | ||
"lint": "eslint src", | ||
"eslint": "eslint src", | ||
"eslintfix": "eslint src --fix", | ||
"prepublish": "npm run compile", | ||
@@ -14,17 +15,18 @@ "test": "echo \"Error: no test specified\" && exit 1" | ||
"dependencies": { | ||
"inquirer": "^0.9.0", | ||
"minimist": "^1.1.3", | ||
"moment": "~2.10", | ||
"fs-bfs": "0.1.0" | ||
"chalk": "1.1.1", | ||
"commander": "2.9.0", | ||
"fs-bfs": "0.1.0", | ||
"inquirer": "0.11.0", | ||
"moment": "2.10" | ||
}, | ||
"engines": { | ||
"node": ">=0.10" | ||
"node": ">=0.12.7" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/solygen/node-cluster-files.git" | ||
"url": "git+https://github.com/solygen/node-cluster-files.git" | ||
}, | ||
"preferGlobal": true, | ||
"bin": { | ||
"cluster": "bin/cluster.js" | ||
"cluster": "bin/cluster-cli.js" | ||
}, | ||
@@ -43,3 +45,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-babel script.js --watch --out-file script-compiled.js-es2015": "6.1.18", | ||
"babel-preset-es2015": "6.1.18", | ||
"babel-cli": "6.1.18", | ||
@@ -46,0 +48,0 @@ "eslint": "1.9.0", |
@@ -9,3 +9,2 @@ node-cluster-files | ||
__cluster-core.js__ | ||
@@ -17,3 +16,17 @@ - core functionality | ||
``` | ||
Usage: cluster <path (default:current path)> [options] | ||
Clusters files in subdirectories based on month of creation | ||
Options: | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-r, --rename also rename files [YYYY-MM-DD_HH-mm-ss] | ||
-o, --overwrite overwrite if file already exists (when rename is disabeld) | ||
-y, --yes skip prompt | ||
``` | ||
__example__ | ||
@@ -34,1 +47,5 @@ ``` | ||
``` | ||
__todo__ | ||
- may use https://www.npmjs.com/package/fs-tools |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
10595
9
204
49
5
4
1
+ Addedchalk@1.1.1
+ Addedcommander@2.9.0
+ Addedansi-escapes@1.4.0(transitive)
+ Addedchalk@1.1.1(transitive)
+ Addedcli-cursor@1.0.2(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addedcommander@2.9.0(transitive)
+ Addedexit-hook@1.1.1(transitive)
+ Addedgraceful-readlink@1.0.1(transitive)
+ Addedinquirer@0.11.0(transitive)
+ Addedis-fullwidth-code-point@1.0.0(transitive)
+ Addedmute-stream@0.0.5(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedonetime@1.1.0(transitive)
+ Addedreadline2@1.0.1(transitive)
+ Addedrestore-cursor@1.0.1(transitive)
+ Addedrx-lite@3.1.2(transitive)
- Removedminimist@^1.1.3
- Removedansi-regex@1.1.1(transitive)
- Removedchalk@1.1.3(transitive)
- Removedinquirer@0.9.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmute-stream@0.0.4(transitive)
- Removedreadline2@0.1.1(transitive)
- Removedrx-lite@2.5.2(transitive)
- Removedstrip-ansi@2.0.1(transitive)
Updatedinquirer@0.11.0
Updatedmoment@2.10