Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CLI dev dependency #75

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ build/Release
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

# Build specific exclusions
*/bin/

# Environment variables
*.env

Expand Down
178 changes: 178 additions & 0 deletions cli/bin/script/acquisition-sdk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.AcquisitionManager = exports.AcquisitionStatus = void 0;
class AcquisitionStatus {
static DeploymentSucceeded = "DeploymentSucceeded";
static DeploymentFailed = "DeploymentFailed";
}
exports.AcquisitionStatus = AcquisitionStatus;
class AcquisitionManager {
_appVersion;
_clientUniqueId;
_deploymentKey;
_httpRequester;
_ignoreAppVersion;
_serverUrl;
constructor(httpRequester, configuration) {
this._httpRequester = httpRequester;
this._serverUrl = configuration.serverUrl;
if (this._serverUrl.slice(-1) !== "/") {
this._serverUrl += "/";
}
this._appVersion = configuration.appVersion;
this._clientUniqueId = configuration.clientUniqueId;
this._deploymentKey = configuration.deploymentKey;
this._ignoreAppVersion = configuration.ignoreAppVersion;
}
queryUpdateWithCurrentPackage(currentPackage, callback) {
if (!currentPackage || !currentPackage.appVersion) {
throw new Error("Calling common acquisition SDK with incorrect package"); // Unexpected; indicates error in our implementation
}
const updateRequest = {
deploymentKey: this._deploymentKey,
appVersion: currentPackage.appVersion,
packageHash: currentPackage.packageHash,
isCompanion: this._ignoreAppVersion,
label: currentPackage.label,
clientUniqueId: this._clientUniqueId,
};
const requestUrl = this._serverUrl + "updateCheck?" + queryStringify(updateRequest);
this._httpRequester.request(0 /* Http.Verb.GET */, requestUrl, (error, response) => {
if (error) {
callback(error, /*remotePackage=*/ null);
return;
}
if (response.statusCode !== 200) {
callback(new Error(response.statusCode + ": " + response.body), /*remotePackage=*/ null);
return;
}
let updateInfo;
try {
const responseObject = JSON.parse(response.body);
updateInfo = responseObject.updateInfo;
}
catch (error) {
callback(error, /*remotePackage=*/ null);
return;
}
if (!updateInfo) {
callback(error, /*remotePackage=*/ null);
return;
}
else if (updateInfo.updateAppVersion) {
callback(/*error=*/ null, {
updateAppVersion: true,
appVersion: updateInfo.appVersion,
});
return;
}
else if (!updateInfo.isAvailable) {
callback(/*error=*/ null, /*remotePackage=*/ null);
return;
}
const remotePackage = {
deploymentKey: this._deploymentKey,
description: updateInfo.description,
label: updateInfo.label,
appVersion: updateInfo.appVersion,
isMandatory: updateInfo.isMandatory,
packageHash: updateInfo.packageHash,
packageSize: updateInfo.packageSize,
downloadUrl: updateInfo.downloadURL,
};
callback(/*error=*/ null, remotePackage);
});
}
reportStatusDeploy(deployedPackage, status, previousLabelOrAppVersion, previousDeploymentKey, callback) {
const url = this._serverUrl + "reportStatus/deploy";
const body = {
appVersion: this._appVersion,
deploymentKey: this._deploymentKey,
};
if (this._clientUniqueId) {
body.clientUniqueId = this._clientUniqueId;
}
if (deployedPackage) {
body.label = deployedPackage.label;
body.appVersion = deployedPackage.appVersion;
switch (status) {
case AcquisitionStatus.DeploymentSucceeded:
case AcquisitionStatus.DeploymentFailed:
body.status = status;
break;
default:
if (callback) {
if (!status) {
callback(new Error("Missing status argument."), /*not used*/ null);
}
else {
callback(new Error('Unrecognized status "' + status + '".'), /*not used*/ null);
}
}
return;
}
}
if (previousLabelOrAppVersion) {
body.previousLabelOrAppVersion = previousLabelOrAppVersion;
}
if (previousDeploymentKey) {
body.previousDeploymentKey = previousDeploymentKey;
}
callback = typeof arguments[arguments.length - 1] === "function" && arguments[arguments.length - 1];
this._httpRequester.request(2 /* Http.Verb.POST */, url, JSON.stringify(body), (error, response) => {
if (callback) {
if (error) {
callback(error, /*not used*/ null);
return;
}
if (response.statusCode !== 200) {
callback(new Error(response.statusCode + ": " + response.body), /*not used*/ null);
return;
}
callback(/*error*/ null, /*not used*/ null);
}
});
}
reportStatusDownload(downloadedPackage, callback) {
const url = this._serverUrl + "reportStatus/download";
const body = {
clientUniqueId: this._clientUniqueId,
deploymentKey: this._deploymentKey,
label: downloadedPackage.label,
};
this._httpRequester.request(2 /* Http.Verb.POST */, url, JSON.stringify(body), (error, response) => {
if (callback) {
if (error) {
callback(error, /*not used*/ null);
return;
}
if (response.statusCode !== 200) {
callback(new Error(response.statusCode + ": " + response.body), /*not used*/ null);
return;
}
callback(/*error*/ null, /*not used*/ null);
}
});
}
}
exports.AcquisitionManager = AcquisitionManager;
function queryStringify(object) {
let queryString = "";
let isFirst = true;
for (const property in object) {
if (object.hasOwnProperty(property)) {
const value = object[property];
if (!isFirst) {
queryString += "&";
}
queryString += encodeURIComponent(property) + "=";
if (value !== null && typeof value !== "undefined") {
queryString += encodeURIComponent(value);
}
isFirst = false;
}
}
return queryString;
}
23 changes: 23 additions & 0 deletions cli/bin/script/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const parser = require("./command-parser");
const execute = require("./command-executor");
const chalk = require("chalk");
function run() {
const command = parser.createCommand();
if (!command) {
parser.showHelp(/*showRootDescription*/ false);
return;
}
execute
.execute(command)
.catch((error) => {
console.error(chalk.red(`[Error] ${error.message}`));
process.exit(1);
})
.done();
}
run();
Loading