@larsgw/formica 0.8.5 → 0.8.7

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.
@@ -1,16 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
- var __assign = (this && this.__assign) || function () {
4
- __assign = Object.assign || function(t) {
5
- for (var s, i = 1, n = arguments.length; i < n; i++) {
6
- s = arguments[i];
7
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
8
- t[p] = s[p];
9
- }
10
- return t;
11
- };
12
- return __assign.apply(this, arguments);
13
- };
14
3
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
4
  if (k2 === undefined) k2 = k;
16
5
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -53,45 +42,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
53
42
  step((generator = generator.apply(thisArg, _arguments || [])).next());
54
43
  });
55
44
  };
56
- var __generator = (this && this.__generator) || function (thisArg, body) {
57
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
58
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
59
- function verb(n) { return function (v) { return step([n, v]); }; }
60
- function step(op) {
61
- if (f) throw new TypeError("Generator is already executing.");
62
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
63
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
64
- if (y = 0, t) op = [op[0] & 2, t.value];
65
- switch (op[0]) {
66
- case 0: case 1: t = op; break;
67
- case 4: _.label++; return { value: op[1], done: false };
68
- case 5: _.label++; y = op[1]; op = [0]; continue;
69
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
70
- default:
71
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
72
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
73
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
74
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
75
- if (t[2]) _.ops.pop();
76
- _.trys.pop(); continue;
77
- }
78
- op = body.call(thisArg, _);
79
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
80
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
81
- }
82
- };
83
45
  Object.defineProperty(exports, "__esModule", { value: true });
84
- var fs_1 = require("fs");
85
- var path = __importStar(require("path"));
86
- var index_1 = require("../index");
87
- var util_1 = require("./util");
46
+ const fs_1 = require("fs");
47
+ const path = __importStar(require("path"));
48
+ const index_1 = require("../index");
49
+ const util_1 = require("./util");
88
50
  function alphabeticSort(a, b) {
89
51
  return a > b ? 1 : a < b ? -1 : 0;
90
52
  }
91
53
  function sortObject(object, sorter) {
92
- var sorted = {};
93
- for (var _i = 0, _a = Object.keys(object).sort(sorter !== null && sorter !== void 0 ? sorter : util_1.numericSort); _i < _a.length; _i++) {
94
- var key = _a[_i];
54
+ const sorted = {};
55
+ for (const key of Object.keys(object).sort(sorter !== null && sorter !== void 0 ? sorter : util_1.numericSort)) {
95
56
  sorted[key] = object[key];
96
57
  }
97
58
  return sorted;
@@ -104,93 +65,61 @@ function addTaxon(index, id, taxon) {
104
65
  index[id].sort(util_1.numericSort);
105
66
  }
106
67
  function main(args) {
107
- return __awaiter(this, void 0, void 0, function () {
108
- var REPO_ROOT, files, gbifIndex, colIndex, resourceIndex;
109
- return __generator(this, function (_a) {
110
- switch (_a.label) {
111
- case 0:
112
- REPO_ROOT = path.resolve(args[0]);
113
- return [4 /*yield*/, fs_1.promises.readdir(path.join(REPO_ROOT, 'txt'))];
114
- case 1:
115
- files = _a.sent();
116
- gbifIndex = {};
117
- colIndex = {};
118
- resourceIndex = {};
119
- return [4 /*yield*/, Promise.all(files.map(function (fileName) {
120
- return __awaiter(this, void 0, void 0, function () {
121
- var id, file;
122
- return __generator(this, function (_a) {
123
- switch (_a.label) {
124
- case 0:
125
- if (!fileName.endsWith('.txt')) {
126
- return [2 /*return*/];
127
- }
128
- id = fileName.slice(0, -4);
129
- return [4 /*yield*/, fs_1.promises.readFile(path.join(REPO_ROOT, 'txt', fileName), 'utf-8')];
130
- case 1:
131
- file = _a.sent();
132
- return [2 /*return*/, Promise.all(index_1.resources.parseTextFileHeader(file).map(function (resource, index) {
133
- return __awaiter(this, void 0, void 0, function () {
134
- var amendedResource, dwcFile, _a, header, dwc, _b, _c, gbifColumn, gbifAcceptedColumn, colColumn, colAcceptedColumn, _i, dwc_1, taxon, gbifId, colId;
135
- return __generator(this, function (_d) {
136
- switch (_d.label) {
137
- case 0:
138
- amendedResource = __assign(__assign({}, resource), { id: "".concat(id, ":").concat(index + 1), taxonCount: 0 });
139
- dwcFile = path.join(REPO_ROOT, 'dwc', "".concat(id, "-").concat(index + 1, ".csv"));
140
- if (!(0, fs_1.existsSync)(dwcFile)) {
141
- return [2 /*return*/];
142
- }
143
- _c = (_b = index_1.csv).parseCsv;
144
- return [4 /*yield*/, fs_1.promises.readFile(dwcFile, 'utf-8')];
145
- case 1:
146
- _a = _c.apply(_b, [_d.sent()]), header = _a[0], dwc = _a.slice(1);
147
- gbifColumn = header.indexOf('gbifTaxonID');
148
- gbifAcceptedColumn = header.indexOf('gbifAcceptedTaxonID');
149
- colColumn = header.indexOf('colTaxonID');
150
- colAcceptedColumn = header.indexOf('colAcceptedTaxonID');
151
- for (_i = 0, dwc_1 = dwc; _i < dwc_1.length; _i++) {
152
- taxon = dwc_1[_i];
153
- gbifId = taxon[gbifColumn];
154
- if (gbifId) {
155
- addTaxon(gbifIndex, gbifId, taxon);
156
- if (taxon[gbifAcceptedColumn] !== taxon[gbifColumn]) {
157
- addTaxon(gbifIndex, taxon[gbifAcceptedColumn], taxon);
158
- }
159
- }
160
- colId = taxon[colColumn];
161
- if (colId) {
162
- addTaxon(colIndex, colId, taxon);
163
- if (taxon[colAcceptedColumn] !== taxon[colColumn]) {
164
- addTaxon(colIndex, taxon[colAcceptedColumn], taxon);
165
- }
166
- }
167
- amendedResource.taxonCount += 1;
168
- }
169
- resourceIndex[amendedResource.id] = amendedResource;
170
- return [2 /*return*/];
171
- }
172
- });
173
- });
174
- }))];
175
- }
176
- });
177
- });
178
- }))];
179
- case 2:
180
- _a.sent();
181
- return [4 /*yield*/, Promise.all([
182
- fs_1.promises.writeFile(path.join(REPO_ROOT, 'gbif.index.json'), JSON.stringify(sortObject(gbifIndex), null, 2)),
183
- fs_1.promises.writeFile(path.join(REPO_ROOT, 'col.index.json'), JSON.stringify(sortObject(colIndex, alphabeticSort), null, 2)),
184
- fs_1.promises.writeFile(path.join(REPO_ROOT, 'index.json'), JSON.stringify(sortObject(resourceIndex), null, 2))
185
- ])];
186
- case 3:
187
- _a.sent();
188
- return [2 /*return*/];
189
- }
190
- });
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ const REPO_ROOT = path.resolve(args[0]);
70
+ const files = yield fs_1.promises.readdir(path.join(REPO_ROOT, 'txt'));
71
+ const gbifIndex = {};
72
+ const colIndex = {};
73
+ const resourceIndex = {};
74
+ yield Promise.all(files.map(function (fileName) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ if (!fileName.endsWith('.txt')) {
77
+ return;
78
+ }
79
+ const id = fileName.slice(0, -4);
80
+ const file = yield fs_1.promises.readFile(path.join(REPO_ROOT, 'txt', fileName), 'utf-8');
81
+ return Promise.all(index_1.resources.parseTextFileHeader(file).map(function (resource, index) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ const amendedResource = Object.assign(Object.assign({}, resource), { id: `${id}:${index + 1}`, taxonCount: 0 });
84
+ const dwcFile = path.join(REPO_ROOT, 'dwc', `${id}-${index + 1}.csv`);
85
+ if (!(0, fs_1.existsSync)(dwcFile)) {
86
+ return;
87
+ }
88
+ const [header, ...dwc] = index_1.csv.parseCsv(yield fs_1.promises.readFile(dwcFile, 'utf-8'));
89
+ const gbifColumn = header.indexOf('gbifTaxonID');
90
+ const gbifAcceptedColumn = header.indexOf('gbifAcceptedTaxonID');
91
+ const colColumn = header.indexOf('colTaxonID');
92
+ const colAcceptedColumn = header.indexOf('colAcceptedTaxonID');
93
+ for (const taxon of dwc) {
94
+ const gbifId = taxon[gbifColumn];
95
+ if (gbifId) {
96
+ addTaxon(gbifIndex, gbifId, taxon);
97
+ if (taxon[gbifAcceptedColumn] !== taxon[gbifColumn]) {
98
+ addTaxon(gbifIndex, taxon[gbifAcceptedColumn], taxon);
99
+ }
100
+ }
101
+ const colId = taxon[colColumn];
102
+ if (colId) {
103
+ addTaxon(colIndex, colId, taxon);
104
+ if (taxon[colAcceptedColumn] !== taxon[colColumn]) {
105
+ addTaxon(colIndex, taxon[colAcceptedColumn], taxon);
106
+ }
107
+ }
108
+ amendedResource.taxonCount += 1;
109
+ }
110
+ resourceIndex[amendedResource.id] = amendedResource;
111
+ });
112
+ }));
113
+ });
114
+ }));
115
+ yield Promise.all([
116
+ fs_1.promises.writeFile(path.join(REPO_ROOT, 'gbif.index.json'), JSON.stringify(sortObject(gbifIndex), null, 2)),
117
+ fs_1.promises.writeFile(path.join(REPO_ROOT, 'col.index.json'), JSON.stringify(sortObject(colIndex, alphabeticSort), null, 2)),
118
+ fs_1.promises.writeFile(path.join(REPO_ROOT, 'index.json'), JSON.stringify(sortObject(resourceIndex), null, 2))
119
+ ]);
191
120
  });
192
121
  }
193
- main(process.argv.slice(2)).catch(function (error) {
122
+ main(process.argv.slice(2)).catch(error => {
194
123
  console.error(error);
195
124
  process.exit(1);
196
125
  });