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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14086
138
28
10
5
+ Addeddotenv@^16.0.3
+ Addedjsdom@^21.1.1
+ Addedreaddirp@^3.6.0
+ Added@tootallnate/once@2.0.0(transitive)
+ Addedabab@2.0.6(transitive)
+ Addedacorn@8.14.0(transitive)
+ Addedacorn-globals@7.0.1(transitive)
+ Addedacorn-walk@8.3.4(transitive)
+ Addedagent-base@6.0.2(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcssstyle@3.0.0(transitive)
+ Addeddata-urls@4.0.0(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddecimal.js@10.4.3(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addeddomexception@4.0.0(transitive)
+ Addeddotenv@16.4.5(transitive)
+ Addedescodegen@2.1.0(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedestraverse@5.3.0(transitive)
+ Addedesutils@2.0.3(transitive)
+ Addedform-data@4.0.1(transitive)
+ Addedhtml-encoding-sniffer@3.0.0(transitive)
+ Addedhttp-proxy-agent@5.0.0(transitive)
+ Addedhttps-proxy-agent@5.0.1(transitive)
+ Addedis-potential-custom-element-name@1.0.1(transitive)
+ Addedjsdom@21.1.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@2.1.3(transitive)
+ Addednwsapi@2.2.13(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedpsl@1.10.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedquerystringify@2.2.0(transitive)
+ Addedreaddirp@3.6.0(transitive)
+ Addedrequires-port@1.0.0(transitive)
+ Addedrrweb-cssom@0.6.0(transitive)
+ Addedsaxes@6.0.0(transitive)
+ Addedsource-map@0.6.1(transitive)
+ Addedsymbol-tree@3.2.4(transitive)
+ Addedtough-cookie@4.1.4(transitive)
+ Addedtr46@4.1.1(transitive)
+ Addeduniversalify@0.2.0(transitive)
+ Addedurl-parse@1.5.10(transitive)
+ Addedw3c-xmlserializer@4.0.0(transitive)
+ Addedwebidl-conversions@7.0.0(transitive)
+ Addedwhatwg-encoding@2.0.0(transitive)
+ Addedwhatwg-mimetype@3.0.0(transitive)
+ Addedwhatwg-url@12.0.1(transitive)
+ Addedws@8.18.0(transitive)
+ Addedxml-name-validator@4.0.0(transitive)
+ Addedxmlchars@2.2.0(transitive)