xrmcli
Version:
A suite of cli tools to support Xrm/D365/Dataverse development
128 lines • 6.57 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const node_fetch_1 = __importDefault(require("node-fetch"));
const fs_1 = require("fs");
const connect_1 = __importStar(require("../utils/connect"));
const validation_1 = __importDefault(require("../utils/validation"));
const header_1 = __importDefault(require("../utils/header"));
const publish_1 = require("../utils/publish");
commander_1.program.name('xrmcli typescript deploy');
(0, connect_1.AddAuthCommandOptions)();
commander_1.program
.requiredOption('--configfile <configfile>', 'Configuration file')
.option('--solution <solution>', 'Solution to add the webresources to')
.action(async (options) => {
var _a;
try {
if ((0, validation_1.default)(options)) {
const { url, configfile } = options;
// load config
const configjson = (0, fs_1.readFileSync)(configfile, { encoding: 'utf-8' });
const config = JSON.parse(configjson);
// upsert webresources
const auth = await (0, connect_1.default)(options);
let publish = false;
const ids = [];
for (let index = 0; index < config.WebResources.length; index += 1) {
const value = config.WebResources[index];
console.log(`Parsing Webresource '${value.name}'`);
const query = `/api/data/v9.2/webresourceset?$select=content,webresourceid&$filter=(name eq '${value.name}')&$top=1`;
const result = await (0, node_fetch_1.default)(`${url}${query}`, {
headers: (0, header_1.default)(auth.accessToken),
method: 'GET',
});
const jsonResult = await result.json();
const fileContents = (0, fs_1.readFileSync)(value.path, { encoding: 'base64' });
if ((jsonResult === null || jsonResult === void 0 ? void 0 : jsonResult.value.length) > 0) {
const { content, webresourceid } = jsonResult === null || jsonResult === void 0 ? void 0 : jsonResult.value[0];
if (content === fileContents) {
console.log(`No changes detected in ${value.name}, file skipped.`);
continue;
}
console.log(`Deploying (Existing) ${value.name}`);
const update = `/api/data/v9.2/webresourceset(${webresourceid})`;
const r = await (0, node_fetch_1.default)(`${url}${update}`, {
headers: (0, header_1.default)(auth.accessToken),
method: 'PATCH',
body: JSON.stringify({
name: value.name,
content: fileContents,
displayname: value.displayname,
description: value.description,
webresourcetype: value.type,
}),
});
if (r.status >= 200 && r.status < 300) {
ids.push(webresourceid);
console.log(`Deployed ${value.name} (${r.status})`);
publish = true;
}
else {
throw new Error(`Deployment of ${value.name} failed! ${r.status} ${r.statusText}`);
}
}
else {
console.log(`Deploying (New) ${value.name}`);
const update = '/api/data/v9.2/webresourceset';
const r = await (0, node_fetch_1.default)(`${url}${update}`, {
headers: (0, header_1.default)(auth.accessToken),
method: 'POST',
body: JSON.stringify({
name: value.name,
content: fileContents,
displayname: value.displayname,
description: value.description,
webresourcetype: value.type,
}),
});
if (r.status >= 200 && r.status < 300) {
ids.push((_a = r.headers.get('odata-entityid')) === null || _a === void 0 ? void 0 : _a.replace(/(.+\()(.+)(\))/, '$2'));
console.log(`Deployed ${value.name} (${r.status})`);
publish = true;
}
else {
throw new Error(`Deployment of ${value.name} failed! ${r.status} ${r.statusText}`);
}
}
}
if (publish) {
await (0, publish_1.publishsome)(auth, url, `<importexportxml><webresources>${ids
.map((value) => `<webresource>{${value}}</webresource>`)
.join()}</webresources></importexportxml>`);
}
console.log(`${ids.length} Webresources deployed.`);
}
}
catch (e) {
console.error(`${e.message || 'Error deploying webresources.'}`);
}
});
commander_1.program.parseAsync();
//# sourceMappingURL=deploy.js.map