Skip to content

Commit

Permalink
feat: support input plamform
Browse files Browse the repository at this point in the history
  • Loading branch information
Avivbens committed Jun 7, 2024
1 parent 51a541b commit f27d4dd
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 18 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"test": "jest"
},
"dependencies": {
"fast-alfred": "^1.3.3",
"fast-alfred": "^1.3.4",
"fuse.js": "^7.0.0",
"libphonenumber-js": "^1.11.2",
"node-mac-contacts": "^1.7.2"
Expand Down Expand Up @@ -82,4 +82,4 @@
"publishConfig": {
"registry": "https://registry.npmjs.org/"
}
}
}
4 changes: 4 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { exec } from 'node:child_process'
import { promisify } from 'node:util'

export const execPromise = promisify(exec)
19 changes: 17 additions & 2 deletions src/main/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ import { DEFAULT_MAX_RESULTS_COUNT } from '@common/constants.js'
import { Variables } from '@common/variables.js'
import type { ContactPayload } from '@models/contact-payload.model.js'
import type { IContact } from '@models/contact.model.js'
import { SUPPORTED_PLATFORMS, type SupportedPlatform } from '@models/platform.model.js'
import { getContacts } from '@services/contacts.service.js'
import { searchContacts } from '@services/search.service.js'

;(() => {
const alfredClient = new FastAlfred()

const [searchTerm, platform] = alfredClient.inputs

if (!SUPPORTED_PLATFORMS.includes(platform as SupportedPlatform)) {
const errorMassage = `Unsupported platform: ${platform}, Supported platforms: ${SUPPORTED_PLATFORMS.join(', ')}`
const error = new Error(errorMassage)

alfredClient.error(error)
return
}

const countryCode: CountryCode = alfredClient.env.getEnv<CountryCode>(Variables.COUNTRY_CODE, {
defaultValue: 'US',
})
Expand All @@ -22,11 +33,15 @@ import { searchContacts } from '@services/search.service.js'

const contacts: IContact[] = getContacts(alfredClient)

const filteredContacts = searchContacts(contacts, alfredClient.input, sliceAmount)
const filteredContacts = searchContacts(contacts, searchTerm, sliceAmount)

const items: AlfredScriptFilter['items'] = filteredContacts.map(
({ firstName, lastName, phoneNumbers }: IContact) => {
const payload: ContactPayload = { phoneNumber: phoneNumbers[0], countryCode }
const payload: ContactPayload = {
phoneNumber: phoneNumbers[0],
countryCode,
platform: platform as SupportedPlatform,
}

return {
title: `${firstName} ${lastName}`,
Expand Down
7 changes: 2 additions & 5 deletions src/main/open-whatsapp.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { FastAlfred } from 'fast-alfred'
import type { PhoneNumber } from 'libphonenumber-js'
import { parsePhoneNumber } from 'libphonenumber-js'
import { exec } from 'node:child_process'
import { promisify } from 'node:util'
import { execPromise } from '@common/utils.js'
import type { ContactPayload } from '@models/contact-payload.model.js'

const execPrm = promisify(exec)

;(async () => {
const alfredClient = new FastAlfred()

Expand All @@ -18,5 +15,5 @@ const execPrm = promisify(exec)

const urlNew = `whatsapp://send?phone=${number}`

await execPrm(`open ${urlNew}`)
await execPromise(`open ${urlNew}`)
})()
2 changes: 2 additions & 0 deletions src/models/contact-payload.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { CountryCode } from 'libphonenumber-js'
import type { SupportedPlatform } from './platform.model.js'

export interface ContactPayload {
phoneNumber: string
countryCode: CountryCode
platform: SupportedPlatform
}
2 changes: 2 additions & 0 deletions src/models/platform.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SUPPORTED_PLATFORMS = ['whatsapp', 'sms', 'call'] as const
export type SupportedPlatform = (typeof SUPPORTED_PLATFORMS)[number]

0 comments on commit f27d4dd

Please sign in to comment.