1 | import * as fs from 'fs-extra'
|
2 | import * as path from 'path'
|
3 | import chalk from 'chalk'
|
4 | import * as _ from 'lodash'
|
5 |
|
6 | import {
|
7 | BUILD_TYPES
|
8 | } from './util/constants'
|
9 |
|
10 | import {
|
11 | getBuildData
|
12 | } from './mini/helper'
|
13 | import { build as buildMini } from './mini'
|
14 | import { IBuildOptions } from './util/types'
|
15 | import { Builder } from '.'
|
16 |
|
17 | const PLUGIN_JSON = 'plugin.json'
|
18 | const PLUGIN_MOCK_JSON = 'plugin-mock.json'
|
19 |
|
20 | export 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 |
|
34 | async 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 |
|
42 | async 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 | }
|