v-transform 1.0.4 → 1.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 +2 -1
- package/src/index.js +15 -23
- package/src/io.js +49 -10
- package/src/transform/index.js +11 -13
- package/src/transform/merge.js +2 -4
- package/src/transform/parser.js +8 -8
package/package.json
CHANGED
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
"vt": "src/index.js"
|
|
5
5
|
},
|
|
6
6
|
"type": "module",
|
|
7
|
-
"version": "1.0
|
|
7
|
+
"version": "1.1.0",
|
|
8
8
|
"main": "index.js",
|
|
9
9
|
"repository": "https://github.com/VickScarlet/vTransform.git",
|
|
10
10
|
"author": "Vick Scarlet <scarlet_vick@outlook.com>",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"commander": "^8.2.0",
|
|
14
|
+
"js-yaml": "^4.1.0",
|
|
14
15
|
"xlsx": "^0.17.1"
|
|
15
16
|
}
|
|
16
17
|
}
|
package/src/index.js
CHANGED
|
@@ -1,31 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
2
|
+
import { Command } from 'commander';
|
|
3
3
|
import { readFile } from 'fs/promises';
|
|
4
|
-
import {
|
|
4
|
+
import { read, getWrite, jobs as getJobs } from './io.js';
|
|
5
5
|
import { jobs as doJobs } from './transform/index.js';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
.command('version')
|
|
9
|
-
.action(async () => {
|
|
10
|
-
console.info(
|
|
11
|
-
JSON.parse(
|
|
12
|
-
await readFile('package.json')
|
|
13
|
-
)
|
|
14
|
-
.version
|
|
15
|
-
);
|
|
16
|
-
});
|
|
7
|
+
(async() => {
|
|
17
8
|
|
|
18
|
-
|
|
9
|
+
const program = new Command();
|
|
10
|
+
|
|
11
|
+
program.name('vt');
|
|
12
|
+
program.version(JSON.parse(await readFile('package.json')).version);
|
|
13
|
+
program
|
|
19
14
|
.command('transform [list...]')
|
|
20
|
-
.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
);
|
|
26
|
-
});
|
|
15
|
+
.option('-t, --type <type>', 'type of transform, available: js, esm, cjs, json', 'json')
|
|
16
|
+
.option('-s, --space <space>', 'format space number', 0)
|
|
17
|
+
.action(async (list, {type, space}) => doJobs(
|
|
18
|
+
await getJobs(list), read, getWrite(type, space),
|
|
19
|
+
));
|
|
27
20
|
|
|
28
|
-
|
|
29
|
-
.name('vt');
|
|
21
|
+
program.parse(process.argv);
|
|
30
22
|
|
|
31
|
-
|
|
23
|
+
})();
|
package/src/io.js
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import { readFile, writeFile, stat, readdir } from 'fs/promises';
|
|
2
2
|
import * as XLSX from 'xlsx';
|
|
3
3
|
import { join, extname, dirname, resolve } from 'path';
|
|
4
|
+
import yaml from 'js-yaml';
|
|
4
5
|
|
|
6
|
+
function stringify(data, space) {
|
|
7
|
+
return JSON.stringify(data, null, space);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function cjs(data, space) {
|
|
11
|
+
return `module.exports = ${stringify(data, space)}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function esm(data, space) {
|
|
15
|
+
return `export default ${stringify(data, space)}`;
|
|
16
|
+
}
|
|
5
17
|
|
|
6
|
-
|
|
18
|
+
function yamlify(data, space) {
|
|
19
|
+
return yaml.dump(data, {
|
|
20
|
+
indent: space || undefined,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function read(xlsxPath) {
|
|
7
25
|
const xlsxFileBuffer = await readFile(xlsxPath);
|
|
8
26
|
const xlsx = XLSX.read(xlsxFileBuffer, {type: 'buffer'});
|
|
9
27
|
const sheets = xlsx.Sheets;
|
|
@@ -16,11 +34,34 @@ async function readXLSX(xlsxPath) {
|
|
|
16
34
|
return data;
|
|
17
35
|
}
|
|
18
36
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
37
|
+
export function getWrite(type, space) {
|
|
38
|
+
let format, extname;
|
|
39
|
+
switch(type) {
|
|
40
|
+
case 'commonjs':
|
|
41
|
+
case 'cjs':
|
|
42
|
+
extname = '.cjs';
|
|
43
|
+
format = cjs; break;
|
|
44
|
+
case 'javascript':
|
|
45
|
+
case 'js':
|
|
46
|
+
extname = '.js';
|
|
47
|
+
format = esm; break;
|
|
48
|
+
case 'esm':
|
|
49
|
+
case 'mjs':
|
|
50
|
+
extname = '.mjs';
|
|
51
|
+
format = esm; break;
|
|
52
|
+
case 'yml':
|
|
53
|
+
case 'yaml':
|
|
54
|
+
extname = '.yaml';
|
|
55
|
+
format = yamlify; break;
|
|
56
|
+
case 'json':
|
|
57
|
+
default:
|
|
58
|
+
extname = '.json';
|
|
59
|
+
format = stringify; break;
|
|
60
|
+
}
|
|
61
|
+
return (data, target, basename) => writeFile(
|
|
62
|
+
join(target, `${basename}${extname}`),
|
|
63
|
+
format(data, Number(space)||0)
|
|
64
|
+
);
|
|
24
65
|
}
|
|
25
66
|
|
|
26
67
|
async function walk(filePath) {
|
|
@@ -63,7 +104,7 @@ async function req(config) {
|
|
|
63
104
|
return jobs;
|
|
64
105
|
}
|
|
65
106
|
|
|
66
|
-
async function jobs(configs) {
|
|
107
|
+
export async function jobs(configs) {
|
|
67
108
|
const list = [];
|
|
68
109
|
const files = [];
|
|
69
110
|
for(const config of configs) {
|
|
@@ -83,6 +124,4 @@ async function jobs(configs) {
|
|
|
83
124
|
list.push({ source, target });
|
|
84
125
|
}
|
|
85
126
|
return list.flat();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export { readXLSX, writeJSON, jobs };
|
|
127
|
+
}
|
package/src/transform/index.js
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
import { join } from 'path';
|
|
2
1
|
import { parser } from './parser.js';
|
|
3
2
|
import { merge } from './merge.js';
|
|
4
3
|
|
|
5
|
-
async function jobs(jobs, r, w) {
|
|
4
|
+
export async function jobs(jobs, r, w) {
|
|
6
5
|
for(const { source, target } of jobs)
|
|
7
6
|
await job(source, target, r, w);
|
|
8
7
|
}
|
|
9
8
|
|
|
10
|
-
async function job(source, target, r, w) {
|
|
9
|
+
export async function job(source, target, r, w) {
|
|
11
10
|
const merges = {};
|
|
12
11
|
for(const xlsxPath of source) {
|
|
13
12
|
const rawData = await r(xlsxPath);
|
|
14
13
|
const { merge: mergeData, write } = transform(rawData);
|
|
15
14
|
if(write) {
|
|
16
15
|
for(const sheetName in write) {
|
|
17
|
-
|
|
18
|
-
w(write[sheetName], savePath);
|
|
16
|
+
w(write[sheetName], target, sheetName);
|
|
19
17
|
}
|
|
20
18
|
}
|
|
21
19
|
|
|
@@ -42,28 +40,28 @@ async function job(source, target, r, w) {
|
|
|
42
40
|
}
|
|
43
41
|
|
|
44
42
|
for(const sheetName in merges) {
|
|
45
|
-
const savePath = join(target, `${sheetName}.json`);
|
|
46
43
|
const data = merge(merges[sheetName]);
|
|
47
|
-
w(data,
|
|
44
|
+
w(data, target, sheetName);
|
|
48
45
|
}
|
|
49
46
|
}
|
|
50
47
|
|
|
51
|
-
function transform(rawSheetsData) {
|
|
48
|
+
export function transform(rawSheetsData) {
|
|
52
49
|
const merge = {};
|
|
53
50
|
const write = {};
|
|
54
51
|
for(const rawSheetName in rawSheetsData) {
|
|
55
52
|
const rawData = rawSheetsData[rawSheetName];
|
|
56
53
|
if(rawSheetName[0] === "#") continue;
|
|
57
54
|
let sheetName = rawSheetName;
|
|
58
|
-
const isArray = rawSheetName.
|
|
55
|
+
const isArray = rawSheetName.substring(
|
|
56
|
+
rawSheetName.length - 5,
|
|
57
|
+
rawSheetName.length
|
|
58
|
+
) === "<arr>";
|
|
59
59
|
if(isArray) sheetName = sheetName.substring(0, sheetName.length - 5);
|
|
60
60
|
const isMerge = rawSheetName[0] === ">";
|
|
61
|
-
if(isMerge) sheetName = sheetName.
|
|
61
|
+
if(isMerge) sheetName = sheetName.substring(1);
|
|
62
62
|
const data = parser(rawData, isArray);
|
|
63
63
|
if(isMerge) merge[sheetName] = data;
|
|
64
64
|
else write[sheetName] = data;
|
|
65
65
|
}
|
|
66
66
|
return { merge, write };
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export { jobs, job, transform };
|
|
67
|
+
}
|
package/src/transform/merge.js
CHANGED
package/src/transform/parser.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import yaml from 'js-yaml';
|
|
2
|
+
|
|
1
3
|
function parseStruct(head) {
|
|
2
4
|
const layer = [];
|
|
3
5
|
const subs = {};
|
|
@@ -89,10 +91,10 @@ function formatRow({type, key, source, subs}, row, original, json) {
|
|
|
89
91
|
const value = row[source];
|
|
90
92
|
if(value != void 0) {
|
|
91
93
|
try {
|
|
92
|
-
return {
|
|
93
|
-
key,
|
|
94
|
-
value: json.includes(source)
|
|
95
|
-
?
|
|
94
|
+
return {
|
|
95
|
+
key,
|
|
96
|
+
value: json.includes(source)
|
|
97
|
+
? yaml.load(value)
|
|
96
98
|
: value
|
|
97
99
|
};
|
|
98
100
|
} catch(e) {
|
|
@@ -148,10 +150,8 @@ function formatSheet(struct, rawSheet, json) {
|
|
|
148
150
|
return data;
|
|
149
151
|
}
|
|
150
152
|
|
|
151
|
-
function parser(rawSheet) {
|
|
153
|
+
export function parser(rawSheet) {
|
|
152
154
|
const struct = parseStruct(rawSheet.shift());
|
|
153
155
|
rawSheet.shift();
|
|
154
156
|
return formatSheet(struct, rawSheet, struct.json);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export { parser };
|
|
157
|
+
}
|