mn-super-app-cli
v1.0.5
Published
CLI hỗ trợ dự án React Native trong hệ sinh thái MAINAM Super App.
Readme
MAINAM Super App CLI
CLI hỗ trợ dự án React Native trong hệ sinh thái MAINAM Super App.
Tool này đang làm 3 việc chính:
- đăng nhập vào dịch vụ phát hành bundle
- chạy Metro kèm QR để thiết bị mobile lấy bundle dev
- build bundle React Native cho
androidvàios, sau đó có thể publish lên App Center nội bộ
Tài liệu nên đọc
Phạm vi của repo
Repo này chỉ chứa CLI Node.js phát hành với tên lệnh:
mn-super-app-cliCLI được thiết kế để chạy bên trong một project React Native, không phải chạy độc lập ở thư mục bất kỳ.
Yêu cầu môi trường
Trước khi dùng CLI, máy cần có:
- Node.js và npm
- React Native CLI khả dụng từ terminal qua lệnh
react-native - một project React Native có
index.jsvàapp.json - kết nối mạng tới server App Center / Miniapp backend mà bạn cấu hình
Self-host và multi-server
Từ version CLI hiện tại, tool hỗ trợ nhiều server profile và nhiều tài khoản độc lập theo từng server.
Các lệnh chính:
mn-super-app-cli server add local https://mini.local.company
mn-super-app-cli server add prod https://mini.company.com
mn-super-app-cli server list
mn-super-app-cli server use local
mn-super-app-cli server current
mn-super-app-cli whoamiNguyên tắc hoạt động:
- mỗi
servercóbaseUrlriêng - mỗi
servercó token đăng nhập riêng login,publish,qrluôn chạy theoserverđang active- config cũ dùng
https://api-mini.isofh.comsẽ được migrate sang profiledefault
Cài đặt
Cài từ source của repo này
Trong repo hiện tại:
npm install
npm run startLưu ý: script npm run start của repo này không chạy Metro. Nó đóng gói package rồi cài global bản CLI hiện tại lên máy.
Sau khi cài xong, bạn có thể gọi:
mn-super-app-cliCài như package global
Nếu publish package lên registry nội bộ hoặc npm, cách dùng kỳ vọng là:
npm install -g mn-super-app-cliCấu trúc project React Native cần có
CLI đọc thông tin từ app.json của project React Native đang đứng tại đó.
Ví dụ tối thiểu:
{
"name": "MainamSuperAppModule",
"appId": "your-app-id-on-isofh-app-center"
}Ý nghĩa:
name: dùng khi generate QR cho chế độ devappId: dùng khi publish bundle hoặc lấy QR của bản đã phát hành
Nếu thiếu app.json, CLI sẽ dừng và báo lỗi.
Cách dùng
1. Cấu hình server
Thêm server mới:
mn-super-app-cli server add <name> <baseUrl>Ví dụ:
mn-super-app-cli server add local https://mini.local.company
mn-super-app-cli server add prod https://mini.company.comXem danh sách server:
mn-super-app-cli server listChuyển server đang dùng:
mn-super-app-cli server use localXem server hiện tại:
mn-super-app-cli server currentXóa server:
mn-super-app-cli server remove <name>2. Đăng nhập
mn-super-app-cli loginCLI sẽ hỏi:
- username của server hiện tại
- password của server hiện tại
Sau khi đăng nhập thành công, token được lưu bằng configstore trên máy local theo đúng server đang active để tái sử dụng cho các lệnh cần xác thực.
Kiểm tra user hiện tại:
mn-super-app-cli whoami3. Đăng xuất
mn-super-app-cli logoutLệnh này xóa thông tin đăng nhập của server đang active.
4. Chạy Metro + in QR dev
mn-super-app-cli startHoặc custom port:
mn-super-app-cli start 8088Lệnh này sẽ:
- lấy danh sách IP nội bộ của máy và thêm
localhost - đọc
nametừapp.json - in URL bundle cho Android/iOS
- render QR ngay trong terminal
- chạy
react-native start --port <port>
QR dev chứa dữ liệu dạng JSON:
{
"bundleAndroid": "http://<ip>:<port>/index.bundle?platform=android&dev=true&minify=false",
"bundleIOS": "http://<ip>:<port>/index.bundle?platform=ios&dev=true&minify=false",
"bundleName": "<name-from-app.json>"
}5. Build bundle local
Build cả 2 nền tảng:
mn-super-app-cli buildChỉ build Android:
mn-super-app-cli build androidChỉ build iOS:
mn-super-app-cli build iosLệnh build sẽ:
- chạy
react-native bundle - tạo thư mục
build/androidhoặcbuild/ios - sinh file
index.bundlevà assets tương ứng - nén thành
build/android.zipvà/hoặcbuild/ios.zip
Tham số hợp lệ cho build chỉ có:
androidios- bỏ trống để build cả hai
6. Publish bundle
Publish theo appId trong app.json:
mn-super-app-cli publishHiện tại CLI sẽ:
- kiểm tra trạng thái đăng nhập
- dùng
baseUrlcủa server đang active - đọc
appIdtừapp.jsonnếu bạn không truyền đối số - build lại bundle cho cả
androidvàios - upload 2 file zip lên API release
- in QR của bản release ra terminal
Bạn cũng có thể truyền appId trực tiếp:
mn-super-app-cli publish your-app-idQR release chứa dữ liệu dạng JSON:
{
"bundleAndroid": "https://<active-server-base-url>/api/app/<android-id>",
"bundleIOS": "https://<active-server-base-url>/api/app/<ios-id>",
"appIcon": "<logo-url>",
"appName": "<app-name>",
"bundleName": "<module-name>",
"appId": "<app-id>"
}Nếu backend trả sẵn full URL bundle trong response, CLI sẽ dùng trực tiếp URL đó thay vì tự ghép theo pattern /api/app/<id>.
7. Lấy lại QR của bundle đã publish
mn-super-app-cli qrLệnh này yêu cầu:
- đã
login - project hiện tại có
app.json app.jsoncóappId
CLI sẽ gọi API để lấy thông tin bundle mới nhất của app rồi in QR release ra terminal.
Luồng dùng phổ biến
Phát triển local
mn-super-app-cli startMở app mobile, scan QR và tải bundle dev từ máy đang chạy Metro.
Tạo bundle kiểm thử
mn-super-app-cli buildSau khi chạy xong, kiểm tra thư mục:
build/Publish bản mới
mn-super-app-cli server use local
mn-super-app-cli login
mn-super-app-cli publishHành vi theo nền tảng
- Trên macOS/Linux, CLI gọi trực tiếp
react-native - Trên Windows, CLI gọi thông qua file
rn.bat
File rn.bat chỉ làm một việc: chuyển vào thư mục project hiện tại rồi chạy lại lệnh react-native.
Các điểm cần lưu ý
- CLI luôn chạy theo thư mục hiện tại (
process.cwd()), nên cần đứng đúng root của project React Native publishhiện tại build lại cảandroidvàios, không có tùy chọn chỉ publish một nền tảng- nếu đã login trước đó trên server hiện tại,
loginsẽ báo tài khoản hiện tại và không hỏi lại - thông tin đăng nhập được lưu local bằng
configstoretheo từng server - profile
defaultđược tạo ra để tương thích với config cũ dùnghttps://api-mini.isofh.com - output build cũ trong thư mục
build/sẽ bị xóa trước khi build/publish
Phát triển CLI
Cài dependencies
npm installCài bản local vào máy để test nhanh
npm run startScript này sẽ:
npm packnpm install -gfile tgz vừa tạo- xóa file tgz tạm
File chính
cli.js: toàn bộ logic command linern.bat: wrapper cho Windowspackage.json: metadata package và lệnh cài local
Phiên bản hiện tại
Theo package.json, version hiện tại là 1.0.4.
