-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSeed.ts
149 lines (118 loc) · 3.72 KB
/
Seed.ts
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
/**
* Data Type of one of Vue File.
*/
import {FileReport} from '../../types';
import {Model} from './model';
export class Seed {
private _name: string; // file name
private _props: string; // props string
private _fileSize: number; // this file size. (in byte)
private _lastModifiedTime: number; // Last modified time of this file.
private _count: number;
// eslint-disable-next-line no-use-before-define
private _children: Seed[] = []; // imported modules from this file.
/**
* @param file - filename
* @param count - number of files reference this file.
* @param _model - Model instance.
*/
constructor(file: FileReport, count: number, private _model: Model) {
this._name = file.name;
this._props = file.props;
this._fileSize = file.size;
this._lastModifiedTime = file.lastModifiedTime;
this._count = count;
}
private renderChildren(): string {
let html = '';
for (let i = 0, len = this._children.length; i < len; i++) {
html += this._children[i].render();
}
return `<div class="group">
${html}
</div>`;
}
private renderProps(): string {
return this._props ? `<pre class="file__props">${this._props}</pre>` : '';
}
private renderMetaData(): string {
let metaString = '';
const fileSize = (this._fileSize / 1024).toFixed(2);
const lastUpdated = this._lastModifiedTime === 0 ? 0 : this._model.getHowManyDaysAgo(new Date(this._lastModifiedTime));
if (fileSize) {
metaString += `<span class="file__meta meta__fileSize">FileSize: ${fileSize} KB</span>`;
}
metaString += `<span class="file__meta meta__lastUpdated">LastUpdated: ${lastUpdated} days ago</span>`;
return metaString;
}
private renderSummary(): string {
let name = this._name;
if (this._name.endsWith('.vue')) {
name = `<svg class="icon" viewBox="0 0 128 128" width="24" height="24"><use xlink:href="#logo"></use></svg>${name}`;
} else if (this._name.endsWith('.js')) {
name = `<img class="icon" src="https://raw.githubusercontent.com/voodootikigod/logo.js/master/js.png" alt="">${name}`;
} else if (this._name.endsWith('.ts')) {
name = `<img class="icon" src="../dist/icon/icon-ts.png" alt="">${name}`;
}
return `<summary>${name}</summary>`;
}
private renderDetails(): string {
const openAttribute = this._model.env === 'node' ? ' open' : '';
return `<details class="detail"${openAttribute}>
${this.renderSummary()}
${this.renderProps()}
${this.renderMetaData()}
</details>`;
}
public render(): string {
const contents = this.renderDetails();
let childHTML = '';
let seedClassName = '';
if (this._children.length > 0) {
childHTML = this.renderChildren();
} else {
seedClassName = ' -no-child';
}
if (this.isJS()) {
seedClassName += ' js';
}
if (this.isTS()) {
seedClassName += ' ts';
}
return `<div class="seed${seedClassName}">
<div class="file">
<div class="filename">
<div class="file__text">${contents}</div>
${this.getCountText()}
</div>
</div>
${childHTML}
</div>`;
}
private getCountText(): string {
if (this._count === 0) {
return '';
} else if (this._count === 1) {
return '<span class="file__count">1 time referenced.</span>';
}
return `<span class="file__count">${this._count} times referenced.</span>`;
}
get children(): Seed[] {
return this._children;
}
set children(value: Seed[]) {
this._children = value;
}
/**
* @return boolean
*/
isJS(): boolean {
return this._name.endsWith('.js');
}
/**
* @return boolean
*/
isTS(): boolean {
return this._name.endsWith('.ts');
}
}