v-transform 2.0.3 → 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,7 +4,7 @@
4
4
  "vt": "src/index.js"
5
5
  },
6
6
  "type": "module",
7
- "version": "2.0.3",
7
+ "version": "2.1.0",
8
8
  "main": "index.js",
9
9
  "scripts": {
10
10
  "clean": "npx rimraf test",
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
@@ -12,7 +12,7 @@ export async function transform(options) {
12
12
  console.info(`Transformed in ${Date.now() - now}ms`);
13
13
  }
14
14
 
15
- async function task({ files, dest, cwd, type, space }) {
15
+ async function task({ files, dest, cwd, type, space, addition }) {
16
16
  console.info('Transform task config:', { files, dest, cwd, type, space });
17
17
  const m = new Map();
18
18
  for (const file of files) {
@@ -31,8 +31,13 @@ async function task({ files, dest, cwd, type, space }) {
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 {