-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
182 lines (148 loc) · 4.96 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
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
const path = require('path');
const fs = require('fs');
const sharp = require('sharp');
const AdmZip = require('adm-zip');
// Start up an express app to create routes for API endpoints
const cors = require('cors');
const express = require('express');
const app = express();
const port = 8080;
app.use(cors());
// Generate Color Files Data
const generateColorFilesData = async (file) => {
const filePath = path.join(__dirname, `assets/svg/${file}.svg`);
const colorFileDir = path.join(__dirname, `assets/colors/${file}`);
if (!fs.existsSync(colorFileDir)) {
fs.mkdirSync(colorFileDir);
}
// Create colored png files
const colors = {
yellow: 'rgb(223, 156, 49)',
red: 'rgb(239, 68, 68)',
blue: 'rgb(99, 102, 241)',
green: 'rgb(16, 185, 129)'
};
// Create png file instance
const svgFile = fs.readFileSync(filePath);
return Object.keys(colors).map(color => {
// Create the colored png file
sharp(svgFile)
.tint(colors[color])
.toFile(`assets/colors/${file}/${file}-${color}.png`)
.catch((err) => {
console.error(err);
});
});
}
const generateZip = async (file) => {
const zip = new AdmZip();
const colorFileDir = path.join(__dirname, `assets/colors/${file}`);
const zipFileDir = path.join(__dirname, `assets/zip`);
if (!fs.existsSync(zipFileDir)) {
fs.mkdirSync(zipFileDir);
}
// Add the color files to the zip
const colorFiles = fs.readdirSync(colorFileDir);
colorFiles.forEach(colorFile => {
zip.addLocalFile(`assets/colors/${file}/${colorFile}`);
});
zip.writeZip(`${zipFileDir}/${file}.zip`);
}
// Get Files function
const getFiles = async (dir) => {
return new Promise((resolve, reject) => {
fs.readdir(dir, (err, files) => {
// If there is an error, reject the promise
if (err) reject(err);
// Otherwise, resolve the promise with the list of files
// Map the list of files to include the desired information
// name,
const fileData = files.map(file => {
const slug = file.split('.')[0];
// Generate all the color files data
generateColorFilesData(slug);
// Generate the zip file
generateZip(slug);
// Format the file name
const fileName = file.split('.')[0].split('-').map(word => word[0].toUpperCase() + word.slice(1)).join(' ');
// Get the file size in kb
const stats = fs.statSync(path.join(dir, file));
const fileSizeInKb = stats.size / 1000;
// Return the file data object
return {
name: fileName,
slug: slug,
file: file,
path: path.join(dir, file),
uri: `http://localhost:${port}/assets/png/${file}`,
size: fileSizeInKb
}
});
// Return the list of files
resolve(fileData);
});
});
}
// Get a single file
const getFile = async (file) => {
const filePath = path.join(__dirname, `assets/png/${file}.png`);
const svgFilePath = path.join(__dirname, `assets/svg/${file}.svg`);
// Get the file size in kb
const stats = fs.statSync(filePath);
const fileSizeInKb = stats.size / 1000;
// Check if color directory exists for file
const colorFileDir = path.join(__dirname, `assets/colors/${file}`);
let colorFilesData = [];
// Generate color files data for given file
await generateColorFilesData(file);
await generateZip(file);
return new Promise((resolve, reject) => {
// Add the color files data to the colorFilesData array
const colorFiles = fs.readdirSync(colorFileDir);
colorFiles.forEach(colorFile => {
// Return the file data object
colorFilesData.push({
name: colorFile.split('.')[0],
slug: colorFile.split('.')[0],
file: colorFile,
uri: `http://localhost:${port}/assets/colors/${file}/${colorFile}`,
});
});
// Create a file data object
const fileData = {
name: file.split('.')[0].split('-').map(word => word[0].toUpperCase() + word.slice(1)).join(' '),
slug: file,
uri: `http://localhost:${port}/assets/png/${file}.png`,
size: fileSizeInKb,
svg: fs.readFileSync(svgFilePath, 'utf8'),
colors: colorFilesData,
downloadUri: `http://localhost:${port}/assets/zip/${file}.zip`,
}
// Resolve the promise with the file data
resolve(fileData);
});
}
// Create an index route to query and return all data
app.get('/', (req, res) => {
const pngPath = path.join(__dirname, 'assets/png');
// Get the list of files
getFiles(pngPath).then((files) => {
res.send(files);
});
});
// Create an endpoint for a specific object
app.get('/:slug', (req, res) => {
const slug = req.params.slug;
// Get file info
getFile(slug).then((file) => {
res.send(file);
}).catch((err) => {
res.send(err);
})
});
// Expose static assets
app.use('/assets', express.static(path.join(__dirname, 'assets')));
// Create a listener to start the server
app.listen(port, () => {
console.log(`CORS-enabled web server listening on port ${port}`);
});