@simplysm/sd-cli 7.0.81 → 7.0.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
- import { INpmConfig, ISdCliClientPackageCordovaConfig, TSdCliCordovaPlatform } from "../commons";
1
+ import { INpmConfig, ISdCliClientBuilderCordovaConfig } from "../commons";
2
2
  import * as path from "path";
3
3
  import { FsUtil, Logger, SdProcess } from "@simplysm/sd-core-node";
4
- import { NotImplementError } from "@simplysm/sd-core-common";
5
4
  import JSZip from "jszip";
5
+ import xml2js from "xml2js";
6
6
 
7
7
  export class SdCliCordova {
8
8
  protected readonly _logger: Logger;
@@ -12,11 +12,14 @@ export class SdCliCordova {
12
12
  public readonly cordovaPath = path.resolve(this._rootPath, ".cordova");
13
13
  private readonly _binPath = path.resolve(process.cwd(), "node_modules/.bin/cordova.cmd");
14
14
 
15
- public get platforms(): TSdCliCordovaPlatform[] {
16
- return this._config.platforms;
15
+ public get platforms(): ("browser" | "android")[] {
16
+ return [
17
+ ...this._config.targets?.browser ? ["browser" as const] : [],
18
+ ...this._config.targets?.android ? ["android" as const] : []
19
+ ];
17
20
  }
18
21
 
19
- public constructor(private readonly _rootPath: string, private readonly _config: ISdCliClientPackageCordovaConfig) {
22
+ public constructor(private readonly _rootPath: string, private readonly _config: ISdCliClientBuilderCordovaConfig) {
20
23
  this._npmConfig = FsUtil.readJson(path.resolve(this._rootPath, "package.json"));
21
24
  this._logger = Logger.get(["simplysm", "sd-cli", this.constructor.name, this._npmConfig.name]);
22
25
  }
@@ -52,156 +55,156 @@ export class SdCliCordova {
52
55
  }
53
56
  }
54
57
 
55
- // 미설치 플러그인들 설치
56
- if (this._config.plugins) {
57
- const pluginsFetch = FsUtil.exists(path.resolve(this.cordovaPath, "plugins/fetch.json"))
58
- ? await FsUtil.readJsonAsync(path.resolve(this.cordovaPath, "plugins/fetch.json"))
59
- : undefined;
60
- const alreadyPlugins = pluginsFetch != undefined
61
- ? Object.values(pluginsFetch)
62
- .map((item: any) => (item.source.id !== undefined ? item.source.id.replace(/@.*$/, "") : item.source.url))
63
- : [];
64
-
65
- for (const plugin of this._config.plugins.distinct()) {
66
- if (!alreadyPlugins.includes(plugin)) {
67
- await this._execAsync(`${this._binPath} plugin add ${plugin}`, this.cordovaPath);
68
- }
58
+ // 설치 미빌드 플랫폼 삭제
59
+ for (const alreadyPlatform of alreadyPlatforms) {
60
+ if (this._config[alreadyPlatform] == null) {
61
+ await this._execAsync(`${this._binPath} platform remove ${alreadyPlatform}`, this.cordovaPath);
69
62
  }
70
63
  }
71
64
 
72
- // ICON 파일 복사
73
- if (this._config.icon !== undefined) {
74
- await FsUtil.copyAsync(path.resolve(this._rootPath, this._config.icon), path.resolve(this.cordovaPath, "res", "icon", "icon.png"));
75
- }
76
-
77
- // CONFIG
78
- const configFilePath = path.resolve(this.cordovaPath, "config.xml");
79
- let configFileContent = await FsUtil.readFileAsync(configFilePath);
80
-
81
- // CONFIG: 버전 설정
82
- configFileContent = configFileContent.replace(/version="[^"]*"/g, `version="${this._npmConfig.version}"`);
83
-
84
- // CONFIG: ICON 설정
85
- if (this._config.icon !== undefined && !configFileContent.includes("<icon")) {
86
- configFileContent = configFileContent.replace("</widget>", " <icon src=\"res/icon/icon.png\" />\r\n</widget>");
65
+ // 미설치 플러그인들 설치
66
+ const pluginsFetch = FsUtil.exists(path.resolve(this.cordovaPath, "plugins/fetch.json"))
67
+ ? await FsUtil.readJsonAsync(path.resolve(this.cordovaPath, "plugins/fetch.json"))
68
+ : undefined;
69
+ const alreadyPlugins = pluginsFetch != undefined
70
+ ? Object.values(pluginsFetch)
71
+ .map((item: any) => (item.source.id !== undefined ? item.source.id.replace(/@.*$/, "") : item.source.url))
72
+ : [];
73
+
74
+ for (const plugin of this._config.plugins?.distinct() ?? []) {
75
+ if (!alreadyPlugins.includes(plugin)) {
76
+ await this._execAsync(`${this._binPath} plugin add ${plugin}`, this.cordovaPath);
77
+ }
87
78
  }
88
79
 
89
- // CONFIG: ANDROID usesCleartextTraffic 설정
90
- if (this._config.platforms.includes("android")) {
91
- if (!configFileContent.includes("xmlns:android=\"http://schemas.android.com/apk/res/android\"")) {
92
- configFileContent = configFileContent.replace(
93
- "xmlns=\"http://www.w3.org/ns/widgets\"",
94
- `xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android"`
95
- );
96
- }
97
- if (!configFileContent.includes("application android:usesCleartextTraffic=\"true\" />")) {
98
- if (configFileContent.includes("<platform name=\"android\">")) {
99
- configFileContent = configFileContent.replace("<platform name=\"android\">", `<platform name="android">
100
- <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
101
- <application android:usesCleartextTraffic="true" />
102
- </edit-config>`);
103
- }
104
- else {
105
- configFileContent = configFileContent.replace("<content src=\"index.html\" />", `<content src="index.html" />
106
- <platform name="android">
107
- <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
108
- <application android:usesCleartextTraffic="true" />
109
- </edit-config>
110
- </platform>`);
111
- }
80
+ // 설치된 미사용 플러그인 삭제
81
+ for (const alreadyPlugin of alreadyPlugins) {
82
+ if (!(this._config.plugins?.distinct() ?? []).includes(alreadyPlugin)) {
83
+ await this._execAsync(`${this._binPath} plugin remove ${alreadyPlugin}`, this.cordovaPath);
112
84
  }
113
85
  }
114
86
 
115
- // CONFIG: 파일쓰기
116
- await FsUtil.writeFileAsync(configFilePath, configFileContent);
117
- }
118
-
119
- public async buildAsync(outPath: string): Promise<void> {
120
- const packageType = this._config.buildOption?.bundle ? "bundle" : "apk";
121
-
122
- // ANDROID 서명 처리
123
- if (this._config.buildOption?.sign?.android) {
87
+ // ANDROID SIGN 파일 복사
88
+ if (this._config.targets?.android?.sign) {
124
89
  await FsUtil.copyAsync(
125
- path.resolve(this._rootPath, this._config.buildOption.sign.android.keystore),
126
- path.resolve(this.cordovaPath, path.basename(this._config.buildOption.sign.android.keystore))
90
+ path.resolve(this._rootPath, this._config.targets.android.sign.keystore),
91
+ path.resolve(this.cordovaPath, "android.keystore")
127
92
  );
128
- await FsUtil.writeJsonAsync(
129
- path.resolve(this.cordovaPath, "build.json"),
130
- {
93
+ }
94
+ else {
95
+ await FsUtil.removeAsync(path.resolve(this.cordovaPath, "android.keystore"));
96
+ }
97
+
98
+ // 빌드 옵션 파일 생성
99
+ await FsUtil.writeJsonAsync(
100
+ path.resolve(this.cordovaPath, "build.json"),
101
+ {
102
+ ...this._config.targets?.android ? {
131
103
  android: {
132
104
  release: {
133
- keystore: path.basename(this._config.buildOption.sign.android.keystore),
134
- storePassword: this._config.buildOption.sign.android.storePassword,
135
- alias: this._config.buildOption.sign.android.alias,
136
- password: this._config.buildOption.sign.android.password,
137
- keystoreType: this._config.buildOption.sign.android.keystoreType,
138
- packageType
105
+ packageType: this._config.targets.android.bundle ? "bundle" : "apk",
106
+ ...this._config.targets.android.sign ? {
107
+ keystore: path.resolve(this.cordovaPath, "android.keystore"),
108
+ storePassword: this._config.targets.android.sign.storePassword,
109
+ alias: this._config.targets.android.sign.alias,
110
+ password: this._config.targets.android.sign.password,
111
+ keystoreType: this._config.targets.android.sign.keystoreType
112
+ } : {}
139
113
  }
140
114
  }
141
- }
142
- );
115
+ } : {}
116
+ }
117
+ );
118
+
119
+ // ICON 파일 복사
120
+ if (this._config.icon !== undefined) {
121
+ await FsUtil.copyAsync(path.resolve(this._rootPath, this._config.icon), path.resolve(this.cordovaPath, "res", "icon.png"));
122
+ }
123
+ else {
124
+ await FsUtil.removeAsync(path.resolve(this.cordovaPath, "res", "icon.png"));
143
125
  }
144
126
 
145
- // CONFIG
127
+ // CONFIG: 초기값 백업
146
128
  const configFilePath = path.resolve(this.cordovaPath, "config.xml");
147
- let configFileContent = await FsUtil.readFileAsync(configFilePath);
129
+ const configBackFilePath = path.resolve(this.cordovaPath, "config.xml.bak");
130
+ if (!FsUtil.exists(configBackFilePath)) {
131
+ await FsUtil.copyAsync(configFilePath, configBackFilePath);
132
+ }
133
+
134
+ // CONFIG: 초기값 읽기
135
+ const configFileContent = await FsUtil.readFileAsync(configBackFilePath);
136
+ const configXml = await xml2js.parseStringPromise(configFileContent);
137
+
138
+
139
+ // CONFIG: 버전 설정
140
+ configXml.widget.$.version = this._npmConfig.version;
141
+
142
+ // CONFIG: ICON 설정
143
+ if (this._config.icon !== undefined) {
144
+ configXml["widget"]["icon"] = [{ "$": { "src": "res/icon.png" } }];
145
+ }
146
+
147
+ // CONFIG: 접근허용 세팅
148
+ configXml["widget"]["allow-navigation"] = [{ "$": { "href": "*://*/*" } }];
148
149
 
149
- // CONFIG: 접근허용 일단 모두 지우기
150
- configFileContent = configFileContent.replace(/ {4}<allow-navigation href="[^"]*" \/>\n/g, "");
150
+ // CONFIG: ANDROID usesCleartextTraffic 설정
151
+ if (this._config.targets?.android) {
152
+ configXml.widget.$["xmlns:android"] = "http://schemas.android.com/apk/res/android";
153
+
154
+ configXml["widget"]["platform"] = configXml["widget"]["platform"] ?? [];
155
+ configXml["widget"]["platform"].push({
156
+ "$": {
157
+ "name": "android"
158
+ },
159
+ "edit-config": [{
160
+ "$": {
161
+ "file": "app/src/main/AndroidManifest.xml",
162
+ "mode": "merge",
163
+ "target": "/manifest/application"
164
+ },
165
+ "application": [{
166
+ "android:usesCleartextTraffic": "true"
167
+ }]
168
+ }]
169
+ });
170
+ }
151
171
 
152
- // CONFIG: 접근허용 등록
153
- configFileContent = configFileContent.replace("</widget>", ` <allow-navigation href="*://*/*" />\n</widget>`);
172
+ // CONFIG: 파일 새로 쓰기
173
+ const configResultContent = new xml2js.Builder().buildObject(configXml);
174
+ await FsUtil.writeFileAsync(configFilePath, configResultContent);
154
175
 
155
- // CONFIG: 파일쓰기
156
- await FsUtil.writeFileAsync(configFilePath, configFileContent);
176
+ // 플랫폼 www 준비
177
+ await this._execAsync(`${this._binPath} prepare`, this.cordovaPath);
178
+ }
157
179
 
180
+ public async buildAsync(outPath: string): Promise<void> {
158
181
  // 실행
159
- const buildType = this._config.buildOption?.debug ? "debug" : "release";
182
+ const buildType = this._config.debug ? "debug" : "release";
160
183
  for (const platform of this.platforms) {
161
- await this._execAsync(`${this._binPath} build ${platform} --${buildType} --packageType=${packageType}`, this.cordovaPath);
184
+ await this._execAsync(`${this._binPath} build ${platform} --${buildType}`, this.cordovaPath);
162
185
  }
163
186
 
164
- // 결과물 복사
165
- for (const platform of this.platforms) {
166
- if (platform === "android") {
167
- const targetOutPath = path.resolve(outPath, platform);
168
- const apkFileName = this._config.buildOption?.sign?.android ? `app-${buildType}.apk` : `app-${buildType}-unsigned.apk`;
169
- const distApkFileName = path.basename(`${this._config.appName}${this._config.buildOption?.sign?.android ? "" : "-unsigned"}-v${this._npmConfig.version}.apk`);
170
- const latestDistApkFileName = path.basename(`${this._config.appName}${this._config.buildOption?.sign?.android ? "" : "-unsigned"}-latest.apk`);
171
- await FsUtil.mkdirsAsync(targetOutPath);
172
- await FsUtil.copyAsync(
173
- path.resolve(this.cordovaPath, "platforms/android/app/build/outputs/apk", buildType, apkFileName),
174
- path.resolve(targetOutPath, distApkFileName)
175
- );
176
- await FsUtil.copyAsync(
177
- path.resolve(this.cordovaPath, "platforms/android/app/build/outputs/apk", buildType, apkFileName),
178
- path.resolve(targetOutPath, latestDistApkFileName)
179
- );
180
- }
181
- else if (platform === "electron") {
182
- const targetOutPath = path.resolve(outPath, platform);
183
- const outFileName = `${this._config.appName} Setup ${this._npmConfig.version}.exe`;
184
- const distFileName = outFileName;
185
- const latestDistFileName = `${this._config.appName} Setup latest.exe`;
186
- await FsUtil.mkdirsAsync(targetOutPath);
187
- await FsUtil.copyAsync(
188
- path.resolve(this.cordovaPath, "platforms/electron/build/", outFileName),
189
- path.resolve(targetOutPath, distFileName)
190
- );
191
- await FsUtil.copyAsync(
192
- path.resolve(this.cordovaPath, "platforms/electron/build/", outFileName),
193
- path.resolve(targetOutPath, latestDistFileName)
194
- );
195
- }
196
- else {
197
- throw new NotImplementError();
198
- }
187
+ // 결과물 복사: ANDROID
188
+ if (this._config.targets?.android) {
189
+ const targetOutPath = path.resolve(outPath, "android");
190
+ const apkFileName = this._config.targets.android.sign ? `app-${buildType}.apk` : `app-${buildType}-unsigned.apk`;
191
+ const distApkFileName = path.basename(`${this._config.appName}${this._config.targets.android.sign ? "" : "-unsigned"}-v${this._npmConfig.version}.apk`);
192
+ const latestDistApkFileName = path.basename(`${this._config.appName}${this._config.targets.android.sign ? "" : "-unsigned"}-latest.apk`);
193
+ await FsUtil.mkdirsAsync(targetOutPath);
194
+ await FsUtil.copyAsync(
195
+ path.resolve(this.cordovaPath, "platforms/android/app/build/outputs/apk", buildType, apkFileName),
196
+ path.resolve(targetOutPath, distApkFileName)
197
+ );
198
+ await FsUtil.copyAsync(
199
+ path.resolve(this.cordovaPath, "platforms/android/app/build/outputs/apk", buildType, apkFileName),
200
+ path.resolve(targetOutPath, latestDistApkFileName)
201
+ );
199
202
  }
200
203
 
201
- if (this.platforms.includes("android")) {
204
+ if (this._config.targets?.android) {
202
205
  // 자동업데이트를 위한 zip 파일 쓰기
203
206
  const zip = new JSZip();
204
- const resultFiles = await FsUtil.globAsync(path.resolve(this.cordovaPath, "www", "**/*"), {
207
+ const resultFiles = await FsUtil.globAsync(path.resolve(this.cordovaPath, "platforms", "android", "app", "src", "main", "assets", "www", "**/*"), {
205
208
  dot: true,
206
209
  nodir: true
207
210
  });
@@ -218,24 +221,11 @@ export class SdCliCordova {
218
221
  }
219
222
  }
220
223
 
221
- public static async runWebviewOnDeviceAsync(cordovaPath: string, platform: TSdCliCordovaPlatform, url: string): Promise<void> {
224
+ public static async runWebviewOnDeviceAsync(cordovaPath: string, platform: "browser" | "android", url: string): Promise<void> {
222
225
  await FsUtil.removeAsync(path.resolve(cordovaPath, "www"));
223
226
  await FsUtil.mkdirsAsync(path.resolve(cordovaPath, "www"));
224
227
  await FsUtil.writeFileAsync(path.resolve(cordovaPath, "www/index.html"), `'${url}'로 이동중... <script>setTimeout(function () {window.location.href = "${url}"}, 3000);</script>`.trim());
225
228
 
226
- // CONFIG
227
- const configFilePath = path.resolve(cordovaPath, "config.xml");
228
- let configFileContent = await FsUtil.readFileAsync(configFilePath);
229
-
230
- // CONFIG: 접근허용 일단 모두 지우기
231
- configFileContent = configFileContent.replace(/ {4}<allow-navigation href="[^"]*" \/>\n/g, "");
232
-
233
- // CONFIG: 접근허용 등록
234
- configFileContent = configFileContent.replace("</widget>", ` <allow-navigation href="*://*/*" />\n</widget>`);
235
-
236
- // CONFIG: 파일쓰기
237
- await FsUtil.writeFileAsync(configFilePath, configFileContent);
238
-
239
229
  const binPath = path.resolve(process.cwd(), "node_modules/.bin/cordova.cmd");
240
230
  await SdProcess.spawnAsync(`${binPath} run ${platform} --device`, { cwd: cordovaPath }, true);
241
231
  }
@@ -0,0 +1,25 @@
1
+ import { FsUtil, SdProcess } from "@simplysm/sd-core-node";
2
+ import { INpmConfig } from "../commons";
3
+ import path from "path";
4
+
5
+ export class SdCliElectron {
6
+ public static async runWebviewOnDeviceAsync(rootPath: string, pkgName: string): Promise<void> {
7
+ const electronPath = path.resolve(rootPath, `packages/${pkgName}/.electron`);
8
+
9
+ const npmConfig = (await FsUtil.readJsonAsync(path.resolve(rootPath, `packages/${pkgName}/package.json`))) as INpmConfig;
10
+ await FsUtil.writeJsonAsync(path.resolve(electronPath, `package.json`), {
11
+ name: npmConfig.name,
12
+ version: npmConfig.version,
13
+ description: npmConfig.description,
14
+ main: "electron.js",
15
+ author: npmConfig.author,
16
+ license: npmConfig.license
17
+ });
18
+
19
+ await FsUtil.copyAsync(path.resolve(rootPath, `packages/${pkgName}/src/electron.dev.js`), path.resolve(electronPath, "electron.js"));
20
+
21
+ await SdProcess.spawnAsync(`electron "${electronPath}"`, {
22
+ cwd: rootPath
23
+ }, true);
24
+ }
25
+ }