Skip to content

Commit

Permalink
feat: add watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed May 28, 2024
1 parent 4aac71f commit fb26fcc
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,69 @@ After that, the `locales.types.ts` file is **automatically** generated in the `s

You can also specify the [glob pattern](<https://en.wikipedia.org/wiki/Glob_(programming)?useskin=vector>) to search for `.ftl` files are specified after the command. For example `npx fluent2ts locales/**/*.ftl`. By default it is `**/*.ftl`.

WIP.
## Watch mode

You can also use `watch mode` and regenerate the types when the file has been modified. To do this, simply specify the `--watch` (or `-w`) argument. For example, `npx fluent2ts -w`.

<video controls autoplay>
<source src="https://github.com/kravetsone/fluent2ts/assets/57632712/318350a0-318a-43a0-82f6-b70cdb431ab6" type="video/mp4">
</video>

### Output

For example for this:

```fluent
# Simple things are simple.
hello-user = Hello, {$userName}!
# Complex things are possible.
shared-photos =
{$userName} {$photoCount ->
[one] added a new photo
*[other] added {$photoCount} new photos
} to {$userGender ->
[male] his stream
[female] her stream
*[other] their stream
}.
```

The following will be generated:

```ts
import type {
FluentBundle,
FluentVariable,
Message as FluentMessage,
// @ts-ignore
} from "@fluent/bundle";

export interface LocalesMap {
"hello-user": {
userName: FluentVariable;
};
"shared-photos": {
userName: FluentVariable;
photoCount: FluentVariable;
userGender: FluentVariable;
};
}

export interface Message<Key extends keyof LocalesMap> extends FluentMessage {
id: Key;
}

export interface TypedFluentBundle extends FluentBundle {
getMessage<Key extends keyof LocalesMap>(key: Key): Message<Key>;
formatPattern<Key extends keyof LocalesMap>(
key: Key,
...args: LocalesMap[Key] extends never ? [] : [args: LocalesMap[Key]]
): string;
formatPattern<Key extends keyof LocalesMap>(
key: Key,
args: LocalesMap[Key] extends never ? null : LocalesMap[Key],
errors?: Error[] | null
): string;
}
```
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fluent2ts",
"version": "0.0.3",
"version": "0.0.4",
"module": "dist/index.js",
"type": "module",
"scripts": {
Expand All @@ -20,7 +20,7 @@
},
"dependencies": {
"@fluent/syntax": "^0.19.0",
"glob": "^10.3.12",
"glob": "^10.4.1",
"minimist": "^1.2.8",
"prettier": "^3.2.5"
},
Expand Down
21 changes: 19 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ function isPlaceable(x: PatternElement): x is Placeable & {
);
}

for await (const path of paths) {
async function processFile(path: string) {
const file = String(await fs.readFile(path));

const resource = parse(file, {});

const generated: string[] = [
`import type { FluentBundle, FluentVariable, Message as FluentMessage } from "@fluent/bundle"`,
"import type {",
" FluentBundle, ",
" FluentVariable, ",
" Message as FluentMessage ",
" // @ts-ignore",
`} from "@fluent/bundle"`,
"",
"export interface LocalesMap {",
...resource.body.filter(isMessage).map((entry) => {
Expand Down Expand Up @@ -82,3 +87,15 @@ for await (const path of paths) {
}),
);
}

for await (const path of paths) {
await processFile(path);
if (args.w || args.watch) {
const events = fs.watch(path);

for await (const event of events) {
if (event.eventType !== "change") continue;
await processFile(path);
}
}
}

0 comments on commit fb26fcc

Please sign in to comment.