New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

node-rename

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-rename - npm Package Compare versions

Comparing version 1.0.7 to 1.1.0

2

dist/cli.d.ts
#!/usr/bin/env node
export declare const runCli: () => void;
export declare const runCli: () => Promise<void>;
#!/usr/bin/env node
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -8,2 +67,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

exports.runCli = void 0;
var path = __importStar(require("path"));
var yargs_1 = __importDefault(require("yargs"));

@@ -14,32 +74,52 @@ var helpers_1 = require("yargs/helpers");

var utils_1 = require("./utils");
var runCli = function () {
var argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
.usage('Usage: node-rename --case [camel|upper|...] --pattern "./src/**/*.ts"')
.option('case', {
alias: 'c',
type: 'string',
description: "Rename case type. Available options: ".concat(rename_1.CASE_TYPES.join('|')),
demandOption: true,
})
.option('pattern', {
alias: 'p',
type: 'string',
description: "Pattern to find folders/files",
demandOption: true,
})
.option('idle', {
alias: 'i',
type: 'boolean',
description: 'Log all items without renaming',
})
.option('ignore', {
type: 'string',
description: "Ignore files/folders",
default: utils_1.DEFAULT_GLOB.ignore,
})
.alias('h', 'help')
.alias('v', 'version').argv;
(0, node_rename_1.nodeRename)({ pattern: argv.pattern, caseType: argv.case, ignore: argv.ignore, idle: argv.idle });
};
var runCli = function () { return __awaiter(void 0, void 0, void 0, function () {
var argv, pattern, type, ignore, log, handler, handlerPath, handlerFn;
return __generator(this, function (_a) {
argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
.usage('Usage: node-rename --pattern "./src/**/*.ts" --type [camel|upper|...]')
.option('pattern', {
alias: 'p',
type: 'string',
description: "Pattern to find folders/files",
demandOption: true,
})
.option('type', {
alias: 'c',
type: 'string',
description: "Rename case type. Available options: ".concat(rename_1.CASE_TYPES.join('|')),
})
.option('ignore', {
type: 'string',
description: "Ignore files/folders",
default: utils_1.DEFAULT_GLOB.ignore,
})
.option('log', {
type: 'boolean',
description: 'Log all items without renaming',
})
.option('handler', {
type: 'string',
description: 'Custom renaming handler path',
})
.alias('h', 'help')
.alias('v', 'version').argv;
pattern = argv.pattern, type = argv.type, ignore = argv.ignore, log = argv.log, handler = argv.handler;
if (handler) {
handlerPath = path.resolve(handler);
try {
handlerFn = require(handlerPath);
console.log('handler', handlerFn(''));
(0, node_rename_1.nodeRename)({ pattern: pattern, ignore: ignore, log: log, handler: handlerFn });
}
catch (e) {
console.log("Handler error: ".concat(handlerPath));
}
}
else {
(0, node_rename_1.nodeRename)({ pattern: pattern, type: type, ignore: ignore, log: log });
}
return [2 /*return*/];
});
}); };
exports.runCli = runCli;
(0, exports.runCli)();
import { CaseType } from './rename';
interface Options {
pattern: string;
caseType: CaseType;
type?: CaseType;
handler?: (text: string) => string;
ignore?: string;
idle?: boolean;
log?: boolean;
}

@@ -8,0 +9,0 @@ /**

@@ -35,6 +35,6 @@ "use strict";

var nodeRename = function (options) {
var pattern = options.pattern, caseType = options.caseType, ignore = options.ignore, idle = options.idle;
var pattern = options.pattern, type = options.type, ignore = options.ignore, log = options.log, handler = options.handler;
var renamedItems = new Map();
if (!rename_1.CASE_TYPES.includes(caseType)) {
console.log("Unknown case type ".concat(caseType, ". Select one of ").concat(rename_1.CASE_TYPES.join('|')));
if (type && !rename_1.CASE_TYPES.includes(type)) {
console.log("Unknown case type ".concat(type, ". Select one of ").concat(rename_1.CASE_TYPES.join('|')));
return renamedItems;

@@ -49,6 +49,11 @@ }

foundItems.sort(function (aPath, bPath) { return Number((0, utils_1.isDir)(bPath)) - Number((0, utils_1.isDir)(aPath)); });
if (!type && !handler) {
console.log(foundItems.join('\n'));
console.log("Found by pattern: ".concat(foundItems.length, " items."));
return new Map(foundItems.map(function (item) { return [item, item]; }));
}
/**
* Renaming logic...
*/
var renameFn = (0, rename_1.rename)(caseType);
var renameFn = handler !== null && handler !== void 0 ? handler : (0, rename_1.rename)(type !== null && type !== void 0 ? type : 'lower');
var renamedNames = new Map();

