v-transform 2.0.2 → 2.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,14 +4,17 @@
4
4
  "vt": "src/index.js"
5
5
  },
6
6
  "type": "module",
7
- "version": "2.0.2",
7
+ "version": "2.1.0",
8
8
  "main": "index.js",
9
9
  "scripts": {
10
10
  "clean": "npx rimraf test",
11
11
  "vt": "node src/index.js",
12
12
  "transform": "node src/index.js transform"
13
13
  },
14
- "repository": "https://github.com/VickScarlet/vTransform.git",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/VickScarlet/vTransform.git"
17
+ },
15
18
  "author": "Vick Scarlet <scarlet_vick@outlook.com>",
16
19
  "license": "MIT",
17
20
  "dependencies": {
package/src/index.js CHANGED
@@ -3,26 +3,27 @@ import { Command } from 'commander';
3
3
  import { readFile } from 'fs/promises';
4
4
  import { transform } from './transform.js';
5
5
 
6
- (async() => {
6
+ (async () => {
7
7
 
8
- const program = new Command();
8
+ const program = new Command();
9
9
 
10
- program.name('vt');
11
- program.version(JSON.parse(await readFile('package.json')).version);
12
- program
13
- .command('transform [list...]')
14
- .option('-t, --type <type>', 'type of transform, available: js, esm, cjs, json', 'json')
15
- .option('-s, --space <space>', 'format space number', 0)
16
- .option('-c, --config <config>', 'configure file', null)
17
- .option('-w, --cwd <cwd>', 'current work dir', null)
18
- .option('-o, --output <output>', 'output dir', null)
19
- .option('-d, --dest <dest>', 'dest dir', null)
20
- .action((list, options) => {
21
- if(!options.dest) options.dest = options.output;
22
- options.list = list;
23
- transform(options);
24
- });
10
+ program.name('vt');
11
+ program.version(JSON.parse(await readFile('package.json')).version);
12
+ program
13
+ .command('transform [list...]')
14
+ .option('-t, --type <type>', 'type of transform, available: js, esm, cjs, json', 'json')
15
+ .option('-s, --space <space>', 'format space number', 0)
16
+ .option('-c, --config <config>', 'configure file', null)
17
+ .option('-w, --cwd <cwd>', 'current work dir', null)
18
+ .option('-o, --output <output>', 'output dir', null)
19
+ .option('-d, --dest <dest>', 'dest dir', null)
20
+ .option('-a, --addition', 'addition version', false)
21
+ .action((list, options) => {
22
+ if (!options.dest) options.dest = options.output;
23
+ options.list = list;
24
+ transform(options);
25
+ });
25
26
 
26
- program.parse(process.argv);
27
+ program.parse(process.argv);
27
28
 
28
29
  })();
package/src/loader.js CHANGED
@@ -3,38 +3,43 @@ import yaml from 'js-yaml';
3
3
  import path from 'path';
4
4
  import { readFile } from 'fs/promises';
5
5
 
6
- export async function load({type, space, config, dest, list, cwd}) {
6
+ export async function load({ type, space, config, dest, list, cwd, addition }) {
7
+ const version = new Date().toISOString();
7
8
  type = type || 'json';
8
9
  cwd = cwd || process.cwd();
9
10
  space = Number(space) || 0;
10
11
  dest = dest || cwd;
11
- const def = {cwd, type, space, dest};
12
- const m = (...l)=>globit(Object.assign({}, ...l));
12
+ const def = { cwd, type, space, dest, addition };
13
+ const m = (...l) => globit(Object.assign({}, ...l));
13
14
  const cfgs = [];
14
15
 
15
- if(list?.length)
16
- cfgs.unshift(await m(def, {glob: list}));
16
+ if (list?.length)
17
+ cfgs.unshift(await m(def, { glob: list }));
17
18
 
18
- if(config) {
19
+ if (config) {
19
20
  const c = await loadConfig(config);
20
21
  const dir = path.dirname(config);
21
22
  const cdef = {};
22
- if(c.cwd) cdef.cwd = path.resolve(dir, c.cwd);
23
- if(c.type) cdef.type = c.type;
24
- if(typeof c.space == 'number') cdef.space = c.space;
25
- if(c.dest) cdef.dest = path.resolve(dir, c.dest);
26
- for(const cfg of c.configurations) {
27
- if(cfg.cwd) cfg.cwd = path.resolve(dir, cfg.cwd);
28
- if(cfg.dest) cfg.dest = path.resolve(dir, cfg.dest);
23
+ if (c.cwd) cdef.cwd = path.resolve(dir, c.cwd);
24
+ if (c.type) cdef.type = c.type;
25
+ if (typeof c.space == 'number') cdef.space = c.space;
26
+ if (c.dest) cdef.dest = path.resolve(dir, c.dest);
27
+ for (const cfg of c.configurations) {
28
+ if (cfg.cwd) cfg.cwd = path.resolve(dir, cfg.cwd);
29
+ if (cfg.dest) cfg.dest = path.resolve(dir, cfg.dest);
29
30
  cfgs.push(await m(def, cdef, cfg));
30
31
  }
31
32
  }
32
33
 
34
+ for (const cfg of cfgs)
35
+ if (cfg.addition)
36
+ cfg.addition = { version, timestamp: version }
37
+
33
38
  return cfgs;
34
39
  }
35
40
 
36
41
  async function loadConfig(config) {
37
- switch(path.extname(config)) {
42
+ switch (path.extname(config)) {
38
43
  case '.json':
39
44
  case '.yaml':
40
45
  case '.yml':
@@ -50,18 +55,18 @@ async function loadConfig(config) {
50
55
  }
51
56
 
52
57
  async function globit(options) {
53
- const {glob: gs, cwd} = options;
58
+ const { glob: gs, cwd } = options;
54
59
  const files = [];
55
- const g = p=>new Promise(r=>glob(
56
- p, {cwd}, (err, files) => r(err?[]:files)
60
+ const g = p => new Promise(r => glob(
61
+ p, { cwd }, (err, files) => r(err ? [] : files)
57
62
  ))
58
- if(Array.isArray(gs))
59
- for(const p of gs)
63
+ if (Array.isArray(gs))
64
+ for (const p of gs)
60
65
  files.push(...await g(p));
61
- else if(typeof gs == 'string')
66
+ else if (typeof gs == 'string')
62
67
  files.push(...await g(gs));
63
68
  else
64
69
  throw new Error(`Unknown glob type: ${gs}`);
65
70
 
66
- return Object.assign({files}, options);
71
+ return Object.assign({ files }, options);
67
72
  }
package/src/transform.js CHANGED
@@ -7,37 +7,42 @@ import { dump } from './dump.js';
7
7
  export async function transform(options) {
8
8
  const now = Date.now();
9
9
  const configurations = await load(options);
10
- for(const config of configurations)
10
+ for (const config of configurations)
11
11
  await task(config);
12
12
  console.info(`Transformed in ${Date.now() - now}ms`);
13
13
  }
14
14
 
15
- async function task({files, dest, cwd, type, space}) {
16
- console.info('Transform task config:', {files, dest, cwd, type, space});
15
+ async function task({ files, dest, cwd, type, space, addition }) {
16
+ console.info('Transform task config:', { files, dest, cwd, type, space });
17
17
  const m = new Map();
18
- for(const file of files) {
18
+ for (const file of files) {
19
19
  const dir = path.resolve(dest, path.dirname(file));
20
- for(const {name, data} of (await prepare(path.resolve(cwd, file)))) {
21
- if(name[0] === "#") continue;
20
+ for (const { name, data } of (await prepare(path.resolve(cwd, file)))) {
21
+ if (name[0] === "#") continue;
22
22
  let sheet = name.split('#')[0];
23
23
  sheet = sheet.replace('<arr>', '');
24
- if(sheet[0] === '>') sheet = sheet.substring(1);
24
+ if (sheet[0] === '>') sheet = sheet.substring(1);
25
25
  const keys = sheet.split('.');
26
26
  sheet = path.resolve(dir, keys.shift());
27
- if(!m.has(sheet))
27
+ if (!m.has(sheet))
28
28
  m.set(sheet, new JobData());
29
29
  m.get(sheet).append({
30
30
  keys, data: parser(data)
31
31
  });
32
32
  }
33
33
  }
34
- for(const [sheet, data] of m)
35
- await dump(sheet, data.result(), type, space);
34
+ for (const [sheet, job] of m) {
35
+ const data = job.result();
36
+ if (addition)
37
+ await dump(sheet, { data, ...addition }, type, space);
38
+ else
39
+ await dump(sheet, data, type, space);
40
+ }
36
41
  }
37
42
 
38
43
  class JobData {
39
44
  constructor(data) {
40
- if(data) this.append(data);
45
+ if (data) this.append(data);
41
46
  }
42
47
 
43
48
  #data = [];
@@ -47,28 +52,40 @@ class JobData {
47
52
  }
48
53
 
49
54
  result() {
50
- if(!this.#data.length) return {};
51
- const d = this.#data;
55
+ if (!this.#data.length) return {};
52
56
  let result;
53
- if(Array.isArray(d[0].data)) {
54
- result = [];
55
- for(const {data} of d)
56
- result.push(...Object.values(data));
57
- } else {
58
- result = {};
59
- for(const {keys, data} of d) {
60
- if(!keys.length) {
61
- Object.assign(result, data);
62
- continue;
63
- }
64
- let r = result;
65
- for(const key of keys) {
66
- if(!r[key]) r[key] = {};
67
- r = r[key];
68
- }
69
- Object.assign(r, data);
57
+ for (const { keys, data } of this.#data) {
58
+ if (!keys.length) {
59
+ result = this.#combine(result, data);
60
+ continue;
61
+ }
62
+ if (!result) result = {};
63
+ let r = result;
64
+ let last = keys.pop();
65
+ for (const key of keys) {
66
+ if (!r[key]) r[key] = {};
67
+ r = r[key];
70
68
  }
69
+ r[last] = this.#combine(r[last], data);
70
+ }
71
+ return result;
72
+ }
73
+
74
+ #combine(a, b) {
75
+ if (a == null) return b;
76
+ if (b == null) return a;
77
+ if (Array.isArray(a) && Array.isArray(b)) {
78
+ if (Array.isArray(b)) return a.concat(b);
79
+ a.push(b);
80
+ return a;
71
81
  }
82
+ if (Array.isArray(a))
83
+ return this.#combine(a, Object.values(b));
84
+ if (Array.isArray(b))
85
+ return this.#combine(Object.values(a), b);
86
+ const result = {};
87
+ for (const key in a) result[key] = this.#combine(a[key], b[key]);
88
+ for (const key in b) if (!result[key]) result[key] = b[key];
72
89
  return result;
73
90
  }
74
91
  }