Skip to content

Commit 85c5403

Browse files
committed
cli: emit type definition module on watch start
1 parent 419d89a commit 85c5403

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

cli/index.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,38 @@
33
const chokidar = require('chokidar')
44
const fs = require('fs')
55
const glob = require('glob')
6-
const { start, buildFluentTypeModule } = require('../dist')
6+
const { start, updateContent, buildFluentTypeModule } = require('../dist')
77

88
const startWatcher = (fileSystemApi, typeDefinitionFilepath) => {
99
const typeDefinitionFilename = `${typeDefinitionFilepath}/translations.ftl.d.ts`
1010

11+
const emitFluentTypeModule = () => {
12+
const fluentTypeModule = buildFluentTypeModule()
13+
14+
fileSystemApi.writeFile(
15+
typeDefinitionFilename,
16+
fluentTypeModule,
17+
{ encoding: 'utf-8' },
18+
(err) => {
19+
if (err) {
20+
console.log('❌ Error')
21+
console.log(err)
22+
return
23+
}
24+
25+
console.log(`🏁 Type definition updated: ${typeDefinitionFilename}`)
26+
}
27+
)
28+
}
29+
1130
glob('**/*.ftl', { ignore: ['node_modules/**/*', '.git/**/*'] }, (errors, matches) => {
1231
const files = matches.map(path => ({
1332
path,
1433
content: fileSystemApi.readFileSync(path, { encoding: 'utf-8' }),
1534
}))
1635

1736
start(files)
37+
emitFluentTypeModule()
1838
})
1939

2040
const watcher = chokidar.watch('**/*.ftl', { ignored: ['node_modules/**/*', '.git/**/*'] })
@@ -25,22 +45,9 @@ const startWatcher = (fileSystemApi, typeDefinitionFilepath) => {
2545
console.log(`🔍 File was changed: ${path}`)
2646

2747
const content = fileSystemApi.readFileSync(path, { encoding: 'utf-8' })
28-
const fluentTypeModule = buildFluentTypeModule({ path, content })
29-
30-
fileSystemApi.writeFile(
31-
typeDefinitionFilename,
32-
fluentTypeModule,
33-
{ encoding: 'utf-8' },
34-
(err) => {
35-
if (err) {
36-
console.log('❌ Error')
37-
console.log(err)
38-
return
39-
}
40-
41-
console.log(`🏁 Type definition updated: ${typeDefinitionFilename}`)
42-
}
43-
)
48+
updateContent({ path, content })
49+
50+
emitFluentTypeModule()
4451
})
4552
}
4653

src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import { updateGlobalState } from './global-state'
44
const start = (files: CliFluentFile[]) =>
55
files.forEach(file => updateGlobalState({ type: 'addContent', payload: file }))
66

7-
const buildFluentTypeModule = (file: CliFluentFile) => {
7+
const updateContent = (file: CliFluentFile) =>
88
updateGlobalState({ type: 'updateContent', payload: file })
9-
const fluentTypeModule = build()
10-
return fluentTypeModule
11-
}
9+
10+
const buildFluentTypeModule = build
1211

1312
// TODO: is necessary to export as common js to be available to cli.js
1413
// but probably there are better approach to do that
15-
module.exports = { start, buildFluentTypeModule }
14+
module.exports = { start, updateContent, buildFluentTypeModule }

test/index.test.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import dedent from 'dedent-js'
2-
const { buildFluentTypeModule, start } = require('../src')
2+
const { start, updateContent, buildFluentTypeModule } = require('../src')
33

44
test('Should match the types definitions', async () => {
5-
const fixturePt = dedent`
5+
const fixturePtFirstVersion = dedent`
66
hello = Olá { $firstName }
77
how-are-you = Como você está?
88
bye = Tchau
@@ -13,13 +13,12 @@ test('Should match the types definitions', async () => {
1313
`
1414

1515
start([
16-
{ path: 'pt.ftl', content: fixturePt },
16+
{ path: 'pt.ftl', content: fixturePtFirstVersion },
1717
{ path: 'jp.ftl', content: fixtureJp },
1818
])
1919

20-
const fluentTypeModule = buildFluentTypeModule({ path: 'pt.ftl', content: fixturePt })
21-
22-
expect(fluentTypeModule).toBe(dedent`
20+
const fluentTypeModuleFirstVersion = buildFluentTypeModule()
21+
expect(fluentTypeModuleFirstVersion).toBe(dedent`
2322
// This file is automatically generated.
2423
// Please do not change this file!
2524
@@ -51,4 +50,41 @@ test('Should match the types definitions', async () => {
5150
: never
5251
)
5352
`)
53+
54+
const fixturePtSecondVersion = dedent`
55+
hello = Olá { $firstName }
56+
how-are-you = Como você está?
57+
`
58+
updateContent({ path: 'pt.ftl', content: fixturePtSecondVersion })
59+
60+
const fluentTypeModuleSecondVersion = buildFluentTypeModule()
61+
expect(fluentTypeModuleSecondVersion).toBe(dedent`
62+
// This file is automatically generated.
63+
// Please do not change this file!
64+
65+
import { FluentBundle, FluentArgument } from '@fluent/bundle'
66+
67+
type Message<T extends MessagesKey> = {
68+
id: T
69+
value: T
70+
attributes: Record<string, T>
71+
}
72+
73+
declare global {
74+
interface FluentBundleTyped extends FluentBundle {
75+
getMessage<T extends MessagesKey>(id: T): Message<T>
76+
formatPattern: <T extends MessagesKey>(...args: PatternArguments<T>) => string
77+
}
78+
}
79+
80+
type MessagesKey = 'hello' |
81+
'how-are-you'
82+
type PatternArguments<T extends MessagesKey> = (
83+
T extends 'hello'
84+
? [T, { 'firstName': FluentArgument,'lastName': FluentArgument }]:
85+
T extends 'how-are-you'
86+
? [T]
87+
: never
88+
)
89+
`)
5490
})

0 commit comments

Comments
 (0)