katabatic 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "katabatic",
3
3
  "license": "MIT",
4
- "version": "1.0.0",
4
+ "version": "1.1.0",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
@@ -16,8 +16,12 @@
16
16
  "bin": {
17
17
  "katabatic": "src/index.js"
18
18
  },
19
+ "engines": {
20
+ "node": ">=20.0.0"
21
+ },
19
22
  "dependencies": {
20
- "@katabatic/compiler": "^1.0.0",
23
+ "@katabatic/compiler": "^1.1.0",
24
+ "@katabatic/dev-server-hmr": "^1.0.0",
21
25
  "@web/dev-server": "^0.4.6"
22
26
  }
23
27
  }
package/src/index.js CHANGED
@@ -1,32 +1,51 @@
1
1
  #! /usr/bin/env node
2
-
3
2
  import path from 'path'
4
3
  import { watch as watchDir, existsSync } from 'fs'
4
+ import { parseArgs } from 'node:util'
5
5
  import { startDevServer } from '@web/dev-server'
6
+ import { hmrPlugin } from '@katabatic/dev-server-hmr'
6
7
  import { walkDir } from './utils/files.js'
7
- import * as argv from './utils/argv.js'
8
- import { serve } from './serve.js'
9
- import { buildIndex, buildModule, buildRouter } from './build.js'
10
8
  import { hash } from './utils/misc.js'
9
+ import { printHelp, withHelp } from './utils/help.js'
10
+ import { route } from './route.js'
11
+ import { buildIndex, buildModule, buildRouter } from './build.js'
12
+
13
+ const options = {
14
+ root: { type: 'string', short: 'r', description: 'project root directory (default: .)' },
15
+ srcDir: { type: 'string', description: 'src files directory (default .)' },
16
+ outDir: { type: 'string', description: 'out files directory (default .)' },
17
+ routesDir: {
18
+ type: 'string',
19
+ description:
20
+ 'page and layout files directory when using the file system router (default ./routes)'
21
+ },
22
+ help: { type: 'boolean', short: 'h', description: 'get help' },
23
+ watch: { type: 'boolean', short: 'w', description: 'watches the file system' },
24
+ serve: { type: 'boolean', short: 's', description: 'starts the development server' },
25
+ hot: { type: 'boolean', description: 'generate code to support hot module replacement' },
26
+ rewriteRelativeImportExtensions: {
27
+ type: 'boolean',
28
+ description: 'rewrite imported katabatic files extension (.html or .ktb) in .js'
29
+ }
30
+ }
11
31
 
12
- const command = argv.command()
32
+ const { values: args } = withHelp(options, () => parseArgs({ options }))
13
33
 
14
- const rootPath = path.resolve(argv.option('root')) ?? process.cwd()
15
- const srcDirPath = path.join(rootPath, argv.option('srcDir') ?? '.')
16
- const outDirPath = path.join(rootPath, argv.option('outDir') ?? '.')
17
- const routesDirPath = path.join(srcDirPath, argv.option('routesDir') ?? './routes')
34
+ const rootPath = path.resolve(args.root ?? '.')
35
+ const srcDirPath = path.join(rootPath, args.srcDir ?? '.')
36
+ const outDirPath = path.join(rootPath, args.outDir ?? '.')
37
+ const routesDirPath = path.join(srcDirPath, args.routesDir ?? './routes')
18
38
  const hasRoutes = existsSync(routesDirPath)
19
39
 
20
- const rewriteRelativeImportExtensions = argv.option('rewriteRelativeImportExtensions') ?? false
40
+ const hot = (args.hot || args.serve) ?? false
41
+ const rewriteRelativeImportExtensions = args.rewriteRelativeImportExtensions ?? false
21
42
 
22
- switch (command) {
23
- case 'build':
24
- build()
25
- break
26
- default:
27
- watch(build())
28
- start()
29
- break
43
+ if (args.help) {
44
+ printHelp(options)
45
+ } else {
46
+ const registry = build()
47
+ if (args.watch) watch(registry)
48
+ if (args.serve) serve()
30
49
  }
31
50
 
32
51
  function build() {
@@ -76,11 +95,11 @@ function watch(registry) {
76
95
  })
77
96
  }
78
97
 
79
- function start() {
80
- const plugins = []
98
+ function serve() {
99
+ const plugins = [hmrPlugin()]
81
100
 
82
101
  if (hasRoutes) {
83
- plugins.push(serve(outDirPath))
102
+ plugins.push(route(outDirPath))
84
103
  }
85
104
 
86
105
  startDevServer({
@@ -88,7 +107,7 @@ function start() {
88
107
  rootDir: rootPath,
89
108
  port: 3000,
90
109
  watch: true,
91
- open: true,
110
+ open: false,
92
111
  nodeResolve: true,
93
112
  plugins
94
113
  },
@@ -127,6 +146,7 @@ function resolve(srcFilePath, isModule) {
127
146
  isLayout,
128
147
  routerImport,
129
148
  hash: moduleHash,
149
+ hot,
130
150
  rewriteRelativeImportExtensions,
131
151
  src: {
132
152
  filePath: srcFilePath
@@ -1,13 +1,12 @@
1
1
  import path from 'path'
2
2
 
3
- export function serve(outDirPath) {
3
+ export function route(outDirPath) {
4
4
  let router
5
5
 
6
6
  return {
7
- name: 'serve',
7
+ name: 'route',
8
8
  async serve({ request }) {
9
9
  router ??= await import(path.join(outDirPath, 'router.js'))
10
- console.log(request.url)
11
10
  return router.route(request.url)
12
11
  }
13
12
  }
@@ -0,0 +1,32 @@
1
+ export function printHelp(options, error) {
2
+ if (error) {
3
+ console.log(error.message)
4
+ console.log('')
5
+ }
6
+
7
+ console.log('usage: katabatic [options]')
8
+ console.log('')
9
+ for (const [name, config] of Object.entries(options)) {
10
+ let line = ''.padEnd(4)
11
+ line += config.short ? `-${config.short}, ` : ''
12
+ line += `--${name}`
13
+ line = line.padEnd(25)
14
+
15
+ if (line.length > 25) {
16
+ console.log(line)
17
+ line = ''.padEnd(25)
18
+ }
19
+
20
+ line += config.description ?? ''
21
+ console.log(line)
22
+ }
23
+ }
24
+
25
+ export function withHelp(options, fn) {
26
+ try {
27
+ return fn()
28
+ } catch (error) {
29
+ printHelp(options, error)
30
+ process.exit(1)
31
+ }
32
+ }
package/src/utils/argv.js DELETED
@@ -1,12 +0,0 @@
1
- export function command() {
2
- return process.argv[2]
3
- }
4
-
5
- export function option(name) {
6
- const index = process.argv.indexOf(`-${name}`)
7
-
8
- if (index >= 0) {
9
- const value = process.argv[index + 1]
10
- return value && value[0] !== '-' ? value : undefined
11
- }
12
- }