UNPKG

sepack

Version:

Simple cli tool for android project. Generate android project base on template kotlin mvvm, debug and install project without Android Studio.

250 lines 11.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Command = void 0; const fs_1 = __importDefault(require("fs")); const shelljs_1 = __importDefault(require("shelljs")); const utils_1 = require("./utils"); const ora_1 = __importDefault(require("ora")); class Command { constructor() { this.os = process.platform; this.spinnerBar = ora_1.default(); this.spinnerBar.color = 'white'; if (!shelljs_1.default.which('adb')) { this.isAdbInstalled = false; } else { this.isAdbInstalled = true; } } isSdkValid(path) { const sdkValid = shelljs_1.default.ls(path).toString(); return sdkValid.includes('build-tools'); } buildProject(sdk, isShowLog, isSkipTest) { return __awaiter(this, void 0, void 0, function* () { const sdkFixer = sdk !== null && sdk !== void 0 ? sdk : yield utils_1.sdkPath(); return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { const isAndroidProject = shelljs_1.default.find(["gradlew", "gradlew.bat"]).length > 0; if (isAndroidProject) { const isContainLocalProp = shelljs_1.default.find('local.properties').length > 0; if (!this.isSdkValid(sdkFixer)) { utils_1.errorLine(`Sdk folder invalid, please add flag "--sdk 'your-sdk-folder'"`); resolve(false); } else { if (!isContainLocalProp) { utils_1.outLog('Build', 'Setting up local.properties'); const writeLocalProp = yield this.writeLocalProp(sdkFixer); if (writeLocalProp) { resolve(yield this.buildProject(sdkFixer, isShowLog, isSkipTest)); } } else if (isContainLocalProp) { // start build ! utils_1.outLog('Build', 'Start build project'); if (!isShowLog) { this.spinnerBar.text = 'Building project...'; this.spinnerBar.start(); } var cmd = `${this.gradlew()} build`; if (isSkipTest) { cmd = `${this.gradlew()} build -x test`; } shelljs_1.default.exec(cmd, { silent: !isShowLog }, (code, stdout, stderr) => { if (!isShowLog) { this.spinnerBar.stop(); utils_1.outLog('Build', 'Done'); if (stderr.includes('BUILD FAILED')) { utils_1.errorLine(stderr); } } }); resolve(true); } else { utils_1.errorLine(`local.properties not found, please add flag "--sdk 'your-sdk-folder'"`); resolve(false); } } } else { utils_1.errorLine('This folder is invalid android project!'); resolve(false); } })); }); } writeLocalProp(sdkValue) { return __awaiter(this, void 0, void 0, function* () { return new Promise(resolve => { const isValid = shelljs_1.default.find(sdkValue).length > 0; if (isValid) { const sdkString = `sdk.dir=${utils_1.slash(sdkValue)}`; shelljs_1.default.touch('local.properties'); shelljs_1.default.sed('-i', "", sdkString, 'local.properties'); resolve(true); } else { utils_1.errorLine(`'${sdkValue}' is not valid sdk folder!`); } }); }); } gradlew() { switch (this.os) { case "win32": return "gradlew.bat"; default: return "./gradlew"; } } runApp(resume, isShowLog) { return __awaiter(this, void 0, void 0, function* () { if (this.isAdbInstalled) { return new Promise(resolve => { const sepackConfigJson = 'sepack_config.json'; const isConfigExist = shelljs_1.default.find(sepackConfigJson).length > 0; if (isConfigExist) { const rawData = fs_1.default.readFileSync(sepackConfigJson); const packageName = JSON.parse(rawData.toString()).package_name; if (!isShowLog) { this.spinnerBar.text = 'Building project...'; this.spinnerBar.start(); } if (!resume) { shelljs_1.default.exec(`${this.gradlew()} assembleDebug`, { silent: !isShowLog }, (code, stdout, stderr) => { if (stderr.includes('FAILED')) { this.spinnerBar.stop(); utils_1.errorLine(stderr); } else { this.spinnerBar.text = 'Install application'; shelljs_1.default.exec(`${this.gradlew()} installDebug`, { silent: !isShowLog }, (code, stdout, stderr) => { if (stderr.includes('FAILED')) { this.spinnerBar.stop(); utils_1.errorLine(stderr); resolve(false); } else { this.launchingApp(packageName, isShowLog, resolve); } }); } }); } else { this.launchingApp(packageName, isShowLog, resolve); } } else { utils_1.errorLine(`${sepackConfigJson} not found! Please run 'sepack init' for turn on sepack android project`); } }); } else { utils_1.errorLine("Adb not installed!"); return false; } }); } launchingApp(packageName, silent, resolve) { this.spinnerBar.text = 'Start build project'; shelljs_1.default.exec(`adb shell cmd package resolve-activity --brief -c android.intent.category.LAUNCHER ${packageName}`, { silent: !silent }, (data, stderr, stdout) => { const launcher = stderr.split("\n")[1]; this.spinnerBar.stop(); utils_1.outLog('Run', 'Launcing app'); shelljs_1.default.exec(`adb shell cmd activity start-activity ${launcher}`, { silent: !silent }); resolve(true); }); } log(tag, level) { if (this.isAdbInstalled) { const rawData = fs_1.default.readFileSync('sepack_config.json'); const packageName = JSON.parse(rawData.toString()).package_name; process.on('SIGINT', () => { shelljs_1.default.exec(`adb shell am force-stop ${packageName}`); }); setTimeout(() => { const pid = shelljs_1.default.exec(`adb shell pidof -s ${packageName}`); shelljs_1.default.exec(`adb logcat '${tag}:${level}' -v color --pid=${pid}`); }, 2000); } else { utils_1.errorLine("Adb not installed!"); } } init() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { const pkgName = yield this.searchPkgName(); const appName = yield this.searchAppName(); const json = ` { "project_name": "${appName}", "package_name": "${pkgName}" } `; const file = "sepack_config.json"; shelljs_1.default.touch(file); shelljs_1.default.sed("-i", "", json, file); resolve(true); })); }); } searchPkgName() { return __awaiter(this, void 0, void 0, function* () { return new Promise(resolve => { const appBuildGradle = utils_1.slash('app/build.gradle'); const isAppBuildGradleisExist = shelljs_1.default.find(appBuildGradle).length > 0; if (isAppBuildGradleisExist) { const text = shelljs_1.default.grep('-i', 'applicationId', appBuildGradle).stdout .replace('\n', '') .replace('"', '') .replace(`"`, '') .replace('applicationId', '') .trim(); resolve(text); } else { utils_1.errorLine(utils_1.slash('app/app.gradle not found')); } }); }); } searchAppName() { return __awaiter(this, void 0, void 0, function* () { return new Promise(resolve => { const appSettingGradle = 'settings.gradle'; const isAppSettingGradleisExist = shelljs_1.default.find(appSettingGradle).length > 0; if (isAppSettingGradleisExist) { const text = shelljs_1.default.grep('-i', 'rootProject.name', appSettingGradle).stdout .replace('\n', '') .replace('"', '') .replace(`"`, '') .replace('=', '') .replace('rootProject.name', '') .trim(); resolve(text); } else { utils_1.errorLine(utils_1.slash('settings.gradle not found')); } }); }); } } exports.Command = Command; //# sourceMappingURL=command.js.map