1 | const debug = require('debug')('ice:util:db');
|
2 | const path = require('path');
|
3 | const globby = require('globby');
|
4 |
|
5 | const pkgJSON = require('./pkg-json');
|
6 | const getType = require('./type');
|
7 |
|
8 | module.exports = async function getDB(cwd) {
|
9 | const type = getType(cwd);
|
10 | let db = {
|
11 | blocks: [],
|
12 | components: [],
|
13 | scaffolds: [],
|
14 | };
|
15 |
|
16 |
|
17 | if (type) {
|
18 | const pkgjson = pkgJSON.getPkgJSON(cwd);
|
19 | db[`${type}s`].push({
|
20 | source: {
|
21 | npm: pkgjson.name,
|
22 | version: pkgjson.version,
|
23 | },
|
24 | });
|
25 | return db;
|
26 | }
|
27 |
|
28 |
|
29 | const dbBasePath = path.join(cwd, 'build');
|
30 | const paths = await globby(['*.json'], { cwd: dbBasePath });
|
31 | debug('db json files: %j', paths);
|
32 |
|
33 | const fileIndex = paths.indexOf('materials.json');
|
34 |
|
35 | if (fileIndex < 0) {
|
36 | throw new Error('materials.json can\'t be find');
|
37 | }
|
38 |
|
39 | const dbPath = path.join(dbBasePath, paths[fileIndex]);
|
40 | db = pkgJSON.getJSON(dbPath);
|
41 | if (!Array.isArray(db.blocks) || !Array.isArray(db.scaffolds)) {
|
42 | throw new Error(`${dbPath} is not a valid materials json`);
|
43 | }
|
44 | return db;
|
45 | };
|