UNPKG

1.75 kBPlain TextView Raw
1import * as fs from 'fs-extra'
2import * as path from 'path'
3import chalk from 'chalk'
4import * as _ from 'lodash'
5
6import {
7 BUILD_TYPES
8} from './util/constants'
9
10import {
11 getBuildData
12} from './mini/helper'
13import { build as buildMini } from './mini'
14import { IBuildOptions } from './util/types'
15import { Builder } from '.'
16
17const PLUGIN_JSON = 'plugin.json'
18const PLUGIN_MOCK_JSON = 'plugin-mock.json'
19
20export async function build (appPath: string, { watch, platform }: IBuildOptions, builder: Builder) {
21 switch (platform) {
22 case BUILD_TYPES.WEAPP:
23 await buildWxPlugin(appPath, { watch, type: BUILD_TYPES.WEAPP }, builder)
24 break
25 case BUILD_TYPES.ALIPAY:
26 await buildAlipayPlugin(appPath, { watch, type: BUILD_TYPES.ALIPAY }, builder)
27 break
28 default:
29 console.log(chalk.red('输入插件类型错误,目前只支持 weapp/alipay 插件类型'))
30 break
31 }
32}
33
34async function buildWxPlugin (appPath, { watch, type }, builder) {
35 await buildMini(appPath, { watch, type: BUILD_TYPES.PLUGIN }, null, builder)
36 const { outputDirName } = getBuildData()
37 await buildMini(appPath, { watch, type }, {
38 outputDirName: `${outputDirName}/miniprogram`
39 }, builder)
40}
41
42async function buildAlipayPlugin (appPath, { watch, type }, builder) {
43 await buildMini(appPath, { watch, type }, null, builder)
44 const {
45 sourceDir,
46 outputDir
47 } = getBuildData()
48 const pluginJson = path.join(sourceDir, PLUGIN_JSON)
49 const pluginMockJson = path.join(sourceDir, PLUGIN_MOCK_JSON)
50
51 if (fs.existsSync(pluginJson)) {
52 fs.copyFileSync(pluginJson, path.join(outputDir, PLUGIN_JSON))
53 }
54 if (fs.existsSync(pluginMockJson)) {
55 fs.copyFileSync(pluginMockJson, path.join(outputDir, PLUGIN_MOCK_JSON))
56 }
57}