Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading