-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2org.js
199 lines (182 loc) · 4.47 KB
/
csv2org.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import fs from 'fs';
import fastcsv from 'fast-csv';
if (process.argv.length < 2) {
console.error('Usage: node csv2org.js <filename>');
process.exit(1);
}
const filename = process.argv[2];
const orgName = filename.split('-').at(-1).trim().split('.')[0];
const stream = fs.createReadStream(process.argv[2]);
const rows = [];
let headers = null;
const csv = fastcsv
.parse({ headers: true })
.on('data', function (row) {
rows.push(row);
})
.on(
'end',
function () {
convert(rows);
}
)
.on('headers', (heads) => {
headers = heads;
})
.on('error', function (error) {
console.error(error);
});
function roleNameToID(name) {
return name.replaceAll(' ', '').toLowerCase();
}
stream.pipe(csv);
function convert(rows) {
const roleDescriptionAndTeams = rows[0];
const withoutTallies = rows.slice(5);
function getTeamName(content) {
return (content.split('|')[1] ?? '').trim();
}
function getTeamID(name) {
return name.replaceAll(' ', '').toLowerCase();
}
const teams = [];
for (const key in roleDescriptionAndTeams) {
const content = roleDescriptionAndTeams[key].trim();
if (content !== '') {
const name = getTeamName(content);
if (teams.find((team) => team.name === name) === undefined)
teams.push({
id: getTeamID(name),
name: name,
description: ''
});
}
}
const processes = [];
const concerns = [];
let statuses = [];
for (const row of withoutTallies) {
if (/Proposed|Discussed|Pending|Final/.test(row['Status'])) {
statuses = [...statuses, row];
processes.push({
id: '' + processes.length,
icon: '',
organization: 'ischool',
concern: concerns.at(-1).id,
start: null,
repeat: null,
title: row['Task'],
status: row['Status'],
accountable: roleNameToID(Object.keys(row).find((key) => row[key] === 'A') ?? ''),
responsible: Object.keys(row)
.filter((key) => row[key] === 'R')
.map((name) => roleNameToID(name)),
consulted: Object.keys(row)
.filter((key) => row[key] === 'C')
.map((name) => roleNameToID(name)),
informed: Object.keys(row)
.filter((key) => row[key] === 'I')
.map((name) => roleNameToID(name)),
what: row['Notes'],
how: [],
visibility: 'public',
revisions: []
});
} else {
const [name, description] = row['Status'].split('\n');
concerns.push({ id: name.toLowerCase(), name, description });
}
}
const roles = [];
const orgPeople = [];
// Skip the first three columns
for (const header of headers.slice(3)) {
const title = header.split('|')[0].trim();
const people = (header.split('|')[1] ?? '')
.trim()
.split(',')
.map((name) => name.trim());
const rolePeople = [];
for (const name of people) {
if (name && name.length > 0) {
const person = {
id: name.toLowerCase(),
organization: 'ischool',
name: name,
bio: '',
email: '',
icon: '',
supervisor: null
};
orgPeople.push(person);
rolePeople.push(person.id);
}
}
roles.push({
id: roleNameToID(header),
organization: 'ischool',
title: title,
description: roleDescriptionAndTeams[header].split('|')[0].trim(),
people: rolePeople,
team: getTeamID(getTeamName(roleDescriptionAndTeams[header])),
status: 'Proposed',
visibility: 'public',
revisions: []
});
}
const data = {
organizations: [
{
id: 'ischool',
name: orgName,
description:
'The *academics enterprise* encompasses every aspect of teaching, learning, and student experience in the school, with the broad goal of equitable, inclusive, and justice-centered information education.',
admins: [],
staff: [],
teams: teams,
concerns: concerns,
statuses: [
{
id: 'Draft',
name: 'Drafted',
description: 'Still working on it'
},
{
id: 'Discussed',
name: 'Discussed',
description: 'Discussed with stakeholders'
},
{
id: 'Proposed',
name: 'Proposed',
description: 'Ready for feedback'
},
{
id: 'Approved',
name: 'Approved',
description: 'Approved and ready to implement'
},
{
id: 'Implemented',
name: 'Implemented',
description: 'Implemented'
}
],
visibility: 'public',
revisions: []
}
],
processes,
roles,
changes: [],
people: orgPeople
};
const jsonData = JSON.stringify(data);
fs.writeFile('src/database/mock.json', jsonData, (err) => {
if (err) {
console.error(err);
} else {
console.log('Data written to file successfully.');
}
});
}