diff --git a/README.md b/README.md index 18833ad..0b0e795 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cli/emit-fluent-type-module.js b/cli/emit-fluent-type-module.js new file mode 100644 index 0000000..6dc616d --- /dev/null +++ b/cli/emit-fluent-type-module.js @@ -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 } diff --git a/cli/index.js b/cli/index.js index a9576bb..d535c51 100755 --- a/cli/index.js +++ b/cli/index.js @@ -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/**/*'] }) @@ -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!') @@ -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) } diff --git a/cli/run-fluent-typescript.js b/cli/run-fluent-typescript.js new file mode 100644 index 0000000..ba744c3 --- /dev/null +++ b/cli/run-fluent-typescript.js @@ -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 }