@@ -73,4 +78,6 @@ foundItems.forEach(function (item) {

renamedItems.set(oldPath, newPath);
var withRename = !idle;
if (withRename) {
if (log) {
console.log("".concat(oldPath, " => ").concat(newPath));
}
else {
try {

@@ -83,9 +90,12 @@ fs.renameSync(oldPath, newPath);

}
else {
console.log("".concat(oldPath, " => ").concat(newPath));
}
});
console.log("".concat(idle ? 'Will be renamed' : 'Renamed', " (").concat(renamedItems.size, ") to ").concat(caseType, "."));
var finishWords = function (start) { return "".concat(start, " ").concat(renamedItems.size, " items to ").concat(type !== null && type !== void 0 ? type : 'custom renamer', "."); };
if (log) {
console.log(finishWords("Will be renamed:"));
}
else {
console.log(finishWords("Renamed:"));
}
return renamedItems;
};
exports.nodeRename = nodeRename;

@@ -13,2 +13,2 @@ export declare const CASE_TYPES: readonly ["lower", "upper", "camel", "pascal", "snake", "kebab", "train", "random"];

export declare const randomCase: () => string;
export declare const rename: (caseType: CaseType) => (str: string) => string;
export declare const rename: (type: CaseType) => (str: string) => string;

@@ -32,4 +32,4 @@ "use strict";

exports.randomCase = randomCase;
var rename = function (caseType) {
switch (caseType) {
var rename = function (type) {
switch (type) {
case 'lower': {

@@ -60,5 +60,5 @@ return exports.lowerCase;

default:
throw new Error("Unknown type ".concat(caseType));
throw new Error("Unknown type ".concat(type));
}
};
exports.rename = rename;

@@ -24,3 +24,3 @@ {

"license": "MIT",
"version": "1.0.7",
"version": "1.1.0",
"scripts": {

@@ -27,0 +27,0 @@ "build": "tsc",

@@ -10,3 +10,3 @@ # node-rename &middot; [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/facebook/react/blob/main/LICENSE) ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/izemil/node-rename)

## Examples
## CLI Examples

@@ -19,36 +19,64 @@ Cli help to get full documentation:

Simple cli example - rename all files/folders within src directory to kebab case:
Show files/folders by pattern without renaming them:
```bash
node-rename --pattern "./src/**" --case kebab
node-rename --pattern "./src/**"
```
Show files/folders without renaming them, add `--idle` flag:
Then if you want to rename all files/folders within src directory to kebab case add case option:
```bash
node-rename --pattern "./src/**" --case upper --idle
node-rename --pattern "./src/**" --type kebab
```
Node package functions:
Add "--log" option to see renaming result without renaming
```bash
node-rename --pattern "./src/**" --type kebab --log
```
### Custom renaming handler
Create javascript file:
```javascript
// handler.js
module.exports = function (text) {
// any logic with renaming
return text.replace('-', '');
};
```
Then run it:
```bash
node-rename --pattern "./src/**" --handler "./handler.js" --log
```
## Node package usage
Case type functions:
```typescript
import {
camelCase,
kebabCase,
lowerCase,
nodeRename,
pascalCase,
randomCase,
snakeCase,
trainCase,
upperCase,
} from 'node-rename';
import { camelCase, kebabCase, lowerCase, pascalCase, randomCase, snakeCase, trainCase, upperCase } from 'node-rename';
upperCase('some sentence!'); // "SOME SENTENCE!"
// ...other cases...
```
Rename files and folders:
```typescript
import { nodeRename } from 'node-rename';
nodeRename({
pattern: 'src/**/*.ts',
caseType: 'pascal',
idle: true,
}); // Map with old -> new namings
type: 'pascal', // type is optional, type: undefined, only returns files to rename
});
// Or cusom rename handler
nodeRename({
pattern: 'src/**/*.ts',
// your renamer function
handler: (name: string) => name.replace('-', ''),
});
```
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