playpush
Version:
## Overview
91 lines (90 loc) • 3.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.upload = upload;
const googleapis_1 = require("googleapis");
const fs_1 = __importDefault(require("fs"));
const SCOPES = ["https://www.googleapis.com/auth/androidpublisher"];
async function upload(program, keyFile, packageName, bundle, track, changesNotSentForReview, versionName, releaseNotes) {
const auth = authenticate(keyFile, SCOPES);
console.log("Authenticated");
googleapis_1.google.options({ auth });
const play = createAdroidPublisher(googleapis_1.google, auth, packageName);
const editId = await createEdit(program, play, packageName);
console.log("Edit created:", editId);
const upload = await uploadBundle(play, editId, packageName, bundle);
console.log("Bundle uploaded");
if (!upload.versionCode) {
program.error("Invalid upload version code");
return;
}
await setUploadTrack(play, editId, packageName, track, versionName, releaseNotes, upload.versionCode);
console.log("Tack updated");
await commitUpload(play, editId, packageName, changesNotSentForReview);
console.log("Edit commited");
}
function authenticate(keyFile, scopes) {
const auth = new googleapis_1.google.auth.GoogleAuth({
keyFile,
scopes,
});
return auth;
}
function createAdroidPublisher(google, auth, packageName) {
var play = google.androidpublisher({
version: "v3",
auth: auth,
params: {
packageName,
},
});
return play;
}
async function createEdit(program, play, packageName) {
const insert = await play.edits.insert({
requestBody: {},
packageName,
});
if (!insert.data.id) {
program.error("Unable to create edit");
}
return insert.data.id;
}
async function uploadBundle(play, editId, packageName, bundle) {
const upload = await play.edits.bundles.upload({
editId,
packageName,
media: {
mimeType: "application/octet-stream",
body: fs_1.default.createReadStream(bundle),
},
});
return upload.data;
}
async function setUploadTrack(play, editId, packageName, track, versionName, releaseNotes, versionCode) {
await play.edits.tracks.update({
editId,
track,
packageName,
requestBody: {
releases: [
{
name: versionName,
releaseNotes: [{ language: "en-US", text: releaseNotes }],
versionCodes: versionCode ? [versionCode.toString()] : undefined,
status: "completed",
},
],
},
});
}
async function commitUpload(play, editId, packageName, changesNotSentForReview) {
const commit = await play.edits.commit({
editId,
packageName,
changesNotSentForReview,
});
return commit.data;
}