v-transform 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 神戸小鳥
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # vTransform
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "v-transform",
3
+ "bin": {
4
+ "vt": "src/index.js"
5
+ },
6
+ "type": "module",
7
+ "version": "1.0.1",
8
+ "main": "index.js",
9
+ "repository": "https://github.com/VickScarlet/vTransform.git",
10
+ "author": "Vick Scarlet <scarlet_vick@outlook.com>",
11
+ "license": "MIT",
12
+ "dependencies": {
13
+ "commander": "^8.2.0",
14
+ "xlsx": "^0.17.1"
15
+ }
16
+ }
package/src/index.js ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env node
2
+ import commander from 'commander';
3
+ import { readFile } from 'fs/promises';
4
+ import { readXLSX, writeJSON, jobs as getJobs } from './io.js';
5
+ import { jobs as doJobs } from './transform/index.js';
6
+
7
+ commander
8
+ .command('version')
9
+ .action(async () => {
10
+ console.info(
11
+ JSON.parse(
12
+ await readFile('package.json')
13
+ )
14
+ .version
15
+ );
16
+ });
17
+
18
+ commander
19
+ .command('transform [list...]')
20
+ .action(async list => {
21
+ await doJobs(
22
+ await getJobs(list),
23
+ readXLSX,
24
+ writeJSON,
25
+ );
26
+ });
27
+
28
+ commander
29
+ .name('vt');
30
+
31
+ commander.parse(process.argv);
package/src/io.js ADDED
@@ -0,0 +1,88 @@
1
+ import { readFile, writeFile, stat, readdir } from 'fs/promises';
2
+ import * as XLSX from 'xlsx';
3
+ import { join, extname, dirname, resolve } from 'path';
4
+
5
+
6
+ async function readXLSX(xlsxPath) {
7
+ const xlsxFileBuffer = await readFile(xlsxPath);
8
+ const xlsx = XLSX.read(xlsxFileBuffer, {type: 'buffer'});
9
+ const sheets = xlsx.Sheets;
10
+ const data = {};
11
+ for(const sheetName in sheets) {
12
+ const sheetRawData = sheets[sheetName];
13
+ if(!sheetRawData['!ref']) break;
14
+ data[sheetName] = XLSX.utils.sheet_to_json(sheetRawData, { header: 1 });
15
+ }
16
+ return data;
17
+ }
18
+
19
+ async function writeJSON(data, savePath) {
20
+ await writeFile(
21
+ savePath,
22
+ JSON.stringify(data, null, 4),
23
+ )
24
+ }
25
+
26
+ async function walk(filePath) {
27
+ const xlsxPaths = [];
28
+ if(Array.isArray(filePath)) {
29
+ for(const subPath of filePath)
30
+ xlsxPaths.push(await walk(subPath));
31
+ return xlsxPaths.flat();
32
+ }
33
+ const fileStat = await stat(filePath);
34
+ if(!fileStat.isDirectory()) {
35
+ const ext = extname(filePath);
36
+ if( ext=='.xls' || ext=='.xlsx' ) xlsxPaths.push(filePath);
37
+ return xlsxPaths;
38
+ }
39
+
40
+ const dirData = await readdir(filePath);
41
+ for(const subPath of dirData)
42
+ xlsxPaths.push(await walk(join(filePath, subPath)));
43
+ return xlsxPaths.flat();
44
+ }
45
+
46
+ async function req(config) {
47
+ const { configurations } = JSON.parse(await readFile(config));
48
+ const jobs = [];
49
+ const dir = dirname(config);
50
+ for(const { source, target } of configurations) {
51
+ const job = {};
52
+ if(Array.isArray(source)) {
53
+ job.source = [];
54
+ for(const p of source)
55
+ job.source.push(await walk(resolve(dir, p)))
56
+ job.source.flat();
57
+ } else {
58
+ job.source = await walk(resolve(dir, source));
59
+ }
60
+ job.target = resolve(dir, target);
61
+ jobs.push(job);
62
+ }
63
+ return jobs;
64
+ }
65
+
66
+ async function jobs(configs) {
67
+ const list = [];
68
+ const files = [];
69
+ for(const config of configs) {
70
+ switch(extname(config)) {
71
+ case '.json':
72
+ const jobs = await req(config);
73
+ if(jobs) list.push(jobs);
74
+ break;
75
+ default:
76
+ files.push(config);
77
+ break;
78
+ }
79
+ }
80
+ if(files.length > 0) {
81
+ const source = await walk(files);
82
+ const target = dirname(source[0]);
83
+ list.push({ source, target });
84
+ }
85
+ return list.flat();
86
+ }
87
+
88
+ export { readXLSX, writeJSON, jobs };
@@ -0,0 +1,59 @@
1
+ import { join } from 'path';
2
+ import { parser } from './parser.js';
3
+ import { merge } from './merge.js';
4
+
5
+ async function jobs(jobs, r, w) {
6
+ for(const { source, target } of jobs)
7
+ await job(source, target, r, w);
8
+ }
9
+
10
+ async function job(source, target, r, w) {
11
+ const merges = {};
12
+ for(const xlsxPath of source) {
13
+ const rawData = await r(xlsxPath);
14
+ const { merge: mergeData, write } = transform(rawData);
15
+ if(write) {
16
+ for(const sheetName in write) {
17
+ const savePath = join(target, `${sheetName}.json`);
18
+ w(write[sheetName], savePath);
19
+ }
20
+ }
21
+
22
+ if(mergeData) {
23
+ for(const sheetName in mergeData) {
24
+ if(!merges[sheetName]) {
25
+ merges[sheetName] = [ mergeData[sheetName] ];
26
+ } else {
27
+ merges[sheetName].push(mergeData[sheetName]);
28
+ }
29
+ }
30
+
31
+ }
32
+ }
33
+
34
+ for(const sheetName in merges) {
35
+ const savePath = join(target, `${sheetName}.json`);
36
+ const data = merge(merges[sheetName]);
37
+ w(data, savePath);
38
+ }
39
+ }
40
+
41
+ function transform(rawSheetsData) {
42
+ const merge = {};
43
+ const write = {};
44
+ for(const rawSheetName in rawSheetsData) {
45
+ const rawData = rawSheetsData[rawSheetName];
46
+ if(rawSheetName[0] === "#") continue;
47
+ let sheetName = rawSheetName;
48
+ const isArray = rawSheetName.substr(-5) === "<arr>";
49
+ if(isArray) sheetName = sheetName.substring(0, sheetName.length - 5);
50
+ const isMerge = rawSheetName[0] === ">";
51
+ if(isMerge) sheetName = sheetName.substr(1);
52
+ const data = parser(rawData, isArray);
53
+ if(isMerge) merge[sheetName] = data;
54
+ else write[sheetName] = data;
55
+ }
56
+ return { merge, write };
57
+ }
58
+
59
+ export { jobs, job, transform };
@@ -0,0 +1,8 @@
1
+ function merge(datas) {
2
+ if(Array.isArray(datas[0]))
3
+ return datas.flat();
4
+
5
+ return Object.assign({}, ...datas);
6
+ }
7
+
8
+ export { merge };
@@ -0,0 +1,142 @@
1
+ function parseStruct(head) {
2
+ const layer = [];
3
+ const subs = {};
4
+
5
+ for(const col in head) {
6
+ const key = head[col];
7
+ if(key[0] == '#') continue;
8
+ const [first, ...last] = key.split(/[\.:]/);
9
+ if(!subs[first]) {
10
+ layer.push(first);
11
+ }
12
+ if(last.length > 0) {
13
+ if(!subs[first]) subs[first] = {};
14
+ subs[first][col] = last.join('.');
15
+ } else {
16
+ if(first.substr(-2) == '[]') {
17
+ if(!subs[first]) subs[first] = [];
18
+ subs[first].push(col);
19
+ } else {
20
+ subs[first] = col;
21
+ }
22
+ }
23
+
24
+ }
25
+
26
+ const struct = {
27
+ type: 'object',
28
+ subs: [],
29
+ };
30
+
31
+ for(const key of layer) {
32
+ if(key[0] == '$') {
33
+ struct.key = `#${subs[key]}`;
34
+ }
35
+ switch(key.substr(-2)) {
36
+ case '{}':
37
+ struct.subs.push({
38
+ type: 'object',
39
+ key: key.substr(0, key.length -2),
40
+ subs: [ parseStruct(subs[key]) ]
41
+ });
42
+ break;
43
+ case '[]':
44
+ struct.subs.push({
45
+ type: 'array',
46
+ key: key.substr(0, key.length -2),
47
+ subs: Array.isArray(subs[key])
48
+ ? subs[key].map(col => ({
49
+ type: 'value',
50
+ source: col
51
+ }))
52
+ : [parseStruct(subs[key])]
53
+ });
54
+ break;
55
+ default:
56
+ struct.subs.push(
57
+ typeof subs[key] == 'string'
58
+ ? ({
59
+ type: 'value',
60
+ key: key[0] == '$'
61
+ ? key.substr(1)
62
+ : key,
63
+ source: subs[key]
64
+ })
65
+ : {
66
+ key,
67
+ type: 'object',
68
+ subs: parseStruct(subs[key]).subs
69
+ }
70
+ );
71
+ }
72
+ }
73
+
74
+ return struct;
75
+ }
76
+
77
+ function formatRow({type, key, source, subs}, row, original) {
78
+ if(row[0] == '#' || (''+row[0])[0] == '#') return null;
79
+ if(key && key[0] == '#') key = row[key.substr(1)];
80
+ switch(type) {
81
+ case 'value':
82
+ const value = row[source];
83
+ if(value != void 0) {
84
+ return { key, value };
85
+ }
86
+ return null;
87
+ case 'array':
88
+ const arr = original?.[key] || [];
89
+ for(const sub of subs) {
90
+ const data = formatRow(sub, row, arr);
91
+ if(data) {
92
+ arr.push(data.value);
93
+ }
94
+ }
95
+ if(arr.length) {
96
+ return { key, value: arr };
97
+ }
98
+ return null;
99
+ case 'object':
100
+ const obj = original?.[key] || {};
101
+ let r = false;
102
+ for(const sub of subs) {
103
+ const data = formatRow(sub, row, obj);
104
+ if(data) {
105
+ r = true;
106
+ obj[data.key] = data.value;
107
+ }
108
+ }
109
+ if(r) {
110
+ return { key, value: obj };
111
+ }
112
+ return null;
113
+ }
114
+ }
115
+
116
+ function formatSheet(struct, rawSheet) {
117
+ let data;
118
+ if(struct.key == void 0) {
119
+ const subs = struct.subs;
120
+ data = [];
121
+ for(const row of rawSheet) {
122
+ const temp = formatRow(struct, row);
123
+ if(temp) data.push(temp.value);
124
+ }
125
+ } else {
126
+ data = {};
127
+ for(const row of rawSheet) {
128
+ const temp = formatRow(struct, row, data);
129
+ if(temp) data[temp.key] = temp.value;
130
+ }
131
+ }
132
+
133
+ return data;
134
+ }
135
+
136
+ function parser(rawSheet) {
137
+ const struct = parseStruct(rawSheet.shift());
138
+ rawSheet.shift();
139
+ return formatSheet(struct, rawSheet);
140
+ }
141
+
142
+ export { parser };