-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.ts
98 lines (77 loc) · 2.47 KB
/
load.ts
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
import path from "node:path";
import { execSync } from "node:child_process";
import { cwd } from 'node:process'
import { getDirPath } from "./utils";
import { cp } from "fs/promises";
import { getAkConfig, AkConfig } from "./utils/config";
import log from "./utils/log";
const copyBuildToMp = async (akConfig: AkConfig, packageJson: any) => {
const { buildCommand, miniprogramPath, distPath } = akConfig;
const { name } = packageJson;
const curPath = cwd();
const projectPath = path.resolve(curPath, miniprogramPath);
const miniprogramDist = path.join(curPath, distPath);
// 构建 npm 包
execSync(`cd ${curPath} && ${buildCommand}`);
// 找到宿主小程序中对应目录
const pages = getDirPath(projectPath, name)
if (!pages.length) {
log.error(`没有在宿主小程序中找到 ${name} 的路径`)
return;
}
// 直接复制 npm 包到 miniprogram_npm 下
const task = pages.map((i) =>
cp(
miniprogramDist,
path.join(i, "miniprogram_npm", name),
{ recursive: true },
),
);
await Promise.all(task).catch((e) => console.error(e));
}
const updateNpmAndBuild = async (akConfig: AkConfig, packageJson: any, version: string) => {
const { miniprogramPath, privateKeyPath } = akConfig;
const { name } = packageJson;
const projectConfigPath = path.join(miniprogramPath, "project.config.json");
const { appid } = require(projectConfigPath);
const ci = require('miniprogram-ci');
const project = new ci.Project({
projectPath: miniprogramPath,
type: 'miniProgram',
privateKeyPath,
appid,
})
const pages = getDirPath(miniprogramPath, name)
for (const path of pages) {
let command = `cd ${path} && npm install ${name}@${version}`
// if (config.npmRegister) {
// command += ` --registry ${config.npmRegister}`
// }
execSync(command);
}
// 在有需要的时候构建npm
await ci.packNpm(project, {
reporter: (infos: any) => {
log.info(JSON.stringify(infos, null, 2))
}
})
}
export default async (version: string) => {
const akConfig = getAkConfig();
const packageJson = require(path.join(cwd(), "package.json"));
if (!akConfig) {
log.error("没有找到 ak.config.json")
return;
};
if (!packageJson) {
log.error("没有找到 package.json")
return;
};
if (version) {
await updateNpmAndBuild(akConfig, packageJson, version)
} else {
await copyBuildToMp(akConfig, packageJson);
}
log.success("🔫 装填成功!")
process.exit();
};