Compare commits

..

1 Commits

Author SHA1 Message Date
Thomas Allmer
31eca9f63c wip: improve node api 2021-01-27 20:00:27 +01:00
6 changed files with 63 additions and 58 deletions

View File

@@ -1,11 +1,5 @@
# check-html-links
## 0.1.2
### Patch Changes
- f343c50: When reading bigger files, especially bigger files with all content on one line it could mean a read chunk is in the middle of a character. This can lead to strange symbols in the resulting string. The `hightWaterMark` is now increased from the node default of 16KB to 256KB. Additionally, the `hightWaterMark` is now synced for reading and parsing.
## 0.1.1
### Patch Changes

View File

@@ -1,2 +1,3 @@
export { validateFolder } from './src/validateFolder.js';
export { formatErrors } from './src/formatErrors.js';
export { checkHtmlLinks } from './src/checkHtmlLinks.js';

View File

@@ -1,6 +1,6 @@
{
"name": "check-html-links",
"version": "0.1.2",
"version": "0.1.1",
"publishConfig": {
"access": "public"
},

View File

@@ -0,0 +1,50 @@
#!/usr/bin/env node
import chalk from 'chalk';
import { validateFiles } from './validateFolder.js';
import { formatErrors } from './formatErrors.js';
import { listFiles } from './listFiles.js';
export async function checkHtmlLinks({ rootDir = process.cwd() }) {
const performanceStart = process.hrtime();
console.log('👀 Checking if all internal links work...');
const files = await listFiles('**/*.html', rootDir);
const filesOutput =
files.length == 0
? '🧐 No files to check. Did you select the correct folder?'
: `🔥 Found a total of ${chalk.green.bold(files.length)} files to check!`;
console.log(filesOutput);
const { errors, numberLinks } = await validateFiles(files, rootDir);
console.log(`🔗 Found a total of ${chalk.green.bold(numberLinks)} links to validate!\n`);
const performance = process.hrtime(performanceStart);
if (errors.length > 0) {
let referenceCount = 0;
for (const error of errors) {
referenceCount += error.usage.length;
}
const output = [
`❌ Found ${chalk.red.bold(
errors.length.toString(),
)} missing reference targets (used by ${referenceCount} links) while checking ${
files.length
} files:`,
...formatErrors(errors)
.split('\n')
.map(line => ` ${line}`),
`Checking links duration: ${performance[0]}s ${performance[1] / 1000000}ms`,
];
console.error(output.join('\n'));
process.exit(1);
} else {
console.log(
`✅ All internal links are valid. (executed in ${performance[0]}s ${
performance[1] / 1000000
}ms)`,
);
}
}

View File

@@ -1,55 +1,17 @@
#!/usr/bin/env node
import path from 'path';
import chalk from 'chalk';
import { validateFiles } from './validateFolder.js';
import { formatErrors } from './formatErrors.js';
import { listFiles } from './listFiles.js';
import { checkHtmlLinks } from 'check-html-links';
async function main() {
async function cli() {
const userRootDir = process.argv[2];
const rootDir = userRootDir ? path.resolve(userRootDir) : process.cwd();
const performanceStart = process.hrtime();
console.log('👀 Checking if all internal links work...');
const files = await listFiles('**/*.html', rootDir);
const filesOutput =
files.length == 0
? '🧐 No files to check. Did you select the correct folder?'
: `🔥 Found a total of ${chalk.green.bold(files.length)} files to check!`;
console.log(filesOutput);
const { errors, numberLinks } = await validateFiles(files, rootDir);
console.log(`🔗 Found a total of ${chalk.green.bold(numberLinks)} links to validate!\n`);
const performance = process.hrtime(performanceStart);
if (errors.length > 0) {
let referenceCount = 0;
for (const error of errors) {
referenceCount += error.usage.length;
}
const output = [
`❌ Found ${chalk.red.bold(
errors.length.toString(),
)} missing reference targets (used by ${referenceCount} links) while checking ${
files.length
} files:`,
...formatErrors(errors)
.split('\n')
.map(line => ` ${line}`),
`Checking links duration: ${performance[0]}s ${performance[1] / 1000000}ms`,
];
console.error(output.join('\n'));
process.exit(1);
} else {
console.log(
`✅ All internal links are valid. (executed in %ds %dms)`,
performance[0],
performance[1] / 1000000,
);
try {
await checkHtmlLinks({ rootDir });
} catch (error) {
console.log('Check Html Links CLI failed to execute', error);
}
}
main();
cli();

View File

@@ -15,12 +15,10 @@ import path from 'path';
const require = createRequire(import.meta.url);
const { SaxEventType, SAXParser } = saxWasm;
const streamOptions = { highWaterMark: 256 * 1024 };
const saxPath = require.resolve('sax-wasm/lib/sax-wasm.wasm');
const saxWasmBuffer = fs.readFileSync(saxPath);
const parserReferences = new SAXParser(SaxEventType.Attribute, streamOptions);
const parserIds = new SAXParser(SaxEventType.Attribute, streamOptions);
const parserReferences = new SAXParser(SaxEventType.Attribute);
const parserIds = new SAXParser(SaxEventType.Attribute /*, { highWaterMark: 256 * 1024 } */);
/** @type {Error[]} */
let checkLocalFiles = [];
@@ -78,7 +76,7 @@ function extractReferences(htmlFilePath) {
};
return new Promise(resolve => {
const readable = fs.createReadStream(htmlFilePath, streamOptions);
const readable = fs.createReadStream(htmlFilePath);
readable.on('data', chunk => {
// @ts-expect-error
parserReferences.write(chunk);
@@ -114,7 +112,7 @@ function idExists(filePath, id) {
};
return new Promise(resolve => {
const readable = fs.createReadStream(filePath, streamOptions);
const readable = fs.createReadStream(filePath);
readable.on('data', chunk => {
// @ts-expect-error
parserIds.write(chunk);