redate-cli 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "redate-cli",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "A CLI tool for renaming images based on EXIF data.",
5
5
  "type": "module",
6
6
  "main": "src/redate.js",
7
7
  "bin": {
8
8
  "redate": "./src/redate.js"
9
9
  },
10
+ "scripts": {
11
+ "redate": "node ./src/redate.js"
12
+ },
10
13
  "repository": {
11
14
  "type": "git",
12
15
  "url": "git+https://github.com/niilopoutanen/redate.git"
package/src/config.js CHANGED
@@ -5,60 +5,92 @@ import fs from "fs";
5
5
  const CONFIG_DIR = path.join(os.homedir(), ".redate");
6
6
  const CONFIG_FILE = path.join(CONFIG_DIR, "config.json");
7
7
 
8
- const DEFAULT_FORMAT = "yyyy-mm-dd hh-min-ss";
8
+ export const DEFAULT_CONFIG = {
9
+ format: "yyyy-mm-dd hh-min-ss",
10
+
11
+ fileHandling: "rename" // rename, copy, copy_in_folder
12
+ };
9
13
 
10
14
  export const TOKENS = {
11
15
  yyyy: { desc: "4-digit year", value: (date) => date.getFullYear() },
12
- yy: { desc: "2-digit year", value: (date) => String(date.getFullYear()).slice(-2) },
13
- mm: { desc: "Month with leading zero", value: (date) => String(date.getMonth() + 1).padStart(2, "0") },
14
- m: { desc: "Month without leading zero", value: (date) => date.getMonth() + 1 },
15
- mmm: { desc: "Short month name", value: (date) => date.toLocaleString("en-US", { month: "short" }) },
16
+ yy: { desc: "2-digit year", value: (date) => String(date.getFullYear()).slice(-2) },
17
+ mm: { desc: "Month with leading zero", value: (date) => String(date.getMonth() + 1).padStart(2, "0") },
18
+ m: { desc: "Month without leading zero", value: (date) => date.getMonth() + 1 },
19
+ mmm: { desc: "Short month name", value: (date) => date.toLocaleString("en-US", { month: "short" }) },
16
20
  mmmm: { desc: "Full month name", value: (date) => date.toLocaleString("en-US", { month: "long" }) },
17
- dd: { desc: "Day with leading zero", value: (date) => String(date.getDate()).padStart(2, "0") },
18
- d: { desc: "Day without leading zero", value: (date) => date.getDate() },
19
- ddd: { desc: "Day short name", value: (date) => date.toLocaleString("en-US", { weekday: "short" }) },
21
+ dd: { desc: "Day with leading zero", value: (date) => String(date.getDate()).padStart(2, "0") },
22
+ d: { desc: "Day without leading zero", value: (date) => date.getDate() },
23
+ ddd: { desc: "Day short name", value: (date) => date.toLocaleString("en-US", { weekday: "short" }) },
20
24
  dddd: { desc: "Day full name", value: (date) => date.toLocaleString("en-US", { weekday: "long" }) },
21
- hh: { desc: "Hour 0023", value: (date) => String(date.getHours()).padStart(2, "0") },
22
- h: { desc: "Hour 023", value: (date) => date.getHours() },
23
- H: { desc: "Hour 112", value: (date) => ((date.getHours() + 11) % 12 + 1) },
24
- HH: { desc: "Hour 0112", value: (date) => String((date.getHours() + 11) % 12 + 1).padStart(2,"0") },
25
- a: { desc: "AM/PM", value: (date) => date.getHours() < 12 ? "AM" : "PM" },
26
- A: { desc: "am/pm", value: (date) => date.getHours() < 12 ? "am" : "pm" },
27
- min: { desc: "Minutes with leading zero", value: (date) => String(date.getMinutes()).padStart(2, "0") },
28
- m_: { desc: "Minutes without leading zero", value: (date) => date.getMinutes() },
29
- ss: { desc: "Seconds with leading zero", value: (date) => String(date.getSeconds()).padStart(2, "0") },
30
- s: { desc: "Seconds without leading zero", value: (date) => date.getSeconds() },
31
- ms: { desc: "Milliseconds", value: (date) => date.getMilliseconds() },
32
- w: { desc: "Week of year", value: (date) => {
33
- const start = new Date(date.getFullYear(), 0, 1);
34
- const diff = (date - start) + ((start.getDay() + 6) % 7) * 86400000;
35
- return Math.floor(diff / (7 * 86400000)) + 1;
36
- }},
37
- D: { desc: "Day of year", value: (date) => {
38
- const start = new Date(date.getFullYear(), 0, 0);
39
- const diff = date - start;
40
- return Math.floor(diff / 86400000);
41
- }},
25
+ hh: { desc: "Hour 00-23", value: (date) => String(date.getHours()).padStart(2, "0") },
26
+ h: { desc: "Hour 0-23", value: (date) => date.getHours() },
27
+ H: { desc: "Hour 1-12", value: (date) => ((date.getHours() + 11) % 12 + 1) },
28
+ HH: { desc: "Hour 01-12", value: (date) => String((date.getHours() + 11) % 12 + 1).padStart(2, "0") },
29
+ a: { desc: "AM/PM", value: (date) => date.getHours() < 12 ? "AM" : "PM" },
30
+ A: { desc: "am/pm", value: (date) => date.getHours() < 12 ? "am" : "pm" },
31
+ min: { desc: "Minutes with leading zero", value: (date) => String(date.getMinutes()).padStart(2, "0") },
32
+ m_: { desc: "Minutes without leading zero", value: (date) => date.getMinutes() },
33
+ ss: { desc: "Seconds with leading zero", value: (date) => String(date.getSeconds()).padStart(2, "0") },
34
+ s: { desc: "Seconds without leading zero", value: (date) => date.getSeconds() },
35
+ ms: { desc: "Milliseconds", value: (date) => date.getMilliseconds() },
36
+ w: {
37
+ desc: "Week of year", value: (date) => {
38
+ const start = new Date(date.getFullYear(), 0, 1);
39
+ const diff = (date - start) + ((start.getDay() + 6) % 7) * 86400000;
40
+ return Math.floor(diff / (7 * 86400000)) + 1;
41
+ }
42
+ },
43
+ D: {
44
+ desc: "Day of year", value: (date) => {
45
+ const start = new Date(date.getFullYear(), 0, 0);
46
+ const diff = date - start;
47
+ return Math.floor(diff / 86400000);
48
+ }
49
+ },
42
50
  };
43
51
 
44
52
  function ensureConfig() {
45
- if (!fs.existsSync(CONFIG_DIR)) fs.mkdirSync(CONFIG_DIR, { recursive: true });
53
+ if (!fs.existsSync(CONFIG_DIR)) {
54
+ fs.mkdirSync(CONFIG_DIR, { recursive: true });
55
+ }
56
+
46
57
  if (!fs.existsSync(CONFIG_FILE)) {
47
58
  fs.writeFileSync(
48
59
  CONFIG_FILE,
49
- JSON.stringify({ format: DEFAULT_FORMAT }, null, 2)
60
+ JSON.stringify(DEFAULT_CONFIG, null, 2)
50
61
  );
51
62
  }
52
63
  }
53
64
 
54
65
  export function getConfig() {
55
66
  ensureConfig();
56
- return JSON.parse(fs.readFileSync(CONFIG_FILE, "utf-8"));
67
+ const stored = JSON.parse(fs.readFileSync(CONFIG_FILE, "utf-8"));
68
+
69
+ return deepMerge(DEFAULT_CONFIG, stored);
57
70
  }
58
71
 
59
- export function setConfig(newConfig) {
60
- ensureConfig();
61
- fs.writeFileSync(CONFIG_FILE, JSON.stringify(newConfig, null, 2));
72
+ export function setConfig(partialConfig) {
73
+ const current = getConfig();
74
+ const updated = deepMerge(current, partialConfig);
75
+
76
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(updated, null, 2));
77
+ return updated;
62
78
  }
63
79
 
64
- export const DEFAULT_FORMAT_VALUE = DEFAULT_FORMAT;
80
+ function deepMerge(target, source) {
81
+ const output = { ...target };
82
+
83
+ for (const key of Object.keys(source)) {
84
+ if (
85
+ typeof source[key] === "object" &&
86
+ source[key] !== null &&
87
+ !Array.isArray(source[key])
88
+ ) {
89
+ output[key] = deepMerge(target[key] || {}, source[key]);
90
+ } else {
91
+ output[key] = source[key];
92
+ }
93
+ }
94
+
95
+ return output;
96
+ }
package/src/redate.js CHANGED
@@ -3,7 +3,7 @@ import { Command } from "commander";
3
3
  import fs from "fs";
4
4
  import path from "path";
5
5
  import exifr from 'exifr';
6
- import { getConfig, setConfig, TOKENS } from "./config.js";
6
+ import { getConfig, setConfig, TOKENS, DEFAULT_CONFIG } from "./config.js";
7
7
 
8
8
 
9
9
  const program = new Command();
@@ -11,7 +11,7 @@ const program = new Command();
11
11
  program
12
12
  .name("redate")
13
13
  .description("Rename images based on EXIF dates")
14
- .version("0.1.1");
14
+ .version("0.2.1");
15
15
 
16
16
  program
17
17
  .command("process <paths...>")
@@ -34,106 +34,109 @@ program
34
34
  }
