v-transform 3.0.1 → 3.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
@@ -4,7 +4,7 @@
4
4
  "vt": "src/index.js"
5
5
  },
6
6
  "type": "module",
7
- "version": "3.0.1",
7
+ "version": "3.1.0",
8
8
  "main": "index.js",
9
9
  "scripts": {
10
10
  "clean": "npx rimraf test",
package/src/dump.js CHANGED
@@ -15,11 +15,11 @@ function jp(data, space) {
15
15
  return `JSON.parse(\n\t'${json}'\n)`
16
16
  }
17
17
 
18
- function map(raw, pk, space) {
19
- const isArray = !pk || Array.isArray(raw)
20
- if (isArray || typeof raw !== 'object') {
21
- return { data: jp(raw, space), type: isArray ? 'array' : 'object' }
18
+ function map(raw, pk, space, enable) {
19
+ if (Array.isArray(raw)) {
20
+ return { data: jp(raw, space), type: 'array' }
22
21
  }
22
+ if (!pk || !enable) return { data: jp(raw, space), type: 'object' }
23
23
  const data = Object.values(raw).map(v => [v[pk], v])
24
24
  console.info(`🔁 [Dump] Object -> Map [${pk}]`)
25
25
  return { data: `new Map(${jp(data, space)})`, type: 'map' }
@@ -33,28 +33,28 @@ function withType(main, sub, pk) {
33
33
  if (sub === 'any') return `Map<${sub}, ${sub}>`
34
34
  return `Map<${sub}['${pk}'], ${sub}>`
35
35
  case 'object':
36
- return `{ [key: string]: ${sub} }`
36
+ return `Record<${sub}['${pk}'], ${sub}>`
37
37
  default:
38
38
  return sub
39
39
  }
40
40
  }
41
41
 
42
- function jsonify(name, data, space) {
42
+ function jsonify({ name, data, space }) {
43
43
  const { raw, addition, dts } = data
44
44
  const json = j(addition ? { [name]: raw, ...addition } : raw, space)
45
45
  return [json, dts]
46
46
  }
47
47
 
48
- function yamlify(name, data, indent) {
48
+ function yamlify({ name, data, space }) {
49
49
  const { raw, addition, dts } = data
50
- const opt = { indent: indent || undefined }
50
+ const opt = { indent: space || undefined }
51
51
  const yaml = dumpYAML(addition ? { [name]: raw, ...addition } : raw, opt)
52
52
  return [yaml, dts]
53
53
  }
54
54
 
55
- function cjsify(name, data, space, types) {
55
+ function cjsify({ name, data, space, types, toMap }) {
56
56
  const { type, pk, dts, addition } = data
57
- const converted = map(data.raw, pk, space)
57
+ const converted = map(data.raw, pk, space, toMap)
58
58
  const r = []
59
59
  if (types && dts) {
60
60
  r.push(`/** @typedef {import('./${name}').${type}} ${type} */`)
@@ -72,9 +72,9 @@ function cjsify(name, data, space, types) {
72
72
  return [r.join('\n'), dts]
73
73
  }
74
74
 
75
- function esmify(name, data, space, types) {
75
+ function esmify({ name, data, space, types, toMap }) {
76
76
  const { type, pk, dts, addition } = data
77
- const converted = map(data.raw, pk, space)
77
+ const converted = map(data.raw, pk, space, toMap)
78
78
  const rows = []
79
79
  if (types && dts) {
80
80
  rows.push(`/** @typedef {import('./${name}').${type}} ${type} */`)
@@ -90,9 +90,9 @@ function esmify(name, data, space, types) {
90
90
  return [rows.join('\n'), dts]
91
91
  }
92
92
 
93
- function tsify(name, data, space) {
93
+ function tsify({ name, data, space, toMap }) {
94
94
  const { pk, addition } = data
95
- const converted = map(data.raw, pk, space)
95
+ const converted = map(data.raw, pk, space, toMap)
96
96
  const type = withType(converted.type, data.type, pk)
97
97
  const rows = [data.dts]
98
98
  rows.push(`export const ${name} = ${converted.data} as unknown as ${type};`)
@@ -122,7 +122,7 @@ async function write(sheet, data) {
122
122
  await writeFile(sheet, data)
123
123
  }
124
124
 
125
- export async function dump({ sheet, data, type, space, name, ext, types }) {
125
+ export async function dump({ sheet, type, ext, map, types, ...args }) {
126
126
  let e, ify
127
127
  switch (type) {
128
128
  case 'ts':
@@ -151,29 +151,8 @@ export async function dump({ sheet, data, type, space, name, ext, types }) {
151
151
  break
152
152
  }
153
153
 
154
- const [main, dts] = ify(name, data, space, types)
154
+ const [main, dts] = ify({ ...args, types, toMap: map })
155
155
  const result = await write(`${sheet}${ext || e}`, main)
156
156
  if (types && dts) await write(`${sheet}.d.ts`, dts)
157
-
158
- // if (type !== 'ts' && data?.extra?.types) {
159
- // const { types, typeSign } = data.extra
160
- // const cleanTypes = String(types).trim() + '\n'
161
-
162
- // const dtsLines = [
163
- // cleanTypes,
164
- // `export declare const ${name}: ${typeSign};`,
165
- // `export default ${name};`,
166
- // ]
167
-
168
- // if (data.addition) {
169
- // Object.entries(data.addition).forEach(([key, value]) => {
170
- // dtsLines.push(`export declare const ${key}: ${typeof value};`)
171
- // })
172
- // }
173
-
174
- // const dtsContent = dtsLines.join('\n') + '\n'
175
- // await write(`${sheet}.d.ts`, dtsContent)
176
- // }
177
-
178
157
  return result
179
158
  }
package/src/index.js CHANGED
@@ -22,6 +22,7 @@ program
22
22
  .option('-e, --ext <ext>', 'file ext', null)
23
23
  .option('-a, --addition', 'addition version', false)
24
24
  .option('-x, --notypes', 'disable types generation', false)
25
+ .option('-m, --map', 'enable map generation', false)
25
26
  .action((list, options) => {
26
27
  if (!options.dest) options.dest = options.output
27
28
  options.list = list
package/src/loader.js CHANGED
@@ -4,14 +4,15 @@ import path from 'node:path'
4
4
  import { readFile } from 'node:fs/promises'
5
5
 
6
6
  export async function load(args) {
7
- const { config, list, addition, ext } = args
7
+ const { config, list, ...a } = args
8
8
  const version = new Date().toISOString()
9
9
  const type = args.type || 'json'
10
10
  const cwd = args.cwd || process.cwd()
11
11
  const space = Number(args.space) || 0
12
12
  const dest = args.dest || cwd
13
13
  const types = !args.notypes
14
- const def = { cwd, type, space, dest, addition, ext, types }
14
+ const map = args.map || false
15
+ const def = { ...a, cwd, type, space, dest, types, map }
15
16
  const m = (...l) => globit(Object.assign({}, ...l))
16
17
  const cfgs = []
17
18
 
package/src/transform.js CHANGED
@@ -13,8 +13,8 @@ export async function transform(options) {
13
13
  }
14
14
 
15
15
  async function task(options) {
16
- const { files, dest, cwd, type, space, addition, ext, types } = options
17
16
  console.info('Transform task config:', options)
17
+ const { files, dest, cwd, addition, ...opts } = options
18
18
  const m = new Map()
19
19
  for (const file of files) {
20
20
  const dir = path.resolve(dest, path.dirname(file))
@@ -22,8 +22,7 @@ async function task(options) {
22
22
  }
23
23
  for (const [sheet, job] of m) {
24
24
  const data = await job.result(addition)
25
- const d = { sheet, data, type, space, name: job.name, ext, types }
26
- await dump(d)
25
+ await dump({ ...opts, sheet, data, name: job.name })
27
26
  }
28
27
  }
29
28