generated from srsergiorodriguez/serie-mini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserieConfigCli.js
165 lines (144 loc) · 4.57 KB
/
serieConfigCli.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
import inquirer from 'inquirer';
import { createReadStream } from 'fs';
import fs from 'fs/promises';
import csvParser from 'csv-parser';
const dataPath = "./data/";
const metadataFile = "metadata.csv";
const configFile = "serie.config.js";
function getQuestions(keys) {
const questions = [
{
type: 'input',
name: 'Usuario GitHub',
message: 'Nombre de usuario de GitHub en donde alojarás la colección:'
},
{
type: 'input',
name: 'Nombre Repositorio',
message: 'Nombre del repositorio que alojará la colección:',
default: 'serie-mini'
},
{
type: 'input',
name: 'Título',
message: 'Título de la colección (aparecerá en el encabezado del sitio web):',
default: 'Serie Mini'
},
{
type: 'input',
name: 'Subtítulo',
message: 'Subtítulo de la colección (aparecerá en una fuente pequeña, en el encabezado del sitio web):',
default: 'Una plataforma para mini colecciones digitales'
},
{
type: 'input',
name: 'Créditos',
message: 'Mensaje de los créditos (por ejemplo, tu nombre. Aparecerá en el pie de página del sitio web):',
default: 'Por Sergio Rodríguez Gómez. Hecho con Serie Mini'
},
{
type: 'input',
name: 'Mensaje Copyright',
message: 'Mensaje de copyright (aparecerá en el pie de página del sitio web):',
default: "Todos los derechos reservados, " + new Date().getFullYear()
},
{
type: 'checkbox',
name: 'Metadatos para mostrar',
message: 'Selecciona qué metadatos mostrar en la página de cada ítem de la colección:',
choices: keys
},
{
type: 'checkbox',
name: 'Metadatos buscables',
message: 'Selecciona qué metadatos indexar en el buscador (es decir, qué datos quieres que sean buscables dentro de la colección):',
choices: keys
},
]
return questions
}
startConfig();
async function startConfig() {
const metadata = await parseMetadata();
const keys = Object.keys(metadata[0]);
const questions = getQuestions(keys);
msg("\nEste programa de Serie L te guiará paso a paso en la configuración de tu colección. Responde las siguientes preguntas...\n", "blue");
const answers = await inquirer.prompt(questions);
msg("\nEstas fueron tus configuraciones: \n", "blue");
console.log(answers);
const confirmanswer = await inquirer.prompt({
type: 'list',
name: 'confirm',
message: '¿Son correctas?',
choices: [ 'Sí', 'No' ]
});
if (confirmanswer.confirm === "No") {
msg("\nEmpecemos de nuevo\n", "red");
startConfig();
return
}
const config = formatConfig(answers);
await fs.writeFile(`${dataPath}${configFile}`, `const config = ${JSON.stringify(config, null, 2)};\nexport default config;`, (err) => {
if (err) {
console.error('Error writing file:', err);
return;
}
});
}
function formatConfig(answers) {
const firstUppercase = (str) => str.charAt(0).toUpperCase() + str.slice(1);
const config = {
lang: "es",
base: `https://${answers["Usuario GitHub"]}.github.io`,
baseurl: `/${answers["Nombre Repositorio"]}`,
title: answers["Título"],
subtitle: answers["Subtítulo"],
credits: answers["Créditos"],
copyright: answers["Mensaje Copyright"],
pages: {
iiifViewer: true,
metadataToShow: answers["Metadatos para mostrar"].map(d => {
return { key: d, label: firstUppercase(d), type: "text"}
}),
metadataToIndex: answers["Metadatos buscables"]
}
}
return config
}
function msg(msg, color = "red") {
const colors = {
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
}
console.log(`${colors[color]}%s\x1b[0m`, msg);
}
async function parseMetadata() {
const results = [];
await new Promise(r => {
createReadStream(dataPath + metadataFile)
.pipe(csvParser())
.on('data', data => results.push(data))
.on('end', () => r(results))
});
// VALIDATION
// Empty metadata
if (results.length <= 0) {
errorMsg("metadata", "Metadata is empty / Los metadatos están vacíos");
process.exit(1);
}
// Unique pids
if ([...new Set(results.map(d => d.pid))].length !== results.length) {
errorMsg("metadata", "There are non unique pids / Hay pids que no son únicos");
process.exit(1);
}
// Has pids and labels
for (let e of results) {
if (e.pid === undefined || e.label === undefined) {
errorMsg("metadata", "There are rows without pids or labels / Hay filas sin pids o labels");
process.exit(1);
}
}
return results
}