smartuxbuild

Publish and download the applications from smartUX studio

Usage no npm install needed!

<script type="module">
  import smartuxbuild from 'https://cdn.skypack.dev/smartuxbuild';
</script>

README

Overview

This npm package contains all the method for SmartUX application build process from SmartUX Studio.

Requirements

NodeJS

Installation

 $ npm install smartuxbuild

Usage

input parameters:

Parameter Description Example value Data type
protocol protocol of the smartUX studio environment 'http' or 'https' String
hostURL URL of the SmartUX Studio (do not include http:// or https://) studio.smartux.com String
username username to log in to SmartUX Studio admin String
encryptedPassword encrypted password Click here for details String
appsForBuild [optional] List of specific apps for build separated by ':' app1:app2:app3 String
port [optional] port at which the SmartUX studio is running 3000 or default port is 80 String

import the required npm modules in your file

  const smartuxbuild = require(‘smartuxbuild’);
  const fs = require('fs');
  const path = require('path');

example usage:

    // Set the enviroment variables
    const protocol = "<http or https>";
    const hostURL = "<YOUR STUDIO URL>";
    const username = "<STUDIO USERNAME>";
    const encryptedPassword = "<ENCRYPTED PASSWORD>";
    const port = "<port number at which the SmartUX studio is running>";
    // Calling the buildAllAps - this method calls all the methods to make API calls to SmartUX Studio eg. login method to call login API etc.
    martuxbuild.buildAllApps(protocol,hostURL,username,encryptedPassword,appsForBuild).catch((e) => {
        console.log(e);
        process.exit(1);
        });

For advanced users buildAllApps in detail

async function buildAllApps(protocol,hostURL,username,encryptedPassword,appsForBuild) {
    var result = {};
    var lastVersion;
    var appsInStudio = [];
    var appsForBuildArray= [];
    var smartUXPublishedApps = "<Your directory to save the application zip file>";
    if (fs.existsSync(smartUXPublishedApps)) {
        for (const file of fs.readdirSync(smartUXPublishedApps)) {
          fs.unlinkSync(path.join(smartUXPublishedApps, file));
        }
      } else {
        fs.mkdirSync(smartUXPublishedApps);
      }
    if(appsForBuild){
        appsForBuildArray = appsForBuild.split(':');
    }
    else {
        console.log("List of applications for build is not defined. All apps will be published.")
    }
    console.log("Trying to login to the SmartUX studio environment!");
    authObject = await smartuxbuild.login(protocol,hostURL,username,encryptedPassword);
    console.log("Login Successful!" + JSON.stringify(authObject));
    console.log("Getting the list of applications from SmartUX studio!");
    result = await smartuxbuild.getApps(authObject);
    if (result && result.error) {
        throw "Get apps failed with error: " + result.error.message;
    }
    console.log("List of application from SmartUX studio : " + JSON.stringify(result, null, 2));
    appsInStudio = JSON.stringify(result, null, 2);
    appsInStudio = JSON.parse(appsInStudio);
    var buildAllApps = !appsForBuildArray || appsForBuildArray.length===0;
    for (var i = 0; i < appsInStudio.result.length; i++) {
        var appId = appsInStudio.result[i].id;
        var fileName = appId + "_" + smartuxbuild.fullDate + ".zip";
        var appInBuildList = appsForBuildArray.length!==0 && appsForBuildArray.indexOf(appId) > -1;
        if (buildAllApps || appInBuildList) {
            console.log(appId + " Found in the list of applications required to be build");
            console.log("Opening application with app id : " + appId);
            result = await smartuxbuild.open(authObject, appId);
            if (result && result.error) {
                throw "Open apps failed with error: " + result.error.message;
            }
            console.log(appId + " opened in SmartUX Studio successfully!")
            result = await smartuxbuild.getPublishInfo(authObject, appId);
            if (result && result.error) {
                console.log("Publish Info failed for " + appId);
                throw "Error: "+result.error.message;
            }
            lastVersion = result.result.version;
            if(!lastVersion || lastVersion===""){
                lastVersion ="1.0.0";
            }


            console.log("Initiating Git pull for application Id: " + appId+", version: "+lastVersion);
            result = await smartuxbuild.pull(authObject, appId);
            if (result && result.error) {
                throw "Git pull for " + appId + " failed with error: " + result.error.message;
            }
            console.log("Completed Git pull for application Id: " +  appId+", version: "+lastVersion);
            console.log("Initiating publish for application Id: " +  appId+", version: "+lastVersion);
            result = await smartuxbuild.publish(authObject, appId, fileName,lastVersion);
            if (result && result.error) {
                console.log("Publish failed for " + appId);
                throw "Error: "+result.error.message;
            }
            console.log("Completed publish for application Id: " + appId+", version: "+lastVersion);
            console.log("Starting download for application Id: " + appId+", version: "+lastVersion);
            result = await smartuxbuild.download(authObject, appId, fileName, smartUXPublishedApps);
            if (result && result.error) {
                throw "Download failed for " + appId + " with error: " + result.error.message;;
            }
            console.log("Completed download  for application Id: " + appId);
            console.log("Application: " + appId + " successfully saved as " + fileName);
        }
        else {
            console.log(appId + " Application id not found in the list apps for build. This app will not be built!");
            console.log("Exiting!")
        }
    }
}

smartuxbuild npm module contains following methods

Login:

This method makes a POST call to SmartUX Studio to authenticate and get an access token for the subsequent methods for the build process.

input parameters:

Parameter Description Example value Data type
protocol protocol of the smartUX studio environment 'http' or 'https' String
hostURL URL of the SmartUX Studio (do not include http:// or https://) studio.smartux.com String
username username to log in to SmartUX Studio admin String
encryptedPassword encrypted password Click here for details String
appsForBuild [optional] List of specific apps for build separated by ':' app1:app2:app3 String
port [optional] port at which the SmartUX studio is running 3000 or default port is 80 String

example usage:

  authObject = smartuxbuild.login(protocol, hostURL, username, password);

sample output:

{
"protocol":"http",
"hostURL":"ec2-18-236-68-81.us-west-2.compute.amazonaws.com",
"accessToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImFkbWluIiwiaWF0IjoxNTk1ODg3NTQ1LCJleHAiOjE1OTY0OTIzNDV9.azqn9Z1SNxoIyLL1EkefaJOcj7oePV_Lv8cZp8ueA3w"
}

getApps:

This method makes a POST call to SmartUX Studio to get the list of all applications available in SmartUX Studio.

input parameters:

Parameter Description Example value Data type
authObject result object from login method check sample authObject below JSON object

sample authObject:

authObject = {
    "protocol": protocol,
    "hostURL": hostURL,
    "accessToken": accessToken
              };

example usage:

smartuxbuild.getApps(authObject);

sample output:

{"jsonrpc": "2.0",
"result": [
 {"name": "testApp",
"defaultLayout": "PhonePortrait",
"previewToken": "6637a22e",
"theme": "Anchor",
"clientVersion": "6.1.0",
"hidden": false,
"isBuiltInTemplate": false,
"disableSideMenu": false,
"description": "",
"creatorId": "testuser",
"created": 1588151947117,
"screensCount": 11,
"viewOnly": false,
"branch": "master",
"isRemote": true,
"creatorName": "testuser ",
"explorations": [],
"useMockData": false,
"iconUrl": "/previews/home.png",
"id": "backtowork",
"updated": 1595881366524}
]}

open

This method makes a POST call to the SmartUX Studio to open each application in SmartUX Studio.

input parameters:

Parameter Description Example value Data type
authObject result object from login method sample authObject above JSON object
appId id from the app list from getApps method App1 string

example usage:

smartuxbuild.open(authObject, appId);

getPublishInfo:

This method makes a POST call to the SmartUX Studio and returns the last version of the given appId.

input parameters:

Parameter Description Example value Data type
authObject result object from login method sample authObject above JSON object
appId id from the app list from getApps method App1 string

example usage:

smartuxbuild.getPublishInfo(authObject, appId);
```	 
### sample output: 
```javascript 
{
"jsonrpc": "2.0",
"result": {
"smartuxApp": "1.0",
"version": "1.0.1",
"date": 1595887546977,
"creator": "Admin",
"clientVersion": "6.1.0",
"id": " App1",
"name": "App1",
"serverVersion": "6.1.0",
"serverModulesIncluded": true,
"buildType": "AOT",
"cbUrl": "/cb",
"packageName": "App1_2020-07-27-17:5:12.zip" },
"id": ""
}
```	 

## pull:
<p> This method makes a POST call to the SmartUX Studio and performs the git pull operation for the given application id. </p>

### input parameters:

| Parameter  |  Description                              | Example value | Data type                    
|------------|-------------------------------------------|---------------|-----------|
| authObject   |  result object from login method	 | sample authObject above | JSON object |
| appId        | id from the app list from getApps method | App1       | string      |


### example usage: 
```javascript 
smartuxbuild.pull(authObject, appId);
```	
### sample output: 

```javascript 
{
"jsonrpc": "2.0",
"result": {
  "success": true},
"id": ""
}
```	  

## publish:
<p> This method makes a POST call to the SmartUX Studio and executes publish for the given appId. </p>

### input parameters:

| Parameter  |  Description                              | Example value | Data type                    
|------------|-------------------------------------------|---------------|-----------|
| authObject   |  result object from login method	 | sample authObject above | JSON object |
| appId        | id from the app list from getApps method | App1       | string      |
| fileName| Package name from output of getPublishhInfo method |App1_2020-07-27-17:5:12.zip |string |
|lasrVersion |App version from output of getPublishhInfo method |	1.0.1	| string |

### example usage: 
```javascript 
smartuxbuild.publish(authObject, appId, fileName, lastVersion);
```	
### sample output: 
```javascript 
{
"jsonrpc": "2.0",
"result": {
"packageName": "jenkinstestapp_2020-07-28-8:33:55.zip" },
"id": ""
}
```	

## download:

<p> This method makes a GET call to the SmartUX Studio and downloads the application zip file in the specified directory. </p>

### input parameters:

| Parameter  |  Description                              | Example value | Data type                    
|------------|-------------------------------------------|---------------|-----------|
| authObject   |  result object from login method | sample authObject above | JSON object |
| appId        | id from the app list from getApps method | App1       | string      |
| fileName| Package name from output of getPublishhInfo method |App1_2020-07-27-17:5:12.zip |string |
|smartUXPublishedApps |Directory path where app zip will be downloaded  |	‘./smartUXPublishedApps’	| string |

### example usage: 
```javascript 
smartuxbuild.download(authObject, appId, fileName, smartUXPublishedApps);
```