-
Notifications
You must be signed in to change notification settings - Fork 34
/
add-studio.ts
123 lines (115 loc) · 3.24 KB
/
add-studio.ts
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { Issue, CreativeTechnologist } from "./types";
import { list } from "./groups";
const fs = require("fs");
function insertStudio(data: string[], insertText: string, lineNumber: number) {
data.splice(lineNumber, 0, insertText);
const text = data.join("\n");
fs.writeFile("groups.ts", text, (err: Error) => {
if (err) {
console.log(err);
}
});
}
export function parseIssue(input: String): Issue {
const issue = {} as Issue;
// split issue into sections and skip the first item which is just '###'
const sections = input.split("###").slice(1);
for (let i = 0; i < sections.length; i += 1) {
const section = sections[i];
const els = section.split("\n");
const entry = els.slice(1).join("").trim();
switch (i) {
case 0:
issue.type = entry;
break;
case 1:
issue.name = entry;
break;
case 2:
issue.keywords = entry;
break;
case 3:
issue.website = entry;
break;
case 4:
if (!entry.toLowerCase().includes("no response")) {
issue.careers = entry;
}
break;
case 5: {
const regex = /(".*?"|[^",\s]+)(?=\s*,|\s*$)/g;
const found = entry.match(regex);
if (found) {
for (let j = 0; j < found.length; j += 1) {
found[j] = found[j].replace(/"/g, "");
}
issue.locations = found;
} else {
issue.locations = [];
}
break;
}
default:
break;
}
}
return issue;
}
export function addStudio(issue: Issue) {
const tech: CreativeTechnologist = {
keywords: issue.keywords,
link: issue.website,
locations: issue.locations,
};
if (issue.careers) {
tech.careerLink = issue.careers;
}
for (let section = 0; section < list.length; section += 1) {
if (issue.type === list[section].title) {
const data = fs.readFileSync("groups.ts").toString().split("\n");
const studioString = `"${issue.name}": ${JSON.stringify(tech)},`;
const studios = Object.keys(list[section].rows);
// try to insert studio
for (let i = 0; i < studios.length; i += 1) {
const otherStudio = studios[i];
// if this studio needs to be inserted before another studio
if (issue.name.toLowerCase() < otherStudio.toLowerCase()) {
console.log(`${issue.name} should go before ${otherStudio}`);
for (let j = 0; j < data.length; j += 1) {
if (data[j].includes(`"${otherStudio}"`)) {
console.log(`Adding in at at line ${j}`);
insertStudio(data, studioString, j);
return;
}
}
}
}
// otherwise append
const lastStudio = studios[studios.length - 1];
for (let i = 0; i < data.length; i += 1) {
if (data[i].includes(`"${lastStudio}"`)) {
console.log(`This item will be last at line ${i}`);
insertStudio(data, studioString, i);
return;
}
}
}
}
}
// const issue: Issue = {
// careers: "https://brig.ht/contact",
// keywords: "data visualization, digital installations, experiential sites, video games",
// locations: "Paris",
// name: "Bright",
// type: "Creative Technology",
// website: "https://brig.ht/",
// };
process.argv.shift(); // skip node.exe
process.argv.shift(); // skip name of js file
const input = process.argv.join(" ");
// console.log("Input is:");
// console.log(input);
const issue = parseIssue(input);
console.log(`Add from issue:`);
console.log(issue);
addStudio(issue);