app.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * 更新发布工具
  3. *
  4. * Copyright © 2025 imengyu.top imengyu-update-server
  5. */
  6. import { program } from 'commander';
  7. import { input, confirm, select } from '@inquirer/prompts';
  8. import { postAppUpdate, postWebUpdate } from './postUpdate.mjs';
  9. import Table from 'cli-table3';
  10. import { config } from './postConfig.mjs';
  11. import {
  12. axiosInstance,
  13. initAuth,
  14. login,
  15. logout,
  16. checkLogged,
  17. } from './auth.mjs';
  18. import consola from 'consola';
  19. import { logError } from './utils.mjs';
  20. // 版本相关
  21. //========================================
  22. export async function viewVersion(type) {
  23. if (type === 'all' || !type) {
  24. const data = await axiosInstance.get('/version/list');
  25. const table = new Table({
  26. head: ['ID', '版本'],
  27. colWidths: [10, 20 ]
  28. });
  29. data.data.forEach((d) => {
  30. table.push([ d.id, d.version ]);
  31. })
  32. console.log(table.toString());
  33. } else {
  34. let data = null;
  35. try {
  36. if (Number.isNaN(Number(type)))
  37. data = await axiosInstance.get('/version/get-by-name?name=' + type);
  38. else
  39. data = await axiosInstance.get('/version/' + type);
  40. }
  41. catch (e) {
  42. logError('Failed to load version info', e);
  43. return;
  44. }
  45. const table = new Table({
  46. head: ['key', 'data'],
  47. colWidths: [30, 60]
  48. });
  49. table.push(
  50. [ 'ID', data.data.id ],
  51. [ '状态', stateConstant[data.data.status] ],
  52. [ '版本号', data.data.version ],
  53. [ '创建时间', new Date(data.data.createAt).toString() ],
  54. [ '设置', data.data.config ],
  55. [ '激活的Web更新ID', data.data.webUpdateId ],
  56. [ '激活的App更新ID', data.data.appUpdateId ],
  57. [ '激活的下一个App更新ID', data.data.appUpdateNextId ],
  58. );
  59. console.log(table.toString());
  60. }
  61. }
  62. export async function getVersion(action, type) {
  63. switch(action) {
  64. case 'view':
  65. await viewVersion(type);
  66. break;
  67. case 'new': {
  68. const version = await input({ message: 'Enter version name, like (1.0.0)' });
  69. try {
  70. await axiosInstance.post('/version', {
  71. version: version,
  72. status: 1,
  73. config: "{}",
  74. });
  75. consola.success('Add version success');
  76. } catch (e) {
  77. logError('Failed to add version', e);
  78. }
  79. break;
  80. }
  81. case 'delete': {
  82. const versionId = type ? type : await input({ message: '输入版本ID' });
  83. if (!await confirm({ message: `确定删除版本 ${versionId}?`, default: false }))
  84. return;
  85. if (!await confirm({ message: '确认删除版本?此操作会删除所属版本的所有更新项目、存储等,无法恢复,是否确定删除?', default: false }))
  86. return;
  87. try {
  88. await axiosInstance.delete('/version/' + versionId);
  89. consola.success('删除版本成功');
  90. } catch (e) {
  91. logError('删除版本失败', e);
  92. }
  93. break;
  94. }
  95. case 'set-state': {
  96. const versionId = type ? type : await input({ message: '输入版本ID' });
  97. const state = await select({
  98. message: '设置状态',
  99. choices: [
  100. {
  101. name: 'NotEnable',
  102. value: 0,
  103. },
  104. {
  105. name: 'Normal',
  106. value: 1,
  107. },
  108. {
  109. name: 'Deprecated',
  110. value: 2,
  111. },
  112. ],
  113. });
  114. try {
  115. await axiosInstance.put('/version/' + versionId, {
  116. status: state
  117. });
  118. consola.success('设置状态成功');
  119. } catch (e) {
  120. logError('设置状态失败', e);
  121. }
  122. break;
  123. }
  124. case 'set-config': {
  125. const versionId = type ? type : await input({ message: '输入版本ID' });
  126. const config = await input({ message: '输入配置Json' });
  127. try {
  128. await axiosInstance.put('/version/' + versionId, {
  129. config: config,
  130. });
  131. consola.success('设置配置成功');
  132. } catch (e) {
  133. logError('设置配置失败', e);
  134. }
  135. break;
  136. }
  137. case 'set-active-app-update': {
  138. const versionId = await input({ message: '输入版本ID' });
  139. const updateId = await input({ message: '输入更新ID' });
  140. const isNext = await confirm({ message: 'Set as next active?', default: false });
  141. try {
  142. await axiosInstance.post('/update/active/app', { versionId, updateId, isNext });
  143. consola.success('成功');
  144. } catch (e) {
  145. logError('失败', e);
  146. }
  147. break;
  148. }
  149. case 'set-active-web-update': {
  150. const versionId = await input({ message: '输入版本ID' });
  151. const updateId = await input({ message: '输入更新ID' });
  152. try {
  153. await axiosInstance.post('/update/active/web', { versionId, updateId });
  154. consola.success('成功');
  155. } catch (e) {
  156. logError('失败', e);
  157. }
  158. break;
  159. }
  160. default:
  161. logError('未知参数', action);
  162. break;
  163. }
  164. }
  165. //选择方法
  166. //========================================
  167. export async function selectVersion(defaultVersionId = null) {
  168. const data = (await axiosInstance.get('/version/list?full=true&search=' + JSON.stringify({
  169. appId: config.appId
  170. }))).data;
  171. if (data.length === 0) {
  172. logError('没有版本,请在管理后台创建 appId ' + config.appId + ' 的版本');
  173. return;
  174. }
  175. const versionId = (await select({
  176. choices: data.map(p => ({
  177. value: p.id,
  178. name: p.version,
  179. })),
  180. default: defaultVersionId,
  181. message: '选择一个版本',
  182. }));
  183. const versionName = data.find(p => p.id === versionId).version;
  184. return { versionId, versionName };
  185. }
  186. export async function selectUpdate() {
  187. const { versionId } = await selectVersion();
  188. const data = (await axiosInstance.get('/version/update?search=' + JSON.stringify({ versionId }))).data;
  189. if (data.length === 0) {
  190. logError('没有更新');
  191. return;
  192. }
  193. const resultId = (await select({
  194. choices: data.map(p => ({
  195. value: p.id,
  196. name: p.version,
  197. })),
  198. message: '选择一个更新',
  199. }));
  200. return resultId;
  201. }
  202. //更新相关
  203. //========================================
  204. const stateConstant = [ 'Deleted', 'Normal', 'Deprecated' ];
  205. const typeConstant = [ 'Unknow', 'Web', 'app' ];
  206. const storageTypeConstant = [ 'Unknow', 'LocalStorage', 'AliOSS' ];
  207. export async function postUpdate(type, options) {
  208. switch (type) {
  209. case 'web': {
  210. await postWebUpdate(axiosInstance, options);
  211. break;
  212. }
  213. case 'app': {
  214. await postAppUpdate(axiosInstance, options);
  215. break;
  216. }
  217. default:
  218. logError('Unknow type', type);
  219. break;
  220. }
  221. }
  222. export async function deprecateOrDeleteUpdate(updateId) {
  223. if (!updateId)
  224. updateId = await selectUpdate();
  225. const { currentUpdateInfo, currentVersionInfo } = await viewUpdate(updateId);
  226. const deprecate = (await select({
  227. choices: [
  228. {
  229. name: 'Deprecate',
  230. value: 0,
  231. },
  232. {
  233. name: 'Delete',
  234. value: 1,
  235. },
  236. ],
  237. message: '删除或弃用?',
  238. })) === 0;
  239. if (deprecate && currentUpdateInfo.type !== 1) {
  240. consola.warn('只有Web更新允许弃用');
  241. return;
  242. }
  243. if (currentUpdateInfo.status === 0) {
  244. consola.warn(`当前状态 ${stateConstant[currentUpdateInfo.status]} 无法弃用`);
  245. return;
  246. }
  247. if (deprecate) {
  248. if (!await confirm({ message: `确定弃用当前版本 ${currentUpdateInfo.versionCode} ?弃用会删除存储文件以及备份。此操作无法恢复!`, default: false }))
  249. return;
  250. } else {
  251. if (!await confirm({ message: `确定删除当前版本 ${currentUpdateInfo.versionCode} ?此操作无法恢复!`, default: false }))
  252. return;
  253. }
  254. try {
  255. await axiosInstance.post(`/update/${deprecate ? 'deprecate' : 'delete'}`, { updateId });
  256. consola.success(`${deprecate ? '弃用' : '删除'} 成功`);
  257. } catch (e) {
  258. logError(`操作失败`, e);
  259. }
  260. }
  261. export async function viewUpdate(updateId) {
  262. let currentUpdateInfo = null
  263. let currentVersionInfo = null
  264. try {
  265. currentUpdateInfo = (await axiosInstance.get('/update/' + updateId)).data;
  266. } catch (e) {
  267. logError('加载更新信息失败', updateId);
  268. }
  269. try {
  270. currentVersionInfo = (await axiosInstance.get('/version/' + currentUpdateInfo.versionId)).data;
  271. } catch (e) {
  272. logError('加载版本信息失败', currentUpdateInfo.versionId);
  273. }
  274. const table = new Table({
  275. head: ['key', 'data'],
  276. colWidths: [20, 40]
  277. });
  278. table.push(
  279. [ 'ID', currentUpdateInfo.id ],
  280. [ '所属应用', currentUpdateInfo.name ],
  281. [ '更新信息', currentUpdateInfo.updateInfo ],
  282. [ '版本号', currentUpdateInfo.versionCode ],
  283. [ '创建时间', new Date(currentUpdateInfo.createAt).toString() ],
  284. [ '类型', typeConstant[currentUpdateInfo.type] ],
  285. [ '状态', stateConstant[currentUpdateInfo.status] ],
  286. [ '强制更新', currentUpdateInfo.force ],
  287. [ '公共访问路径', currentUpdateInfo.publicUrl ],
  288. [ '存储类型', storageTypeConstant[currentUpdateInfo.storageType] ],
  289. [ '存储路径', currentUpdateInfo.storagePath ],
  290. );
  291. console.log(table.toString());
  292. if (currentUpdateInfo.activeWebVersionName) {
  293. table.push(
  294. [ '使用中的Web版本', currentUpdateInfo.activeWebVersionName ],
  295. );
  296. }
  297. if (currentUpdateInfo.activeAppVersionName) {
  298. table.push(
  299. [ '使用中的App版本', currentUpdateInfo.activeAppVersionName ],
  300. );
  301. }
  302. return {
  303. currentUpdateInfo,
  304. currentVersionInfo,
  305. }
  306. }
  307. export async function getUpdate(action, type, all, options) {
  308. const typeNotANumber = isNaN(new Number(type));
  309. if (action === 'view' && (!type || typeNotANumber)) {
  310. let hasSerch = false;
  311. const search = {};
  312. const sort = {
  313. field: "createAt",
  314. order: "descend"
  315. }
  316. if (typeNotANumber && type !== 'all') {
  317. hasSerch = true;
  318. search.version = type;
  319. }
  320. const data = await axiosInstance.get('/update/list?full=true' + (hasSerch ? ('&search=' + JSON.stringify(search)) : '') + '&sort=' + JSON.stringify(sort));
  321. const table = new Table({
  322. head: ['ID', '版本', '版本号', '类型', '状态'],
  323. colWidths: [10, 10, 20, 10, 15 ]
  324. });
  325. if (type !== 'all' && all !== 'all' && data.data.length > 10) {
  326. data.data = data.data.slice(0, 10);
  327. consola.info('filter!', all);
  328. }
  329. data.data.forEach((d) => {
  330. table.push([
  331. d.id,
  332. (d.activeAppVersionName ? `${d.activeAppVersionName} (App)` : (
  333. d.activeWebVersionName? `${d.activeWebVersionName} (Web)` : '无'
  334. )),
  335. d.versionCode, typeConstant[d.type], stateConstant[d.status]
  336. ]);
  337. })
  338. console.log(table.toString());
  339. } else {
  340. switch (action) {
  341. case 'post': {
  342. await postUpdate(type, options);
  343. break;
  344. }
  345. case 'delete': {
  346. await deprecateOrDeleteUpdate(type);
  347. break;
  348. }
  349. case 'view': {
  350. viewUpdate(type);
  351. break;
  352. }
  353. default:
  354. logError('Unknow action', action);
  355. break;
  356. }
  357. }
  358. }
  359. export async function testVersion(version) {
  360. try {
  361. const data = await axiosInstance.get('/update-get-info?version=' + version);
  362. consola.info('版本信息', data.data);
  363. } catch(e) {
  364. logError('获取失败', e);
  365. }
  366. }