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

✨ Add TypeScript support #1125

Merged
merged 13 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
segersniels marked this conversation as resolved.
Show resolved Hide resolved
"$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
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()
Comment on lines +19 to +23
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Bit unfortunate bit of typing going on here, but not sure what the proper way would be to handle mocked libraries.

})

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