-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (89 loc) · 2.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import fs from "fs";
import { cwd, exit, argv } from "process";
// Input file requiredd structure (output of the elven-tools collection-nft-owners).
// Example:
// [
// {
// "owner": "erd1fz05226fmg8m964pxg208v8vupvj27n642tarnu346099c7fwpzsnq5r4j",
// "tokens": [
// {
// "identifier": "EAPES-8f3c1f-1ca2",
// "metadataFileName": "7329"
// },
// {
// "identifier": "EAPES-8f3c1f-1bf9",
// "metadataFileName": "7160"
// }
// ],
// "tokensCount": 2
// }
// ]
// The input file whic is the output from the 'elven-tools collection-nft-owners' command
const inputFile = `${cwd()}/nft-collection-owners.json`;
try {
fs.accessSync(inputFile, fs.constants.R_OK | fs.constants.W_OK);
} catch (e) {
console.log(e.message);
console.log("\nPlease provide the nft-collection-owners.json file\n");
exit(9);
}
// Predefined filters flags
const filtersFlags = ["identifiers", "fileNames"];
const getFilters = () => {
if (argv.length > 2) {
return argv.filter(
(arg) =>
arg.includes(`${filtersFlags[0]}=`) ||
arg.includes(`${filtersFlags[1]}=`)
);
}
};
const getFilterKeywords = (filters) => {
if (Array.isArray(filters)) {
const obj = {};
for (let filter of filters) {
const split = filter.split("=");
obj[split[0]] = split[1].split(",");
}
return obj;
}
};
// Main filter function if filters are set
const filterTokens = (tokensArr) => {
const filtersKeysObj = getFilterKeywords(getFilters());
if (Array.isArray(tokensArr)) {
return tokensArr.filter((token) => {
if (
!filtersKeysObj ||
Object.keys(filtersKeysObj).length === 0 ||
(filtersKeysObj.fileNames &&
filtersKeysObj.fileNames.some((item) => item === token.metadataFileName)) ||
(filtersKeysObj.identifiers &&
filtersKeysObj.identifiers.some(
(item) => item === token.identifier
))
) {
return true;
}
return false;
});
}
return false;
};
const ownersFile = fs.readFileSync(inputFile, { encoding: "utf8" });
const ownersJSON = JSON.parse(ownersFile);
if (ownersJSON) {
const CSV = ownersJSON
.map((item) => {
return filterTokens(item.tokens)
.map((token) => {
return `${item.owner},${token.identifier},${token.metadataFileName}`;
})
.join("\r\n");
})
.join("\r\n");
fs.writeFileSync(`${cwd()}/output.csv`, CSV);
console.log("Saved the output to the CSV file: data/output.csv");
} else {
console.log("There is no JSON input file!");
}