35
35
  }
36
36
  });
37
- const DEFAULT_FORMAT = "yyyy-mm-dd hh-min-ss";
38
-
39
- const formatCommand = program
40
- .command("format")
41
- .description("Manage date format");
42
-
43
- formatCommand
44
- .command("set <format>")
45
- .description("Set global date format")
46
- .action((format) => {
47
- setConfig({ format });
48
- console.log(`Format set to: ${format}`);
49
- });
50
37
 
51
- formatCommand
52
- .command("get")
53
- .description("Show current global date format")
54
- .action(() => {
38
+ const configCommand = program
39
+ .command("config")
40
+ .description("Manage configuration");
41
+
42
+ configCommand
43
+ .command("get [key]")
44
+ .action((key) => {
55
45
  const config = getConfig();
56
- console.log(`Current format: ${config.format}`);
46
+ if (!key) {
47
+ console.log(config);
48
+ return;
49
+ }
50
+
51
+ console.log(config[key]);
52
+ });
53
+
54
+ configCommand
55
+ .command("set <key> <value>")
56
+ .action((key, value) => {
57
+ setConfig({ [key]: value });
58
+ console.log(`${key} updated to ${value}`);
57
59
  });
58
60
 
59
- formatCommand
61
+ configCommand
60
62
  .command("reset")
61
- .description("Reset format to default")
62
63
  .action(() => {
63
- setConfig({ format: DEFAULT_FORMAT });
64
- console.log(`Format reset to default: ${DEFAULT_FORMAT}`);
64
+ setConfig(DEFAULT_CONFIG);
65
+ console.log("Config reset to defaults");
65
66
  });
66
-
67
67
  program.parse(process.argv);
68
68
 
69
69
 
70
+ const fileHandlers = {
71
+ rename: (src, dest) => {
72
+ fs.renameSync(src, dest);
73
+ },
74
+
75
+ copy: (src, dest) => {
76
+ fs.copyFileSync(src, dest);
77
+ },
78
+
79
+ copy_in_folder: (src, dest) => {
80
+ const dir = path.dirname(src);
81
+ const targetDir = path.join(dir, "redate");
82
+ if (!fs.existsSync(targetDir)) {
83
+ fs.mkdirSync(targetDir);
84
+ }
85
+ fs.copyFileSync(src, path.join(targetDir, path.basename(dest)));
86
+ }
87
+ };
70
88
 
71
89
 
72
90
  async function processFiles(folderPath) {
73
91
  const files = fs.readdirSync(folderPath);
74
-
92
+ const config = getConfig();
75
93
  for (const file of files) {
76
94
  const filePath = path.join(folderPath, file);
77
95
 
78
96
  if (!fs.statSync(filePath).isFile()) continue;
79
97
 
80
- try {
81
- const date = await getDateFromFile(filePath);
82
-
83
- if (!date) {
84
- console.log(`No EXIF date found for file: ${filePath}`);
85
- continue;
86
- }
87
-
88
- const newFileName = formatFileName(date, file);
89
- fs.renameSync(filePath, path.join(folderPath, newFileName));
90
- console.log(`Renamed to: ${newFileName}`);
91
-
92
- } catch (err) {
93
- console.log(`Skipped (unsupported or invalid): ${filePath}`);
94
- }
98
+ processFile(filePath, config);
95
99
  }
96
100
  }
97
101
 
98
102
 
99
103
 
100
- async function processFile(filePath) {
101
- if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) {
102
- console.error(`File does not exist: ${filePath}`);
103
- return;
104
+ async function processFile(filePath, config) {
105
+ if (!config || config == null) {
106
+ config = getConfig();
104
107
  }
105
108
 
106
- console.log(`Processing file: ${filePath}`);
109
+ const date = await getDateFromFile(filePath);
110
+ if (!date) return;
107
111
 
108
- try {
109
- const date = await getDateFromFile(filePath);
112
+ const originalName = path.basename(filePath);
113
+ const newFileName = formatFileName(date, originalName, config);
110
114
 
111
- if (!date) {
112
- console.log(`No EXIF date found for file: ${filePath}`);
113
- return;
114
- }
115
+ applyFileHandling(filePath, newFileName, config);
115
116
 
116
- const dir = path.dirname(filePath);
117
- const originalName = path.basename(filePath);
118
- const newFileName = formatFileName(date, originalName);
117
+ console.log(`Processed: ${newFileName}`);
118
+ }
119
+ function applyFileHandling(srcPath, newFileName, config) {
120
+ const dir = path.dirname(srcPath);
121
+ const dest = path.join(dir, newFileName);
122
+
123
+ const handler = fileHandlers[config.fileHandling];
119
124
 
120
- fs.renameSync(filePath, path.join(dir, newFileName));
121
- console.log(`Renamed to: ${newFileName}`);
122
- } catch (err) {
123
- console.error(`Error reading EXIF data from ${filePath}: ${err}`);
125
+ if (!handler) {
126
+ throw new Error(`Unknown fileHandling: ${config.fileHandling}`);
124
127
  }
125
- }
126
128
 
129
+ handler(srcPath, dest);
130
+ }
127
131
 
128
132
 
129
- export function formatFileName(date, originalName) {
130
- const config = getConfig();
133
+ export function formatFileName(date, originalName, config) {
131
134
  let formatted = config.format;
132
135
 
133
- for (const key in TOKENS) {
134
- if (formatted.includes(key)) {
135
- formatted = formatted.replaceAll(key, TOKENS[key].value(date));
136
- }
136
+ const sortedTokens = Object.keys(TOKENS).sort((a, b) => b.length - a.length);
137
+
138
+ for (const key of sortedTokens) {
139
+ formatted = formatted.replaceAll(key, TOKENS[key].value(date));
137
140
  }
138
141
 
139
142
  const ext = path.extname(originalName);