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

added --no-watch option #24

Merged
merged 4 commits into from
Jan 8, 2021
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ To use the patched version of `Localized`, you should add the prop `typed`. For

Finish! Now you have amazing types on your translations messages 🎉

## Flags

### --no-watch

If you just want to emit the type definition, without running the watcher, you can use the flag `--no-watch`.

For instance:

```
> fluent-typescript vanilla ./assets/locales/ --no-watcha
```

# How types are compiled

## Asymmetric translations
Expand Down
22 changes: 22 additions & 0 deletions cli/emit-fluent-type-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { buildFluentTypeModule } = require('../dist')

const emitFluentTypeModule = (fileSystemApi, typeDefinitionTarget, typeDefinitionFilepath) => {
const fluentTypeModule = buildFluentTypeModule(typeDefinitionTarget)
const typeDefinitionFilename = `${typeDefinitionFilepath}/translations.ftl.d.ts`
fileSystemApi.writeFile(
typeDefinitionFilename,
fluentTypeModule,
{ encoding: 'utf-8' },
(err) => {
if (err) {
console.log('❌ Error')
console.log(err)
return
}

console.log(`🏁 Type definition updated: ${typeDefinitionFilename}`)
}
)
}

module.exports = { emitFluentTypeModule }
51 changes: 17 additions & 34 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,12 @@

const chokidar = require('chokidar')
const fs = require('fs')
const glob = require('glob')
const { normalize } = require('path')
const { start, updateContent, buildFluentTypeModule, targetsSupported } = require('../dist')
const { updateContent, targetsSupported } = require('../dist')
const { emitFluentTypeModule } = require('./emit-fluent-type-module')
const { runFluentTypescript } = require('./run-fluent-typescript')

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

const emitFluentTypeModule = () => {
const fluentTypeModule = buildFluentTypeModule(typeDefinitionTarget)

fileSystemApi.writeFile(
typeDefinitionFilename,
fluentTypeModule,
{ encoding: 'utf-8' },
(err) => {
if (err) {
console.log('❌ Error')
console.log(err)
return
}

console.log(`🏁 Type definition updated: ${typeDefinitionFilename}`)
}
)
}

glob('**/*.ftl', { ignore: ['node_modules/**/*', '.git/**/*'] }, (errors, matches) => {
const files = matches.map(path => ({
path: normalize(path),
content: fileSystemApi.readFileSync(path, { encoding: 'utf-8' }),
}))

start(files)
emitFluentTypeModule()
})
runFluentTypescript(fileSystemApi, typeDefinitionTarget, typeDefinitionFilepath)

const watcher = chokidar.watch('**/*.ftl', { ignored: ['node_modules/**/*', '.git/**/*'] })

Expand All @@ -56,13 +27,14 @@ const startWatcher = (fileSystemApi, typeDefinitionTarget, typeDefinitionFilepat
const content = fileSystemApi.readFileSync(path, { encoding: 'utf-8' })
updateContent({ path, content })

emitFluentTypeModule()
emitFluentTypeModule(fileSystemApi, typeDefinitionTarget, typeDefinitionFilepath)
})
}

if (require.main === module) {
const typeDefinitionTarget = process.argv[2]
const typeDefinitionFilepath = process.argv[3]
const noWatchFlag = process.argv[4]

if (typeDefinitionTarget === undefined) {
console.error('❌ Error: missing argument with the target!')
Expand All @@ -82,5 +54,16 @@ if (require.main === module) {
return
}

if (noWatchFlag === '--no-watch') {
runFluentTypescript(fs, typeDefinitionTarget, typeDefinitionFilepath)
return
}

if (noWatchFlag !== undefined) {
console.error(`❌ Error: Unknown flag "${noWatchFlag}"`)
console.error('Example: fluent-typescript vanilla ./assets/locales/ --no-watch')
return
}

startWatcher(fs, typeDefinitionTarget, typeDefinitionFilepath)
}
18 changes: 18 additions & 0 deletions cli/run-fluent-typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const glob = require('glob')
const { normalize } = require('path')
const { start } = require('../dist')
const { emitFluentTypeModule } = require('./emit-fluent-type-module')

const runFluentTypescript = (fileSystemApi, typeDefinitionTarget, typeDefinitionFilepath) => {
glob('**/*.ftl', { ignore: ['node_modules/**/*', '.git/**/*'] }, (errors, matches) => {
const files = matches.map(path => ({
path: normalize(path),
content: fileSystemApi.readFileSync(path, { encoding: 'utf-8' }),
}))

start(files)
emitFluentTypeModule(fileSystemApi, typeDefinitionTarget, typeDefinitionFilepath)
})
}

module.exports = { runFluentTypescript }