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

enhance: use flag-icons for country flags #3848

Merged
merged 29 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
63a0365
chore(deps): add `flag-icons` as dependency
marcelgerber Aug 5, 2024
258e51b
enhance(regions): add shortCode for Kosovo
marcelgerber Aug 5, 2024
efc7c15
enhance(countries): improve flag styles
marcelgerber Aug 5, 2024
c55673d
feat(flags): script to update flags from `flag-icons`
marcelgerber Aug 5, 2024
dc33515
enhance: update flags
marcelgerber Aug 5, 2024
5715daa
refactor: handle Kosovo specially
marcelgerber Aug 5, 2024
68fa5f4
docs(flags): readme
marcelgerber Aug 5, 2024
7fe64c7
enhance(flags): include flags for unlisted countries
marcelgerber Aug 6, 2024
f8e6178
enhance(flags): include flags for non-countries, if available
marcelgerber Aug 6, 2024
de4c3d6
🐝 update KGZ.svg
ikesau Aug 6, 2024
b9f18cb
🐝 update KIR.svg
ikesau Aug 6, 2024
4b831d3
🐝 update KWT.svg
ikesau Aug 6, 2024
0a79f56
🐝 update ERI.svg
ikesau Aug 6, 2024
204e885
🐝 update GIN.svg
ikesau Aug 6, 2024
edba4ad
🐝 update MDA.svg
ikesau Aug 6, 2024
c25d24a
🐝 update MHL.svg
ikesau Aug 6, 2024
d0a75bc
🐝 update MOZ.svg
ikesau Aug 6, 2024
5afd9b7
🐝 update MLI.svg
ikesau Aug 6, 2024
3089333
🐝 update NAM.svg
ikesau Aug 6, 2024
7c8535e
🐝 update SVN.svg
ikesau Aug 6, 2024
53dcf1b
🐝 update TGO.svg
ikesau Aug 6, 2024
eaffd9d
🐝 update VCT.svg
ikesau Aug 6, 2024
168d079
🐝 update BFA.svg
ikesau Aug 6, 2024
0ed9e80
🐝 update BHS.svg
ikesau Aug 6, 2024
c463ff2
🐝 update BWA.svg
ikesau Aug 6, 2024
50293d0
🐝 update CAF.svg
ikesau Aug 6, 2024
5c4fbed
🐝 update DMA.svg
ikesau Aug 6, 2024
01f55ca
chore(flags): add svgo script
marcelgerber Aug 6, 2024
44afdad
chore(flags): run svgo on flags
marcelgerber Aug 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions devTools/flagUpdater/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Update the flags we have in [public/images/flags](../../public/images/flags).

Flags are copied over from the `4x3` variants of [the `flag-icons` repo](https://github.com/lipis/flag-icons) ([gallery](https://flagicons.lipis.dev/)).
It is probably a good idea to run `yarn up flag-icons` beforehand.

## Run

To run this, run `npx tsx ./devTools/flagUpdater/update.ts`.
18 changes: 18 additions & 0 deletions devTools/flagUpdater/svgo.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
js2svg: { indent: 2, pretty: true },
plugins: [
{
name: "preset-default",
params: {
overrides: {
cleanupIds: false,
},
},
},
"convertStyleToAttrs",
"removeDimensions",
"removeScriptElement",
"removeStyleElement",
"sortAttrs",
],
}
2 changes: 2 additions & 0 deletions devTools/flagUpdater/svgo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
npx svgo --config svgo.config.js ../../public/images/flags
77 changes: 77 additions & 0 deletions devTools/flagUpdater/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Region, regions } from "@ourworldindata/utils"

import path from "path"
import fs from "fs-extra"
import { glob } from "glob"
import findProjectBaseDir from "../../settings/findBaseDir.ts"

const BASE_DIR = findProjectBaseDir(__dirname)
if (!BASE_DIR) throw new Error("Could not find project base directory")
const FLAG_BASE_PATH = path.join(BASE_DIR, "node_modules/flag-icons/flags/4x3")
const FLAG_TARGET_DIR = path.join(BASE_DIR, "public/images/flags")

const main = async () => {
const skippedBecauseMissingShortCode: Region[] = []
const failedBecauseNoFlag: Region[] = []
let successfulCount: number = 0

for (const f of await glob(`${FLAG_TARGET_DIR}/*.svg`)) {
await fs.remove(f)
}

await fs.ensureDir(FLAG_TARGET_DIR)

for (const region of regions) {
// We want to ensure we have a flag for every non-historical country; others are optional
const isNonHistoricalCountry =
region.regionType === "country" && !region.isHistorical

let shortCode = "shortCode" in region && region.shortCode

if (region.code === "OWID_KOS") {
// Kosovo is a special case; it doesn't have an official ISO code,
// but has been assigned the special "XK" and has a flag under that code
shortCode = "XK"
} else if (region.code === "PS_GZA") {
// Gaza Strip and Palestine use the same flag
shortCode = "PS"
}

if (!shortCode) {
if (isNonHistoricalCountry)
skippedBecauseMissingShortCode.push(region)
continue
}

const flagPath = path.join(
FLAG_BASE_PATH,
`${shortCode.toLowerCase()}.svg`
)
const exists = await fs.pathExists(flagPath)
if (!exists) {
if (isNonHistoricalCountry) failedBecauseNoFlag.push(region)
continue
}

const targetPath = path.join(FLAG_TARGET_DIR, `${region.code}.svg`)
await fs.copy(flagPath, targetPath)
successfulCount++
}

console.log(`Successfully copied ${successfulCount} flags.`)

if (skippedBecauseMissingShortCode.length > 0) {
console.log(
`Skipped ${skippedBecauseMissingShortCode.length} countries because they had no short code:`,
skippedBecauseMissingShortCode.map((c) => c.name)
)
}
if (failedBecauseNoFlag.length > 0) {
console.log(
`Failed to copy flags for ${failedBecauseNoFlag.length} countries because the flag was missing:`,
failedBecauseNoFlag.map((c) => c.name)
)
}
}

main()
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-react": "^7.34.0",
"eslint-plugin-react-hooks": "^4.6.0",
"flag-icons": "^7.2.3",
"http-server": "^14.1.1",
"husky": "^9.0.11",
"jest": "^29.7.0",
Expand Down
189 changes: 185 additions & 4 deletions public/images/flags/ABW.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading