Skip to content

Commit

Permalink
feat(valiate-icu): add option to ignore html tag (#2451)
Browse files Browse the repository at this point in the history
  • Loading branch information
philibea authored Feb 7, 2025
1 parent c7bc9b4 commit f2365ce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-weeks-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scaleway/validate-icu-locales": patch
---

Add ignore tag option to validate icu
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const locales = {
'units.hours.label': '{count, plural, =0 {Hour} =1 {Hour} other {Hours}}',
'units.days.label': '{count, plural, =0 {Day} =1 {Day} other {Days}}',
'units.months.label': '{count, plural, =0 {Month} =1 {Month} other {Months}}',
'units.chevron': '<ip.coucou>',
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default {
'units.hours.label': '{count, plural, =0 {Hour} =1 {Hour} other {Hours}}',
'units.days.label': '{count, plural, =0 {Day} =1 {Day} other {Days}}',
'units.months.label': '{count, plural, =0 {Month} =1 {Month} other {Months}}',
'units.chevron': '<ip.coucou>',
} as const
26 changes: 20 additions & 6 deletions packages/validate-icu-locales/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#!/usr/bin/env node

import * as fs from 'fs/promises'
import { readFile } from 'node:fs/promises'
import { parseArgs } from 'node:util'
import type { ParseArgsConfig } from 'node:util'
import { parse } from '@formatjs/icu-messageformat-parser'
import type { ParserError } from '@formatjs/icu-messageformat-parser/error'
import { globby } from 'globby'
import { importFromString } from 'module-from-string'

const args = process.argv.slice(2)
const pattern = args[0]
const options: ParseArgsConfig['options'] = {
ignoreTag: {
type: 'boolean',
short: 'i',
default: false,
},
}

const { values, positionals } = parseArgs({ options, allowPositionals: true })

const pattern = positionals[0]

type Locales = Record<string, string>
type ErrorICU = {
Expand All @@ -29,7 +40,10 @@ const findICUErrors = (
const errors = Object.entries(locales)
.map(([key, value]) => {
try {
parse(value)
parse(value, {
// Need to cast as node doesn't allow generic to parseArgs
ignoreTag: values['ignoreTag'] as boolean,
})

return undefined
} catch (err) {
Expand Down Expand Up @@ -57,7 +71,7 @@ const readFiles = async (files: string[]): Promise<ErrorsICU> => {

if (extension === 'json') {
try {
const data = await fs.readFile(file)
const data = await readFile(file)
const jsonFile = data.toString()

const locales = JSON.parse(jsonFile) as Locales
Expand All @@ -71,7 +85,7 @@ const readFiles = async (files: string[]): Promise<ErrorsICU> => {

if (extension === 'ts' || extension === 'js') {
try {
const data = await fs.readFile(file)
const data = await readFile(file)
const javascriptFile = data.toString()

const mod: unknown = await importFromString(javascriptFile, {
Expand Down

0 comments on commit f2365ce

Please sign in to comment.