@lsbjordao/type-taxon-script 1.0.6 → 1.0.8
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/dist/export.js +3 -3
- package/dist/exportSources.js +1 -1
- package/dist/exportToCsv.js +268 -0
- package/dist/tts.js +22 -1
- package/package.json +11 -9
package/dist/export.js
CHANGED
|
@@ -39,7 +39,7 @@ function ttsExport(genus, load) {
|
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
41
|
if (!fs_1.default.existsSync('./input') && !fs_1.default.existsSync('./output')) {
|
|
42
|
-
console.error("\x1b[31m✖ The ./input and ./output directories are not present within the project.\x1b[0m\n\x1b[33mℹ️ Please run
|
|
42
|
+
console.error("\x1b[31m✖ The ./input and ./output directories are not present within the project.\x1b[0m\n\x1b[33mℹ️ Please run `tts init` before attempting to export a database.\x1b[0m");
|
|
43
43
|
return;
|
|
44
44
|
}
|
|
45
45
|
const spinner = new cli_spinner_1.Spinner('\x1b[36mProcessing... %s\x1b[0m');
|
|
@@ -121,7 +121,7 @@ console.log(jsonData)
|
|
|
121
121
|
}
|
|
122
122
|
deleteJSFiles(`./taxon/${genus}`).then(() => {
|
|
123
123
|
const filePath = './output/';
|
|
124
|
-
console.log(`\x1b[1m\x1b[32m✔
|
|
124
|
+
console.log(`\x1b[1m\x1b[32m✔ JSON database exported: \x1b[33m${filePath}${genus}DB.json\x1b[0m\x1b[1m\x1b[32m\x1b[0m`);
|
|
125
125
|
spinner.stop();
|
|
126
126
|
try {
|
|
127
127
|
fs_1.default.unlinkSync(`./temp/${fileToTranspile}.js`);
|
|
@@ -213,7 +213,7 @@ console.log(jsonData)
|
|
|
213
213
|
}
|
|
214
214
|
deleteJSFiles(`./taxon/${genus}`).then(() => {
|
|
215
215
|
const filePath = './output/';
|
|
216
|
-
console.log(`\x1b[1m\x1b[32m✔
|
|
216
|
+
console.log(`\x1b[1m\x1b[32m✔ JSON database exported: \x1b[33m${filePath}${genus}DB.json\x1b[0m\x1b[1m\x1b[32m\x1b[0m`);
|
|
217
217
|
spinner.stop();
|
|
218
218
|
try {
|
|
219
219
|
fs_1.default.unlinkSync(`./temp/${fileToTranspile}.js`);
|
package/dist/exportSources.js
CHANGED
|
@@ -11,7 +11,7 @@ function ttsExportSources(genus) {
|
|
|
11
11
|
return;
|
|
12
12
|
}
|
|
13
13
|
if (!fs_1.default.existsSync('./input') && !fs_1.default.existsSync('./output')) {
|
|
14
|
-
console.error("\x1b[31m✖ The ./input and ./output directories are not present within the project.\x1b[0m\n\x1b[33mℹ️ Please run
|
|
14
|
+
console.error("\x1b[31m✖ The ./input and ./output directories are not present within the project.\x1b[0m\n\x1b[33mℹ️ Please run `tts init` before attempting to export a database.\x1b[0m");
|
|
15
15
|
return;
|
|
16
16
|
}
|
|
17
17
|
const filePath = `./output/${genus}DB.json`;
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const fs_1 = __importDefault(require("fs"));
|
|
16
|
+
const path_1 = __importDefault(require("path"));
|
|
17
|
+
const child_process_1 = require("child_process");
|
|
18
|
+
const csv_parser_1 = __importDefault(require("csv-parser"));
|
|
19
|
+
const plainjs_1 = require("@json2csv/plainjs");
|
|
20
|
+
const transforms_1 = require("@json2csv/transforms");
|
|
21
|
+
const cli_spinner_1 = require("cli-spinner");
|
|
22
|
+
function deleteJSFiles(folderPath) {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
try {
|
|
25
|
+
const files = yield fs_1.default.promises.readdir(folderPath);
|
|
26
|
+
for (const file of files) {
|
|
27
|
+
if (file.endsWith('.js')) {
|
|
28
|
+
yield fs_1.default.promises.unlink(`${folderPath}/${file}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
console.error('Error deleting files:', err);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function ttsExportToCsv(genus, load) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
if (genus === '') {
|
|
40
|
+
console.error('\x1b[31m✖ Argument `--genus` cannot be empty.\x1b[0m');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (!fs_1.default.existsSync('./input') && !fs_1.default.existsSync('./output')) {
|
|
44
|
+
console.error("\x1b[31m✖ The ./input and ./output directories are not present within the project.\x1b[0m\n\x1b[33mℹ️ Please run `tts init` before attempting to export a database.\x1b[0m");
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const spinner = new cli_spinner_1.Spinner('\x1b[36mProcessing... %s\x1b[0m');
|
|
48
|
+
spinner.setSpinnerString('|/-\\'); // spinner sequence
|
|
49
|
+
spinner.start();
|
|
50
|
+
const taxa = [];
|
|
51
|
+
fs_1.default.mkdirSync('./temp', { recursive: true });
|
|
52
|
+
if (load === 'all') {
|
|
53
|
+
const directoryPath = `./taxon/${genus}/`;
|
|
54
|
+
fs_1.default.readdir(directoryPath, (err, files) => {
|
|
55
|
+
if (err) {
|
|
56
|
+
spinner.stop();
|
|
57
|
+
console.error('Error reading directory:', err);
|
|
58
|
+
process.exit();
|
|
59
|
+
}
|
|
60
|
+
const taxa = files
|
|
61
|
+
.filter(file => file.endsWith('.ts') && file !== 'index.ts')
|
|
62
|
+
.map(file => path_1.default.parse(file).name);
|
|
63
|
+
const importStatements = taxa.map((species) => {
|
|
64
|
+
return `import { ${species.replace(/\s/g, '_').replace(/\-([a-z])/, (_, match) => match.toUpperCase())} } from '../taxon/${genus}/${species.replace(/\s/g, '_')}'`;
|
|
65
|
+
}).join('\n');
|
|
66
|
+
const speciesCall = taxa.map((species) => {
|
|
67
|
+
return ` ${species.replace(/\s/g, '_').replace(/\-([a-z])/, (_, match) => match.toUpperCase())},`;
|
|
68
|
+
}).join('\n');
|
|
69
|
+
const fileContent = `// Import genus ${genus}
|
|
70
|
+
import { ${genus} } from '../taxon/${genus}'
|
|
71
|
+
|
|
72
|
+
// Import species of ${genus}
|
|
73
|
+
${importStatements}
|
|
74
|
+
|
|
75
|
+
const ${genus}_species: ${genus}[] = [
|
|
76
|
+
${speciesCall}
|
|
77
|
+
]
|
|
78
|
+
|
|
79
|
+
// Export ${genus}DB.json
|
|
80
|
+
//import { writeFileSync } from 'fs'
|
|
81
|
+
const jsonData = JSON.stringify(${genus}_species);
|
|
82
|
+
console.log(jsonData)
|
|
83
|
+
//const inputFilePath = '../output/${genus}DB.json'
|
|
84
|
+
//writeFileSync(inputFilePath, jsonData, 'utf-8')
|
|
85
|
+
//console.log('\\x1b[1m\\x1b[32m✔ Process finished.\\x1b[0m')`;
|
|
86
|
+
const tempFilePath = './temp/exportTemp.ts';
|
|
87
|
+
fs_1.default.writeFileSync(tempFilePath, fileContent, 'utf-8');
|
|
88
|
+
const fileToTranspile = 'exportTemp';
|
|
89
|
+
(0, child_process_1.exec)(`tsc ./temp/${fileToTranspile}.ts`, (error, stdout, stderr) => {
|
|
90
|
+
if (stdout) {
|
|
91
|
+
spinner.stop();
|
|
92
|
+
console.error('\x1b[31m✖ TS Error:\x1b[0m\n\n' + `${stdout}`);
|
|
93
|
+
process.exit();
|
|
94
|
+
}
|
|
95
|
+
if (stderr) {
|
|
96
|
+
spinner.stop();
|
|
97
|
+
console.error('\x1b[31m✖ TS Error:\x1b[0m\n\n' + `${stderr}`);
|
|
98
|
+
process.exit();
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
fs_1.default.unlinkSync(`./temp/${fileToTranspile}.ts`);
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
spinner.stop();
|
|
105
|
+
console.error(`An error occurred while deleting the file: ${err}`);
|
|
106
|
+
process.exit();
|
|
107
|
+
}
|
|
108
|
+
(0, child_process_1.exec)(`node ./temp/${fileToTranspile}.js > ./output/${genus}DB.json`, (error, stdout, stderr) => {
|
|
109
|
+
// if (error) {
|
|
110
|
+
// spinner.stop()
|
|
111
|
+
// console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${error.message}`)
|
|
112
|
+
// process.exit()
|
|
113
|
+
// }
|
|
114
|
+
if (stdout) {
|
|
115
|
+
spinner.stop();
|
|
116
|
+
console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${stdout}`);
|
|
117
|
+
process.exit();
|
|
118
|
+
}
|
|
119
|
+
if (stderr) {
|
|
120
|
+
spinner.stop();
|
|
121
|
+
console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${stderr}`);
|
|
122
|
+
process.exit();
|
|
123
|
+
}
|
|
124
|
+
deleteJSFiles(`./taxon/${genus}`).then(() => {
|
|
125
|
+
const filePath = './output/';
|
|
126
|
+
try {
|
|
127
|
+
const data = fs_1.default.readFileSync(`./output/${genus}DB.json`, 'utf8');
|
|
128
|
+
const opts = {
|
|
129
|
+
transforms: [
|
|
130
|
+
(0, transforms_1.flatten)({ separator: '.' })
|
|
131
|
+
]
|
|
132
|
+
};
|
|
133
|
+
const parser = new plainjs_1.Parser(opts);
|
|
134
|
+
const csv = parser.parse(JSON.parse(data));
|
|
135
|
+
fs_1.default.writeFileSync(`./output/${genus}DB.csv`, csv);
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
console.error('Error reading the file:', err);
|
|
139
|
+
}
|
|
140
|
+
console.log(`\x1b[1m\x1b[32m✔ CSV database exported: \x1b[33m${filePath}${genus}DB.csv\x1b[0m\x1b[1m\x1b[32m\x1b[0m`);
|
|
141
|
+
spinner.stop();
|
|
142
|
+
try {
|
|
143
|
+
fs_1.default.unlinkSync(`./temp/${fileToTranspile}.js`);
|
|
144
|
+
fs_1.default.rm('./temp', { recursive: true }, (err) => {
|
|
145
|
+
if (err) {
|
|
146
|
+
console.error('Error deleting directory:', err);
|
|
147
|
+
process.exit();
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
console.error(`An error occurred while deleting the file: ${err}`);
|
|
153
|
+
process.exit();
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
if (load === 'csv') {
|
|
161
|
+
const inputFilePath = './input/taxaToExport.csv';
|
|
162
|
+
const tempFilePath = './temp/exportTemp.ts';
|
|
163
|
+
fs_1.default.createReadStream(inputFilePath)
|
|
164
|
+
.pipe((0, csv_parser_1.default)({ headers: false }))
|
|
165
|
+
.on('data', (data) => {
|
|
166
|
+
taxa.push(data['0']);
|
|
167
|
+
})
|
|
168
|
+
.on('end', () => __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
const importStatements = taxa.map((species) => {
|
|
170
|
+
return `import { ${species.replace(/\s/g, '_').replace(/\-([a-z])/, (_, match) => match.toUpperCase())} } from '../taxon/${genus}/${species.replace(/\s/g, '_')}'`;
|
|
171
|
+
}).join('\n');
|
|
172
|
+
const speciesCall = taxa.map((species) => {
|
|
173
|
+
return ` ${species.replace(/\s/g, '_').replace(/\-([a-z])/, (_, match) => match.toUpperCase())},`;
|
|
174
|
+
}).join('\n');
|
|
175
|
+
const fileContent = `// Import genus ${genus}
|
|
176
|
+
import { ${genus} } from '../taxon/${genus}'
|
|
177
|
+
|
|
178
|
+
// Import species of ${genus}
|
|
179
|
+
${importStatements}
|
|
180
|
+
|
|
181
|
+
const ${genus}_species: ${genus}[] = [
|
|
182
|
+
${speciesCall}
|
|
183
|
+
]
|
|
184
|
+
|
|
185
|
+
// Export ${genus}DB.json
|
|
186
|
+
const jsonData = JSON.stringify(${genus}_species);
|
|
187
|
+
console.log(jsonData)
|
|
188
|
+
// import { writeFileSync } from 'fs'
|
|
189
|
+
// const jsonData = JSON.stringify(${genus}_species)
|
|
190
|
+
// const inputFilePath = '../output/${genus}DB.json'
|
|
191
|
+
// writeFileSync(inputFilePath, jsonData, 'utf-8')
|
|
192
|
+
// console.log('\\x1b[1m\\x1b[32m✔ Process finished.\\x1b[0m')`;
|
|
193
|
+
fs_1.default.writeFileSync(tempFilePath, fileContent, 'utf-8');
|
|
194
|
+
const fileToTranspile = 'exportTemp';
|
|
195
|
+
(0, child_process_1.exec)(`tsc ./temp/${fileToTranspile}.ts`, (error, stdout, stderr) => {
|
|
196
|
+
if (stdout) {
|
|
197
|
+
spinner.stop();
|
|
198
|
+
console.error('\x1b[31m✖ TS Error:\x1b[0m\n\n' + `${stdout}`);
|
|
199
|
+
process.exit();
|
|
200
|
+
}
|
|
201
|
+
if (stderr) {
|
|
202
|
+
spinner.stop();
|
|
203
|
+
console.error('\x1b[31m✖ TS Error:\x1b[0m\n\n' + `${stdout}`);
|
|
204
|
+
process.exit();
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
fs_1.default.unlinkSync(`./temp/${fileToTranspile}.ts`);
|
|
208
|
+
}
|
|
209
|
+
catch (err) {
|
|
210
|
+
spinner.stop();
|
|
211
|
+
console.error(`An error occurred while deleting the file: ${err}`);
|
|
212
|
+
process.exit();
|
|
213
|
+
}
|
|
214
|
+
(0, child_process_1.exec)(`node ./temp/${fileToTranspile}.js > ./output/${genus}DB.json`, (error, stdout, stderr) => {
|
|
215
|
+
// if (error) {
|
|
216
|
+
// spinner.stop()
|
|
217
|
+
// console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${error.message}`)
|
|
218
|
+
// process.exit()
|
|
219
|
+
// }
|
|
220
|
+
if (stdout) {
|
|
221
|
+
spinner.stop();
|
|
222
|
+
console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${stdout}`);
|
|
223
|
+
process.exit();
|
|
224
|
+
}
|
|
225
|
+
if (stderr) {
|
|
226
|
+
spinner.stop();
|
|
227
|
+
console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${stderr}`);
|
|
228
|
+
process.exit();
|
|
229
|
+
}
|
|
230
|
+
deleteJSFiles(`./taxon/${genus}`).then(() => {
|
|
231
|
+
const filePath = './output/';
|
|
232
|
+
try {
|
|
233
|
+
const data = fs_1.default.readFileSync(`./output/${genus}DB.json`, 'utf8');
|
|
234
|
+
const opts = {
|
|
235
|
+
transforms: [
|
|
236
|
+
(0, transforms_1.flatten)({ separator: '.' })
|
|
237
|
+
]
|
|
238
|
+
};
|
|
239
|
+
const parser = new plainjs_1.Parser(opts);
|
|
240
|
+
const csv = parser.parse(JSON.parse(data));
|
|
241
|
+
fs_1.default.writeFileSync(`./output/${genus}DB.csv`, csv);
|
|
242
|
+
}
|
|
243
|
+
catch (err) {
|
|
244
|
+
console.error('Error reading the file:', err);
|
|
245
|
+
}
|
|
246
|
+
console.log(`\x1b[1m\x1b[32m✔ CSV database exported: \x1b[33m${filePath}${genus}DB.csv\x1b[0m\x1b[1m\x1b[32m\x1b[0m`);
|
|
247
|
+
spinner.stop();
|
|
248
|
+
try {
|
|
249
|
+
fs_1.default.unlinkSync(`./temp/${fileToTranspile}.js`);
|
|
250
|
+
fs_1.default.rm('./temp', { recursive: true }, (err) => {
|
|
251
|
+
if (err) {
|
|
252
|
+
console.error('Error deleting directory:', err);
|
|
253
|
+
process.exit();
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
catch (err) {
|
|
258
|
+
console.error(`An error occurred while deleting the file: ${err}`);
|
|
259
|
+
process.exit();
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}));
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
exports.default = ttsExportToCsv;
|
package/dist/tts.js
CHANGED
|
@@ -52,7 +52,7 @@ yargs_1.default.command({
|
|
|
52
52
|
});
|
|
53
53
|
yargs_1.default.command({
|
|
54
54
|
command: 'export',
|
|
55
|
-
describe: 'Export database',
|
|
55
|
+
describe: 'Export a JSON database',
|
|
56
56
|
builder: {
|
|
57
57
|
genus: {
|
|
58
58
|
describe: 'Genus name',
|
|
@@ -85,6 +85,27 @@ yargs_1.default.command({
|
|
|
85
85
|
(0, exportSources_1.default)(argv.genus);
|
|
86
86
|
},
|
|
87
87
|
});
|
|
88
|
+
yargs_1.default.command({
|
|
89
|
+
command: 'exportToCsv',
|
|
90
|
+
describe: 'Export a CSV database',
|
|
91
|
+
builder: {
|
|
92
|
+
genus: {
|
|
93
|
+
describe: 'Genus name',
|
|
94
|
+
demandOption: true,
|
|
95
|
+
type: 'string',
|
|
96
|
+
},
|
|
97
|
+
load: {
|
|
98
|
+
describe: 'Load a list of taxa to export from the csv file: "./input/taxaToExport.csv".',
|
|
99
|
+
demandOption: false,
|
|
100
|
+
type: 'string',
|
|
101
|
+
choices: ['all', 'csv'],
|
|
102
|
+
default: 'all'
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
handler: (argv) => {
|
|
106
|
+
(0, export_1.default)(argv.genus, argv.load);
|
|
107
|
+
},
|
|
108
|
+
});
|
|
88
109
|
yargs_1.default.command({
|
|
89
110
|
command: 'findProperty',
|
|
90
111
|
describe: 'Find a property',
|
package/package.json
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lsbjordao/type-taxon-script",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"author": "Lucas Jordão <tucarj@gmail.com> & André Eppinghaus <andreeppinghaus@gmail.com> & Vicente Calfo <vicentecalfo@gmail.com>",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"description": "TypeTaxonScript",
|
|
7
7
|
"dependencies": {
|
|
8
|
+
"@json2csv/plainjs": "^7.0.5",
|
|
9
|
+
"@json2csv/transforms": "^7.0.5",
|
|
10
|
+
"cli-spinner": "^0.2.10",
|
|
11
|
+
"csv-parser": "^3.0.0",
|
|
8
12
|
"fs": "^0.0.1-security",
|
|
9
|
-
"mustache": "^4.2.0",
|
|
10
13
|
"lodash": "^4.17.21",
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"cli-spinner": "^0.2.10"
|
|
14
|
+
"mustache": "^4.2.0",
|
|
15
|
+
"yargs": "^17.7.2"
|
|
14
16
|
},
|
|
15
17
|
"devDependencies": {
|
|
16
|
-
"
|
|
18
|
+
"@types/cli-spinner": "^0.2.3",
|
|
19
|
+
"@types/lodash": "^4.14.202",
|
|
20
|
+
"@types/mustache": "^4.2.5",
|
|
17
21
|
"@types/node": "^20.4.10",
|
|
18
22
|
"@types/yargs": "^17.0.32",
|
|
19
|
-
"
|
|
20
|
-
"@types/lodash": "^4.14.202",
|
|
21
|
-
"@types/cli-spinner": "^0.2.3"
|
|
23
|
+
"typescript": "^5.1.5"
|
|
22
24
|
},
|
|
23
25
|
"main": "dist/tts.js",
|
|
24
26
|
"files": [
|