UNPKG

8.26 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const path = require("path");
12const child_process_1 = require("child_process");
13const ora = require("ora");
14const util_1 = require("../../util");
15const dowload_1 = require("../../util/dowload");
16const defaultManifestJSON = require("../../config/manifest.default.json");
17exports.default = (ctx) => {
18 ctx.registerPlatform({
19 name: 'quickapp',
20 useConfigName: 'mini',
21 fn({ config }) {
22 return __awaiter(this, void 0, void 0, function* () {
23 const { appPath, nodeModulesPath } = ctx.paths;
24 const { isWatch, port, release } = ctx.runOpts;
25 const { npm, fs, printLog, processTypeEnum, chalk, shouldUseYarn, isWindows, shouldUseCnpm, unzip } = ctx.helper;
26 // 读取 project.quickapp.json
27 const quickappJSONPath = path.join(appPath, 'project.quickapp.json');
28 let quickappJSON;
29 if (fs.existsSync(quickappJSONPath)) {
30 quickappJSON = fs.readJSONSync(quickappJSONPath);
31 }
32 else {
33 printLog(processTypeEnum.WARNING, '缺少配置', `检测到项目目录下未添加 ${chalk.bold('project.quickapp.json')} 文件,将使用默认配置,参考文档 https://nervjs.github.io/taro/docs/project-config.html`);
34 quickappJSON = defaultManifestJSON;
35 }
36 const originalOutputRoot = config.outputRoot;
37 config.outputRoot = `${originalOutputRoot}/src`;
38 // 准备 miniRunner 参数
39 const miniRunnerOpts = Object.assign({}, config, { nodeModulesPath, buildAdapter: config.platform, isBuildPlugin: false, globalObject: 'global', fileType: {
40 templ: '.ux',
41 style: '.css',
42 config: '.json',
43 script: '.js'
44 }, isUseComponentBuildPage: false, quickappJSON, isBuildQuickapp: true });
45 ctx.modifyBuildTempFileContent(({ tempFiles }) => {
46 const replaceKeyMap = {
47 navigationBarTitleText: 'titleBarText',
48 navigationBarBackgroundColor: 'titleBarBackgroundColor',
49 navigationBarTextStyle: 'titleBarTextColor',
50 pageOrientation: 'orientation',
51 backgroundTextStyle: false,
52 onReachBottomDistance: false,
53 backgroundColorBottom: false,
54 backgroundColorTop: false
55 };
56 Object.keys(tempFiles).forEach(key => {
57 const item = tempFiles[key];
58 if (item.config) {
59 util_1.recursiveReplaceObjectKeys(item.config, replaceKeyMap);
60 }
61 });
62 });
63 // build with webpack
64 const miniRunner = yield npm.getNpmPkg('@tarojs/mini-runner', appPath);
65 yield miniRunner(appPath, miniRunnerOpts);
66 const isReady = yield prepareQuickAppEnvironment({
67 originalOutputRoot,
68 fs,
69 appPath,
70 chalk,
71 shouldUseYarn,
72 isWindows,
73 shouldUseCnpm,
74 unzip
75 });
76 if (!isReady) {
77 console.log();
78 console.log(chalk.red('快应用环境准备失败,请重试!'));
79 process.exit(0);
80 }
81 yield runQuickApp({
82 isWatch,
83 originalOutputRoot,
84 port,
85 release
86 });
87 });
88 }
89 });
90};
91function prepareQuickAppEnvironment({ originalOutputRoot, fs, appPath, chalk, shouldUseYarn, isWindows, shouldUseCnpm, unzip }) {
92 return __awaiter(this, void 0, void 0, function* () {
93 let isReady = false;
94 let needDownload = false;
95 let needInstall = false;
96 console.log();
97 if (fs.existsSync(path.join(originalOutputRoot, 'sign'))) {
98 needDownload = false;
99 }
100 else {
101 needDownload = true;
102 }
103 if (needDownload) {
104 const getSpinner = ora('开始下载快应用运行容器...').start();
105 yield dowload_1.downloadGithubRepoLatestRelease('NervJS/quickapp-container', appPath, originalOutputRoot);
106 yield unzip(path.join(originalOutputRoot, 'download_temp.zip'));
107 getSpinner.succeed('快应用运行容器下载完成');
108 }
109 else {
110 console.log(`${chalk.green('✔ ')} 快应用容器已经准备好`);
111 }
112 process.chdir(originalOutputRoot);
113 console.log();
114 if (fs.existsSync(path.join(originalOutputRoot, 'node_modules'))) {
115 needInstall = false;
116 }
117 else {
118 needInstall = true;
119 }
120 if (needInstall) {
121 let command;
122 if (shouldUseYarn()) {
123 if (!isWindows) {
124 command = 'NODE_ENV=development yarn install';
125 }
126 else {
127 command = 'yarn install';
128 }
129 }
130 else if (shouldUseCnpm()) {
131 if (!isWindows) {
132 command = 'NODE_ENV=development cnpm install';
133 }
134 else {
135 command = 'cnpm install';
136 }
137 }
138 else {
139 if (!isWindows) {
140 command = 'NODE_ENV=development npm install';
141 }
142 else {
143 command = 'npm install';
144 }
145 }
146 const installSpinner = ora(`安装快应用依赖环境, 需要一会儿...`).start();
147 try {
148 const stdout = child_process_1.execSync(command);
149 installSpinner.color = 'green';
150 installSpinner.succeed('安装成功');
151 console.log(`${stdout}`);
152 isReady = true;
153 }
154 catch (error) {
155 installSpinner.color = 'red';
156 installSpinner.fail(chalk.red(`快应用依赖环境安装失败,请进入 ${path.basename(originalOutputRoot)} 重新安装!`));
157 console.log(`${error}`);
158 isReady = false;
159 }
160 }
161 else {
162 console.log(`${chalk.green('✔ ')} 快应用依赖已经安装好`);
163 isReady = true;
164 }
165 return isReady;
166 });
167}
168function runQuickApp({ isWatch, originalOutputRoot, port, release }) {
169 return __awaiter(this, void 0, void 0, function* () {
170 const { compile } = require(require.resolve('hap-toolkit/lib/commands/compile', { paths: [originalOutputRoot] }));
171 if (isWatch) {
172 const { launchServer } = require(require.resolve('@hap-toolkit/server', { paths: [originalOutputRoot] }));
173 launchServer({
174 port: port || 12306,
175 watch: isWatch,
176 clearRecords: false,
177 disableADB: false
178 });
179 compile('native', 'dev', true);
180 }
181 else {
182 if (!release) {
183 compile('native', 'dev', false);
184 }
185 else {
186 compile('native', 'prod', false);
187 }
188 }
189 });
190}