1 | 'use strict';
|
2 |
|
3 | const { regexpEscape } = require('./string');
|
4 | const fs = require('fs');
|
5 | const md5 = require('md5');
|
6 | const path = require('path');
|
7 |
|
8 | const RE_UNIQUE_HASH_TOKEN = /%hash%/;
|
9 | const RE_UNIQUE_TOKEN = /%(?:hash|date)%/;
|
10 |
|
11 | const existsCache = new Set();
|
12 |
|
13 | module.exports = {
|
14 | exists,
|
15 | isAbsoluteFilepath,
|
16 | isRelativeFilepath,
|
17 |
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 | isFilepath(str) {
|
24 | return str && (isAbsoluteFilepath(str) || isRelativeFilepath(str));
|
25 | },
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 | filepathName(p) {
|
33 | p = path.resolve(p);
|
34 |
|
35 | let dir = path.resolve(p, '..');
|
36 |
|
37 | if (dir == process.cwd()) dir = '.';
|
38 |
|
39 | return `${path.basename(dir)}${path.sep}${path.basename(p)}`;
|
40 | },
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | filepathType(filepath, fileExtensions) {
|
49 | const ext = path.extname(filepath).slice(1);
|
50 |
|
51 |
|
52 | for (const t in fileExtensions) {
|
53 | const exts = fileExtensions[t];
|
54 |
|
55 | for (let i = 0, n = exts.length; i < n; i++) {
|
56 | if (ext == exts[i]) return t;
|
57 | }
|
58 | }
|
59 |
|
60 | return '';
|
61 | },
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | findFilepath(filepath, type, fileExtensions) {
|
71 | if ('string' == typeof filepath && type && fileExtensions) {
|
72 | let stat;
|
73 |
|
74 | try {
|
75 | stat = fs.statSync(filepath);
|
76 | } catch (err) {
|
77 |
|
78 | }
|
79 |
|
80 |
|
81 | if (stat && stat.isFile()) return filepath;
|
82 |
|
83 | let ext, fp;
|
84 |
|
85 |
|
86 | for (let i = 0, n = fileExtensions[type].length; i < n; i++) {
|
87 | ext = fileExtensions[type][i];
|
88 |
|
89 | fp = filepath + '.' + ext;
|
90 | if (exists(fp)) return fp;
|
91 |
|
92 | fp = path.resolve(filepath, `index.${ext}`);
|
93 | if (exists(fp)) return fp;
|
94 | }
|
95 |
|
96 | return '';
|
97 | }
|
98 |
|
99 | return filepath;
|
100 | },
|
101 |
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 | findUniqueFilepath(pattern) {
|
108 | pattern = path.resolve(pattern);
|
109 |
|
110 |
|
111 | const dir = path.dirname(pattern);
|
112 | let files, reToken;
|
113 |
|
114 |
|
115 | if ((reToken = RE_UNIQUE_TOKEN.exec(pattern))) {
|
116 | try {
|
117 | files = fs.readdirSync(dir);
|
118 | } catch (err) {
|
119 |
|
120 | return '';
|
121 | }
|
122 |
|
123 |
|
124 | const re = new RegExp(
|
125 | regexpEscape(pattern).replace(reToken[0], RE_UNIQUE_HASH_TOKEN.test(pattern) ? '[a-f0-9]{32}' : '[0-9]{13,}')
|
126 | );
|
127 |
|
128 | for (let i = 0, n = files.length; i < n; i++) {
|
129 | const filepath = path.resolve(dir, files[i]);
|
130 |
|
131 | if (re.test(filepath)) return filepath;
|
132 | }
|
133 | }
|
134 |
|
135 | return '';
|
136 | },
|
137 |
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | generateUniqueFilepath(pattern, content) {
|
145 | pattern = path.resolve(pattern);
|
146 |
|
147 | let reToken, wildcard;
|
148 |
|
149 | if ((reToken = RE_UNIQUE_TOKEN.exec(pattern))) {
|
150 | wildcard = reToken[0];
|
151 | if (wildcard == '%hash%') {
|
152 |
|
153 |
|
154 | pattern = pattern.replace(wildcard, content ? content.length == 32 ? content : md5(content) : '');
|
155 | } else if (wildcard == '%date%') {
|
156 | pattern = pattern.replace(wildcard, content ? Date.now() : '');
|
157 | }
|
158 | }
|
159 |
|
160 | return pattern;
|
161 | },
|
162 |
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 | isUniqueFilepath(pattern) {
|
169 | return RE_UNIQUE_TOKEN.test(pattern);
|
170 | }
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | function exists(filepath) {
|
179 | const cached = existsCache.has(filepath);
|
180 |
|
181 |
|
182 | if (cached) return true;
|
183 |
|
184 | const filepathExists = fs.existsSync(filepath);
|
185 |
|
186 | if (filepathExists) existsCache.add(filepath);
|
187 |
|
188 | return filepathExists;
|
189 | }
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | function isRelativeFilepath(filepath) {
|
197 | return 'string' == typeof filepath && filepath.charAt(0) == '.';
|
198 | }
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | function isAbsoluteFilepath(filepath) {
|
206 | return 'string' == typeof filepath && path.resolve(filepath) == filepath;
|
207 | }
|