@voxgig/model 2.3.0 → 3.0.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/lib/watch.ts CHANGED
@@ -3,17 +3,24 @@
3
3
  import Path from 'node:path'
4
4
 
5
5
 
6
- import { Build, BuildResult } from './build'
6
+ import type { Build, BuildResult } from './types'
7
+
8
+ import { makeBuild } from './build'
7
9
  import { FSWatcher } from 'chokidar'
8
10
 
9
11
  import { readFile } from 'fs/promises'
10
12
 
11
13
 
14
+ const print = console.log
15
+ const warn = console.warn
16
+
17
+
12
18
  class Watch {
13
19
  fsw: FSWatcher
14
20
  spec: any
15
21
  last?: BuildResult
16
22
  last_change_time: number
23
+ build: Build | undefined
17
24
 
18
25
  constructor(spec: any) {
19
26
  this.spec = spec
@@ -23,8 +30,11 @@ class Watch {
23
30
  const run = this.run.bind(this)
24
31
 
25
32
  this.fsw.on('change', () => {
33
+
34
+ // TODO: needs a much more robust queue that checks for dups,
35
+ // otherwise BuildContext state will have concurrency corruptions
26
36
  // Avoid rebuilding when, for example, TS rewrites all files in dist
27
- if (55 < Date.now() - this.last_change_time) {
37
+ if (1 < Date.now() - this.last_change_time) {
28
38
  run()
29
39
  }
30
40
  })
@@ -35,14 +45,11 @@ class Watch {
35
45
  if (!Path.isAbsolute(file)) {
36
46
  file = Path.join(this.spec.require, file)
37
47
  }
38
- // console.log('WATCH ADD', file)
39
48
  this.fsw.add(file)
40
49
  }
41
50
 
42
51
 
43
52
  update(br: BuildResult) {
44
- // console.log('BUILD RESULT', br)
45
-
46
53
  let build = (br.build as Build)
47
54
 
48
55
  let files: string[] =
@@ -62,33 +69,34 @@ class Watch {
62
69
  }
63
70
  })
64
71
 
65
- // setTimeout(() => {
66
- // console.log('WATCH', this.fsw.getWatched())
67
- // }, 100)
68
-
69
72
  }
70
73
 
71
74
 
72
- async start() {
73
- await this.run(false)
75
+ // Returns first BuildResult
76
+ async start(): Promise<BuildResult> {
77
+ return await this.run(false)
74
78
  }
75
79
 
76
80
 
77
- async run(once?: boolean) {
81
+ async run(once?: boolean): Promise<BuildResult> {
78
82
  this.last_change_time = Date.now()
79
- console.log('\n@voxgig/model', new Date(this.last_change_time))
83
+ print('\n@voxgig/model', new Date(this.last_change_time))
80
84
 
81
85
  // TODO: build spec should not have src!
82
86
  let src = (await readFile(this.spec.path)).toString()
83
87
  this.spec.src = src
84
88
 
85
- let build = new Build(this.spec)
86
- let br: BuildResult = await build.run()
89
+ this.build = this.build || makeBuild(this.spec)
90
+
91
+ // TODO: better way to do this?
92
+ this.build.src = src
93
+
94
+ let br: BuildResult = await this.build.run()
87
95
 
88
- console.log('\nFILES:\n' + this.descDeps((br as any).build.root.deps) + '\n')
96
+ print('\nFILES:\n' + this.descDeps((br as any).build.root.deps) + '\n')
89
97
 
90
98
  if (br.ok) {
91
- console.log('TOP:', Object.keys(br?.build?.model).join(', '), '\n')
99
+ print('TOP:', Object.keys(br?.build?.model).join(', '), '\n')
92
100
 
93
101
  if (!once) {
94
102
  // There may be new files.
@@ -96,7 +104,7 @@ class Watch {
96
104
  }
97
105
  }
98
106
  else {
99
- console.log('MODEL ERRORS: ' + br.err?.length)
107
+ warn('MODEL ERRORS: ' + br.err?.length)
100
108
  this.handleErrors(br)
101
109
  }
102
110
 
@@ -115,19 +123,23 @@ class Watch {
115
123
  if (br.err) {
116
124
  for (let be of br.err) {
117
125
  if (be.msg) {
118
- console.log(be.msg)
126
+ warn(be.msg)
119
127
  }
120
128
  else if (be.message) {
121
- console.log(be.message)
129
+ warn(be.message)
122
130
  }
123
131
  else {
124
- console.log(be)
132
+ warn(be)
125
133
  }
126
134
  }
127
135
  }
128
136
  }
129
137
 
130
138
  descDeps(deps: Record<string, Record<string, { tar: string }>>) {
139
+ if (null == deps) {
140
+ return ''
141
+ }
142
+
131
143
  let desc = []
132
144
  for (let entryPath of Object.keys(deps)) {
133
145
  desc.push(' ' + entryPath)
package/model.ts CHANGED
@@ -3,23 +3,16 @@
3
3
  // TODO: remove need for this
4
4
  import Fs from 'fs'
5
5
 
6
+
7
+ import type { Build, BuildResult, BuildAction, BuildContext, Spec } from './lib/types'
8
+
9
+
6
10
  import { Config } from './lib/config'
7
- import { Build, BuildAction, BuildResult, Spec } from './lib/build'
8
11
  import { Watch } from './lib/watch'
9
12
 
10
13
  import { model_builder } from './lib/builder/model'
11
14
  import { local_builder } from './lib/builder/local'
12
15
 
13
- import {
14
- dive,
15
- joins,
16
- get,
17
- pinify,
18
- camelify,
19
- } from '@voxgig/util'
20
-
21
-
22
- const intern = makeIntern()
23
16
 
24
17
  class Model {
25
18
  config: Config
@@ -30,33 +23,38 @@ class Model {
30
23
 
31
24
 
32
25
  constructor(spec: Spec) {
26
+ const self = this
33
27
 
34
28
  // Config is a special Watch to handle model config.
35
- this.config = intern.makeConfig(spec, {
29
+ this.config = makeConfig(spec, {
36
30
  path: '/',
37
- build: async (build: Build) => {
38
- // console.log('CONFIG BUILD', build.model)
31
+ build: async function trigger_model(build: Build, ctx: BuildContext) {
32
+ if ('post' !== ctx.step) {
33
+ return { ok: true }
34
+ }
39
35
 
40
36
  let res
41
37
 
42
- if (this.trigger_model) {
38
+ // console.log('TRIGGER', build.id, self.trigger_model, ctx)
39
+ // console.log(new Error().stack)
40
+
41
+ if (self.trigger_model) {
43
42
 
44
43
  // TODO: fix!!!
45
- this.build.use.config.watch.last.build = build
44
+ self.build.use.config.watch.last.build = build
46
45
 
47
- res = this.watch.run(true)
46
+ res = self.watch.run(true)
48
47
  }
49
48
  else {
50
- this.trigger_model = true
49
+ self.trigger_model = true
51
50
  res = { ok: true }
52
51
  }
53
52
 
54
- const watchmap = build.model.sys?.model?.watch
55
- // console.log('WATCHMAP', watchmap)
53
+ const watchmap = build.model?.sys?.model?.watch
56
54
 
57
55
  if (watchmap) {
58
56
  Object.keys(watchmap).map((file: string) => {
59
- this.watch.add(file)
57
+ self.watch.add(file)
60
58
  })
61
59
  }
62
60
 
@@ -70,7 +68,7 @@ class Model {
70
68
  src: spec.src,
71
69
  path: spec.path,
72
70
  base: spec.base,
73
- use: { config: this.config },
71
+ use: { config: self.config },
74
72
  res: [
75
73
  {
76
74
  path: '/',
@@ -85,60 +83,60 @@ class Model {
85
83
  }
86
84
 
87
85
 
88
- this.watch = new Watch(this.build)
86
+ this.watch = new Watch(self.build)
89
87
  }
90
88
 
89
+
91
90
  async run(): Promise<BuildResult> {
92
91
  this.trigger_model = false
93
- let br = await this.config.run()
92
+ const br = await this.config.run()
94
93
  return br.ok ? this.watch.run(true) : br
95
94
  }
96
95
 
97
- async start() {
98
- this.trigger_model = false
99
- await this.config.start()
100
96
 
101
- return this.watch.start()
97
+ async start(): Promise<BuildResult> {
98
+ this.trigger_model = false
99
+ const br = await this.config.start()
100
+ return br.ok ? this.watch.start() : br
102
101
  }
103
102
 
103
+
104
104
  async stop() {
105
105
  return this.watch.stop()
106
106
  }
107
107
  }
108
108
 
109
109
 
110
- function makeIntern() {
111
- return {
112
- makeConfig(spec: Spec, trigger_model_build: BuildAction) {
113
- let cbase = spec.base + '/.model-config'
114
- let cpath = cbase + '/model-config.jsonic'
115
-
116
- // Build should load file
117
- let src = Fs.readFileSync(cpath).toString()
110
+ function makeConfig(spec: Spec, trigger_model_build: BuildAction) {
111
+ let cbase = spec.base + '/.model-config'
112
+ let cpath = cbase + '/model-config.jsonic'
118
113
 
119
- let cspec: Spec = {
120
- src: src,
121
- path: cpath,
122
- base: cbase,
123
- res: [
114
+ // Build should load file
115
+ let src = Fs.readFileSync(cpath).toString()
124
116
 
125
- // Generate full config model and save as a file.
126
- {
127
- path: '/',
128
- build: model_builder
129
- },
117
+ let cspec: Spec = {
118
+ src: src,
119
+ path: cpath,
120
+ base: cbase,
121
+ res: [
130
122
 
131
- trigger_model_build
132
- ],
133
- require: spec.require
134
- }
123
+ // Generate full config model and save as a file.
124
+ {
125
+ path: '/',
126
+ build: model_builder
127
+ },
135
128
 
136
- return new Config(cspec)
137
- }
129
+ // Trigger main model build.
130
+ trigger_model_build
131
+ ],
132
+ require: spec.require
138
133
  }
134
+
135
+ return new Config(cspec)
139
136
  }
140
137
 
141
138
 
139
+
142
140
  export {
143
141
  Model,
144
142
  Spec,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voxgig/model",
3
- "version": "2.3.0",
3
+ "version": "3.0.0",
4
4
  "main": "dist/model.js",
5
5
  "type": "commonjs",
6
6
  "types": "dist/model.d.ts",
@@ -50,9 +50,9 @@
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/jest": "^29.5.12",
53
- "@types/node": "22.3.0",
53
+ "@types/node": "22.5.0",
54
54
  "es-jest": "^2.1.0",
55
- "esbuild": "^0.23.0",
55
+ "esbuild": "^0.23.1",
56
56
  "esbuild-jest": "^0.5.0",
57
57
  "jest": "^29.7.0",
58
58
  "typescript": "^5.5.4"