Skip to content

Commit

Permalink
Merge pull request #138 from svobik7/feature/116-watch
Browse files Browse the repository at this point in the history
Watch mode #133
  • Loading branch information
svobik7 authored Oct 7, 2023
2 parents 8f99874 + 2a83397 commit fbabd8b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 16 deletions.
8 changes: 8 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
# and commit this file to your remote git repository to share the goodness with others.

# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

tasks:
- init: yarn install && yarn run build
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ The requirement is to have English localization served from `/` and Czech from `
```json
{
"scripts": {
"roots": "yarn next-roots"
"roots": "yarn next-roots",
"roots:watch": "yarn next-roots -w"
}
}
```
Expand Down
10 changes: 7 additions & 3 deletions examples/basic/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1888,9 +1888,8 @@ natural-compare@^1.4.0:
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==

"next-roots@link:../..":
version "3.3.0"
dependencies:
path-to-regexp "^6.2.1"
version "0.0.0"
uid ""

next@^13.4.5:
version "13.4.19"
Expand Down Expand Up @@ -1921,6 +1920,11 @@ node-releases@^2.0.13:
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.13.tgz#d5ed1627c23e3461e819b02e57b75e4899b1c81d"
integrity sha512-uYr7J37ae/ORWdZeQ1xxMJe3NtdmqMC/JZK+geofDrkLUApKRHPd18/TxtBOJ4A0/+uUIliorNrfYV6s1b02eQ==

node-watch@^0.7.4:
version "0.7.4"
resolved "https://registry.yarnpkg.com/node-watch/-/node-watch-0.7.4.tgz#34557106948cd4b8ddff9aa3d284774004548824"
integrity sha512-RinNxoz4W1cep1b928fuFhvAQ5ag/+1UlMDV7rbyGthBIgsiEouS4kvRayvvboxii4m8eolKOIBo3OjDqbc+uQ==

normalize-path@^3.0.0, normalize-path@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"semantic-release": "semantic-release"
},
"dependencies": {
"node-watch": "^0.7.4",
"parse-typed-args": "^0.2.0",
"path-to-regexp": "^6.2.1"
},
"devDependencies": {
Expand Down
58 changes: 46 additions & 12 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env node
import nodeWatch from 'node-watch'
import parseArgs from 'parse-typed-args'
import path from 'path'
import { generateFactory } from './commands/generate'
import {
Expand All @@ -10,7 +12,21 @@ import {
import { CliError } from './errors'
import type { CliParams } from './types'

const [, , configPath = 'roots.config.js', configParams = {}] = process.argv
const cliArgs = parseArgs({
opts: {
cfgPath: {
default: 'roots.config.js',
short: 'c',
parse: String,
},
watch: {
switch: true,
short: 'w',
},
},
})(process.argv)

const { watch, cfgPath } = cliArgs.opts

const cliDefaultParams: CliParams = {
localizedDir: path.resolve(DEFAULT_LOCALIZE_DIR),
Expand All @@ -21,28 +37,46 @@ const cliDefaultParams: CliParams = {
packageDir: path.join(process.cwd(), `node_modules/${PKG_NAME}`),
}

const cliFileParams = require(path.join(process.cwd(), configPath))
const cliFileParams = require(path.join(process.cwd(), cfgPath))
const cliParams: CliParams = {
...cliDefaultParams,
...cliFileParams,
...configParams,
}

const config = getConfig(cliParams)

async function main() {
// Make sure commands gracefully respect termination signals (e.g. from Docker)
process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0))

const config = getConfig(cliParams)
const generate = generateFactory(config)

return generate()
}

main().catch((e: Error) => {
if (e instanceof CliError) {
console.error(`\x1b[31mnext-roots\x1b[37m - ${e.message}`)
} else {
console.error(e)
}
})
const run = async () =>
main().catch((e: Error) => {
if (e instanceof CliError) {
console.error(`\x1b[31mnext-roots\x1b[37m - ${e.message}`)
} else {
console.error(e)
}
})

if (watch) {
const watcher = nodeWatch(config.getOriginAbsolutePath(), { recursive: true })

watcher.on('ready', function () {
console.warn(`\x1b[33mnext-roots\x1b[37m - running in watch mode`)
run()
})

watcher.on('change', function () {
console.warn(
`\x1b[33mnext-roots\x1b[37m - origins changed, regenerating...`
)
run()
})
} else {
run()
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4611,6 +4611,11 @@ node-releases@^2.0.8:
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.10.tgz#c311ebae3b6a148c89b1813fd7c4d3c024ef537f"
integrity sha512-5GFldHPXVG/YZmFzJvKK2zDSzPKhEp0+ZR5SVaoSag9fsL5YgHbUHDfnG5494ISANDcK4KwPXAx2xqVEydmd7w==

node-watch@^0.7.4:
version "0.7.4"
resolved "https://registry.yarnpkg.com/node-watch/-/node-watch-0.7.4.tgz#34557106948cd4b8ddff9aa3d284774004548824"
integrity sha512-RinNxoz4W1cep1b928fuFhvAQ5ag/+1UlMDV7rbyGthBIgsiEouS4kvRayvvboxii4m8eolKOIBo3OjDqbc+uQ==

nopt@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/nopt/-/nopt-6.0.0.tgz#245801d8ebf409c6df22ab9d95b65e1309cdb16d"
Expand Down Expand Up @@ -5093,6 +5098,11 @@ parse-passwd@^1.0.0:
resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6"
integrity sha512-1Y1A//QUXEZK7YKz+rD9WydcE1+EuPr6ZBgKecAB8tmoW6UFv0NREVJe1p+jRxtThkcbbKkfwIbWJe/IeE6m2Q==

parse-typed-args@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/parse-typed-args/-/parse-typed-args-0.2.0.tgz#34c5c101659435cf75e9bcfa475135a29afe03d5"
integrity sha512-xFLk7SuXQuOfXF9wim9Ti65WAEUqUWAL+QZvts/4MivXcESjvU74pKkp4rLj4hS63ZrvNAvpWLxvupE4wpqCWA==

path-exists@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515"
Expand Down

1 comment on commit fbabd8b

@vercel
Copy link

@vercel vercel bot commented on fbabd8b Oct 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.