1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const {existsSync, statSync} = require('fs')
|
11 | const {join, normalize: pathNormalize, sep} = require('path')
|
12 | const globby = require('globby')
|
13 | const {get} = require('lodash')
|
14 | const constants = require('./constants')
|
15 |
|
16 | function getExistFile({cwd, files, returnRelative}) {
|
17 | for (const file of files) {
|
18 | const absFilePath = join(cwd, file)
|
19 | if (existsSync(absFilePath)) {
|
20 | return returnRelative ? file : absFilePath
|
21 | }
|
22 | }
|
23 | }
|
24 |
|
25 | exports.getExistFile = getExistFile
|
26 |
|
27 | function normalize(assetUrl) {
|
28 | assetUrl = pathNormalize(assetUrl)
|
29 |
|
30 | return sep === '\\' ? assetUrl.replace(/\\/g, '/') : assetUrl
|
31 | }
|
32 |
|
33 | exports.normalize = normalize
|
34 |
|
35 | function findPkgs({cwd, packagesPatterns = constants.packagesPatterns, cb, ignoreNoBuild = true}) {
|
36 | const pkgPathNames = globby.sync(packagesPatterns, {
|
37 | cwd,
|
38 | onlyDirectories: true,
|
39 | ignore: ['**/node_modules/**'],
|
40 | })
|
41 |
|
42 | const pkgs = []
|
43 |
|
44 | pkgPathNames.forEach((pkgPathName) => {
|
45 | const pkgPath = join(cwd, pkgPathName)
|
46 |
|
47 | if (!statSync(pkgPath).isDirectory()) {
|
48 | return
|
49 | }
|
50 |
|
51 | const pkgJsonPath = join(cwd, pkgPathName, `package.json`)
|
52 |
|
53 | if (!existsSync(pkgJsonPath)) {
|
54 | return
|
55 | }
|
56 |
|
57 |
|
58 | const pkgJson = require(pkgJsonPath)
|
59 |
|
60 | if (ignoreNoBuild && get(pkgJson, 'hzLib.noBuild', false) === true) {
|
61 | return
|
62 | }
|
63 |
|
64 | let pkg = {
|
65 | pkgJson,
|
66 | pkgPath,
|
67 | }
|
68 |
|
69 | if (cb) {
|
70 | pkg = cb(pkg)
|
71 | }
|
72 |
|
73 | pkgs.push(pkg)
|
74 | })
|
75 |
|
76 | return pkgs
|
77 | }
|
78 |
|
79 | exports.findPkgs = findPkgs
|
80 |
|
81 | function testDefault(obj) {
|
82 | return obj.default || obj
|
83 | }
|
84 |
|
85 | exports.testDefault = testDefault
|
86 |
|
87 | function getConfig({cwd, files, returnRelative}) {
|
88 | const configFile = getExistFile({
|
89 | cwd,
|
90 | files,
|
91 | returnRelative,
|
92 | })
|
93 |
|
94 | if (existsSync(configFile)) {
|
95 |
|
96 | const config = testDefault(require(configFile))
|
97 |
|
98 | return config
|
99 | }
|
100 | return {}
|
101 | }
|
102 |
|
103 | exports.getConfig = getConfig
|