Skip to content

Commit

Permalink
feat: better typings generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dmythro committed Sep 11, 2023
1 parent 01f473a commit bc55fea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
11 changes: 11 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ declare type TCountryToString = Record<TCountryCode, string>
declare type TStringToCountry = Record<string, TCountryCode>
declare type TLanguages = Record<TLanguageCode, ILanguage>
declare type TLanguageToString = Record<TLanguageCode, string>

declare const getCountryCode: (countryName: string) => TCountryCode | false;
declare const getCountryData: (iso2: TCountryCode) => ICountryData;
declare const getCountryDataList: () => ICountryData[];
declare const getEmojiFlag: (countryCode: TCountryCode) => string;

declare const continents: TContinents
declare const countries: TCountries
declare const languages: TLanguages

export { continents, countries, getCountryCode, getCountryData, getCountryDataList, getEmojiFlag, languages };
26 changes: 20 additions & 6 deletions packages/scripts/tasks/generateTypings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,26 @@ export const generateTypings = (): void => {
const current = fs.readFileSync('../countries/src/types.ts', {
encoding: 'utf-8',
})
const typings = current
.replace(/export/g, 'declare')
.replace(/import .* from '.*'\n/g, '')
.replace('keyof typeof continents', "'" + Object.keys(continents).join("' | '") + "'")
.replace('keyof typeof countries', "'" + Object.keys(countries).join("' | '") + "'")
.replace('keyof typeof languages', "'" + Object.keys(languages).join("' | '") + "'")
const typings =
current
.replace(/export/g, 'declare')
.replace(/import .* from '.*'\n/g, '')
.replace('keyof typeof continents', "'" + Object.keys(continents).join("' | '") + "'")
.replace('keyof typeof countries', "'" + Object.keys(countries).join("' | '") + "'")
.replace('keyof typeof languages', "'" + Object.keys(languages).join("' | '") + "'") +
[
'',
'declare const getCountryCode: (countryName: string) => TCountryCode | false;',
'declare const getCountryData: (iso2: TCountryCode) => ICountryData;',
'declare const getCountryDataList: () => ICountryData[];',
'declare const getEmojiFlag: (countryCode: TCountryCode) => string;',
'',
'declare const continents: TContinents',
'declare const countries: TCountries',
'declare const languages: TLanguages',
'',
'export { continents, countries, getCountryCode, getCountryData, getCountryDataList, getEmojiFlag, languages };',
].join('\n')

saveTextFile('index.d.ts', typings.trim())
}
Expand Down

0 comments on commit bc55fea

Please sign in to comment.