This repository has been archived by the owner on Dec 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
theme_finder.js
362 lines (293 loc) · 7.62 KB
/
theme_finder.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* © 2017 Liferay, Inc. <https://liferay.com>
*
* SPDX-License-Identifier: MIT
*/
const _ = require('lodash');
const async = require('async');
const child_process = require('child_process');
const fs = require('fs');
const globby = require('globby');
const npmKeyword = require('npm-keyword');
const os = require('os');
const packageJson = require('package-json');
const path = require('path');
const spawn = require('cross-spawn');
const {URL} = require('url');
const lfrThemeConfig = require('./liferay_theme_config');
const themeConfig = lfrThemeConfig.getConfig();
function getLiferayThemeModule(name, cb) {
getPackageJSON(
{
name,
},
(err, pkg) => {
if (!isLiferayTheme(pkg)) {
pkg = null;
err = new Error(
'Package is not a Liferay theme or themelet module'
);
}
cb(err, pkg);
}
);
}
/**
* Given a package URL, attempts to download it, extract the package.json, and
* validate it.
*/
function getLiferayThemeModuleFromURL(url, cb) {
try {
// Throw if `url` is invalid.
new URL(url);
let config;
// Install the package in a temporary directory in order to get
// the package.json.
withScratchDirectory(() => {
child_process.spawnSync('npm', ['init', '-y']);
// Ideally, we wouldn't install any dependencies at all, but this is
// the closest we can get (production only, skipping optional
// dependencies).
child_process.spawnSync('npm', [
'install',
'--ignore-scripts',
'--no-optional',
'--production',
url,
]);
// Just in case package name doesn't match URL basename, read it.
const {dependencies} = JSON.parse(fs.readFileSync('package.json'));
const themeName = Object.keys(dependencies)[0];
const json = path.join('node_modules', themeName, 'package.json');
config = JSON.parse(fs.readFileSync(json));
});
if (!isLiferayTheme(config)) {
throw new Error(`URL ${url} is not a liferay-theme`);
} else {
return config;
}
} catch (err) {
cb(err);
}
}
function getLiferayThemeModules(config, cb) {
if (_.isUndefined(cb)) {
cb = config;
config = {};
}
const globalModules = _.isUndefined(config.globalModules)
? true
: config.globalModules;
config.keyword = config.keyword || 'liferay-theme';
const searchFn = globalModules ? searchGlobalModules : searchNpm;
searchFn.call(this, config, moduleResults => {
reportDiscardedModules(
moduleResults,
LiferayThemeModuleStatus.NO_PACKAGE_JSON,
'with no package.json'
);
reportDiscardedModules(
moduleResults,
LiferayThemeModuleStatus.NO_LIFERAY_THEME,
'with no liferayTheme section in package.json'
);
reportDiscardedModules(
moduleResults,
LiferayThemeModuleStatus.TARGET_VERSION_DOES_NOT_MATCH,
`not targeting ${themeConfig.version} version`
);
reportDiscardedModules(
moduleResults,
LiferayThemeModuleStatus.THEMELET_FLAG_DOES_NOT_MATCH,
'with mismatching themelet flag'
);
cb(moduleResults[LiferayThemeModuleStatus.OK] || []);
});
}
module.exports = {
getLiferayThemeModule,
getLiferayThemeModuleFromURL,
getLiferayThemeModules,
};
/**
* Execute `cb()` in the context of a temporary directory.
*
* Note that `cb()` should be entirely synchronous, because clean-up is
* performed as soon as it returns.
*/
function withScratchDirectory(cb) {
const template = path.join(os.tmpdir(), 'theme-finder-');
const directory = fs.mkdtempSync(template);
const cwd = process.cwd();
try {
process.chdir(directory);
cb();
} finally {
process.chdir(cwd);
}
}
function isLiferayTheme(config) {
return (
config &&
config.liferayTheme &&
config.keywords &&
config.keywords.indexOf('liferay-theme') !== -1
);
}
function reportDiscardedModules(moduleResults, outcome, message) {
if (moduleResults[outcome]) {
// eslint-disable-next-line no-console
console.log(
'Warning: found',
Object.keys(moduleResults[outcome]).length,
'packages (matching criteria)',
message
);
}
}
function findThemeModulesIn(paths) {
let modules = [];
_.forEach(paths, rootPath => {
if (!rootPath) {
return;
}
modules = globby
.sync(['*-theme', '*-themelet'], {
cwd: rootPath,
})
.map(match => path.join(rootPath, match))
.concat(modules);
});
return modules;
}
function getNpmPaths() {
let paths = [];
const win32 = process.platform === 'win32';
_.forEach(
path.join(process.cwd(), '..').split(path.sep),
(part, index, parts) => {
let lookup = path.join(
...parts.slice(0, index + 1).concat(['node_modules'])
);
if (!win32) {
lookup = '/' + lookup;
}
paths.push(lookup);
}
);
if (process.env.NODE_PATH) {
paths = _.compact(process.env.NODE_PATH.split(path.delimiter)).concat(
paths
);
} else {
const results = spawn.sync('npm', ['root', '-g']);
if (!results.error && results.stdout) {
const npmRoot = results.stdout.toString();
if (npmRoot) {
paths.push(_.trim(npmRoot));
}
}
if (win32) {
paths.push(path.join(process.env.APPDATA, 'npm/node_modules'));
} else {
paths.push('/usr/lib/node_modules');
paths.push('/usr/local/lib/node_modules');
}
}
return paths.reverse();
}
function getPackageJSON(theme, cb) {
packageJson(theme.name, {fullMetadata: true})
.then(pkg => cb(null, pkg))
.catch(cb);
}
const LiferayThemeModuleStatus = {
NO_PACKAGE_JSON: 'NO_PACKAGE_JSON',
NO_LIFERAY_THEME: 'NO_LIFERAY_THEME',
TARGET_VERSION_DOES_NOT_MATCH: 'TARGET_VERSION_DOES_NOT_MATCH',
THEMELET_FLAG_DOES_NOT_MATCH: 'THEMELET_FLAG_DOES_NOT_MATCH',
OK: 'OK',
};
function getLiferayThemeModuleStatus(pkg, themelet) {
if (pkg) {
const liferayTheme = pkg.liferayTheme;
if (!liferayTheme) {
return LiferayThemeModuleStatus.NO_LIFERAY_THEME;
}
const liferayThemeVersion = liferayTheme.version;
if (
_.isArray(liferayThemeVersion) &&
!_.includes(liferayThemeVersion, themeConfig.version)
) {
return LiferayThemeModuleStatus.TARGET_VERSION_DOES_NOT_MATCH;
}
if (
!_.isArray(liferayThemeVersion) &&
liferayThemeVersion !== '*' &&
liferayThemeVersion !== themeConfig.version
) {
return LiferayThemeModuleStatus.TARGET_VERSION_DOES_NOT_MATCH;
}
const liferayThemelet = liferayTheme.themelet || false;
if (themelet !== liferayThemelet) {
return LiferayThemeModuleStatus.THEMELET_FLAG_DOES_NOT_MATCH;
}
return LiferayThemeModuleStatus.OK;
}
return LiferayThemeModuleStatus.NO_PACKAGE_JSON;
}
function matchesSearchTerms(pkg, searchTerms) {
const description = pkg.description;
return (
pkg.name.indexOf(searchTerms) > -1 ||
(description && description.indexOf(searchTerms) > -1)
);
}
function reduceModuleResults(modules, config) {
const searchTerms = config.searchTerms;
const themelet = config.themelet;
return _.reduce(
modules,
(result, item) => {
if (searchTerms && !matchesSearchTerms(item, searchTerms)) {
return result;
}
const outcome = getLiferayThemeModuleStatus(item, themelet);
result[outcome] = result[outcome] || {};
result[outcome][item.name] = item;
return result;
},
{}
);
}
function searchGlobalModules(config, cb) {
let modules = findThemeModulesIn(getNpmPaths());
modules = _.reduce(
modules,
(result, item) => {
try {
const json = require(path.join(item, 'package.json'));
json.realPath = item;
result.push(json);
} catch (err) {
// Swallow.
}
return result;
},
[]
);
const moduleResults = reduceModuleResults(modules, config);
cb(moduleResults);
}
function searchNpm(config, cb) {
npmKeyword(config.keyword).then(packages => {
async.map(packages, getPackageJSON, (err, results) => {
if (err) {
cb(err);
return;
}
const moduleResults = reduceModuleResults(results, config);
cb(moduleResults);
});
});
}