Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cluster-files

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cluster-files - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

bin/cluster-cli.js

107

bin/cluster-core.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc