Skip to content

Commit

Permalink
✨ Add TypeScript support (#1125)
Browse files Browse the repository at this point in the history
* ➕ Add typescript@5.1.3

* ➕ Add @babel/preset-typescript@7.22.5

* ➕ Add @types/update-notifier

* ➕ Add @jest/globals

* 🚨 Add support for typescript linting

* ⬆️ Bump deps

* 🏗️ Support both flow and typescript in babel config

* ♻️ Convert entrypoint into TS

* ♻️ Refactor into typescript test

* 🔥 Remove comments and unused config properties

* 🏷️ Use @types/node instead of @jest/globals for global typing

* 🏷️ Override unfortunate @types/jest global conflict

* ✅ Make sure that w/e we're passing to commit matches the options
  • Loading branch information
segersniels committed Jun 14, 2023
1 parent c0b0ab8 commit e4a3f19
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 39 deletions.
23 changes: 11 additions & 12 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"presets": [
"@babel/preset-flow"
],
"presets": ["@babel/preset-flow"],
"plugins": [
[
"module-resolver",
Expand All @@ -18,13 +16,14 @@
],
"env": {
"test": {
"presets": [
"@babel/preset-env",
"@babel/preset-flow"
],
"plugins": [
"babel-plugin-transform-import-meta"
]
"presets": ["@babel/preset-env", "@babel/preset-flow"],
"plugins": ["babel-plugin-transform-import-meta"]
}
},
"overrides": [
{
"test": ["./src/**/*.ts"],
"presets": ["@babel/preset-typescript"]
}
}
}
]
}
35 changes: 29 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lib"
],
"scripts": {
"build": "babel src -d lib",
"build": "babel src -d lib --extensions \".js,.ts\"",
"clean": "rm -rf lib",
"coverage": "codecov",
"flow": "flow",
Expand Down Expand Up @@ -64,6 +64,11 @@
"@babel/plugin-syntax-import-assertions": "^7.18.6",
"@babel/preset-env": "7.22.5",
"@babel/preset-flow": "7.22.5",
"@babel/preset-typescript": "^7.22.5",
"@types/jest": "^29.5.2",
"@types/update-notifier": "^6.0.4",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"babel-plugin-module-extension-resolver": "^1.0.0-rc.2",
"babel-plugin-module-resolver": "5.0.0",
"babel-plugin-transform-import-meta": "^2.2.0",
Expand All @@ -80,7 +85,8 @@
"lint-staged": "13.2.2",
"pkg": "5.8.1",
"prettier": "2.8.8",
"turbo": "^1.10.3"
"turbo": "^1.10.3",
"typescript": "^5.1.3"
},
"jest": {
"coverageDirectory": "./coverage/",
Expand Down Expand Up @@ -113,16 +119,34 @@
"parser": "hermes-eslint",
"plugins": [
"ft-flow",
"immutable"
"immutable",
"@typescript-eslint"
],
"env": {
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended"
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"overrides": [
{
"files": [
"./**/*.spec.js",
"./**/*.spec.ts"
],
"env": {
"jest": true
}
},
{
"files": [
"./**/*.ts"
],
"parser": "@typescript-eslint/parser"
}
],
"overrides": [],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
Expand All @@ -133,7 +157,6 @@
"no-param-reassign": "error"
},
"globals": {
"jest": true,
"$Values": "readonly"
}
},
Expand Down
25 changes: 14 additions & 11 deletions src/cli.js → src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#!/usr/bin/env node
// @flow
import meow from 'meow'
import updateNotifier from 'update-notifier'
import { readFileSync } from 'fs'

import FLAGS from '@constants/flags'
import findGitmojiCommand from '@utils/findGitmojiCommand'
import commands from './commands'
import commands from '@commands/index'
import type { CommitOptions } from '@commands/commit'
import type { SearchOptions } from '@commands/search'

const packageJson: Object = readFileSync(
const packageJson = readFileSync(
new URL('../package.json', import.meta.url)
)
).toString()

updateNotifier({ pkg: JSON.parse(packageJson) }).notify({ isGlobal: true })
updateNotifier({ pkg: JSON.parse(packageJson) }).notify({
isGlobal: true
})

const cli = meow(
`
Expand Down Expand Up @@ -40,7 +43,7 @@ const cli = meow(
$ gitmoji bug linter -s
`,
{
importMeta: { url: import.meta.url },
importMeta: { url: import.meta.url } as ImportMeta,
flags: {
[FLAGS.COMMIT]: { type: 'boolean', alias: 'c' },
[FLAGS.CONFIG]: { type: 'boolean', alias: 'g' },
Expand All @@ -55,15 +58,15 @@ const cli = meow(
}
)

export const options = ({
[FLAGS.COMMIT]: (options: Object) => commands.commit(options),
export const options = {
[FLAGS.COMMIT]: (options: CommitOptions) => commands.commit(options),
[FLAGS.CONFIG]: () => commands.config(),
[FLAGS.HOOK]: (options: Object) => commands.commit(options),
[FLAGS.HOOK]: (options: CommitOptions) => commands.commit(options),
[FLAGS.INIT]: () => commands.createHook(),
[FLAGS.LIST]: () => commands.list(),
[FLAGS.REMOVE]: () => commands.removeHook(),
[FLAGS.SEARCH]: (options: Object) => commands.search(options),
[FLAGS.SEARCH]: (options: SearchOptions) => commands.search(options),
[FLAGS.UPDATE]: () => commands.update()
}: { [$Values<typeof FLAGS>]: Function })
}

findGitmojiCommand(cli, options)
2 changes: 1 addition & 1 deletion src/commands/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import filterGitmojis from '@utils/filterGitmojis'
import getEmojis from '@utils/getEmojis'
import printEmojis from '@utils/printEmojis'

type SearchOptions = {
export type SearchOptions = {
query?: string[]
}

Expand Down
File renamed without changes.
14 changes: 8 additions & 6 deletions test/cli.spec.js → test/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ describe('cli', () => {
})

it('should match meow with cli information', () => {
meow.mock.calls[0][1].importMeta = 'import.meta.url'
(
(meow as jest.Mock).mock.calls[0][1] as { importMeta: string }
).importMeta = 'import.meta.url'

expect(meow.mock.calls).toMatchSnapshot()
expect((meow as jest.Mock).mock.calls).toMatchSnapshot()
})

it('should call commit command on commit', () => {
options.commit('client')
expect(commands.commit).toHaveBeenCalledWith('client')
options.commit({ mode: 'client' })
expect(commands.commit).toHaveBeenCalledWith({ mode: 'client' })
})

it('should call config command on config', () => {
Expand All @@ -32,8 +34,8 @@ describe('cli', () => {
})

it('should call commit command on hook', () => {
options.hook('hook')
expect(commands.commit).toHaveBeenLastCalledWith('hook')
options.hook({ mode: 'hook' })
expect(commands.commit).toHaveBeenLastCalledWith({ mode: 'hook' })
})

it('should call createHook command on init', () => {
Expand Down
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es6",
"module": "esnext",
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"@utils/*": ["src/utils/*"],
"@commands/*": ["src/commands/*"],
"@constants/*": ["src/constants/*"]
},
"resolveJsonModule": true,
"allowJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}

0 comments on commit e4a3f19

Please sign in to comment.