dynamicsmobile
Version:
Allows development of off-line mobile and web business apps over the Dynamics Mobile platform. More info on https://www.dynamicsmobile.com
157 lines (135 loc) • 5.44 kB
JavaScript
/***
Dynamics Mobile
www.dynamicsmobile.com
2025 All rights reserved
*/
const fetch = require('node-fetch');
const fs = require('fs');
const os = require('os');
const path = require('path');
const chalk = require('chalk');
const { fakeServer } = require('sinon');
async function uploadModel() {
var createDb = false;
var commandLineArgs = process.argv;
if (commandLineArgs && commandLineArgs.length > 0 && commandLineArgs.indexOf("resetdb") > 0) {
createDb = true;
}
var baseAPIUrl = "https://api.portal.dynamicsmobile.com";
console.log(chalk.white.bgGreen(' DMS '), `Dynamics Mobile is initiating datamodel update ${createDb ? '(with resetdb)' : ''}....`);
var homedir = os.homedir();
var folder = path.join(homedir, '.dms');
var profilePath = path.join(folder, 'profile.cfg');
if (!fs.existsSync(profilePath)) {
console.log(chalk.white.bgRed(' ERROR '), 'Dynamics Mobile profile does not exists. Use "dms login" command from command line, first');
process.exit(1);
return;
}
var profile = fs.readFileSync(profilePath, 'utf8');
try {
profile = JSON.parse(profile);
}
catch (err) {
console.log(chalk.white.bgRed(' ERROR '), 'Dynamics Mobile profile is invalid. Use "dms login" from the command line!');
console.log();
process.exit(1);
return;
}
if (!fs.existsSync('./package.json')) {
console.log(chalk.white.bgRed(' ERROR '), 'File package.json does not exists. Use "dms init" command from command line, first');
console.log();
process.exit(1);
return;
}
var app = fs.readFileSync('./package.json', 'utf8');
try {
app = JSON.parse(app);
}
catch (err) {
console.log(chalk.white.bgRed(' ERROR '), 'File package.json has invalid format!');
console.log();
process.exit(1);
return;
}
if (app.dms.appArea != profile.appArea) {
console.log();
console.log(chalk.white.bgRed(' ERROR '), `Wrong Application Area!`);
console.log(`apparea in package.json/dms/apparea`, chalk.bgGreen(` ${app.dms.appArea} `), `is not the same as the current profile apparea`, chalk.bgGreen(` ${profile.appArea} `));
console.log();
process.exit(1);
return;
}
var devRootMenu = {};
var devRootMenuPath = '.bin/user/apparea/SANDBOX/APP/en/root-menu.json'
if (fs.existsSync(devRootMenuPath)) {
devRootMenu = JSON.parse(fs.readFileSync(devRootMenuPath));
}
console.log(chalk.white.bgGreen(' DMS '), `App ${app.name.toUpperCase()}, v.${app.version}`);
var dataModel = "{}";
if (fs.existsSync('./.bin/dms-bo.json')) {
dataModel = fs.readFileSync('./.bin/dms-bo.json', 'utf8');
}
else {
console.log(chalk.white.bgYellowBright(' WARN '), 'Generated dms-bo.json file was not found, model will not be uploaded ...');
return;
}
var tasksMetadata = [];
var tasksPath = path.resolve('.bin/user/apparea/SANDBOX/APP/en/Tasks');
var taskFiles = fs.readdirSync(tasksPath);
taskFiles.forEach(function (taskFile) {
var taskFilePath = path.join(tasksPath, taskFile);
var isDirectory = fs.lstatSync(taskFilePath).isDirectory()
if (!isDirectory) {
var taskJson = fs.readFileSync(taskFilePath, 'utf8');
var task = JSON.parse(taskJson);
tasksMetadata.push({ id: task.id })
}
});
let url;
if (profile.user) {
url = `${baseAPIUrl}/studio/${app.name.toUpperCase()}/deploy?mode=datamodel&version=${app.version}&createDb=${createDb}`;
}
else {
url = `${baseAPIUrl}/studio/${app.name.toUpperCase()}/deploy2?mode=datamodel&version=${app.version}&createDb=${createDb}`;
}
try {
const headers = {
'Content-Type': 'application/json',
...(profile.user ? { 'Authorization': profile.token } : { 'x-api-key': profile.token })
};
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify({
appDataModel: JSON.parse(dataModel),
tasksMetadata: tasksMetadata,
appDef: app.dms,
devRootMenu: devRootMenu
})
});
if (!response.ok) {
console.log(chalk.white.bgRed(' DMS '), `Dynamics Mobile Cloud returned error status ${response.status}>> ${await response.text()}. Use "dms login" command from command line, first`);
console.log();
process.exit(1);
return;
}
const data = await response.json();
if (data && data.success) {
console.log(chalk.white.bgGreen(' DMS '), `Datamodel was updated successfuly (${(dataModel.length / 1024).toFixed(0)} Kb)`);
if (fs.existsSync('./.bin/dms-bo.json')) {
fs.unlinkSync('./.bin/dms-bo.json');
}
} else {
console.log(chalk.red(`DMS ERROR 103: Dynamics Mobile Cloud did not return success. Use "dms login" command from command line, first`));
console.log();
process.exit(1);
return;
}
} catch (err) {
console.log(chalk.white.bgRed(" DMS "), "ERROR 101: ", err);
console.log();
process.exit(1);
return;
}
}
uploadModel();