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

Refactor/ts scripts #7178

Merged
merged 13 commits into from
Aug 8, 2022
Merged

Conversation

nikkhielseath
Copy link
Contributor

Description

Migrate generate-header-ids script to TypeScript.

Related Issue

@github-actions github-actions bot added dependencies 📦 Changes related to project dependencies review needed 👀 tooling 🔧 Changes related to tooling of the project labels Jul 25, 2022
@@ -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
Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pettinarip

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

Copy link
Contributor Author

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.

Copy link
Member

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"
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pettinarip

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

src/scripts/generate-heading-ids.ts Outdated Show resolved Hide resolved
tsconfig.json Outdated
Comment on lines 14 to 15
"typeRoots": ["./node_modules/@types"],
"types": ["node"]
Copy link
Contributor Author

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 Show resolved Hide resolved
// Sluggers should be per file
const slugger = new GitHubSlugger()
const slugger: IGitHubSlugger = new GitHubSlugger()
Copy link
Contributor Author

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
Copy link

gatsby-cloud bot commented Jul 25, 2022

Gatsby Cloud Build Report

ethereum-org-website-dev

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 29m

Copy link
Member

@pettinarip pettinarip left a 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.

src/scripts/generate-heading-ids.ts Outdated Show resolved Hide resolved
src/scripts/generate-heading-ids.ts Outdated Show resolved Hide resolved
Copy link
Member

@pettinarip pettinarip left a 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",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pettinarip

I am using esm flag to mark it as ES Module along with mts extension for the file

Copy link
Contributor Author

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,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pettinarip

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",
Copy link
Contributor Author

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",
Copy link
Contributor Author

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.

Copy link
Member

@pettinarip pettinarip left a comment

Choose a reason for hiding this comment

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

lgtm. Ty @SNikhill

@pettinarip pettinarip merged commit b5d591a into ethereum:dev Aug 8, 2022
@nikkhielseath nikkhielseath deleted the refactor/ts-scripts branch August 8, 2022 18:51
@minimalsm minimalsm mentioned this pull request Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies 📦 Changes related to project dependencies tooling 🔧 Changes related to tooling of the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants