1 | import * as path from "path";
|
2 | import * as fs from "fs-extra";
|
3 | import * as childProcess from "child_process";
|
4 | import {IClientPackageConfig, IProjectConfig} from "../commons/IProjectConfig";
|
5 | import {CliHelper} from "../commons/CliHelper";
|
6 |
|
7 | export async function runDeviceAsync(argv: { config?: string; package: string; release: boolean; debug: boolean }): Promise<void> {
|
8 | process.env.NODE_ENV = "development";
|
9 |
|
10 | let configFilePath = argv.config;
|
11 | configFilePath = configFilePath ? path.resolve(process.cwd(), configFilePath)
|
12 | : fs.existsSync(path.resolve(process.cwd(), "simplism.ts")) ? path.resolve(process.cwd(), "simplism.ts")
|
13 | : fs.existsSync(path.resolve(process.cwd(), "simplism.js")) ? path.resolve(process.cwd(), "simplism.js")
|
14 | : path.resolve(process.cwd(), "simplism.json");
|
15 |
|
16 | if (path.extname(configFilePath) === ".ts") {
|
17 |
|
18 | require("ts-node/register");
|
19 | }
|
20 |
|
21 |
|
22 | const projectConfig = eval("require(configFilePath)") as IProjectConfig;
|
23 |
|
24 | const config = projectConfig.packages.single(item => item.name === argv.package) as IClientPackageConfig | undefined;
|
25 | if (!config || !config.cordova || !config.platforms || !config.platforms.includes("android") || !config.devServer) {
|
26 | throw new Error("클라이언트 설정이 잘못되었습니다. [패키지: " + argv.package + "]");
|
27 | }
|
28 |
|
29 | if (argv.release || argv.debug) {
|
30 | const cordovaProjectPath = path.resolve(process.cwd(), "packages", argv.package, ".cordova");
|
31 |
|
32 | if (argv.release && config.cordova.sign) {
|
33 | fs.copySync(
|
34 | path.resolve(process.cwd(), ".sign", config.cordova.sign, "release-signing.jks"),
|
35 | path.resolve(cordovaProjectPath, "platforms", "android", "release-signing.jks")
|
36 | );
|
37 | fs.copySync(
|
38 | path.resolve(process.cwd(), ".sign", config.cordova.sign, "release-signing.properties"),
|
39 | path.resolve(cordovaProjectPath, "platforms", "android", "release-signing.properties")
|
40 | );
|
41 | }
|
42 |
|
43 | let configFileContent = fs.readFileSync(path.resolve(cordovaProjectPath, "config.xml"), "utf-8");
|
44 | configFileContent = configFileContent.replace(/<allow-navigation href="[^"]"\s?\/>/g, "");
|
45 | fs.writeFileSync(path.resolve(cordovaProjectPath, "config.xml"), configFileContent, "utf-8");
|
46 |
|
47 | const cordovaBinPath = path.resolve(process.cwd(), "node_modules", ".bin", "cordova.cmd");
|
48 | childProcess.spawnSync(
|
49 | cordovaBinPath,
|
50 | [
|
51 | "run",
|
52 | "android",
|
53 | "--device",
|
54 | argv.release ? "--release" : ""
|
55 | ],
|
56 | {
|
57 | shell: true,
|
58 | stdio: "inherit",
|
59 | cwd: cordovaProjectPath
|
60 | }
|
61 | );
|
62 | }
|
63 | else {
|
64 | const host = CliHelper.getCurrentIP(config.devServer.host);
|
65 | const devServerUrl = `http://${host}:${config.devServer.port}`;
|
66 | const cordovaProjectPath = path.resolve(process.cwd(), "packages", argv.package, ".cordova");
|
67 | fs.removeSync(path.resolve(cordovaProjectPath, "www"));
|
68 | fs.mkdirsSync(path.resolve(cordovaProjectPath, "www"));
|
69 | fs.writeFileSync(path.resolve(cordovaProjectPath, "www/index.html"), `'${devServerUrl}'로 이동중... <script>window.location.href = "${devServerUrl}";</script>`.trim(), "utf-8");
|
70 |
|
71 | let configFileContent = fs.readFileSync(path.resolve(cordovaProjectPath, "config.xml"), "utf-8");
|
72 | if (!new RegExp(`<allow-navigation href="${devServerUrl}(/\\*)?"\\s?/>`).test(configFileContent)) {
|
73 | configFileContent = configFileContent.replace("</widget>", `<allow-navigation href="${devServerUrl}" /></widget>`);
|
74 | configFileContent = configFileContent.replace("</widget>", `<allow-navigation href="${devServerUrl}/*" /></widget>`);
|
75 | fs.writeFileSync(path.resolve(cordovaProjectPath, "config.xml"), configFileContent, "utf-8");
|
76 | }
|
77 |
|
78 | childProcess.spawnSync(
|
79 | "cordova",
|
80 | ["run", "android", "--device"],
|
81 | {
|
82 | stdio: "inherit",
|
83 | shell: true,
|
84 | cwd: cordovaProjectPath
|
85 | }
|
86 | );
|
87 | }
|
88 | }
|