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

fixids

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fixids - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

128

bin/index.js

@@ -13,3 +13,4 @@ #!/usr/bin/env node

import path from 'path';
import cheerio from 'cheerio';
import readdirp from 'readdirp';
import { JSDOM } from 'jsdom';
import randomstring from 'randomstring';

@@ -19,2 +20,3 @@ import promptSync from 'prompt-sync';

import chalk from "chalk";
import { config } from 'dotenv';
const generateRandomId = (largeId) => {

@@ -32,32 +34,46 @@ return randomstring.generate({ length: largeId, charset: 'alphanumeric' });

};
const addIdsToHtmlFiles = (directory, largeId) => __awaiter(void 0, void 0, void 0, function* () {
const addIdsToHtmlFiles = (directory, largeId, tagsToModify, excludeTags) => __awaiter(void 0, void 0, void 0, function* () {
let reviewSpinner;
try {
console.log(chalk.green.bold(`๐Ÿค–: Browsing Directory "${directory}"`));
let totalFixed = 0;
const files = fs.readdirSync(directory);
const files = yield readdirp.promise(path.join(process.cwd(), directory), {
fileFilter: '*.html',
directoryFilter: (di) => !di.basename.startsWith('.'),
});
const totalFiles = files.length;
const reviewSpinner = oraStart(chalk.green(`Total Files: ${totalFiles}`));
reviewSpinner = oraStart(chalk.green(`Total Files: ${totalFiles}`));
files.forEach((file, index) => {
if (file.endsWith('.html')) {
try {
const filePath = path.join(process.cwd(), directory, file);
reviewSpinner.text = chalk.green.bold(`๐Ÿค–: Fixing file [${index + 1} - ${totalFiles}]: ${filePath}`);
const $ = cheerio.load(fs.readFileSync(filePath));
$('*').each((i, element) => {
if (!$(element).attr('id')) {
const randomId = generateRandomId(largeId);
$(element).attr('id', randomId);
try {
const filePath = file.fullPath;
reviewSpinner.text = chalk.green.bold(`๐Ÿค–: Fixing file [${index + 1} - ${totalFiles}]: ${filePath}`);
const fileContent = fs.readFileSync(filePath, 'utf8');
const dom = new JSDOM(fileContent, { contentType: "text/html", includeNodeLocations: true });
const document = dom.window.document.implementation.createHTMLDocument();
document.documentElement.innerHTML = dom.serialize();
const elements = document.querySelectorAll('*:not(html):not(head):not(body):not(script):not(style)');
elements.forEach((element) => {
var _a;
if (tagsToModify.length === 0 || tagsToModify.includes(element.tagName.toLowerCase())) {
if (!element.hasAttribute('id')
&& !excludeTags.includes(element.tagName.toLowerCase())
&& ((_a = element.parentElement) === null || _a === void 0 ? void 0 : _a.tagName.toLowerCase()) !== 'head') {
element.setAttribute('id', generateRandomId(largeId));
}
});
$('html').replaceWith($('html').html() || '');
$('head').replaceWith($('head').html() || '');
$('body').replaceWith($('body').html() || '');
fs.writeFileSync(filePath, $.html());
totalFixed++;
reviewSpinner.text = chalk.green.bold(`๐Ÿค–: File fixed correctly [${index + 1} - ${totalFiles}]: ${filePath}`);
}
catch (err) {
reviewSpinner.error(chalk.red.bold(`๐Ÿค–: Error in file "${file}"`, err));
}
}
});
let fixedContent = document.documentElement.innerHTML;
if (!fileContent.includes('<html'))
fixedContent = fixedContent.replace(/<\/?html>/gi, '');
if (!fileContent.includes('<head'))
fixedContent = fixedContent.replace(/<\/?head>/gi, '');
if (!fileContent.includes('<body'))
fixedContent = fixedContent.replace(/<\/?body>/gi, '');
fs.writeFileSync(filePath, fixedContent);
totalFixed++;
reviewSpinner.text = chalk.green.bold(`๐Ÿค–: File fixed correctly [${index + 1} - ${totalFiles}]: ${filePath}`);
}
catch (err) {
reviewSpinner.fail(chalk.red.bold(`๐Ÿค–: Error in file "${file.fullPath}"`, err));
}
});

@@ -69,29 +85,36 @@ reviewSpinner.stop();

console.log(chalk.red(`๐Ÿค–: Error browsing directory "${directory}"`, err));
if (reviewSpinner)
reviewSpinner.stop();
}
});
const main = () => __awaiter(void 0, void 0, void 0, function* () {
config({ path: path.join(process.cwd(), '.env.local') });
const prompt = promptSync();
let isValid = false;
let directory = '';
let largeId = 8;
while (isValid === false) {
directory = prompt(chalk.blue.bold(`๐Ÿค–: Which directory do you want to fix?: `));
if (!directory || directory == '' || !fs.existsSync(path.join(process.cwd(), directory))) {
console.log(chalk.red(`๐Ÿค–: Enter a valid directory ...`));
let directory = process.env.FIX_DIRECTORY || '';
let largeId = parseInt(process.env.FIX_LARGE_ID) || 8;
let allowedTags = process.env.FIX_ALLOWED_TAGS || '';
let excludeTags = process.env.FIX_EXCLUDED_TAGS || 'html, head, body';
if (!directory) {
let isValid = false;
while (!isValid) {
directory = prompt(chalk.blue.bold(`๐Ÿค–: Which directory do you want to fix?: `));
if (!directory || directory === '' || !fs.existsSync(path.join(process.cwd(), directory))) {
console.log(chalk.red(`๐Ÿค–: Enter a valid directory ...`));
}
else {
isValid = true;
}
}
else {
isValid = true;
}
}
isValid = false;
while (isValid === false) {
try {
largeId = parseInt(prompt(chalk.blue.bold(`๐Ÿค–: How long do you want the ids to be?: `)));
if (isNaN(largeId)) {
console.log(chalk.red(`๐Ÿค–: Enter a valid large...`));
}
else {
if (largeId == 0 || largeId > 20) {
console.log(chalk.red(`๐Ÿค–: Values โ€‹โ€‹must be between 1 and 20...`));
if (!largeId) {
let isValid = false;
while (!isValid) {
try {
largeId = parseInt(prompt(chalk.blue.bold(`๐Ÿค–: How long do you want the ids to be?: `)));
if (isNaN(largeId)) {
console.log(chalk.red(`๐Ÿค–: Enter a valid large...`));
}
else if (largeId === 0 || largeId > 20) {
console.log(chalk.red(`๐Ÿค–: Values must be between 1 and 20...`));
}
else {

@@ -101,10 +124,21 @@ isValid = true;

}
catch (err) {
console.log(chalk.red(`๐Ÿค–: Enter a valid large ...`));
}
}
catch (err) {
console.log(chalk.red(`๐Ÿค–: Enter a valid large ...`));
}
if (!allowedTags) {
allowedTags = prompt(chalk.blue.bold(`๐Ÿค–: You want to limit the tags that need to be fixed (example: button, a, img, li)?: `)) || '';
}
if (!allowedTags) {
excludeTags = prompt(chalk.blue.bold(`๐Ÿค–: Do you want to exclude tags so that ids are not attached to them (example: span, i, bold)?: `)) || 'html, head, body';
if (excludeTags.trim()) {
excludeTags = `html, head, body, ${excludeTags}`;
}
}
addIdsToHtmlFiles(directory, largeId);
const tagsToModify = allowedTags.split(',').map(el => el.trim());
const excludeTagsArr = excludeTags.split(',').map(el => el.trim());
addIdsToHtmlFiles(directory, largeId, tagsToModify, excludeTagsArr);
});
main();
//# sourceMappingURL=index.js.map
{
"name": "fixids",
"version": "1.0.2",
"version": "1.0.3",
"description": "",

@@ -22,7 +22,10 @@ "main": "bin/index.js",

"cheerio": "^1.0.0-rc.12",
"dotenv": "^16.0.3",
"fs": "^0.0.1-security",
"jsdom": "^21.1.1",
"ora": "^6.3.0",
"path": "^0.12.7",
"prompt-sync": "^4.2.0",
"randomstring": "^1.2.3"
"randomstring": "^1.2.3",
"readdirp": "^3.6.0"
},

@@ -29,0 +32,0 @@ "devDependencies": {

@@ -18,2 +18,11 @@

![WABOT](https://github.com/luiscruzga/fixids/blob/main/fixids.png?raw=true)
![WABOT](https://github.com/luiscruzga/fixids/blob/main/fixids.png?raw=true)
You can use a .env.local file in your repository directory to be able to indicate the parameters with which to run:
```javascript
FIX_DIRECTORY=src
FIX_LARGE_ID=12
FIX_ALLOWED_TAGS=button, a, li
FIX_EXCLUDED_TAGS=
```

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