@jsonic/multisource 0.0.8 → 0.3.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/multisource.ts DELETED
@@ -1,152 +0,0 @@
1
- /* Copyright (c) 2021 Richard Rodger, MIT License */
2
- /* $lab:coverage:off$ */
3
- 'use strict'
4
-
5
- import { Jsonic, Plugin, Rule, RuleSpec, Context, util } from 'jsonic'
6
-
7
- // TODO: get package sub file refs working with ts
8
- import { makeMemResolver } from './resolver/mem'
9
- import { makeFileResolver } from './resolver/file'
10
-
11
- //import { Json } from './json'
12
- //import { Csv } from './csv'
13
- /* $lab:coverage:on$ */
14
-
15
-
16
- // TODO: .jsonic suffix optional
17
- // TODO: jsonic-cli should provide basepath
18
- // TODO: auto load index.jsonic, index.<folder-name>.jsonic
19
-
20
-
21
- let DEFAULTS = {
22
- markchar: '@',
23
- }
24
-
25
-
26
- interface Meta {
27
- path?: string // Base path for this parse run.
28
- deps?: DependencyMap // Provide an empty object to be filled.
29
- }
30
-
31
-
32
- interface Resolution {
33
- path: string // Original path (possibly relative)
34
- full: string // Normalized full path
35
- base: string // Current base path
36
- src?: string // Undefined if no resolution
37
- }
38
-
39
-
40
- type Resolver = (path: string, ctx?: Context) => Resolution
41
-
42
-
43
- interface Dependency {
44
- tar: string | typeof TOP, // Target that depends on source (src).
45
- src: string, // Source that target (tar) depends on.
46
- wen: number, // Time of resolution.
47
- }
48
-
49
- type DependencyMap = {
50
- [tar_full_path: string]: {
51
- [src_full_path: string]: Dependency
52
- }
53
- }
54
-
55
-
56
- const TOP = Symbol('TOP')
57
-
58
-
59
- let MultiSource: Plugin = function multisource(jsonic: Jsonic) {
60
- let popts = util.deep({}, DEFAULTS, jsonic.options.plugin.multisource)
61
- let markchar = popts.markchar
62
- let resolver = (popts.resolver as Resolver)
63
- let tn = '#T<' + markchar + '>'
64
-
65
- jsonic.options({
66
- token: {
67
- [tn]: { c: markchar }
68
- },
69
- error: {
70
- multifile_unsupported_file: 'unsupported file: $path'
71
- },
72
- hint: {
73
- multifile_unsupported_file:
74
- `This file type is not supported and cannot be parsed: $path.`,
75
- },
76
- })
77
-
78
- // These inherit previous plugins - they are not clean new instances.
79
- //let json = jsonic.make().use(Json, jsonic.options.plugin.json || {})
80
- //let csv = jsonic.make().use(Csv, jsonic.options.plugin.csv || {})
81
-
82
- let ST = jsonic.token.ST
83
- let AT = jsonic.token(tn)
84
-
85
-
86
- jsonic.rule('val', (rs: RuleSpec) => {
87
- rs.def.open.push(
88
- { s: [AT, ST] }, // NOTE: must use strings to specify path: @"...path..."
89
- )
90
-
91
- let orig_bc = rs.def.bc
92
- rs.def.bc = function(rule: Rule, ctx: Context) {
93
- if (rule.open[0] && AT === rule.open[0].tin) {
94
-
95
- // console.log('MS res meta', ctx.meta)
96
-
97
- let val: any = undefined
98
- let path = rule.open[1].val
99
- let res = resolver(path, ctx)
100
-
101
- if (null != res.src) {
102
- let msmeta: Meta = ctx.meta.multisource || {}
103
- let meta = {
104
- ...ctx.meta,
105
- multisource: {
106
- ...msmeta,
107
- path: res.full
108
- }
109
- }
110
-
111
- // console.log('MSMETA path', path, res.full)
112
-
113
-
114
- val = jsonic(res.src, meta)
115
-
116
- if (msmeta.deps) {
117
- let depmap = (msmeta.deps as DependencyMap)
118
- let parent = (msmeta.path || TOP) as string
119
- if (null != parent) {
120
- (depmap[parent] = depmap[parent] || {})[res.full] = {
121
- tar: parent,
122
- src: res.full,
123
- wen: Date.now()
124
- }
125
- }
126
- }
127
-
128
- }
129
-
130
- rule.open[0].val = val
131
- }
132
- return orig_bc(...arguments)
133
- }
134
-
135
- return rs
136
- })
137
- }
138
-
139
-
140
- export {
141
- MultiSource,
142
- Resolver,
143
- Resolution,
144
- TOP,
145
-
146
- // Re-exported from jsonic for convenience
147
- Context,
148
-
149
- // TODO: remove for better tree shaking
150
- makeFileResolver,
151
- makeMemResolver,
152
- }
package/resolver/file.ts DELETED
@@ -1,49 +0,0 @@
1
-
2
- import Fs from 'fs'
3
- import Path from 'path'
4
-
5
-
6
-
7
- import { Context } from 'jsonic'
8
- import { Resolver, Resolution } from '../multisource'
9
-
10
-
11
- function makeFileResolver(): Resolver {
12
-
13
- return function FileResolver(path: string, ctx?: Context): Resolution {
14
- let msmeta = ctx && ctx.meta && ctx.meta.multisource || {}
15
- let popts = ctx && ctx.opts && ctx.opts &&
16
- ctx.opts.plugin && ctx.opts.plugin.multisource || {}
17
-
18
- let basefile =
19
- null == msmeta.path ?
20
- null == popts.path ?
21
- path : popts.path : msmeta.path
22
-
23
- let fstats = Fs.statSync(basefile)
24
- let basepath = basefile
25
-
26
- if (fstats.isFile()) {
27
- let basedesc = Path.parse(basefile)
28
- basepath = basedesc.dir
29
- }
30
-
31
- let isabsolute = Path.isAbsolute(path)
32
- let fullpath = isabsolute ? path :
33
- (null == basepath ? path : Path.resolve(basepath, path))
34
-
35
- let src = Fs.readFileSync(fullpath).toString()
36
-
37
- return {
38
- path: path,
39
- full: fullpath,
40
- base: basepath,
41
- src,
42
- }
43
- }
44
- }
45
-
46
-
47
- export {
48
- makeFileResolver
49
- }
package/resolver/mem.ts DELETED
@@ -1,36 +0,0 @@
1
-
2
-
3
- import { Context } from 'jsonic'
4
- import { Resolver, Resolution } from '../multisource'
5
-
6
-
7
- function makeMemResolver(map: { [fullpath: string]: string }): Resolver {
8
-
9
- return function MemResolver(path: string, ctx?: Context): Resolution {
10
- let msmeta = ctx && ctx.meta && ctx.meta.multisource || {}
11
- let popts = ctx && ctx.opts && ctx.opts &&
12
- ctx.opts.plugin && ctx.opts.plugin.multisource || {}
13
-
14
- let basepath = null == msmeta.path ? popts.path : msmeta.path
15
-
16
- let isabsolute = path.startsWith('/')
17
- let fullpath =
18
- isabsolute ? path : (null == basepath ? '' : basepath) + '/' + path
19
-
20
- //console.log('MEM', path, basepath, isabsolute, fullpath)
21
-
22
- let src = map[fullpath]
23
-
24
- return {
25
- path: path,
26
- full: fullpath,
27
- base: basepath,
28
- src,
29
- }
30
- }
31
- }
32
-
33
-
34
- export {
35
- makeMemResolver
36
- }