generate.js
1.2 KB
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
const { readdirSync, readFileSync, writeFileSync, existsSync } = require('fs');
const { join, basename } = require('path');
const mkdirp = require('mkdirp');
function parseJSON(root) {
const dirs = readdirSync(root);
const type = basename(root);
const list = dirs.reduce((memo, dir) => {
if (dir.charAt(0) === '.') return;
const absDirPath = join(root, dir);
const pkg = require(join(absDirPath, 'package.json'));
const url = `https://github.com/umijs/umi-blocks/tree/master/${type}/${dir}`;
const img = `https://github.com/umijs/umi-blocks/blob/master/${type}/${dir}/snapshot.png?raw=true`;
memo.push({
name: pkg.name,
description: pkg.description,
url,
tags: [],
img,
previewUrl: '',
features: [],
});
return memo;
}, []);
return { list };
}
function generate(root) {
const dist = join(root, '..', 'dist');
const type = basename(root);
console.log(`Generate json for ${type}`);
mkdirp.sync(dist);
const json = parseJSON(root);
writeFileSync(
join(dist, `${type}.json`),
JSON.stringify(json, null, 2),
'utf-8',
);
}
generate(join(__dirname, '..', 'templates'));
generate(join(__dirname, '..', 'blocks'));