-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.ts
123 lines (111 loc) · 3.46 KB
/
gulpfile.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import gulp from 'gulp'
import { TaskCallback } from 'undertaker'
import path from 'path'
import packageJson from './package.json'
import fs from 'fs/promises'
import os from 'os'
import { delDist, buildCjs, buildEsm, npmlink, buildNpm } from './builds/index'
import { exec as exec_, ExecOptions, execFile } from 'child_process'
import { promisify } from 'util'
import chalk from 'chalk'
const exec = promisify(exec_)
const pkgRoot = path.join(__dirname, '.')
const srcGlob = path.join(pkgRoot, './index.ts')
const dest = path.join(pkgRoot, path.dirname(packageJson.main))
const cjsFilename = path.basename(packageJson.main)
const esmFilename = path.basename(packageJson.module)
const delPath = path.join(pkgRoot, 'dist')
export async function build(done: TaskCallback) {
try {
await delDist(delPath)
await Promise.all([
buildCjs(srcGlob, dest, { 'index.js': cjsFilename }, false),
buildEsm(srcGlob, dest, { 'index.js': esmFilename }, false),
])
done()
} catch (error: any) {
done(error)
}
}
export async function mpTestLink(done: TaskCallback) {
const log = function (list: any[]) {
list.forEach((item) => {
if (!item) return
if (item.code === 1) {
console.log(chalk.red(item.stdout))
} else {
console.log(chalk.green(item.stdout))
}
})
}
// 运行miniprogram-tests的小程序之前先对使用到的包进行link
const packagesPath = path.join(pkgRoot, 'packages')
const packageDirNameList = await fs.readdir(packagesPath)
const packagePathList = packageDirNameList.map((dirname) => {
const packagePath = path.join(packagesPath, dirname)
return packagePath
})
const packageNameList: string[] = (
await Promise.all(
packageDirNameList.map((dirname) => {
const packagePath = path.join(packagesPath, dirname)
return fs.readFile(path.join(packagePath, 'package.json'))
})
)
).map((pkgJsonBuf) => {
return JSON.parse(pkgJsonBuf.toString()).name
})
const packageList = packagePathList.map((pkgPath, idx) => {
return {
pkgPath,
pkgName: packageNameList[idx],
}
})
// 环境变量不生效时可以手动设置
// const PNPM_HOME = '/Users/zhao/Library/pnpm'
const execEnvOptions = {
env: {
...process.env,
// PNPM_HOME,
// PATH: `${PNPM_HOME}:${process.env.PATH}`,
},
}
// const createLinkResults = await Promise.all(
// packageList.map(({ pkgPath }) => {
// return exec(`pnpm link --global`, {
// ...execEnvOptions,
// cwd: pkgPath,
// })
// })
// )
// log(createLinkResults)
const mpTestDirPath = path.join(pkgRoot, 'miniprogram-tests')
let mpTestDirList = await fs.readdir(mpTestDirPath)
const blackList = ['common', 'test-lib.ts']
mpTestDirList = mpTestDirList.filter(
(dirname) => !blackList.includes(dirname)
)
const linkPkgResults = await Promise.all(
mpTestDirList
.map((dirname) => {
const mpTestProjectPath = path.join(mpTestDirPath, dirname)
return packageList.map(({ pkgPath, pkgName }) => {
return fs
.stat(path.join(mpTestProjectPath, 'node_modules', pkgName))
.then(
(stat) => {},
(err) => {
return exec(`pnpm link ${pkgPath}`, {
...execEnvOptions,
cwd: mpTestProjectPath,
})
}
)
})
})
.flat()
)
log(linkPkgResults)
await buildNpm()
done()
}