1 | ///@ts-check
|
2 | ;
|
3 | const path = require("path");
|
4 | const fs = require("fs");
|
5 |
|
6 | /**
|
7 | * 读取 包
|
8 | * @param {string} cwd
|
9 | * @param {string} [name]
|
10 | * @returns {Record<string,object>} miniprogram components Dependencies
|
11 | *
|
12 | */
|
13 | function loadPackage(cwd, name) {
|
14 | return JSON.parse(fs.readFileSync(path.resolve(cwd, name || "package.json")).toString());
|
15 | }
|
16 | /**
|
17 | *
|
18 | * @param {string} modulePath
|
19 | * @returns {Record<string,string>} miniprogram components Dependencies
|
20 | *
|
21 | */
|
22 | function getDependencies(modulePath) {
|
23 | // read parse not require
|
24 | // for file may update
|
25 | const packageConfig = loadPackage(modulePath);
|
26 | return packageConfig.dependencies || {};
|
27 | // const dependencyNames = packageConfig.dependencies ? Object.keys(packageConfig.dependencies) : [];
|
28 |
|
29 | // return dependencyNames.reduce((dependencies, dependencyName) => {
|
30 | // const modulePath = path.resolve(cwd, "node_modules", dependencyName);
|
31 | // return Object.assign(
|
32 | // dependencies,
|
33 | // {
|
34 | // [dependencyName]: modulePath,
|
35 | // },
|
36 | // // resolveDependencies(cwd, modulePath),
|
37 | // );
|
38 | // }, {});
|
39 | }
|
40 |
|
41 | /**
|
42 | * 向上递归解析依赖路径
|
43 | * @param {string} name
|
44 | * @param {string} modulePath
|
45 | * @param {string} cwd
|
46 | */
|
47 | function resolveDependencyPath(name, modulePath, cwd) {
|
48 | cwd = path.normalize(cwd);
|
49 | modulePath = path.normalize(modulePath);
|
50 | do {
|
51 | const dependencyPath = path.resolve(modulePath, "node_modules", name);
|
52 | if (fs.existsSync(dependencyPath)) {
|
53 | return dependencyPath;
|
54 | }
|
55 | modulePath = path.dirname(modulePath)
|
56 | } while (cwd !== modulePath && modulePath !== '/' && modulePath);
|
57 | return ''
|
58 | }
|
59 |
|
60 | // /**
|
61 | // *
|
62 | // * @param {string} cwd
|
63 | // * @param {string} [modulePath]
|
64 | // */
|
65 | // function resolveDependencies(cwd, modulePath) {
|
66 | // modulePath = modulePath || cwd;
|
67 | // // read parse not require
|
68 | // // for file may update
|
69 | // const packageConfig = JSON.parse(fs.readFileSync(path.resolve(modulePath, "package.json")).toString());
|
70 | // const dependencyNames = packageConfig.dependencies ? Object.keys(packageConfig.dependencies) : [];
|
71 |
|
72 | // return dependencyNames.reduce((dependencies, dependencyName) => {
|
73 | // const modulePath = path.resolve(cwd, "node_modules", dependencyName);
|
74 | // return Object.assign(
|
75 | // dependencies,
|
76 | // {
|
77 | // [dependencyName]: modulePath,
|
78 | // },
|
79 | // // resolveDependencies(cwd, modulePath),
|
80 | // );
|
81 | // }, {});
|
82 | // }
|
83 |
|
84 |
|
85 | // /**
|
86 | // *
|
87 | // * @param {string} cwd
|
88 | // */
|
89 | // // function getMiniprogramDistPath(cwd) {
|
90 | // // const packageConfig = require(path.resolve(cwd, "package.json"));
|
91 | // // return path.resolve(cwd, packageConfig.miniprogram || "miniprogram_dist");
|
92 | // // }
|
93 |
|
94 | // /**
|
95 | // * 筛选出 miniprogram components 式的包
|
96 | // * @param {Record<string,string>} dependencies //{[key:string]:string}
|
97 | // * @param {string} cwd
|
98 | // * @returns {Record<string,string>} miniprogram components Dependencies
|
99 | // */
|
100 | // function splitMiniProgramNpm(dependencies, cwd) {
|
101 | // /**
|
102 | // * @type {Record<string,string>}
|
103 | // */
|
104 | // const MpDependencies = {};
|
105 | // Object.keys(dependencies).forEach(function (name) {
|
106 | // const modulePath = dependencies[name];
|
107 | // const mpDistPath = getMiniprogramDistPath(modulePath);
|
108 | // if (mpDistPath && fs.existsSync(mpDistPath)) {
|
109 | // MpDependencies[name] = mpDistPath;
|
110 | // const depDependencies = getDependencies(dependencies[name]);
|
111 | // Object.keys(depDependencies).forEach(depName => {
|
112 | // // const depPath = resolveDependencyPath(depName,modulePath,cwd)
|
113 | // if (depName in dependencies) {
|
114 | // // if(depPath === dependencies[depName]){
|
115 | // // // skip
|
116 | // // }else{
|
117 | // // // resolve
|
118 | // // }
|
119 | // delete depDependencies[depName]
|
120 | // } else if (depName in MpDependencies) {
|
121 | // delete depDependencies[depName]
|
122 | // } else {
|
123 | // depDependencies[depName] = resolveDependencyPath(depName, modulePath, cwd)
|
124 | // }
|
125 | // })
|
126 | // Object.assign(MpDependencies, splitMiniProgramNpm(depDependencies, cwd))
|
127 | // Object.assign(dependencies, depDependencies)
|
128 | // delete dependencies[name];
|
129 | // }
|
130 | // })
|
131 | // return MpDependencies;
|
132 | // }
|
133 |
|
134 | /**
|
135 | * 筛选出 miniprogram components 式的包
|
136 | * @param {string[]} dependencies
|
137 | * @param {string} npmPath
|
138 | * @param {string} cwd
|
139 | * @returns {[Record<string,string>,Record<string,string>]} miniprogram components Dependencies
|
140 | */
|
141 | function getMp_NpmDependencies(dependencies, npmPath, cwd) {
|
142 | /**
|
143 | * @type {Record<string,string>}
|
144 | */
|
145 | const MpDependencies = {};
|
146 | /**
|
147 | * @type {Record<string,string>}
|
148 | */
|
149 | const NpmDependencies = {};
|
150 | dependencies.forEach(function (name) {
|
151 | const modulePath = path.resolve(npmPath, 'node_modules', name);
|
152 | const packageConfig = loadPackage(modulePath);
|
153 | const mpDistPath = path.resolve(modulePath, packageConfig.miniprogram || "miniprogram_dist");
|
154 |
|
155 | if (mpDistPath && fs.existsSync(mpDistPath)) {
|
156 | // mp 组件
|
157 | MpDependencies[name] = mpDistPath;
|
158 | const depDependencies = Object.keys(getDependencies(modulePath));
|
159 | depDependencies.forEach(depName => {
|
160 | if (depName in dependencies) {
|
161 | delete depDependencies[depName]
|
162 | } else if (depName in MpDependencies) {
|
163 | delete depDependencies[depName]
|
164 | } else {
|
165 | depDependencies[depName] = resolveDependencyPath(depName, modulePath, cwd)
|
166 | }
|
167 | })
|
168 | const depMpNpm = getMp_NpmDependencies(depDependencies, modulePath, cwd);
|
169 | Object.assign(MpDependencies, depMpNpm[0])
|
170 | Object.assign(NpmDependencies, depMpNpm[1])
|
171 | } else {
|
172 | // 普通NPM
|
173 | NpmDependencies[name] = path.resolve(modulePath, packageConfig.module || packageConfig["jsnext:main"] || packageConfig.main || "index.js");
|
174 | }
|
175 | })
|
176 | return [MpDependencies, NpmDependencies];
|
177 | }
|
178 |
|
179 | module.exports = {
|
180 | loadPackage,
|
181 | getDependencies,
|
182 | getMp_NpmDependencies,
|
183 | // resolveDependencies,
|
184 | // getMiniprogramDistPath,
|
185 | // splitMiniProgramNpm
|
186 | } |
\ | No newline at end of file |