1 | const
|
2 | path = require('path'),
|
3 | sgUtil = require('../util'),
|
4 | Collector = require('./Collector');
|
5 |
|
6 | class IconCollector extends Collector {
|
7 |
|
8 | constructor(...args) {
|
9 | super(...args);
|
10 |
|
11 | this.globPattern = path.join('**', '*.svg');
|
12 | this.icons = new Map();
|
13 | }
|
14 |
|
15 | collectSection(...args) {
|
16 | const
|
17 | section = Object.assign(super.collectSection(...args), {
|
18 | sourceDir: path.resolve(__dirname, '..', '..', 'app/sections/icons'),
|
19 | warnOnMissingDataFile: false
|
20 | });
|
21 |
|
22 | this.setIndexFile.call(this, section);
|
23 | }
|
24 |
|
25 | setIndexFile(section) {
|
26 | const
|
27 | {getGetSectionDataFunction, getIcons, logLevel} = this,
|
28 | {title, sourceDir, destDir} = section,
|
29 | filename = path.relative(process.cwd(), path.join(sourceDir, 'index.pug')),
|
30 | file = this.createFileObj({filename, section});
|
31 |
|
32 | Object.defineProperty(file, 'title', {
|
33 | get: () => title
|
34 | });
|
35 |
|
36 | Object.defineProperty(file, 'data', {
|
37 | get: () => {
|
38 | if (!file._data || file._data.timestamp !== file.timestamp) {
|
39 | file._data = getGetSectionDataFunction.call(file);
|
40 | file._data.locals.icons = getIcons.call(this, destDir);
|
41 | file._data.pageTitle = file.title;
|
42 | file._data.collector = file.collector;
|
43 | file._data.timestamp = Date.now();
|
44 |
|
45 | if (logLevel > 2) {
|
46 | sgUtil.log(`Component Data: \u001b[1m${file.title}\u001b[22m generated. (${file.timestamp})`, 'info');
|
47 | }
|
48 | }
|
49 | else if (logLevel > 2) {
|
50 | sgUtil.log(`Component Data: \u001b[1m${file.title}\u001b[22m use cached. (${file.timestamp})`);
|
51 | }
|
52 | return file._data;
|
53 | }
|
54 | });
|
55 |
|
56 | return this.CollectorStore.setFile(file);
|
57 | }
|
58 |
|
59 | setFile({filename, section: {sourceDir, destDir}}) {
|
60 | this.CollectorPaths.set(filename, sourceDir, destDir);
|
61 | this.icons.set(filename, sgUtil.readFileContents(filename));
|
62 | }
|
63 |
|
64 | unsetFile(filename) {
|
65 | this.icons.delete(filename);
|
66 | }
|
67 |
|
68 | getIcons(destDir) {
|
69 | const
|
70 | getComponentName = this.getComponentName.bind(this),
|
71 | copySource = this.getCopySourceFunction(),
|
72 | icons = {};
|
73 |
|
74 | this.icons.forEach((icon, filename) => {
|
75 | copySource(filename, getComponentName(destDir, filename));
|
76 | icons[this.CollectorPaths.relativeFileName(filename)] = icon;
|
77 | });
|
78 |
|
79 | return icons;
|
80 | }
|
81 |
|
82 | initDataLoader() {
|
83 | return null;
|
84 | }
|
85 | }
|
86 |
|
87 | module.exports = IconCollector; |
\ | No newline at end of file |