Skip to content

Commit

Permalink
added --no-watch option
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Kranz committed Jan 6, 2021
1 parent 790a8a8 commit 5e8ef19
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,11 @@ const { normalize } = require('path')
const { start, updateContent, buildFluentTypeModule, targetsSupported } = require('../dist')

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()
})
initFluent(fileSystemApi, typeDefinitionTarget, typeDefinitionFilepath)

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

Expand All @@ -48,13 +23,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 noWatchParameter = process.argv[4]

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

startWatcher(fs, typeDefinitionTarget, typeDefinitionFilepath)
if (noWatchParameter !== '--no-watch') {
startWatcher(fs, typeDefinitionTarget, typeDefinitionFilepath)
} else {
initFluent(fs, typeDefinitionTarget, typeDefinitionFilepath)
}
}

function 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}`)
}
)
}

function initFluent(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)
})
}

0 comments on commit 5e8ef19

Please sign in to comment.