Comparing version 1.0.3 to 1.0.4
@@ -13,80 +13,13 @@ #!/usr/bin/env node | ||
import path from 'path'; | ||
import readdirp from 'readdirp'; | ||
import { JSDOM } from 'jsdom'; | ||
import randomstring from 'randomstring'; | ||
import promptSync from 'prompt-sync'; | ||
import ora from 'ora'; | ||
import chalk from "chalk"; | ||
import { config } from 'dotenv'; | ||
const generateRandomId = (largeId) => { | ||
return randomstring.generate({ length: largeId, charset: 'alphanumeric' }); | ||
}; | ||
const oraStart = (text = '') => { | ||
return ora({ | ||
text, | ||
spinner: { | ||
interval: 800, | ||
frames: ['🚀', '🤖', '🚀', '🤖', '🚀', '🤖', '🚀', '🤖'], | ||
}, | ||
}).start(); | ||
}; | ||
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 = yield readdirp.promise(path.join(process.cwd(), directory), { | ||
fileFilter: '*.html', | ||
directoryFilter: (di) => !di.basename.startsWith('.'), | ||
}); | ||
const totalFiles = files.length; | ||
reviewSpinner = oraStart(chalk.green(`Total Files: ${totalFiles}`)); | ||
files.forEach((file, index) => { | ||
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)); | ||
} | ||
} | ||
}); | ||
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)); | ||
} | ||
}); | ||
reviewSpinner.stop(); | ||
console.log(chalk.green.bold(`🤖: A total of ${totalFixed} files have been fixed.`)); | ||
} | ||
catch (err) { | ||
console.log(chalk.red(`🤖: Error browsing directory "${directory}"`, err)); | ||
if (reviewSpinner) | ||
reviewSpinner.stop(); | ||
} | ||
}); | ||
import { printTitle } from './utils/title'; | ||
import { addIdsToHtmlFiles } from './utils/fix'; | ||
const main = () => __awaiter(void 0, void 0, void 0, function* () { | ||
printTitle(); | ||
config({ path: path.join(process.cwd(), '.env.local') }); | ||
const prompt = promptSync(); | ||
let directory = process.env.FIX_DIRECTORY || ''; | ||
let largeId = parseInt(process.env.FIX_LARGE_ID) || 8; | ||
let largeId = parseInt(process.env.FIX_LARGE_ID); | ||
let allowedTags = process.env.FIX_ALLOWED_TAGS || ''; | ||
@@ -130,8 +63,11 @@ let excludeTags = process.env.FIX_EXCLUDED_TAGS || 'html, head, body'; | ||
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 = prompt(chalk.blue.bold(`🤖: Do you want to exclude tags so that ids are not attached to them (example: span, i, bold)?: `)) || ''; | ||
if (excludeTags.trim() !== '') { | ||
excludeTags = `html, head, body, ${excludeTags}`; | ||
} | ||
else { | ||
excludeTags = 'html, head, body'; | ||
} | ||
} | ||
const tagsToModify = allowedTags.split(',').map(el => el.trim()); | ||
const tagsToModify = allowedTags.trim() !== '' ? allowedTags.split(',').map(el => el.trim()) : []; | ||
const excludeTagsArr = excludeTags.split(',').map(el => el.trim()); | ||
@@ -138,0 +74,0 @@ addIdsToHtmlFiles(directory, largeId, tagsToModify, excludeTagsArr); |
{ | ||
"name": "fixids", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "bin/index.js", |
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
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
16666
8
169
6