@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/dist/model.js CHANGED
@@ -11,31 +11,34 @@ const config_1 = require("./lib/config");
11
11
  const watch_1 = require("./lib/watch");
12
12
  const model_1 = require("./lib/builder/model");
13
13
  const local_1 = require("./lib/builder/local");
14
- const intern = makeIntern();
15
14
  class Model {
16
15
  constructor(spec) {
17
16
  this.trigger_model = false;
17
+ const self = this;
18
18
  // Config is a special Watch to handle model config.
19
- this.config = intern.makeConfig(spec, {
19
+ this.config = makeConfig(spec, {
20
20
  path: '/',
21
- build: async (build) => {
22
- // console.log('CONFIG BUILD', build.model)
23
- var _a, _b;
21
+ build: async function trigger_model(build, ctx) {
22
+ var _a, _b, _c;
23
+ if ('post' !== ctx.step) {
24
+ return { ok: true };
25
+ }
24
26
  let res;
25
- if (this.trigger_model) {
27
+ // console.log('TRIGGER', build.id, self.trigger_model, ctx)
28
+ // console.log(new Error().stack)
29
+ if (self.trigger_model) {
26
30
  // TODO: fix!!!
27
- this.build.use.config.watch.last.build = build;
28
- res = this.watch.run(true);
31
+ self.build.use.config.watch.last.build = build;
32
+ res = self.watch.run(true);
29
33
  }
30
34
  else {
31
- this.trigger_model = true;
35
+ self.trigger_model = true;
32
36
  res = { ok: true };
33
37
  }
34
- const watchmap = (_b = (_a = build.model.sys) === null || _a === void 0 ? void 0 : _a.model) === null || _b === void 0 ? void 0 : _b.watch;
35
- // console.log('WATCHMAP', watchmap)
38
+ const watchmap = (_c = (_b = (_a = build.model) === null || _a === void 0 ? void 0 : _a.sys) === null || _b === void 0 ? void 0 : _b.model) === null || _c === void 0 ? void 0 : _c.watch;
36
39
  if (watchmap) {
37
40
  Object.keys(watchmap).map((file) => {
38
- this.watch.add(file);
41
+ self.watch.add(file);
39
42
  });
40
43
  }
41
44
  return res;
@@ -46,7 +49,7 @@ class Model {
46
49
  src: spec.src,
47
50
  path: spec.path,
48
51
  base: spec.base,
49
- use: { config: this.config },
52
+ use: { config: self.config },
50
53
  res: [
51
54
  {
52
55
  path: '/',
@@ -59,46 +62,43 @@ class Model {
59
62
  ],
60
63
  require: spec.require
61
64
  };
62
- this.watch = new watch_1.Watch(this.build);
65
+ this.watch = new watch_1.Watch(self.build);
63
66
  }
64
67
  async run() {
65
68
  this.trigger_model = false;
66
- let br = await this.config.run();
69
+ const br = await this.config.run();
67
70
  return br.ok ? this.watch.run(true) : br;
68
71
  }
69
72
  async start() {
70
73
  this.trigger_model = false;
71
- await this.config.start();
72
- return this.watch.start();
74
+ const br = await this.config.start();
75
+ return br.ok ? this.watch.start() : br;
73
76
  }
74
77
  async stop() {
75
78
  return this.watch.stop();
76
79
  }
77
80
  }
78
81
  exports.Model = Model;
79
- function makeIntern() {
80
- return {
81
- makeConfig(spec, trigger_model_build) {
82
- let cbase = spec.base + '/.model-config';
83
- let cpath = cbase + '/model-config.jsonic';
84
- // Build should load file
85
- let src = fs_1.default.readFileSync(cpath).toString();
86
- let cspec = {
87
- src: src,
88
- path: cpath,
89
- base: cbase,
90
- res: [
91
- // Generate full config model and save as a file.
92
- {
93
- path: '/',
94
- build: model_1.model_builder
95
- },
96
- trigger_model_build
97
- ],
98
- require: spec.require
99
- };
100
- return new config_1.Config(cspec);
101
- }
82
+ function makeConfig(spec, trigger_model_build) {
83
+ let cbase = spec.base + '/.model-config';
84
+ let cpath = cbase + '/model-config.jsonic';
85
+ // Build should load file
86
+ let src = fs_1.default.readFileSync(cpath).toString();
87
+ let cspec = {
88
+ src: src,
89
+ path: cpath,
90
+ base: cbase,
91
+ res: [
92
+ // Generate full config model and save as a file.
93
+ {
94
+ path: '/',
95
+ build: model_1.model_builder
96
+ },
97
+ // Trigger main model build.
98
+ trigger_model_build
99
+ ],
100
+ require: spec.require
102
101
  };
102
+ return new config_1.Config(cspec);
103
103
  }
104
104
  //# sourceMappingURL=model.js.map
package/dist/model.js.map CHANGED
@@ -1 +1 @@
1
- {"version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10
- path?: string
11
- build?: Build
12
- builders?: BuildResult[]
13
- err?: any[]
14
- }
15
-
16
- interface BuildAction {
17
- path: string
18
- build: Builder
19
- }
20
-
21
- type Builder = (
22
- build: Build
23
- ) => Promise<BuildResult>
24
-
25
-
26
- interface Spec {
27
- src: string
28
- path?: string
29
- base?: string
30
- res?: BuildAction[]
31
- require?: any
32
- use?: { [name: string]: any }
33
- }
7
+ import type {
8
+ Build,
9
+ BuildResult,
10
+ BuildAction,
11
+ Builder,
12
+ BuildContext,
13
+ Spec,
14
+ } from './types'
34
15
 
35
16
 
36
17
 
37
18
 
38
- class Build {
39
- src: string
40
- base: string
41
- path: string
42
- root: Val = Nil.make()
43
- opts: { [key: string]: any }
44
- res: BuildAction[]
45
- spec: Spec
19
+ class BuildImpl implements Build {
20
+ id
21
+ src
22
+ base
23
+ path
24
+ root: any = Nil.make()
25
+ opts: any
26
+ res
27
+ spec
46
28
  model: any
47
- use: { [name: string]: any } = {}
29
+ use = {}
48
30
  err: any[] = []
31
+ ctx: BuildContext
49
32
 
50
33
 
51
34
  constructor(spec: Spec) {
35
+ this.id = String(Math.random()).substring(3, 15)
36
+
52
37
  this.spec = spec
53
38
  this.src = spec.src
54
39
  this.base = null == spec.base ? '' : spec.base
55
40
  this.path = null == spec.path ? '' : spec.path
56
41
  this.opts = {}
42
+ this.ctx = { step: 'pre', state: {} }
57
43
 
58
44
  if (null != spec.base) {
59
45
  this.opts.base = spec.base
@@ -71,19 +57,36 @@ class Build {
71
57
 
72
58
 
73
59
  async run(): Promise<BuildResult> {
74
- // console.log('BUILD RUN', this.path)
75
-
76
60
  let hasErr = false
77
61
 
78
- this.root = Aontu(this.src, this.opts)
79
- hasErr = this.root.err && 0 < this.root.err.length
62
+ this.ctx.step = 'pre'
63
+ const brlog = []
80
64
 
81
- if (hasErr) {
82
- this.err.push(...this.root.err)
65
+ for (let builder of this.res) {
66
+ try {
67
+ let br = await builder.build(this, this.ctx)
68
+ br.step = this.ctx.step
69
+ br.path = builder.path
70
+ br.builder = builder.build.name
71
+ brlog.push(br)
72
+ }
73
+ catch (e: any) {
74
+ this.err.push(e)
75
+ hasErr = true
76
+ break
77
+ }
83
78
  }
84
79
 
85
80
 
86
- let brlog = []
81
+ if (!hasErr) {
82
+ this.root = Aontu(this.src, this.opts)
83
+ hasErr = this.root.err && 0 < this.root.err.length
84
+
85
+ if (hasErr) {
86
+ this.err.push(...this.root.err)
87
+ }
88
+ }
89
+
87
90
 
88
91
  if (!hasErr) {
89
92
  let genctx = new Context({ root: this.root })
@@ -94,32 +97,39 @@ class Build {
94
97
  this.err.push(...genctx.err)
95
98
  }
96
99
  else {
97
-
98
100
  // TODO: only call if path value has changed
101
+
102
+ this.ctx.step = 'post'
103
+
99
104
  for (let builder of this.res) {
100
105
  try {
101
- let br = await builder.build(this)
106
+ let br = await builder.build(this, this.ctx)
107
+ br.step = this.ctx.step
102
108
  br.path = builder.path
103
109
  br.builder = builder.build.name
104
110
  brlog.push(br)
105
111
  }
106
112
  catch (e: any) {
107
113
  this.err.push(e)
114
+ break
108
115
  }
109
116
  }
110
117
  }
111
118
  }
112
119
 
113
- return { ok: !hasErr, build: this, builders: brlog, err: this.err }
120
+ const br = { ok: !hasErr, build: this, builders: brlog, err: this.err }
121
+
122
+ return br
114
123
  }
115
124
  }
116
125
 
117
126
 
127
+ function makeBuild(spec: Spec) {
128
+ return new BuildImpl(spec)
129
+ }
130
+
118
131
  export {
119
- Build,
120
- Builder,
121
- BuildResult,
122
- BuildAction,
132
+ makeBuild,
123
133
  Spec,
124
134
  Val
125
135
  }
@@ -1,64 +1,104 @@
1
1
 
2
2
  import Path from 'path'
3
3
 
4
- import { Build, Builder } from '../build'
4
+ import type { Build, Builder, BuildContext } from '../types'
5
5
 
6
6
 
7
7
  // Runs any builders local to the repo.
8
- const local_builder: Builder = async (build: Build) => {
9
- try {
10
- // TODO: need to provide project root via build
11
- let root = Path.resolve(build.path, '..', '..')
8
+ const local_builder: Builder = async (build: Build, ctx: BuildContext) => {
9
+ ctx.state.local = (ctx.state.local || {})
10
+ let actionDefs = ctx.state.local.actionDefs
12
11
 
13
- // TODO: build should do this
14
- // console.log('LOCAL root:', root)
15
- let configbuild = build.use.config
12
+ if (null == actionDefs) {
13
+ try {
14
+ actionDefs = ctx.state.local.actionDefs = []
16
15
 
17
- let config = configbuild.watch.last.build.model
18
- // console.log('CONFIG BUILD')
19
- // console.dir(config, { depth: null })
16
+ // TODO: need to provide project root via build
17
+ let root = Path.resolve(build.path, '..', '..')
20
18
 
21
- let builders = config.sys.model.builders
22
19
 
23
- let ok = true
24
- let brlog = []
20
+ // TODO: build should do this
21
+ let configbuild = build.use.config
25
22
 
26
- // TODO: order by comma sep string
27
- for (let name in builders) {
28
- // console.log('LOCAL BUILDER', name)
23
+ let config = configbuild.watch.last.build.model
29
24
 
30
- let builder = builders[name]
31
- let action_path = Path.join(root, builder.load)
25
+ let builders = config.sys.model.builders
32
26
 
33
- // TODO: need to watch these files too, and their deps!
34
- console.log('ACTION PATH 1', name, action_path)
27
+ // TODO: order by comma sep string
28
+ // Load builders
29
+ for (let name in builders) {
30
+ let builder = builders[name]
31
+ let action_path = Path.join(root, builder.load)
35
32
 
36
- clear(action_path)
37
- let action = require(action_path)
38
- let br = await action(build.model, build)
39
- ok = ok && null != br && br.ok
40
- brlog.push(br)
33
+ clear(action_path)
34
+ let action = require(action_path)
35
+
36
+ if (action instanceof Promise) {
37
+ action = await action
38
+ }
39
+
40
+ const step = action.step || 'post'
41
+
42
+ actionDefs.push({ name, builder, action, step })
43
+ }
44
+ }
45
+ catch (e: any) {
46
+ throw e
47
+ }
48
+ }
49
+
50
+
51
+ if ('pre' === ctx.step) {
52
+ try {
53
+ let ok = true
54
+ let brlog = []
55
+
56
+ for (let actionDef of actionDefs) {
57
+ if ('pre' === actionDef.step || 'all' === actionDef.step) {
58
+ let br = await actionDef.action(build.model, build)
59
+ ok = ok && null != br && br.ok
60
+ brlog.push(br)
61
+ }
62
+ }
63
+
64
+ return { ok: true, step: ctx.step, active: true }
65
+ }
66
+ catch (e: any) {
67
+ throw e
41
68
  }
69
+ }
70
+
71
+ else if ('post' === ctx.step) {
72
+ try {
73
+ let ok = true
74
+ let brlog = []
75
+
76
+ for (let actionDef of actionDefs) {
77
+ if ('post' === actionDef.step || 'all' === actionDef.step) {
78
+ let br = await actionDef.action(build.model, build)
79
+ ok = ok && null != br && br.ok
80
+ brlog.push(br)
81
+ }
82
+ }
42
83
 
43
- return { ok: ok, brlog }
44
- } catch (e: any) {
45
- console.log('MODEL BUILD local', e)
46
- throw e
84
+ return { ok: true, step: ctx.step, active: true }
85
+ }
86
+ catch (e: any) {
87
+ throw e
88
+ }
47
89
  }
48
90
 
91
+ return { ok: false, why: 'bad-step', step: ctx.step, active: false }
49
92
  }
50
93
 
51
94
 
52
95
  // Adapted from https://github.com/sindresorhus/import-fresh - Thanks!
53
96
  function clear(path: string) {
54
97
  let filePath = require.resolve(path)
55
- // console.log('CM fp', filePath)
56
98
 
57
99
  if (require.cache[filePath]) {
58
100
  const children = require.cache[filePath].children.map(child => child.id)
59
101
 
60
- // console.log('CM-B', filePath, children)
61
-
62
102
  // Delete module from cache
63
103
  delete require.cache[filePath]
64
104
 
@@ -71,8 +111,6 @@ function clear(path: string) {
71
111
  if (require.cache[filePath] && require.cache[filePath].parent) {
72
112
  let i = require.cache[filePath].parent.children.length
73
113
 
74
- // console.log('CM-A', filePath, require.cache[filePath].parent.children)
75
-
76
114
  while (i--) {
77
115
  if (require.cache[filePath].parent.children[i].id === filePath) {
78
116
  require.cache[filePath].parent.children.splice(i, 1)
@@ -3,11 +3,14 @@ import Path from 'path'
3
3
 
4
4
  import { writeFile } from 'fs/promises'
5
5
 
6
- import { Build, Builder } from '../build'
6
+ import type { Build, Builder, BuildContext } from '../types'
7
7
 
8
8
 
9
9
  // Builds the main model file, after unification.
10
- const model_builder: Builder = async (build: Build) => {
10
+ const model_builder: Builder = async (build: Build, ctx: BuildContext) => {
11
+ if ('post' !== ctx.step) {
12
+ return { ok: true, step: ctx.step, active: false }
13
+ }
11
14
 
12
15
  try {
13
16
  let json = JSON.stringify(build.root.gen(), null, 2)
@@ -20,12 +23,11 @@ const model_builder: Builder = async (build: Build) => {
20
23
 
21
24
  let file = build.opts.base + '/' + filename + '.json'
22
25
 
23
- // console.log('MODEL BUILDER', file)
24
26
  await writeFile(file, json)
25
27
 
26
- return { ok: true }
27
- } catch (e: any) {
28
- console.log('MODEL BUILD model', e)
28
+ return { ok: true, step: ctx.step, active: true }
29
+ }
30
+ catch (e: any) {
29
31
  throw e
30
32
  }
31
33
  }
package/lib/config.ts CHANGED
@@ -1,17 +1,10 @@
1
- /* Copyright © 2021-2023 Voxgig Ltd, MIT License. */
1
+ /* Copyright © 2021-2024 Voxgig Ltd, MIT License. */
2
2
 
3
3
 
4
- // load config as a model
5
- // from model / config.jsonic by default
6
- // config defines builders
4
+ import type { BuildResult, Spec } from './types'
7
5
 
8
-
9
-
10
-
11
- import { BuildResult, Spec } from './build'
12
6
  import { Watch } from './watch'
13
7
 
14
-
15
8
  import { model_builder } from './builder/model'
16
9
 
17
10
 
@@ -29,10 +22,6 @@ class Config {
29
22
  path: spec.path,
30
23
  base: spec.base,
31
24
  res: [
32
- {
33
- path: '/',
34
- build: model_builder
35
- },
36
25
  ...(spec.res || [])
37
26
  ],
38
27
  require: spec.require
package/lib/types.ts ADDED
@@ -0,0 +1,67 @@
1
+
2
+
3
+
4
+ interface Build {
5
+ id: string
6
+ src: string
7
+ base: string
8
+ path: string
9
+ root: any
10
+ opts: { [key: string]: any }
11
+ res: BuildAction[]
12
+ spec: Spec
13
+ model: any
14
+ use: { [name: string]: any }
15
+ err: any[]
16
+ ctx: BuildContext
17
+
18
+ run: () => Promise<BuildResult>
19
+ }
20
+
21
+
22
+ interface BuildResult {
23
+ ok: boolean
24
+ builder?: string
25
+ path?: string
26
+ build?: Build
27
+ builders?: BuildResult[]
28
+ step?: string
29
+ err?: any[]
30
+ }
31
+
32
+ interface BuildAction {
33
+ path: string
34
+ build: Builder
35
+ }
36
+
37
+
38
+ interface BuildContext {
39
+ step: 'pre' | 'post'
40
+ state: Record<string, any>
41
+ }
42
+
43
+ type Builder = (
44
+ build: Build,
45
+ ctx: BuildContext,
46
+ ) => Promise<BuildResult>
47
+
48
+
49
+ interface Spec {
50
+ src: string
51
+ path?: string
52
+ base?: string
53
+ res?: BuildAction[]
54
+ require?: any
55
+ use?: { [name: string]: any }
56
+ }
57
+
58
+
59
+
60
+ export type {
61
+ Build,
62
+ BuildResult,
63
+ BuildAction,
64
+ Builder,
65
+ BuildContext,
66
+ Spec,
67
+ }