UNPKG

2.25 kBJavaScriptView Raw
1'use strict'
2
3/**
4 * 文件说明:
5 * ----------------------------------------
6 * 创建用户: leo
7 * 创建日期 2019/1/3
8 */
9
10const {existsSync, statSync} = require('fs')
11const {join, normalize: pathNormalize, sep} = require('path')
12const globby = require('globby')
13const {get} = require('lodash')
14const constants = require('./constants')
15
16function 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
25exports.getExistFile = getExistFile
26
27function normalize(assetUrl) {
28 assetUrl = pathNormalize(assetUrl)
29
30 return sep === '\\' ? assetUrl.replace(/\\/g, '/') : assetUrl
31}
32
33exports.normalize = normalize
34
35function 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 // eslint-disable-next-line global-require
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
79exports.findPkgs = findPkgs
80
81function testDefault(obj) {
82 return obj.default || obj
83}
84
85exports.testDefault = testDefault
86
87function getConfig({cwd, files, returnRelative}) {
88 const configFile = getExistFile({
89 cwd,
90 files,
91 returnRelative,
92 })
93
94 if (existsSync(configFile)) {
95 // eslint-disable-next-line global-require
96 const config = testDefault(require(configFile))
97
98 return config
99 }
100 return {}
101}
102
103exports.getConfig = getConfig