|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +"use strict"; |
| 4 | + |
| 5 | +var jsforce = require("jsforce"); |
| 6 | + |
| 7 | +if (!process.env.GIT_SHA) { |
| 8 | + console.error( |
| 9 | + "GIT_SHA is a required environment variable so the build is named" |
| 10 | + ); |
| 11 | + process.exit(1); |
| 12 | +} |
| 13 | + |
| 14 | +console.debug = console.log; |
| 15 | + |
| 16 | +let conn = new jsforce.Connection({ |
| 17 | + loginUrl: process.env.SALESFORCE_URL || "https://login.salesforce.com", |
| 18 | +}); |
| 19 | + |
| 20 | +let PackageUploadRequest = conn.tooling.sobject("PackageUploadRequest"); |
| 21 | +let MetadataPackage = conn.tooling.sobject("MetadataPackage"); |
| 22 | +let MetadataPackageVersion = conn.tooling.sobject("MetadataPackageVersion"); |
| 23 | + |
| 24 | +conn |
| 25 | + .login(process.env.SALESFORCE_USERNAME, process.env.SALESFORCE_PASSWORD) |
| 26 | + .then((userInfo) => { |
| 27 | + return MetadataPackage.find() |
| 28 | + .then((res) => { |
| 29 | + if (res.length != 1) { |
| 30 | + console.error("org should only have one package"); |
| 31 | + process.exit(1); |
| 32 | + } else { |
| 33 | + return res[0].Id; |
| 34 | + } |
| 35 | + }) |
| 36 | + .then((id) => { |
| 37 | + return PackageUploadRequest.create({ |
| 38 | + MetadataPackageId: id, |
| 39 | + IsReleaseVersion: false, // only automatically create beta versions so we don't bork it permanently |
| 40 | + VersionName: process.env.GIT_SHA || "unknown git sha", |
| 41 | + }); |
| 42 | + // return {id: '0HDf4000000CaTNGA0'} // debug |
| 43 | + }) |
| 44 | + .then((res) => { |
| 45 | + console.log("***** package submitted *****"); |
| 46 | + console.log(res); |
| 47 | + return waitUntilUploaded(res.id); |
| 48 | + }) |
| 49 | + .then((req) => { |
| 50 | + console.log("***** package upload request full details ****"); |
| 51 | + console.log(req); |
| 52 | + if (req.Status != "SUCCESS") { |
| 53 | + throw new Error( |
| 54 | + "PACKAGE UPLOAD ERROR: " + JSON.stringify(req.Errors) |
| 55 | + ); |
| 56 | + } |
| 57 | + return MetadataPackageVersion.find({ |
| 58 | + Id: req.MetadataPackageVersionId, |
| 59 | + }); |
| 60 | + }) |
| 61 | + .then((res) => { |
| 62 | + console.log("***** package uploaded *****"); |
| 63 | + console.log(res); |
| 64 | + console.log( |
| 65 | + "INSTALL URL: /packaging/installPackage.apexp?p0=" + res[0].Id |
| 66 | + ); |
| 67 | + // TODO put the url somewhere so people can test it? |
| 68 | + }); |
| 69 | + }) |
| 70 | + .catch((err) => { |
| 71 | + console.error(err); |
| 72 | + process.exit(1); |
| 73 | + }); |
| 74 | + |
| 75 | +function wait(msToWait) { |
| 76 | + return new Promise((resolve) => setTimeout(resolve, msToWait)); |
| 77 | +} |
| 78 | + |
| 79 | +function waitUntilUploaded(id) { |
| 80 | + // check every second until done |
| 81 | + console.log("Checking in 1 second for " + id); |
| 82 | + return wait(1000) |
| 83 | + .then((_) => { |
| 84 | + return PackageUploadRequest.retrieve(id); |
| 85 | + }) |
| 86 | + .then((res) => { |
| 87 | + // console.log(res) |
| 88 | + console.log("Status:", res.Status); |
| 89 | + if (res.Status == undefined) { |
| 90 | + throw new Error("Status not found on " + res); |
| 91 | + } else if (res.Status == "IN_PROGRESS") { |
| 92 | + console.log("Checking again..."); |
| 93 | + return waitUntilUploaded(id); |
| 94 | + } else { |
| 95 | + return res; |
| 96 | + } |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +function describeObject(objectName) { |
| 101 | + return conn.tooling |
| 102 | + .sobject(objectName) |
| 103 | + .describe() |
| 104 | + .then((meta) => { |
| 105 | + console.log(meta); |
| 106 | + meta.fields.map((f) => console.log(f.name)); |
| 107 | + }); |
| 108 | +} |
0 commit comments