-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Refactor/ts scripts #7178
Refactor/ts scripts #7178
Conversation
Define a config object to pass in optional parameters to traverse headers
Throw error if no valid path is provided
src/scripts/generate-heading-ids.ts
Outdated
@@ -13,16 +13,27 @@ const GitHubSlugger = require("github-slugger") | |||
|
|||
// TODO we should auto-run this script when new markdown files are added to the project | |||
|
|||
const toc = {} | |||
interface IGitHubSlugger { | |||
slug: (headingText: string) => string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactor: I should install @types/github-slugger
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are Definitely Typed types detected with a CommonJS require syntax?
I can use typeof gitHubSlugger
to specify its type, but, can't use BananaSlug
which is the name of its type class definition from the @types/github-slugger package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or an alternate solution: declare the script as ES6 Module and use the import
syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, lets try that. Lets use it as an es6 module where you can do these imports using import
.
Reading a bit, I think you will need to override the tsconfig for ts-node
to use commonjs module, like this:
// in tsconfig.json add
"ts-node": {
"compilerOptions": {
"module": "commonjs"
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, I am using the .mts
extension but, if you want then, I think we can use moduleTypes
to define the module type for this script explicitly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, since module type is inferred from the nearest package.json, we can add a package.json
in the scripts folder and specify the type as module
for all the scripts.
But, this option would require converting the other scripts to use the ESM import and will go out of the scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I guess I like to have everything in .ts
instead of mixing them with different extensions to make it simpler to other developers. But I don't have a strong opinion about that, I think we are fine with using it as .mts
.
tsconfig.json
Outdated
"typeRoots": ["./node_modules/@types"], | ||
"types": ["node"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: remove this
src/scripts/generate-heading-ids.ts
Outdated
// Sluggers should be per file | ||
const slugger = new GitHubSlugger() | ||
const slugger: IGitHubSlugger = new GitHubSlugger() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: use correct type for GitHub slugger
Gatsby Cloud Build Reportethereum-org-website-dev 🎉 Your build was successful! See the Deploy preview here. Build Details🕐 Build time: 29m |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job @SNikhill. Not sure if this was ready for review as there are a couple of TODO comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in the last comment, lets try to use es6 imports by configuring the tsconfig
file.
In addition, lets
- install the
github-slugger
types - rename this file to
generateHeadingIds.ts
if everything works as expected
@@ -100,7 +102,7 @@ | |||
"crowdin-clean": "rm -rf .crowdin && mkdir .crowdin", | |||
"crowdin-import": "ts-node src/scripts/crowdin-import.ts", | |||
"format": "prettier --write \"**/*.{js,jsx,json,md}\"", | |||
"generate-heading-ids": "node src/scripts/generate-heading-ids.js", | |||
"generate-heading-ids": "ts-node --esm src/scripts/generateHeadingIds.mts", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am using esm
flag to mark it as ES Module along with mts
extension for the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to manually set the syntax to typescript
in neovim for this mts
file to get the syntax highlighting.
const addHeaderID = (line, slugger, write = false) => { | ||
const addHeaderID = ( | ||
line: string, | ||
slugger: BananaSlug, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type for GitHub Slugger is now inferred. Class Name: BananaSlug
@@ -90,6 +91,7 @@ | |||
"prettier": "^2.2.1", | |||
"pretty-quick": "^3.1.0", | |||
"react-test-renderer": "^17.0.1", | |||
"ts-node": "^10.9.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to list ts-node as a devDependency
.
@@ -100,7 +102,7 @@ | |||
"crowdin-clean": "rm -rf .crowdin && mkdir .crowdin", | |||
"crowdin-import": "ts-node src/scripts/crowdin-import.ts", | |||
"format": "prettier --write \"**/*.{js,jsx,json,md}\"", | |||
"generate-heading-ids": "node src/scripts/generate-heading-ids.js", | |||
"generate-heading-ids": "ts-node --esm src/scripts/generateHeadingIds.mts", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to manually set the syntax to typescript
in neovim for this mts
file to get the syntax highlighting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. Ty @SNikhill
Description
Migrate generate-header-ids script to TypeScript.
Related Issue