-
Notifications
You must be signed in to change notification settings - Fork 5
/
assets.js
executable file
·402 lines (376 loc) · 13.6 KB
/
assets.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env node
/*
Node script to create SVG assets and their related
JS files for the service icons of each provider
*/
import fs from 'fs-extra';
import path from 'path';
import url from 'url';
import axios from 'axios';
import camelCase from 'camelcase';
import glob from 'glob-promise';
import nunjucks from 'nunjucks';
import pino from 'pino';
import unzipper from 'unzipper';
import xml2js from 'xml2js';
import tmp from 'tmp';
const logger = pino({
prettyPrint: {
colorize: true,
translateTime: true,
ignore: 'pid,hostname',
},
});
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const parser = new xml2js.Parser();
const builder = new xml2js.Builder();
const providerJsTemplate = fs.readFileSync(path.join(__dirname, 'provider.js.njk')).toString();
const docsTemplate = fs.readFileSync(path.join(__dirname, 'resources_content.html.njk')).toString();
const imagesDir = provider => path.join(__dirname, '..', 'src', 'images', provider);
const renderJSFile = provider => path.join(__dirname, '..', 'src', 'js', 'icons', `${provider}.js`);
const resourceDocsFile = path.join(__dirname, '..', 'src', 'resources_content.html');
const assetsOverrideDir = path.join(__dirname, 'assets_override');
const tap = fn => data => {
fn(data);
return data;
};
const groupBy = key => objs =>
objs.reduce(
(acc, o) => ({
...acc,
[o[key]]: [...(acc[o[key]] || []), o],
}),
{}
);
const getParentDir = (f, level = 1) => path.dirname(f).split(path.sep).slice(-level)[0];
const basename = filepath => path.basename(filepath, path.extname(filepath));
const sanitizeName = s => s.replace(/[^a-zA-Z0-9_$-]/g, '').replace(/-+/, '-');
const mkdir = async dir => fs.promises.mkdir(dir, {recursive: true});
const rmdir = async dir => fs.promises.rmdir(dir, {recursive: true, maxRetries: 10});
// download a file onto a temporary directory
const downloadFile = async url => {
const outputFile = tmp.tmpNameSync({});
const writer = fs.createWriteStream(outputFile);
const response = await axios({
url,
method: 'GET',
responseType: 'stream',
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => resolve(outputFile));
writer.on('error', reject);
});
};
const unzipFile = async (sourceFile, outputDir) => {
const w = fs.createReadStream(sourceFile).pipe(unzipper.Extract({path: outputDir}));
return new Promise((resolve, reject) => {
w.on('finish', resolve);
w.on('error', reject);
});
};
const copyFromDir = async (sourceDir, destDir) => fs.copy(sourceDir, destDir);
const fromRemoteZip = async (sourceUrl, destDir) =>
downloadFile(sourceUrl).then(downloadedFile => unzipFile(downloadedFile, destDir));
// utils for the assets generation pipeline
const extractFilename = ({source, ...rest}) => ({...rest, source, filename: path.basename(source).toLowerCase()});
const replacePatterns = patterns => ({filename, ...rest}) => ({
...rest,
filename: patterns.length
? patterns.reduce((acc, [pattern, replacement]) => acc.replace(pattern, replacement), filename)
: filename,
});
const addTarget = ({provider, filename, ...rest}) => ({
...rest,
filename,
provider,
target: path.join(imagesDir(provider), filename),
});
const addImportName = ({filename, ...rest}) => ({
...rest,
filename,
importName: camelCase(sanitizeName(basename(filename))),
});
const readContent = async ({source, ...rest}) =>
fs.promises.readFile(source).then(s => ({
...rest,
source,
originalContent: s.toString(),
}));
const resizeContent = async ({originalContent, ...rest}) =>
parser
.parseStringPromise(originalContent)
.then(({svg: {$: attr, ...svgRest}, ...topRest}) => ({
...topRest,
svg: {...svgRest, $: {...attr, width: '80px', height: '80px'}},
}))
.then(o => builder.buildObject(o))
.then(resizedContent => ({...rest, originalContent, resizedContent}));
const removeDuplicateAssets = assets =>
assets.filter((v, i, a) => a.findIndex(t => t.importName === v.importName) === i);
const compareAssets = ({importName: n1}, {importName: n2}) => (n1 < n2 ? -1 : n2 > n1 ? 1 : 0);
// save the assets (SVGs) into their final destination
const saveAssets = async assets =>
Promise.all(
assets.map(({target, resizedContent, ...rest}) =>
mkdir(path.dirname(target))
.then(() => fs.promises.writeFile(target, resizedContent))
.then(() => ({...rest, target, resizedContent}))
)
);
// render the JS file importing all the SVG icons for a provider
const renderJS = provider => async assets => {
const toRender = assets.map(({target, ...rest}) => ({
...rest,
target,
importPath: path.relative(path.dirname(renderJSFile(provider)), target),
}));
const rendered = nunjucks.renderString(providerJsTemplate, {assets: toRender});
return fs.promises.writeFile(renderJSFile(provider), rendered).then(() => toRender);
};
const AWS = 'aws';
const Azure = 'azure';
const K8s = 'k8s';
const GCP = 'gcp';
const Generic = 'generic';
// the following patterns in the form [regex, string replacement] allow for
// customisation of asset names for each provider.
//
// NOTE: order is important in the following as the regex
// substitutions will be applied in order of definition
const awsPatterns = [
[/lot/gi, 'iot'],
[/lense/gi, 'lens'],
[/&/gi, 'and'],
[/_48_dark/gi, ''],
[/_64/gi, ''],
[/res_amazon-/gi, ''],
[/res_aws-/gi, ''],
[/^res_/i, ''],
[/arch_aws-/gi, ''],
[/arch_amazon-/gi, ''],
[/^arch_/i, ''],
[/aurora_amazon-/gi, ''],
[/cloud9_cloud9/gi, 'cloud9'],
[/elasticache_elasticache/gi, 'elasticache'],
[/emr_emr/gi, 'emr'],
[/identity-and-access-management/gi, 'identity-access-management'],
[/identity-access-management/gi, 'iam'],
[/iam_aws-iam/gi, 'iam'],
[/iam_aws/gi, 'iam'],
[/lambda_lambda/gi, 'lambda'],
[/managed-blockchain_blockchain/gi, 'managed-blockchain'],
[/alrternate/gi, 'alternate'],
[/aternate/gi, 'alternate'],
[/res-amazon-simple-storage_s3-replication/gi, 's3-replication'],
[/shield_aws-shield/gi, 'shield'],
[/snowball_snowball/gi, 'snowball'],
[/amazon-simple-storage_s3/gi, 's3'],
[/s3-standard/gi, 's3'],
[/simple-storage_service/gi, 's3'],
[/simple-storage/gi, 's3'],
[/simple-email-service/gi, 'ses'],
[/simple-queue-service/gi, 'sqs'],
[/database-migration-service_database-migration/gi, 'database-migration'],
[/elastic-block-store/gi, 'ebs'],
[/elastic-container-service/gi, 'ecs'],
[/elastic-container-registry/gi, 'ecr'],
[/elastic-file-system/gi, 'efs'],
[/elastic-load-balancing/gi, 'elb'],
[/elb_application-load-balancer/gi, 'alb'],
[/elb_network-load-balancer/gi, 'nlb'],
[/simple-notification-service/gi, 'sns'],
];
const azurePatterns = [
[/^[0-9]+-icon-service-/i, ''],
[/^azure-/i, ''],
[/ /, '-'],
[/machinesazurearc/, 'machines-azure-arc'],
];
const k8sPatterns = [];
const gcpPatterns = [
[/-521/, ''],
[/-512/, ''],
[/-color.*\./, '.'],
[/^cloud-?/, ''],
[/debugger/, 'cloud-debugger'],
[/GKEOn/i, 'gke-on'],
];
const genericPatterns = [];
// custom provider functions to compute the name of the group a
// service should belong from the name of the SVG file
const awsComputeGroup = ({source, ...rest}) => ({
...rest,
source,
group: getParentDir(source, 2)
.toLowerCase()
.replace('arch_', '')
.replace('res_', '')
.replace('lot', 'iot')
.replace(/[-_]/g, ' ')
.replace(/ +/g, ' ')
.trim()
.replace(/^\w/, c => c.toUpperCase())
.replace('Iot', 'Internet of things')
.replace('enagagement', 'engagement'),
});
const azureComputeGroup = ({source, ...rest}) => ({
...rest,
source,
group: getParentDir(source)
.toLowerCase()
.trim()
.replace(/\+/g, 'and')
.replace('iot', 'Internet of things')
.replace(/^\w/, c => c.toUpperCase()),
});
const k8sComputeGroup = ({source, ...rest}) => ({
...rest,
source,
group: 'Generic', // no group information available in k8s
});
const gcpComputeGroup = ({source, ...rest}) => ({
...rest,
source,
group: getParentDir(source, 1).toLowerCase().trim(),
});
const genericComputeGroup = ({source, ...rest}) => ({
...rest,
source,
group: 'Generic',
});
// providers configs
// each provider object contains the following:
// - url: url of the file containing the SVG icons for the services
// - filter: a function taking the path of an SVG and returning a boolean
// indicating whether the SVG icon should be used or not
// - prepare: a function taking the icon path as input and returning a Promise
// resolving to an object containing all the necessary metadata,
// e.g. source path, the svg content, the group name etc.
const config = {
[AWS]: {
fetch: targetDir =>
fromRemoteZip(
'https://d1.awsstatic.com/webteam/architecture-icons/Q32020/AWS-Architecture-Assets-For-Light-and-Dark-BG_20200911.478ff05b80f909792f7853b1a28de8e28eac67f4.zip',
targetDir
),
filter: filepath => filepath.match(/(64.*|Dark)\.svg$/i),
prepare: filepath =>
Promise.resolve({provider: AWS, source: filepath})
.then(extractFilename)
.then(awsComputeGroup)
.then(replacePatterns(awsPatterns))
.then(addTarget)
.then(addImportName)
.then(readContent)
.then(resizeContent),
},
[Azure]: {
fetch: targetDir =>
fromRemoteZip('https://arch-center.azureedge.net/icons/Azure_Public_Service_Icons_V2.zip', targetDir),
filter: () => true, // keep all svg in downloaded assets
prepare: filepath =>
Promise.resolve({provider: Azure, source: filepath})
.then(extractFilename)
.then(azureComputeGroup)
.then(replacePatterns(azurePatterns))
.then(addTarget)
.then(addImportName)
.then(readContent)
.then(resizeContent),
},
[K8s]: {
fetch: targetDir => fromRemoteZip('https://github.com/kubernetes/community/archive/master.zip', targetDir),
filter: filepath => filepath.match(/.*unlabeled\/.*\.svg$/i),
prepare: filepath =>
Promise.resolve({provider: K8s, source: filepath})
.then(extractFilename)
.then(k8sComputeGroup)
.then(replacePatterns(k8sPatterns))
.then(addTarget)
.then(addImportName)
.then(readContent)
.then(resizeContent),
},
[GCP]: {
fetch: targetDir =>
fromRemoteZip('https://cloud.google.com/icons/files/google-cloud-icons.zip', targetDir).then(() =>
copyFromDir(path.join(assetsOverrideDir, 'gcp'), targetDir)
),
filter: filepath => filepath.match(/.*color\.svg$/i) && !filepath.includes('modifiers'),
prepare: filepath =>
Promise.resolve({provider: GCP, source: filepath})
.then(extractFilename)
.then(gcpComputeGroup)
.then(replacePatterns(gcpPatterns))
.then(addTarget)
.then(addImportName)
.then(readContent)
.then(resizeContent),
},
[Generic]: {
fetch: targetDir => copyFromDir(path.join(assetsOverrideDir, 'generic'), targetDir),
filter: () => true, // keep all svg
prepare: filepath =>
Promise.resolve({provider: Generic, source: filepath})
.then(extractFilename)
.then(genericComputeGroup)
.then(replacePatterns(genericPatterns))
.then(addTarget)
.then(addImportName)
.then(readContent)
.then(resizeContent),
},
};
// define the entire processing pipeline for a provider
// it will download the provider file containing the SVGs onto a temporary directory
// extract the SVG icons, filter them, resize them if necessary, save them onto
// their final destination (src/images/<provider>/<service>.svg) and create the JS
// file used to import the icons in the app
const processProvider = ([provider, options]) => {
const providerDir = path.join('.tmp', provider);
const removeProviderDir = () => rmdir(providerDir);
const createProviderDir = () => mkdir(providerDir);
const fetchAssets = () => options.fetch(providerDir);
const selectSVGs = () => glob.promise(`${providerDir}/**/*.svg`, {nodir: true});
const filterRelevantFiles = files => files.filter(options.filter);
const prepareAssets = files => Promise.all(files.map(options.prepare));
const sortAssets = assets => assets.sort(compareAssets);
const removeImagesDir = () => rmdir(imagesDir(provider));
logger.info(`Producing assets for ${provider}`);
return removeProviderDir()
.then(createProviderDir)
.then(fetchAssets)
.then(removeImagesDir)
.then(selectSVGs)
.then(filterRelevantFiles)
.then(prepareAssets)
.then(removeDuplicateAssets)
.then(sortAssets)
.then(saveAssets)
.then(renderJS(provider))
.then(tap(removeProviderDir))
.then(tap(() => logger.info(`Finished producing assets for ${provider}`)))
.catch(err => logger.error(err));
};
// utility functions to render the documentation (src/resources_content.html) listing the
// available services and their groups after all the providers have been processed
const groupByProvider = assets =>
Object.entries(groupBy('provider')(assets)).map(([k, v]) => ({provider: k, assets: v}));
const groupByGroup = assets =>
assets.map(({provider, assets}) => ({
name: provider,
groups: Object.entries(groupBy('group')(assets)).map(([k, v]) => ({name: k, assets: v})),
}));
const renderResourceDocs = providers => {
const rendered = nunjucks.renderString(docsTemplate, {providers});
return fs.promises.writeFile(resourceDocsFile, rendered);
};
// kick off the processing in parallel for each provider
Promise.all(Object.entries(config).map(processProvider))
.then(assets => assets.flat())
.then(groupByProvider)
.then(groupByGroup)
.then(renderResourceDocs)
.catch(err => logger.error(err));