1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.postinstall = void 0;
|
4 | const path_1 = require("path");
|
5 | const fs_1 = require("fs");
|
6 | const child_process_1 = require("child_process");
|
7 | const plugins_1 = require("./plugins");
|
8 | const utils_1 = require("./utils");
|
9 | const command_core_1 = require("@midwayjs/command-core");
|
10 | const matchReg = /(?:^|\s)(?:midway-bin|mw)\s+([a-z]+?)(?:\s|$)/i;
|
11 | const postinstall = async (baseDir) => {
|
12 | const pkgJson = getPkgJson(baseDir);
|
13 | const pkgJsonList = [];
|
14 | if (pkgJson) {
|
15 | pkgJsonList.push(pkgJson);
|
16 | }
|
17 | let isLerna = false;
|
18 |
|
19 | if (fs_1.existsSync(path_1.join(baseDir, 'lerna.json'))) {
|
20 | const lernaPackagesJson = getLernaPackagesJson();
|
21 | pkgJsonList.push(...lernaPackagesJson);
|
22 | isLerna = true;
|
23 | }
|
24 | const modMap = {};
|
25 | const installedModMap = {};
|
26 | pkgJsonList.forEach(pkgJson => {
|
27 | if (!pkgJson) {
|
28 | return;
|
29 | }
|
30 | Object.assign(installedModMap, pkgJson.dependencies, pkgJson.devDependencies);
|
31 | if (pkgJson.scripts) {
|
32 | Object.keys(pkgJson.scripts).forEach(script => {
|
33 | const cmd = pkgJson.scripts[script];
|
34 | cmdToMod(cmd, modMap, installedModMap);
|
35 | });
|
36 | }
|
37 | });
|
38 | const allMods = Object.keys(modMap);
|
39 | const npm = utils_1.findNpm().cmd;
|
40 | for (const name of allMods) {
|
41 | console.log('[midway] auto install', name);
|
42 | await command_core_1.installNpm({
|
43 | baseDir,
|
44 | register: npm,
|
45 | npmName: name,
|
46 | slience: true,
|
47 | isLerna,
|
48 | });
|
49 | }
|
50 | console.log('[midway] auto install complete');
|
51 | };
|
52 | exports.postinstall = postinstall;
|
53 | const cmdToMod = (cmd, modMap, installedModMap) => {
|
54 | if (matchReg.test(cmd)) {
|
55 | const command = matchReg.exec(cmd)[1];
|
56 | const mod = plugins_1.PluginList.filter(plugin => {
|
57 | return !plugin.installed && plugin.command === command;
|
58 | });
|
59 | if (Array.isArray(mod) && mod.length) {
|
60 | mod.forEach(modInfo => {
|
61 | const modName = modInfo.mod;
|
62 | if (installedModMap[modName]) {
|
63 | return;
|
64 | }
|
65 | if (!modMap[modName]) {
|
66 | modMap[modName] = true;
|
67 | }
|
68 | });
|
69 | }
|
70 | }
|
71 | };
|
72 | const getLernaPackagesJson = () => {
|
73 | const pkgJsonList = [];
|
74 | try {
|
75 | const originData = child_process_1.execSync('npx lerna ls --json').toString();
|
76 | const packageInfoList = JSON.parse(originData);
|
77 | packageInfoList.forEach(packageInfo => {
|
78 | const pkgJson = getPkgJson(packageInfo.location);
|
79 | if (pkgJson) {
|
80 | pkgJsonList.push(pkgJson);
|
81 | }
|
82 | });
|
83 | }
|
84 | catch (_a) {
|
85 |
|
86 | }
|
87 | return pkgJsonList;
|
88 | };
|
89 | const getPkgJson = (dirPath) => {
|
90 | const pkgFile = path_1.join(dirPath, 'package.json');
|
91 | if (!fs_1.existsSync(pkgFile)) {
|
92 | return;
|
93 | }
|
94 | return JSON.parse(fs_1.readFileSync(pkgFile).toString());
|
95 | };
|
96 |
|
\ | No newline at end of file |