Skip to content

Commit

Permalink
fix: migrate to cjs moduling system
Browse files Browse the repository at this point in the history
  • Loading branch information
Avivbens committed Jun 8, 2024
1 parent 44cd844 commit 9808170
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 29 deletions.
2 changes: 0 additions & 2 deletions .fast-alfred.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ module.exports = {
assets: ['./src/assets/contacts.node'],
assetsDir: 'Release',
targetDir: 'build',
outputFormat: 'esm',
esmHelpers: true,
},
workflowMetadata: {
name: 'Engage Contact',
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"email": "avivbens87@gmail.com"
}
],
"type": "module",
"main": "dist/index.js",
"scripts": {
"prebuild": "rm -rf ./dist",
Expand Down Expand Up @@ -84,4 +83,4 @@
"publishConfig": {
"registry": "https://registry.npmjs.org/"
}
}
}
2 changes: 1 addition & 1 deletion src/main/clear-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FastAlfred } from 'fast-alfred'
import { CACHE_CONTACTS_KEY } from '@common/constants.js'
import { CACHE_CONTACTS_KEY } from '@common/constants'

;(() => {
const alfredClient = new FastAlfred()
Expand Down
20 changes: 10 additions & 10 deletions src/main/contacts.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { AlfredScriptFilter } from 'fast-alfred'
import { FastAlfred } from 'fast-alfred'
import type { CountryCode } from 'libphonenumber-js'
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'

;(() => {
import { DEFAULT_MAX_RESULTS_COUNT } from '@common/constants'
import { Variables } from '@common/variables'
import type { ContactPayload } from '@models/contact-payload.model'
import type { IContact } from '@models/contact.model'
import { SUPPORTED_PLATFORMS, type SupportedPlatform } from '@models/platform.model'
import { getContacts } from '@services/contacts.service'
import { searchContacts } from '@services/search.service'

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

const [searchTerm, platform] = alfredClient.inputs
Expand All @@ -33,7 +33,7 @@ import { searchContacts } from '@services/search.service.js'

const contacts: IContact[] = getContacts(alfredClient)

const filteredContacts = searchContacts(contacts, searchTerm, sliceAmount)
const filteredContacts = await searchContacts(contacts, searchTerm, sliceAmount)

const items: AlfredScriptFilter['items'] = filteredContacts
.map(({ firstName, lastName, phoneNumbers, emailAddresses }: IContact) => {
Expand Down
6 changes: 3 additions & 3 deletions src/main/engage-contact.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FastAlfred } from 'fast-alfred'
import type { PhoneNumber } from 'libphonenumber-js'
import { parsePhoneNumber } from 'libphonenumber-js'
import { execPromise } from '@common/utils.js'
import type { ContactPayload } from '@models/contact-payload.model.js'
import { buildOpenUrl } from '@services/platform-url-builder.service.js'
import { execPromise } from '@common/utils'
import type { ContactPayload } from '@models/contact-payload.model'
import { buildOpenUrl } from '@services/platform-url-builder.service'

;(async () => {
const alfredClient = new FastAlfred()
Expand Down
2 changes: 1 addition & 1 deletion src/models/contact-payload.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CountryCode } from 'libphonenumber-js'
import type { SupportedPlatform } from './platform.model.js'
import type { SupportedPlatform } from './platform.model'

export interface ContactPayload {
phoneNumber: string
Expand Down
6 changes: 3 additions & 3 deletions src/services/contacts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { FastAlfred } from 'fast-alfred'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
import contacts from 'node-mac-contacts'
import { CACHE_CONTACTS_KEY, CACHE_TTL } from '@common/constants.js'
import { AuthStatus } from '@models/auth-status.enum.js'
import type { IContact } from '@models/contact.model.js'
import { CACHE_CONTACTS_KEY, CACHE_TTL } from '@common/constants'
import { AuthStatus } from '@models/auth-status.enum'
import type { IContact } from '@models/contact.model'

export function isAuth(): boolean {
const status: AuthStatus = contacts.getAuthStatus()
Expand Down
4 changes: 2 additions & 2 deletions src/services/platform-url-builder.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ContactPayload } from '@models/contact-payload.model.js'
import type { SupportedPlatform } from '@models/platform.model.js'
import type { ContactPayload } from '@models/contact-payload.model'
import type { SupportedPlatform } from '@models/platform.model'

const PLATFORMS_URLS: Record<SupportedPlatform, (referrer: string) => string> = {
whatsapp: (referrer: string) => `whatsapp://send?phone=${referrer}`,
Expand Down
2 changes: 1 addition & 1 deletion src/services/search.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IContact } from '@models/contact.model.js'
import type { IContact } from '@models/contact.model'

type SearchField = keyof IContact
export const SEARCH_FIELDS_CONFIG: SearchField[] = ['firstName', 'lastName', 'phoneNumbers', 'emailAddresses']
9 changes: 5 additions & 4 deletions src/services/search.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Fuse from 'fuse.js'
import type { IContact } from '@models/contact.model.js'
import { SEARCH_FIELDS_CONFIG } from './search.config.js'
import type { IContact } from '@models/contact.model'
import { SEARCH_FIELDS_CONFIG } from './search.config'

export async function searchContacts(contacts: IContact[], searchTerm: string, limit: number): Promise<IContact[]> {
const Fuse = (await import('fuse.js/min-basic')).default

export function searchContacts(contacts: IContact[], searchTerm: string, limit: number): IContact[] {
const fuse = new Fuse(contacts, {
keys: SEARCH_FIELDS_CONFIG,
isCaseSensitive: false,
Expand Down

0 comments on commit 9808170

Please sign in to comment.