forked from farcasterxyz/hub-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add cli docs linter (farcasterxyz#1353)
- Loading branch information
1 parent
efd1bff
commit aac4220
Showing
7 changed files
with
141 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@farcaster/hubble": patch | ||
--- | ||
|
||
chore: Add cli options documentation linter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const { execSync } = require("child_process"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
module.exports = function clidocs() { | ||
const docFileName = "www/docs/docs/cli.md"; | ||
|
||
try { | ||
const helpOutput = execSync("node --no-warnings build/cli.js start --help").toString(); | ||
|
||
const optionNames = []; | ||
const regex = /--\w+(-\w+)*/g; | ||
let match; | ||
|
||
// rome-ignore lint/suspicious/noAssignInExpressions: <explanation> | ||
while ((match = regex.exec(helpOutput)) !== null) { | ||
optionNames.push(match[0]); | ||
} | ||
|
||
// Step 2: Read the contents of the cli.md file | ||
const cliDocPath = path.resolve(__dirname, "..", docFileName); | ||
const cliDocContent = fs.readFileSync(cliDocPath, "utf-8"); | ||
|
||
// Step 3: Check that each option name appears in the cli.md file | ||
let anyError = false; | ||
optionNames.forEach((optionName) => { | ||
if (!cliDocContent.includes(optionName)) { | ||
console.warn(`Documentation for option "${optionName}" is missing in ${docFileName}`); | ||
anyError = true; | ||
} | ||
}); | ||
|
||
if (anyError) { | ||
process.exit(1); | ||
} else { | ||
console.log("✨ CLI docs are up to date"); | ||
} | ||
} catch (error) { | ||
console.error("Error executing command:", error); | ||
process.exit(1); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* This is a custom linter that checks for the presence of a "datasource" property | ||
* in the Grafana dashboard JSON file. If the property is present, it must be set | ||
* to "Graphite". This is to ensure that the dashboard is not using any other | ||
* datasource. | ||
* | ||
* It runs as a part of "yarn lint" | ||
*/ | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
module.exports = function grafana() { | ||
const filePath = path.join(__dirname, "../grafana/grafana-dashboard.json"); | ||
|
||
function checkDataSource(lineArray) { | ||
let lineNumber = 0; | ||
|
||
for (const line of lineArray) { | ||
lineNumber++; | ||
|
||
// Check if the current line contains the undesired datasource entry | ||
if (line.includes('"datasource":') && !line.replaceAll(" ", "").includes('"datasource":"Graphite"')) { | ||
return lineNumber; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
fs.readFile(filePath, "utf-8", (err, data) => { | ||
if (err) { | ||
console.error(`Error reading ${filePath}:`, err); | ||
process.exit(1); | ||
} | ||
|
||
// Split the content into lines | ||
const lineArray = data.split("\n"); | ||
const errorLine = checkDataSource(lineArray); | ||
|
||
if (errorLine !== null) { | ||
console.error(`line ${errorLine}: ${lineArray[errorLine - 1]}`); | ||
console.error(`Error: "datasource" has to be "Graphite"`); | ||
console.error(`Replace with: "datasource": "Graphite"`); | ||
console.error(`Error: ${filePath}`); | ||
|
||
process.exit(1); | ||
} else { | ||
console.log("✨ Grafana Dashboard linter passed"); | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,20 @@ | ||
/** | ||
* This is a custom linter that checks for the presence of a "datasource" property | ||
* in the Grafana dashboard JSON file. If the property is present, it must be set | ||
* to "Graphite". This is to ensure that the dashboard is not using any other | ||
* datasource. | ||
* Custom linters for Hubble. There are some additional things we want to check | ||
* for that are not covered by the default linters. | ||
* | ||
* It runs as a part of "yarn lint" | ||
* For eg. we want to check if the Grafana dashboards are valid JSON and if the | ||
* datasource is set to "Graphite". | ||
* | ||
* Add linters as .cjs files in this directory and they will be executed as a | ||
* part of "yarn lint". | ||
*/ | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const filePath = path.join(__dirname, "../grafana/grafana-dashboard.json"); | ||
|
||
function checkDataSource(lineArray) { | ||
let lineNumber = 0; | ||
|
||
for (const line of lineArray) { | ||
lineNumber++; | ||
async function executeAll() { | ||
const grafana = require("./grafanadash.cjs"); | ||
await grafana(); | ||
|
||
// Check if the current line contains the undesired datasource entry | ||
if (line.includes('"datasource":') && !line.replaceAll(" ", "").includes('"datasource":"Graphite"')) { | ||
return lineNumber; | ||
} | ||
} | ||
|
||
return null; | ||
const clidocs = require("./clidocs.cjs"); | ||
await clidocs(); | ||
} | ||
|
||
fs.readFile(filePath, "utf-8", (err, data) => { | ||
if (err) { | ||
console.error(`Error reading ${filePath}:`, err); | ||
process.exit(1); | ||
} | ||
|
||
// Split the content into lines | ||
const lineArray = data.split("\n"); | ||
const errorLine = checkDataSource(lineArray); | ||
|
||
if (errorLine !== null) { | ||
console.error(`line ${errorLine}: ${lineArray[errorLine - 1]}`); | ||
console.error(`Error: "datasource" has to be "Graphite"`); | ||
console.error(`Replace with: "datasource": "Graphite"`); | ||
console.error(`Error: ${filePath}`); | ||
|
||
process.exit(1); | ||
} else { | ||
console.log("✨ Custom linter passed"); | ||
} | ||
}); | ||
executeAll(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters