Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto-generate list of Units and AreaUnits and link all modules with units param #2694

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@monorepolint/config": "0.5.0-alpha.132",
"@monorepolint/core": "0.5.0-alpha.132",
"@monorepolint/rules": "0.5.0-alpha.132",
"@phenomnomnominal/tsquery": "6.1.3",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ts-morph would be the alternative library here if you decide you don't like tsquery, I don't have a strong opinion here

"@types/node": "18.11.9",
"@typescript-eslint/eslint-plugin": "^6.10.0",
"@typescript-eslint/parser": "^6.10.0",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions scripts/generate-readmes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { glob } = require("glob");
const path = require("path");
const { loadJsonFileSync } = require("load-json-file");
const yaml = require("yamljs");
const { ast, query } = require("@phenomnomnominal/tsquery");

(async () => {
// documentation v14 has moved to ESM so need to import as if async, and wrap
Expand Down Expand Up @@ -59,6 +60,72 @@ const yaml = require("yamljs");
/{module}/,
name
)}`;

if (packagePath.includes("turf-helpers")) {
const helpersTs = fs.readFileSync(indexPath, "utf8").toString();
const theAst = ast(helpersTs);

// To learn how to write tsquery selectors, see: https://tsquery-playground.firebaseapp.com/ and paste the code from turf-helpers/index.ts

// query Units string literal names
// ToDo: malformed selector. In addition to Units, it includes StringLiterals from AreaUnits as well (hectares, acres, degrees, radians)
const unitLiterals = query(
theAst,
'TypeAliasDeclaration:has(Identifier[name="Units"]) UnionType LiteralType StringLiteral'
);
const unitArray = unitLiterals.map((sLiteral) =>
sLiteral.getText().replaceAll('"', "")
);
const unitNames =
unitLiterals.length > 0 ? "Units: " + unitArray.join(", ") : "";
console.log(unitNames);

// query all AreaUnits string literals (including exclusions)
const areaUnitAllLiterals = query(
theAst,
'TypeAliasDeclaration:has(Identifier[name="AreaUnits"]) UnionType > LiteralType StringLiteral'
);
const areaUnitAllArray = areaUnitAllLiterals.map((sLiteral) =>
sLiteral.getText().replaceAll('"', "")
);
console.log("areaUnitAllArray", areaUnitAllArray);

// query AreaUnits string literals to Exclude
const areaUnitExcludeLiterals = query(
theAst,
'TypeAliasDeclaration:has(Identifier[name="AreaUnits"]) UnionType TypeReference LiteralType StringLiteral'
);
const areaUnitExcludeArray = areaUnitExcludeLiterals.map(
(sLiteral) => sLiteral.getText().replaceAll('"', "")
);
console.log("areaUnitExcludeArray", areaUnitExcludeArray);

// AreaUnits to add = all AreaUnits - Exclude AreaUnits
const newAreaUnitArray = areaUnitAllArray.filter(
(name) => !areaUnitExcludeArray.includes(name)
);
// console.log("newAreaUnitArray", newAreaUnitArray)

// Subtract Exclude AreaUnits from all AreaUnits and add new ones
const areaUnitArray = unitArray
.filter((name) => !areaUnitExcludeArray.includes(name))
.concat(newAreaUnitArray);
// console.log("areaUnitArray", areaUnitArray)
const areaUnitNames =
areaUnitArray.length > 0
? "Area Units: " + areaUnitArray.join(", ")
: "";
console.log("areaUnitNames", areaUnitNames);

// extract area unit names. unit names - exclusions + inclusions

const insertText = "## helpers\n";
markdown = markdown.replace(
insertText,
`${insertText}\n### units\n\n${unitNames}\n`
);
}

if (diagrams.length)
markdown += "\n\n### Diagrams\n\n" + diagramToMarkdown(diagrams);
fs.writeFileSync(path.join(directory, "README.md"), markdown);
Expand Down