@lsbjordao/type-taxon-script 1.1.8 → 1.1.9
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 +238 -0
- package/dist/exportOntology.js +356 -0
- package/dist/exportSources.js +63 -0
- package/dist/exportToCsv.js +268 -0
- package/dist/findProperty.js +58 -0
- package/dist/import.js +98 -0
- package/dist/init.js +131 -0
- package/dist/new.js +47 -0
- package/dist/src/export.js +238 -0
- package/dist/src/exportOntology.js +366 -0
- package/dist/src/exportSources.js +63 -0
- package/dist/src/exportToCsv.js +268 -0
- package/dist/src/findProperty.js +58 -0
- package/dist/src/import.js +98 -0
- package/dist/src/init.js +141 -0
- package/dist/src/new.js +47 -0
- package/dist/src/tts.js +130 -0
- package/dist/tts.js +130 -0
- package/package.json +1 -1
package/dist/export.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
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 cli_spinner_1 = require("cli-spinner");
|
|
20
|
+
function deleteJSFiles(folderPath) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
try {
|
|
23
|
+
const files = yield fs_1.default.promises.readdir(folderPath);
|
|
24
|
+
for (const file of files) {
|
|
25
|
+
if (file.endsWith('.js')) {
|
|
26
|
+
yield fs_1.default.promises.unlink(`${folderPath}/${file}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
console.error('Error deleting files:', err);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function ttsExport(genus, load) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
if (genus === '') {
|
|
38
|
+
console.error('\x1b[31m✖ Argument `--genus` cannot be empty.\x1b[0m');
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
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[36mℹ️ Please run \x1b[33m`tts init`\x1b[36m before attempting to export a database.\x1b[0m");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const spinner = new cli_spinner_1.Spinner('\x1b[36mProcessing... %s\x1b[0m');
|
|
46
|
+
spinner.setSpinnerString('|/-\\'); // spinner sequence
|
|
47
|
+
spinner.start();
|
|
48
|
+
const taxa = [];
|
|
49
|
+
fs_1.default.mkdirSync('./temp', { recursive: true });
|
|
50
|
+
if (load === 'all') {
|
|
51
|
+
const directoryPath = `./taxon/${genus}/`;
|
|
52
|
+
fs_1.default.readdir(directoryPath, (err, files) => {
|
|
53
|
+
if (err) {
|
|
54
|
+
spinner.stop();
|
|
55
|
+
console.error('Error reading directory:', err);
|
|
56
|
+
process.exit();
|
|
57
|
+
}
|
|
58
|
+
const taxa = files
|
|
59
|
+
.filter(file => file.endsWith('.ts') && file !== 'index.ts')
|
|
60
|
+
.map(file => path_1.default.parse(file).name);
|
|
61
|
+
const importStatements = taxa.map((species) => {
|
|
62
|
+
return `import { ${species.replace(/\s/g, '_').replace(/\-([a-z])/, (_, match) => match.toUpperCase())} } from '../taxon/${genus}/${species.replace(/\s/g, '_')}'`;
|
|
63
|
+
}).join('\n');
|
|
64
|
+
const speciesCall = taxa.map((species) => {
|
|
65
|
+
return ` ${species.replace(/\s/g, '_').replace(/\-([a-z])/, (_, match) => match.toUpperCase())},`;
|
|
66
|
+
}).join('\n');
|
|
67
|
+
const fileContent = `// Import genus ${genus}
|
|
68
|
+
import { ${genus} } from '../taxon/${genus}'
|
|
69
|
+
|
|
70
|
+
// Import species of ${genus}
|
|
71
|
+
${importStatements}
|
|
72
|
+
|
|
73
|
+
const ${genus}_species: ${genus}[] = [
|
|
74
|
+
${speciesCall}
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
// Export ${genus}DB.json
|
|
78
|
+
//import { writeFileSync } from 'fs'
|
|
79
|
+
const jsonData = JSON.stringify(${genus}_species);
|
|
80
|
+
console.log(jsonData)
|
|
81
|
+
//const inputFilePath = '../output/${genus}DB.json'
|
|
82
|
+
//writeFileSync(inputFilePath, jsonData, 'utf-8')
|
|
83
|
+
//console.log('\\x1b[1m\\x1b[32m✔ Process finished.\\x1b[0m')`;
|
|
84
|
+
const tempFilePath = './temp/exportTemp.ts';
|
|
85
|
+
fs_1.default.writeFileSync(tempFilePath, fileContent, 'utf-8');
|
|
86
|
+
const fileToTranspile = 'exportTemp';
|
|
87
|
+
(0, child_process_1.exec)(`tsc ./temp/${fileToTranspile}.ts`, (error, stdout, stderr) => {
|
|
88
|
+
if (stdout) {
|
|
89
|
+
spinner.stop();
|
|
90
|
+
console.error('\x1b[31m✖ TS Error:\x1b[0m\n\n' + `${stdout}`);
|
|
91
|
+
process.exit();
|
|
92
|
+
}
|
|
93
|
+
if (stderr) {
|
|
94
|
+
spinner.stop();
|
|
95
|
+
console.error('\x1b[31m✖ TS Error:\x1b[0m\n\n' + `${stderr}`);
|
|
96
|
+
process.exit();
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
fs_1.default.unlinkSync(`./temp/${fileToTranspile}.ts`);
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
spinner.stop();
|
|
103
|
+
console.error(`An error occurred while deleting the file: ${err}`);
|
|
104
|
+
process.exit();
|
|
105
|
+
}
|
|
106
|
+
(0, child_process_1.exec)(`node ./temp/${fileToTranspile}.js > ./output/${genus}DB.json`, (error, stdout, stderr) => {
|
|
107
|
+
// if (error) {
|
|
108
|
+
// spinner.stop()
|
|
109
|
+
// console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${error.message}`)
|
|
110
|
+
// process.exit()
|
|
111
|
+
// }
|
|
112
|
+
if (stdout) {
|
|
113
|
+
spinner.stop();
|
|
114
|
+
console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${stdout}`);
|
|
115
|
+
process.exit();
|
|
116
|
+
}
|
|
117
|
+
if (stderr) {
|
|
118
|
+
spinner.stop();
|
|
119
|
+
console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${stderr}`);
|
|
120
|
+
process.exit();
|
|
121
|
+
}
|
|
122
|
+
deleteJSFiles(`./taxon/${genus}`).then(() => {
|
|
123
|
+
const filePath = './output/';
|
|
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
|
+
spinner.stop();
|
|
126
|
+
try {
|
|
127
|
+
fs_1.default.unlinkSync(`./temp/${fileToTranspile}.js`);
|
|
128
|
+
fs_1.default.rm('./temp', { recursive: true }, (err) => {
|
|
129
|
+
if (err) {
|
|
130
|
+
console.error('Error deleting directory:', err);
|
|
131
|
+
process.exit();
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
catch (err) {
|
|
136
|
+
console.error(`An error occurred while deleting the file: ${err}`);
|
|
137
|
+
process.exit();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
if (load === 'csv') {
|
|
145
|
+
const inputFilePath = './input/taxaToExport.csv';
|
|
146
|
+
const tempFilePath = './temp/exportTemp.ts';
|
|
147
|
+
fs_1.default.createReadStream(inputFilePath)
|
|
148
|
+
.pipe((0, csv_parser_1.default)({ headers: false }))
|
|
149
|
+
.on('data', (data) => {
|
|
150
|
+
taxa.push(data['0']);
|
|
151
|
+
})
|
|
152
|
+
.on('end', () => __awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
const importStatements = taxa.map((species) => {
|
|
154
|
+
return `import { ${species.replace(/\s/g, '_').replace(/\-([a-z])/, (_, match) => match.toUpperCase())} } from '../taxon/${genus}/${species.replace(/\s/g, '_')}'`;
|
|
155
|
+
}).join('\n');
|
|
156
|
+
const speciesCall = taxa.map((species) => {
|
|
157
|
+
return ` ${species.replace(/\s/g, '_').replace(/\-([a-z])/, (_, match) => match.toUpperCase())},`;
|
|
158
|
+
}).join('\n');
|
|
159
|
+
const fileContent = `// Import genus ${genus}
|
|
160
|
+
import { ${genus} } from '../taxon/${genus}'
|
|
161
|
+
|
|
162
|
+
// Import species of ${genus}
|
|
163
|
+
${importStatements}
|
|
164
|
+
|
|
165
|
+
const ${genus}_species: ${genus}[] = [
|
|
166
|
+
${speciesCall}
|
|
167
|
+
]
|
|
168
|
+
|
|
169
|
+
// Export ${genus}DB.json
|
|
170
|
+
const jsonData = JSON.stringify(${genus}_species);
|
|
171
|
+
console.log(jsonData)
|
|
172
|
+
// import { writeFileSync } from 'fs'
|
|
173
|
+
// const jsonData = JSON.stringify(${genus}_species)
|
|
174
|
+
// const inputFilePath = '../output/${genus}DB.json'
|
|
175
|
+
// writeFileSync(inputFilePath, jsonData, 'utf-8')
|
|
176
|
+
// console.log('\\x1b[1m\\x1b[32m✔ Process finished.\\x1b[0m')`;
|
|
177
|
+
fs_1.default.writeFileSync(tempFilePath, fileContent, 'utf-8');
|
|
178
|
+
const fileToTranspile = 'exportTemp';
|
|
179
|
+
(0, child_process_1.exec)(`tsc ./temp/${fileToTranspile}.ts`, (error, stdout, stderr) => {
|
|
180
|
+
if (stdout) {
|
|
181
|
+
spinner.stop();
|
|
182
|
+
console.error('\x1b[31m✖ TS Error:\x1b[0m\n\n' + `${stdout}`);
|
|
183
|
+
process.exit();
|
|
184
|
+
}
|
|
185
|
+
if (stderr) {
|
|
186
|
+
spinner.stop();
|
|
187
|
+
console.error('\x1b[31m✖ TS Error:\x1b[0m\n\n' + `${stdout}`);
|
|
188
|
+
process.exit();
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
fs_1.default.unlinkSync(`./temp/${fileToTranspile}.ts`);
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
spinner.stop();
|
|
195
|
+
console.error(`An error occurred while deleting the file: ${err}`);
|
|
196
|
+
process.exit();
|
|
197
|
+
}
|
|
198
|
+
(0, child_process_1.exec)(`node ./temp/${fileToTranspile}.js > ./output/${genus}DB.json`, (error, stdout, stderr) => {
|
|
199
|
+
// if (error) {
|
|
200
|
+
// spinner.stop()
|
|
201
|
+
// console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${error.message}`)
|
|
202
|
+
// process.exit()
|
|
203
|
+
// }
|
|
204
|
+
if (stdout) {
|
|
205
|
+
spinner.stop();
|
|
206
|
+
console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${stdout}`);
|
|
207
|
+
process.exit();
|
|
208
|
+
}
|
|
209
|
+
if (stderr) {
|
|
210
|
+
spinner.stop();
|
|
211
|
+
console.error('\x1b[31m✖ JS execution time error:\x1b[0m\n\n' + `${stderr}`);
|
|
212
|
+
process.exit();
|
|
213
|
+
}
|
|
214
|
+
deleteJSFiles(`./taxon/${genus}`).then(() => {
|
|
215
|
+
const filePath = './output/';
|
|
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
|
+
spinner.stop();
|
|
218
|
+
try {
|
|
219
|
+
fs_1.default.unlinkSync(`./temp/${fileToTranspile}.js`);
|
|
220
|
+
fs_1.default.rm('./temp', { recursive: true }, (err) => {
|
|
221
|
+
if (err) {
|
|
222
|
+
console.error('Error deleting directory:', err);
|
|
223
|
+
process.exit();
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
catch (err) {
|
|
228
|
+
console.error(`An error occurred while deleting the file: ${err}`);
|
|
229
|
+
process.exit();
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
}));
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
exports.default = ttsExport;
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
const fs_1 = __importDefault(require("fs"));
|
|
39
|
+
const path_1 = __importDefault(require("path"));
|
|
40
|
+
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
41
|
+
const n3_1 = require("n3");
|
|
42
|
+
const { namedNode, literal, quad, blankNode } = n3_1.DataFactory;
|
|
43
|
+
const NS = {
|
|
44
|
+
tts: 'https://tts.example.org/ontology/',
|
|
45
|
+
inst: 'https://tts.example.org/instance/',
|
|
46
|
+
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
|
|
47
|
+
rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
|
|
48
|
+
owl: 'http://www.w3.org/2002/07/owl#',
|
|
49
|
+
skos: 'http://www.w3.org/2004/02/skos/core#',
|
|
50
|
+
xsd: 'http://www.w3.org/2001/XMLSchema#',
|
|
51
|
+
dcterms: 'http://purl.org/dc/terms/',
|
|
52
|
+
prov: 'http://www.w3.org/ns/prov#'
|
|
53
|
+
};
|
|
54
|
+
function tts(local) {
|
|
55
|
+
return namedNode(`${NS.tts}${encodeURIComponent(local)}`);
|
|
56
|
+
}
|
|
57
|
+
function inst(local) {
|
|
58
|
+
return namedNode(`${NS.inst}${encodeURIComponent(local)}`);
|
|
59
|
+
}
|
|
60
|
+
function rdf(local) {
|
|
61
|
+
return namedNode(`${NS.rdf}${local}`);
|
|
62
|
+
}
|
|
63
|
+
function rdfs(local) {
|
|
64
|
+
return namedNode(`${NS.rdfs}${local}`);
|
|
65
|
+
}
|
|
66
|
+
function owl(local) {
|
|
67
|
+
return namedNode(`${NS.owl}${local}`);
|
|
68
|
+
}
|
|
69
|
+
function skos(local) {
|
|
70
|
+
return namedNode(`${NS.skos}${local}`);
|
|
71
|
+
}
|
|
72
|
+
function xsd(local) {
|
|
73
|
+
return namedNode(`${NS.xsd}${local}`);
|
|
74
|
+
}
|
|
75
|
+
function dcterms(local) {
|
|
76
|
+
return namedNode(`${NS.dcterms}${local}`);
|
|
77
|
+
}
|
|
78
|
+
function prov(local) {
|
|
79
|
+
return namedNode(`${NS.prov}${local}`);
|
|
80
|
+
}
|
|
81
|
+
function createWriter() {
|
|
82
|
+
return new n3_1.Writer({
|
|
83
|
+
prefixes: {
|
|
84
|
+
tts: NS.tts,
|
|
85
|
+
inst: NS.inst,
|
|
86
|
+
rdf: NS.rdf,
|
|
87
|
+
rdfs: NS.rdfs,
|
|
88
|
+
owl: NS.owl,
|
|
89
|
+
skos: NS.skos,
|
|
90
|
+
xsd: NS.xsd,
|
|
91
|
+
dcterms: NS.dcterms,
|
|
92
|
+
prov: NS.prov
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function writerToString(writer) {
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
writer.end((err, result) => {
|
|
99
|
+
if (err)
|
|
100
|
+
reject(err);
|
|
101
|
+
else
|
|
102
|
+
resolve(result);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function isClassLike(value) {
|
|
107
|
+
return typeof value === 'function' && !!value.prototype;
|
|
108
|
+
}
|
|
109
|
+
function isPrimitive(value) {
|
|
110
|
+
return (typeof value === 'string' ||
|
|
111
|
+
typeof value === 'number' ||
|
|
112
|
+
typeof value === 'boolean');
|
|
113
|
+
}
|
|
114
|
+
function normalizeDatatype(datatype) {
|
|
115
|
+
if (!datatype)
|
|
116
|
+
return null;
|
|
117
|
+
if (datatype.startsWith('xsd:')) {
|
|
118
|
+
return namedNode(datatype.replace('xsd:', NS.xsd));
|
|
119
|
+
}
|
|
120
|
+
if (datatype.startsWith('http://') || datatype.startsWith('https://')) {
|
|
121
|
+
return namedNode(datatype);
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
function addMappings(writer, subject, mappings) {
|
|
126
|
+
if (!(mappings === null || mappings === void 0 ? void 0 : mappings.length))
|
|
127
|
+
return;
|
|
128
|
+
for (const m of mappings) {
|
|
129
|
+
writer.addQuad(subject, skos(m.relation), namedNode(m.iri));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function toPascalLike(input) {
|
|
133
|
+
return input
|
|
134
|
+
.replace(/\.ts$/, '')
|
|
135
|
+
.replace(/[_\-\s]+([a-zA-Z0-9])/g, (_, c) => c.toUpperCase())
|
|
136
|
+
.replace(/^[a-z]/, c => c.toUpperCase());
|
|
137
|
+
}
|
|
138
|
+
function localId(value, fallback) {
|
|
139
|
+
var _a, _b;
|
|
140
|
+
return (_b = (_a = value === null || value === void 0 ? void 0 : value._id) !== null && _a !== void 0 ? _a : value === null || value === void 0 ? void 0 : value.id) !== null && _b !== void 0 ? _b : fallback;
|
|
141
|
+
}
|
|
142
|
+
function primitiveToLiteral(value) {
|
|
143
|
+
if (typeof value === 'string')
|
|
144
|
+
return literal(value);
|
|
145
|
+
if (typeof value === 'number')
|
|
146
|
+
return literal(String(value), xsd('decimal'));
|
|
147
|
+
return literal(String(value), xsd('boolean'));
|
|
148
|
+
}
|
|
149
|
+
function importModules(pattern) {
|
|
150
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
151
|
+
const files = yield (0, fast_glob_1.default)(pattern, { absolute: true });
|
|
152
|
+
const modules = [];
|
|
153
|
+
for (const file of files) {
|
|
154
|
+
const mod = yield Promise.resolve(`${toFileUrl(file)}`).then(s => __importStar(require(s)));
|
|
155
|
+
modules.push({ file, mod });
|
|
156
|
+
}
|
|
157
|
+
return modules;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
function toFileUrl(filePath) {
|
|
161
|
+
return `file://${path_1.default.resolve(filePath)}`;
|
|
162
|
+
}
|
|
163
|
+
function inferRangeFromValue(value) {
|
|
164
|
+
if (typeof value === 'number')
|
|
165
|
+
return xsd('decimal');
|
|
166
|
+
if (typeof value === 'boolean')
|
|
167
|
+
return xsd('boolean');
|
|
168
|
+
if (typeof value === 'string')
|
|
169
|
+
return xsd('string');
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
function serializeSourceLikeArray(writer, subject, values) {
|
|
173
|
+
for (const v of values) {
|
|
174
|
+
if (typeof v === 'string') {
|
|
175
|
+
writer.addQuad(subject, dcterms('source'), literal(v));
|
|
176
|
+
}
|
|
177
|
+
else if (v && typeof v === 'object') {
|
|
178
|
+
const refNode = blankNode();
|
|
179
|
+
writer.addQuad(subject, dcterms('source'), refNode);
|
|
180
|
+
for (const [rk, rv] of Object.entries(v)) {
|
|
181
|
+
if (rv == null)
|
|
182
|
+
continue;
|
|
183
|
+
if (typeof rv === 'string') {
|
|
184
|
+
writer.addQuad(refNode, tts(rk), literal(rv));
|
|
185
|
+
}
|
|
186
|
+
else if (typeof rv === 'number') {
|
|
187
|
+
writer.addQuad(refNode, tts(rk), literal(String(rv), xsd('decimal')));
|
|
188
|
+
}
|
|
189
|
+
else if (typeof rv === 'boolean') {
|
|
190
|
+
writer.addQuad(refNode, tts(rk), literal(String(rv), xsd('boolean')));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function serializeObject(writer, subject, object, objectTypeName, objectId, visited) {
|
|
197
|
+
var _a, _b, _c, _d;
|
|
198
|
+
if (visited.has(object))
|
|
199
|
+
return;
|
|
200
|
+
visited.add(object);
|
|
201
|
+
writer.addQuad(subject, rdf('type'), tts(objectTypeName));
|
|
202
|
+
for (const [key, value] of Object.entries(object)) {
|
|
203
|
+
if (value == null)
|
|
204
|
+
continue;
|
|
205
|
+
if (key.startsWith('_'))
|
|
206
|
+
continue;
|
|
207
|
+
if (key === 'sources' && Array.isArray(value)) {
|
|
208
|
+
serializeSourceLikeArray(writer, subject, value);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const predicate = tts(key);
|
|
212
|
+
if (isPrimitive(value)) {
|
|
213
|
+
writer.addQuad(subject, predicate, primitiveToLiteral(value));
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (Array.isArray(value)) {
|
|
217
|
+
for (let i = 0; i < value.length; i++) {
|
|
218
|
+
const item = value[i];
|
|
219
|
+
if (item == null)
|
|
220
|
+
continue;
|
|
221
|
+
if (isPrimitive(item)) {
|
|
222
|
+
writer.addQuad(subject, predicate, primitiveToLiteral(item));
|
|
223
|
+
}
|
|
224
|
+
else if (typeof item === 'object') {
|
|
225
|
+
const childId = localId(item, `${objectId}-${key}-${i + 1}`);
|
|
226
|
+
const childNode = inst(childId);
|
|
227
|
+
writer.addQuad(subject, predicate, childNode);
|
|
228
|
+
serializeObject(writer, childNode, item, (_b = (_a = item.constructor) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : toPascalLike(key), childId, visited);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (typeof value === 'object') {
|
|
234
|
+
const childId = localId(value, `${objectId}-${key}`);
|
|
235
|
+
const childNode = inst(childId);
|
|
236
|
+
writer.addQuad(subject, predicate, childNode);
|
|
237
|
+
serializeObject(writer, childNode, value, (_d = (_c = value.constructor) === null || _c === void 0 ? void 0 : _c.name) !== null && _d !== void 0 ? _d : toPascalLike(key), childId, visited);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function buildOntologyTTL() {
|
|
242
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
243
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
244
|
+
const writer = createWriter();
|
|
245
|
+
const modules = yield importModules('./characters/v1/**/*.ts');
|
|
246
|
+
for (const { mod } of modules) {
|
|
247
|
+
for (const exported of Object.values(mod)) {
|
|
248
|
+
if (!isClassLike(exported))
|
|
249
|
+
continue;
|
|
250
|
+
const cls = exported;
|
|
251
|
+
const classNode = tts(cls.name);
|
|
252
|
+
writer.addQuad(classNode, rdf('type'), owl('Class'));
|
|
253
|
+
writer.addQuad(classNode, rdfs('label'), literal((_b = (_a = cls.semantic) === null || _a === void 0 ? void 0 : _a.label) !== null && _b !== void 0 ? _b : cls.name));
|
|
254
|
+
if ((_c = cls.semantic) === null || _c === void 0 ? void 0 : _c.definition) {
|
|
255
|
+
writer.addQuad(classNode, rdfs('comment'), literal(cls.semantic.definition));
|
|
256
|
+
}
|
|
257
|
+
addMappings(writer, classNode, (_d = cls.semantic) === null || _d === void 0 ? void 0 : _d.ontologyMappings);
|
|
258
|
+
const attrs = (_e = cls.semanticAttributes) !== null && _e !== void 0 ? _e : {};
|
|
259
|
+
for (const [attrName, meta] of Object.entries(attrs)) {
|
|
260
|
+
const propNode = tts(attrName);
|
|
261
|
+
const kind = meta.kind === 'object' ? owl('ObjectProperty') : owl('DatatypeProperty');
|
|
262
|
+
writer.addQuad(propNode, rdf('type'), kind);
|
|
263
|
+
writer.addQuad(propNode, rdfs('domain'), classNode);
|
|
264
|
+
writer.addQuad(propNode, rdfs('label'), literal((_f = meta.label) !== null && _f !== void 0 ? _f : attrName));
|
|
265
|
+
if (meta.definition) {
|
|
266
|
+
writer.addQuad(propNode, rdfs('comment'), literal(meta.definition));
|
|
267
|
+
}
|
|
268
|
+
const dt = normalizeDatatype(meta.datatype);
|
|
269
|
+
if (dt) {
|
|
270
|
+
writer.addQuad(propNode, rdfs('range'), dt);
|
|
271
|
+
}
|
|
272
|
+
if (meta.unit) {
|
|
273
|
+
writer.addQuad(propNode, dcterms('format'), literal(meta.unit));
|
|
274
|
+
}
|
|
275
|
+
addMappings(writer, propNode, meta.ontologyMappings);
|
|
276
|
+
if ((_g = meta.allowedValues) === null || _g === void 0 ? void 0 : _g.length) {
|
|
277
|
+
for (const value of meta.allowedValues) {
|
|
278
|
+
writer.addQuad(propNode, tts('allowedValue'), literal(value));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return writerToString(writer);
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
function buildInstancesTTL(genus) {
|
|
288
|
+
var _a, _b;
|
|
289
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
290
|
+
const writer = createWriter();
|
|
291
|
+
const modules = yield importModules(`./taxon/${genus}/**/*.ts`);
|
|
292
|
+
const visited = new WeakSet();
|
|
293
|
+
for (const { file, mod } of modules) {
|
|
294
|
+
const fileName = path_1.default.basename(file, '.ts');
|
|
295
|
+
for (const [exportName, exported] of Object.entries(mod)) {
|
|
296
|
+
if (!exported || typeof exported !== 'object')
|
|
297
|
+
continue;
|
|
298
|
+
const subjectId = localId(exported, exportName || fileName);
|
|
299
|
+
const subjectNode = inst(subjectId);
|
|
300
|
+
serializeObject(writer, subjectNode, exported, (_b = (_a = exported.constructor) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : toPascalLike(fileName), subjectId, visited);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return writerToString(writer);
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
function mergeTTL(ttls) {
|
|
307
|
+
const nonEmpty = ttls.filter(Boolean);
|
|
308
|
+
if (!nonEmpty.length)
|
|
309
|
+
return '';
|
|
310
|
+
const prefixLines = new Set();
|
|
311
|
+
const bodies = [];
|
|
312
|
+
for (const ttl of nonEmpty) {
|
|
313
|
+
const lines = ttl.split('\n');
|
|
314
|
+
const localBody = [];
|
|
315
|
+
for (const line of lines) {
|
|
316
|
+
if (line.startsWith('@prefix')) {
|
|
317
|
+
prefixLines.add(line);
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
localBody.push(line);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
bodies.push(localBody.join('\n').trim());
|
|
324
|
+
}
|
|
325
|
+
return `${Array.from(prefixLines).join('\n')}\n\n${bodies.join('\n\n')}\n`;
|
|
326
|
+
}
|
|
327
|
+
function ttsExportOntology(genus) {
|
|
328
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
329
|
+
if (!genus) {
|
|
330
|
+
console.error('\x1b[31m✖ Argument `--genus` cannot be empty.\x1b[0m');
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (!fs_1.default.existsSync('./characters/v1')) {
|
|
334
|
+
console.error('\x1b[31m✖ Directory ./characters/v1 not found.\x1b[0m');
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (!fs_1.default.existsSync(`./taxon/${genus}`)) {
|
|
338
|
+
console.error(`\x1b[31m✖ Directory ./taxon/${genus} not found.\x1b[0m`);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
fs_1.default.mkdirSync('./output', { recursive: true });
|
|
342
|
+
const ontologyTTL = yield buildOntologyTTL();
|
|
343
|
+
const instancesTTL = yield buildInstancesTTL(genus);
|
|
344
|
+
const semanticTTL = mergeTTL([ontologyTTL, instancesTTL]);
|
|
345
|
+
const ontologyPath = `./output/${genus}Ontology.ttl`;
|
|
346
|
+
const instancesPath = `./output/${genus}Instances.ttl`;
|
|
347
|
+
const semanticPath = `./output/${genus}Semantic.ttl`;
|
|
348
|
+
fs_1.default.writeFileSync(ontologyPath, ontologyTTL, 'utf8');
|
|
349
|
+
fs_1.default.writeFileSync(instancesPath, instancesTTL, 'utf8');
|
|
350
|
+
fs_1.default.writeFileSync(semanticPath, semanticTTL, 'utf8');
|
|
351
|
+
console.log(`\x1b[1m\x1b[32m✔ Ontology schema exported: \x1b[33m${ontologyPath}\x1b[0m`);
|
|
352
|
+
console.log(`\x1b[1m\x1b[32m✔ RDF instances exported: \x1b[33m${instancesPath}\x1b[0m`);
|
|
353
|
+
console.log(`\x1b[1m\x1b[32m✔ Combined semantic graph exported: \x1b[33m${semanticPath}\x1b[0m`);
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
exports.default = ttsExportOntology;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
8
|
+
function ttsExportSources(genus) {
|
|
9
|
+
if (genus === '') {
|
|
10
|
+
console.error('\x1b[31m✖ Argument `--genus` cannot be empty.\x1b[0m');
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
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[36mℹ️ Please run \x1b[33m`tts init`\x1b[36m before attempting to export a database.\x1b[0m");
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const filePath = `./output/${genus}DB.json`;
|
|
18
|
+
fs_1.default.readFile(filePath, 'utf8', (err, data) => {
|
|
19
|
+
if (err) {
|
|
20
|
+
console.error('Error reading the file:', err);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const jsonData = JSON.parse(data);
|
|
25
|
+
const findObjectsWithSources = (obj, currentPath = []) => {
|
|
26
|
+
let objectsWithSources = [];
|
|
27
|
+
const findObjectsWithSourcesRecursively = (currentObj, path) => {
|
|
28
|
+
if (lodash_1.default.isObject(currentObj)) {
|
|
29
|
+
lodash_1.default.forOwn(currentObj, (value, key) => {
|
|
30
|
+
if (key === 'sources' && Array.isArray(value) && value.length > 0) {
|
|
31
|
+
value.forEach((source) => {
|
|
32
|
+
objectsWithSources.push({
|
|
33
|
+
index: path[0],
|
|
34
|
+
path: path.join('.'),
|
|
35
|
+
specificEpithet: lodash_1.default.get(jsonData[path[0]], 'specificEpithet'),
|
|
36
|
+
source: source
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (lodash_1.default.isObject(value)) {
|
|
41
|
+
findObjectsWithSourcesRecursively(value, [...path, key]);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
findObjectsWithSourcesRecursively(obj, currentPath);
|
|
47
|
+
objectsWithSources.forEach(item => {
|
|
48
|
+
item.path = item.path.replace(new RegExp(`^${item.index}\\.|${item.index}$`), '');
|
|
49
|
+
});
|
|
50
|
+
return objectsWithSources;
|
|
51
|
+
};
|
|
52
|
+
const objectsWithSources = findObjectsWithSources(jsonData.map((item, index) => (Object.assign(Object.assign({}, item), { index }))));
|
|
53
|
+
const filePathOutput = `./output/${genus}SourcesDB.json`;
|
|
54
|
+
const jsonContent = JSON.stringify(objectsWithSources, null, 2);
|
|
55
|
+
fs_1.default.writeFileSync(filePathOutput, jsonContent, 'utf-8');
|
|
56
|
+
console.log(`\x1b[1m\x1b[32m✔ Database exported: \x1b[33m${filePathOutput}\x1b[0m\x1b[1m\x1b[32m\x1b[0m`);
|
|
57
|
+
}
|
|
58
|
+
catch (jsonErr) {
|
|
59
|
+
console.error('Error parsing JSON:', jsonErr);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
exports.default = ttsExportSources;
|