caselyjs 0.0.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/LICENSE +21 -0
- package/README.md +61 -0
- package/index.js +1 -0
- package/package.json +28 -0
- package/rename.js +56 -0
- package/tools/case-identifier.js +10 -0
- package/tools/mutate-case.js +21 -0
- package/tools/resolver.js +36 -0
- package/tools/transforms/to-camel.js +58 -0
- package/tools/transforms/to-kebab.js +59 -0
- package/tools/transforms/to-pascal.js +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Vikash Kumar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# 🛠️ caselyjs
|
|
2
|
+
|
|
3
|
+
**renamejs** is a CLI and programmatic tool that helps you **rename files and folders** in bulk using a consistent naming convention like `kebab-case`, `camelCase`, or `PascalCase`.
|
|
4
|
+
|
|
5
|
+
> Say goodbye to manually renaming 100+ files in your project!
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 📦 Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install renamejs
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## 🚀 Usage
|
|
16
|
+
|
|
17
|
+
### Minimum setup you need to start with `renamejs`
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
// index.js
|
|
21
|
+
import { rename } from 'renamejs';
|
|
22
|
+
|
|
23
|
+
rename.config({
|
|
24
|
+
path: 'src'
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
rename.execute();
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## ⚙️ Config Options
|
|
31
|
+
|
|
32
|
+
### Default Options
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
rename.config({
|
|
36
|
+
path: 'src',
|
|
37
|
+
case: 'kebab',
|
|
38
|
+
file: ['js', 'jsx'],
|
|
39
|
+
operate: 'partial'
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
> [!NOTE]\
|
|
44
|
+
> `path` is a required property.\
|
|
45
|
+
> Don't need to pass any path as `'src/**/*'`, recursive is supported by default.
|
|
46
|
+
>
|
|
47
|
+
> currently, supporting three `case`s as `'kebab' | 'camel' | 'pascal'`\
|
|
48
|
+
> default file extensions `['js', 'jsx']`, can include more extenstion in the array.\
|
|
49
|
+
> eg.
|
|
50
|
+
> ```js
|
|
51
|
+
> rename.config({
|
|
52
|
+
> // ...previous
|
|
53
|
+
> file: ['js', 'jsx', 'ts', 'tsx']
|
|
54
|
+
> })
|
|
55
|
+
> ```
|
|
56
|
+
> default operate mode is `'partial'`, which modifies only files.\
|
|
57
|
+
> operate has two variants as `'partial' | 'full'`\
|
|
58
|
+
> full will allow you to rename folders too.
|
|
59
|
+
|
|
60
|
+
### Author
|
|
61
|
+
**Made with ❤️ by Ninja-Vikash**
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./rename.js"
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "caselyjs",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A tool for renaming files and folders in a efficient way",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"rename": "node index.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/Ninja-Vikash/renamejs.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"rename",
|
|
16
|
+
"case",
|
|
17
|
+
"utility",
|
|
18
|
+
"file-renamer",
|
|
19
|
+
"casely",
|
|
20
|
+
"case-converter"
|
|
21
|
+
],
|
|
22
|
+
"author": "ninja-vikash",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/Ninja-Vikash/renamejs/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/Ninja-Vikash/renamejs#readme"
|
|
28
|
+
}
|
package/rename.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
|
|
3
|
+
import { pathResolver } from "./tools/resolver.js";
|
|
4
|
+
import { caseIdentifier } from "./tools/case-identifier.js";
|
|
5
|
+
import { mutateCase } from "./tools/mutate-case.js";
|
|
6
|
+
|
|
7
|
+
// ------------------------------------------------
|
|
8
|
+
|
|
9
|
+
let defaultConfig = {
|
|
10
|
+
path: "",
|
|
11
|
+
file: ["js", "jsx"],
|
|
12
|
+
case: "kebab",
|
|
13
|
+
operate: "partial",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// -------------------------------------------------
|
|
17
|
+
|
|
18
|
+
export const rename = {
|
|
19
|
+
config(options = {}) {
|
|
20
|
+
defaultConfig = {
|
|
21
|
+
...defaultConfig,
|
|
22
|
+
...options,
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
async execute() {
|
|
27
|
+
const { files, folders } = pathResolver(defaultConfig.path, {
|
|
28
|
+
recursive: true,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
for (const file of files) {
|
|
32
|
+
const value = file.split("/").pop();
|
|
33
|
+
const fullPath = path.resolve(file);
|
|
34
|
+
const [fileName, fileExt] = value.split(".");
|
|
35
|
+
const currentCase = caseIdentifier(fileName);
|
|
36
|
+
|
|
37
|
+
if (defaultConfig.file.includes(fileExt)) {
|
|
38
|
+
if (currentCase !== defaultConfig.case) {
|
|
39
|
+
await mutateCase(fullPath, currentCase, defaultConfig.case);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (defaultConfig.operate === "full") {
|
|
45
|
+
for (const folder of folders) {
|
|
46
|
+
const value = folder.split("/").pop();
|
|
47
|
+
const fullPath = path.resolve(folder);
|
|
48
|
+
const currentCase = caseIdentifier(value);
|
|
49
|
+
|
|
50
|
+
if (currentCase !== defaultConfig.case) {
|
|
51
|
+
await mutateCase(fullPath, currentCase, defaultConfig.case);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function caseIdentifier(val) {
|
|
2
|
+
const str = val.split(".")[0]
|
|
3
|
+
|
|
4
|
+
if (/^[a-z]+$/.test(str)) return 'kebab';
|
|
5
|
+
if (/^[a-z]+(-[a-z]+)+$/.test(str)) return 'kebab';
|
|
6
|
+
if (/^[a-z]+(?:[A-Z][a-z]*)*$/.test(str)) return 'camel';
|
|
7
|
+
if (/^[A-Z][a-z]+(?:[A-Z][a-z]*)*$/.test(str)) return 'pascal';
|
|
8
|
+
|
|
9
|
+
return 'unknown';
|
|
10
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { toCamel } from "./transforms/to-camel.js";
|
|
2
|
+
import { toKebab } from "./transforms/to-kebab.js";
|
|
3
|
+
import { toPascal } from "./transforms/to-pascal.js";
|
|
4
|
+
|
|
5
|
+
// --------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
export async function mutateCase(path, currentType, expectedType) {
|
|
8
|
+
|
|
9
|
+
if (expectedType === "camel") {
|
|
10
|
+
toCamel(path, currentType);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (expectedType === "kebab") {
|
|
14
|
+
toKebab(path, currentType);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (expectedType === "pascal") {
|
|
18
|
+
toPascal(path, currentType);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* List all files and folders in a given path
|
|
6
|
+
* @param {string} targetPath - The directory to scan
|
|
7
|
+
* @param {Object} options
|
|
8
|
+
* @param {boolean} [options.recursive=false] - If true, lists recursively
|
|
9
|
+
* @returns {{ files: string[], folders: string[] }}
|
|
10
|
+
*/
|
|
11
|
+
export function pathResolver(targetPath, options = { recursive: false }) {
|
|
12
|
+
const result = {
|
|
13
|
+
files: [],
|
|
14
|
+
folders: [],
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function scan(dir) {
|
|
18
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
19
|
+
|
|
20
|
+
for (const entry of entries) {
|
|
21
|
+
const fullPath = path.join(dir, entry.name);
|
|
22
|
+
|
|
23
|
+
if (entry.isDirectory()) {
|
|
24
|
+
result.folders.push(fullPath.replace(/\\/g, '/'));
|
|
25
|
+
if (options.recursive) {
|
|
26
|
+
scan(fullPath);
|
|
27
|
+
}
|
|
28
|
+
} else if (entry.isFile()) {
|
|
29
|
+
result.files.push(fullPath.replace(/\\/g, '/'));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
scan(targetPath);
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
function toCamelCase(str) {
|
|
5
|
+
return str
|
|
6
|
+
.replace(/[-_](.)/g, (_, group1) => group1.toUpperCase())
|
|
7
|
+
.replace(/^(.)/, (_, group1) => group1.toLowerCase());
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function kebabToCamel(targetPath) {
|
|
11
|
+
const parent = path.dirname(targetPath);
|
|
12
|
+
const currentName = path.basename(targetPath);
|
|
13
|
+
const newName = toCamelCase(currentName);
|
|
14
|
+
const newPath = path.join(parent, newName);
|
|
15
|
+
|
|
16
|
+
if (currentName === newName) {
|
|
17
|
+
console.log(`✅ Already in camelCase: ${currentName}`);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
fs.renameSync(targetPath, newPath);
|
|
23
|
+
console.log(`✨ Renamed Kebab → Camel: ${currentName} ➝ ${newName}`);
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.error(`❌ Rename failed: ${err.message}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function pascalToCamel(targetPath) {
|
|
30
|
+
const parent = path.dirname(targetPath);
|
|
31
|
+
const currentName = path.basename(targetPath);
|
|
32
|
+
const newName = currentName.charAt(0).toLowerCase() + currentName.slice(1);
|
|
33
|
+
const newPath = path.join(parent, newName);
|
|
34
|
+
|
|
35
|
+
if (currentName === newName) {
|
|
36
|
+
console.log(`✅ Already in camelCase: ${currentName}`);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
fs.renameSync(targetPath, newPath);
|
|
42
|
+
console.log(`✨ Renamed Pascal → Camel: ${currentName} ➝ ${newName}`);
|
|
43
|
+
} catch (err) {
|
|
44
|
+
console.error(`❌ Rename failed: ${err.message}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ----------------------------------------------------
|
|
49
|
+
|
|
50
|
+
export function toCamel(targetPath, currentType) {
|
|
51
|
+
if (currentType === 'kebab') {
|
|
52
|
+
kebabToCamel(targetPath);
|
|
53
|
+
} else if (currentType === 'pascal') {
|
|
54
|
+
pascalToCamel(targetPath);
|
|
55
|
+
} else {
|
|
56
|
+
console.log('⚠️ Unsupported case type passed.');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
function toKebabCase(str) {
|
|
5
|
+
return str
|
|
6
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
7
|
+
.replace(/([A-Z])([A-Z][a-z])/g, '$1-$2')
|
|
8
|
+
.toLowerCase();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function pascalToKebab(targetPath) {
|
|
12
|
+
const parent = path.dirname(targetPath);
|
|
13
|
+
const currentName = path.basename(targetPath);
|
|
14
|
+
const newName = toKebabCase(currentName);
|
|
15
|
+
const newPath = path.join(parent, newName);
|
|
16
|
+
|
|
17
|
+
if (currentName === newName) {
|
|
18
|
+
console.log(`✅ Already in kebab-case: ${currentName}`);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
fs.renameSync(targetPath, newPath);
|
|
24
|
+
console.log(`✨ Renamed Pascal → Kebab: ${currentName} ➝ ${newName}`);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
console.error(`❌ Rename failed: ${err.message}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function camelToKebab(targetPath) {
|
|
31
|
+
const parent = path.dirname(targetPath);
|
|
32
|
+
const currentName = path.basename(targetPath);
|
|
33
|
+
const newName = toKebabCase(currentName);
|
|
34
|
+
const newPath = path.join(parent, newName);
|
|
35
|
+
|
|
36
|
+
if (currentName === newName) {
|
|
37
|
+
console.log(`✅ Already in kebab-case: ${currentName}`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
fs.renameSync(targetPath, newPath);
|
|
43
|
+
console.log(`✨ Renamed Camel → Kebab: ${currentName} ➝ ${newName}`);
|
|
44
|
+
} catch (err) {
|
|
45
|
+
console.error(`❌ Rename failed: ${err.message}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ----------------------------------------------------
|
|
50
|
+
|
|
51
|
+
export function toKebab(targetPath, currentType) {
|
|
52
|
+
if (currentType === 'pascal') {
|
|
53
|
+
pascalToKebab(targetPath);
|
|
54
|
+
} else if (currentType === 'camel') {
|
|
55
|
+
camelToKebab(targetPath);
|
|
56
|
+
} else {
|
|
57
|
+
console.log('⚠️ Unsupported case type passed.');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
function toPascalCase(str) {
|
|
5
|
+
return str
|
|
6
|
+
.replace(/[-_](.)/g, (_, group1) => group1.toUpperCase())
|
|
7
|
+
.replace(/^(.)/, (_, group1) => group1.toUpperCase());
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function kebabToPascal(targetPath) {
|
|
11
|
+
const parent = path.dirname(targetPath);
|
|
12
|
+
const currentName = path.basename(targetPath);
|
|
13
|
+
const newName = toPascalCase(currentName);
|
|
14
|
+
const newPath = path.join(parent, newName);
|
|
15
|
+
|
|
16
|
+
if (currentName === newName) {
|
|
17
|
+
console.log(`✅ Already in PascalCase: ${currentName}`);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
fs.renameSync(targetPath, newPath);
|
|
23
|
+
console.log(`✨ Renamed Kebab → Pascal: ${currentName} ➝ ${newName}`);
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.error(`❌ Rename failed: ${err.message}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function camelToPascal(targetPath) {
|
|
30
|
+
const parent = path.dirname(targetPath);
|
|
31
|
+
const currentName = path.basename(targetPath);
|
|
32
|
+
const newName = currentName.charAt(0).toUpperCase() + currentName.slice(1);
|
|
33
|
+
const newPath = path.join(parent, newName);
|
|
34
|
+
|
|
35
|
+
if (currentName === newName) {
|
|
36
|
+
console.log(`✅ Already in PascalCase: ${currentName}`);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
fs.renameSync(targetPath, newPath);
|
|
42
|
+
console.log(`✨ Renamed Camel → Pascal: ${currentName} ➝ ${newName}`);
|
|
43
|
+
} catch (err) {
|
|
44
|
+
console.error(`❌ Rename failed: ${err.message}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ----------------------------------------------------
|
|
49
|
+
|
|
50
|
+
export function toPascal(targetPath, currentType) {
|
|
51
|
+
if (currentType === 'kebab') {
|
|
52
|
+
kebabToPascal(targetPath);
|
|
53
|
+
} else if (currentType === 'camel') {
|
|
54
|
+
camelToPascal(targetPath);
|
|
55
|
+
} else {
|
|
56
|
+
console.log('⚠️ Unsupported case type passed.');
|
|
57
|
+
}
|
|
58
|
+
}
|