Skip to content

Commit

Permalink
Merge pull request #2 from dylanyoung-dev/dev
Browse files Browse the repository at this point in the history
Completed Web Experiences + Yaml Support
  • Loading branch information
dylanyoung-dev authored Sep 13, 2022
2 parents c7828a2 + a5f6297 commit d4bac4a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 63 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sitecore-cdp-serializer",
"version": "0.1.2",
"version": "0.1.5",
"description": "A Sitecore CDP/Personalize Serialization tool to abstract the APIs related to Tenant asset management and automated deployments from Source Control and/or physical files",
"type": "module",
"exports": "./lib/index.js",
Expand Down Expand Up @@ -33,12 +33,14 @@
"commander": "^9.4.0",
"configstore": "6.0.0",
"node-fetch": "3.2.10",
"json-diff": "0.9.0"
"json-diff": "0.9.0",
"js-yaml": "4.1.0"
},
"devDependencies": {
"@types/node": "18.6.3",
"@types/configstore": "6.0.0",
"@types/json-diff": "0.7.0",
"@types/js-yaml": "4.0.5",
"nodemon": "^2.0.19",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
Expand Down
143 changes: 82 additions & 61 deletions src/utils/deploy/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fs from 'fs';
import { Template, TemplateElement } from '../../commands/api/templates/Template.interface.js';
import Configstore from 'configstore';
import { TemplateService } from '../../commands/api/templates/Template.service.js';
import yaml from 'js-yaml';
import { diffString } from 'json-diff';

const deployTemplates = async (artifactDirectory: string, config: Configstore) => {
Expand All @@ -26,111 +27,131 @@ const deployTemplates = async (artifactDirectory: string, config: Configstore) =
return;
}

await deployDecisionTemplates(artifactDirectory, config);
//await deployWebTemplates(artifactDirectory, config);
await deployTemplateTypes(artifactDirectory, 'DECISION', config);
await deployTemplateTypes(artifactDirectory, 'WEB', config);

logline(chalk.greenBright(`Finished deploying templates`));
};

const deployDecisionTemplates = async (artifactDirectory: string, config: Configstore): Promise<void> => {
const deployTemplateTypes = async (
artifactDirectory: string,
templateType: string,
config: Configstore
): Promise<void> => {
const templateService = TemplateService(config);
const decisionFolder = path.join(artifactDirectory, 'templates', 'decision');
const templateFolder = path.join(artifactDirectory, 'templates', templateType);
const clientKey: string = config.get('clientKey');

logline(chalk.greenBright(`Starting to deploy decision templates`));
logline(chalk.greenBright(`Starting to deploy ${templateType} templates`));

if (!(await checkFolder(decisionFolder))) {
logline(chalk.red(`Decision templates folder doesn't exist - will not deploy`));
if (!(await checkFolder(templateFolder))) {
logline(chalk.red(`${templateType} templates folder doesn't exist - will not deploy`));
return;
}

const templatesToRun: string[] = await getFolders(decisionFolder);
const templatesToRun: string[] = await getFolders(templateFolder);

if (templatesToRun.length === 0) {
logline(chalk.red(`No decision templates found - will not deploy`));
logline(chalk.red(`No ${templateType} templates found - will not deploy`));
}

await Promise.all(
templatesToRun.map(
async (templateToRun) =>
await deployIndividualTemplates(templateToRun, decisionFolder, 'DECISION', templateService, clientKey)
await deployIndividualTemplates(templateToRun, templateFolder, templateType, templateService)
)
);

logline(chalk.greenBright(`Finished deploying decision templates`));
logline(chalk.greenBright(`Finished deploying ${templateType} templates`));
};

const deployWebTemplates = async (artifactDirectory: string, config: Configstore): Promise<void> => {
const templateService = TemplateService(config);
const decisionFolder = path.join(artifactDirectory, 'templates', 'decision');
const clientKey: string = config.get('clientKey');

logline(chalk.greenBright(`Starting to deploy decision templates`));
const getConfigFile = async (startPath: string) => {
// Read File Sync by yml yaml or json from the file system
let extension = getExtensions(startPath, 'config');

if (!(await checkFolder(decisionFolder))) {
logline(chalk.red(`Decision templates folder doesn't exist - will not deploy`));
return;
if (extension === undefined) {
return null;
}

const templatesToRun: string[] = await getFolders(decisionFolder);
let configContents: Template = yaml.load(
fs.readFileSync(path.join(startPath, `config.${extension}`), 'utf8')
) as Template;

if (templatesToRun.length === 0) {
logline(chalk.red(`No decision templates found - will not deploy`));
return configContents;
};

const getExtensions = (startDirectory: string, fileEntry: string): string | undefined => {
const files = fs.readdirSync(startDirectory);

const filename: string | undefined = files.find((file) => {
// return the first files that include given entry
return file.includes(fileEntry);
});

if (filename === undefined) {
return undefined;
}

await Promise.all(
templatesToRun.map(
async (templateToRun) =>
await deployIndividualTemplates(templateToRun, decisionFolder, 'WEB', templateService, clientKey)
)
);
const extension = filename.split('.').pop();

logline(chalk.greenBright(`Finished deploying decision templates`));
return extension;
};

const deployIndividualTemplates = async (
templateToRun: string,
folderPath: string,
templateType: string,
templateService: any,
clientKey: string
) => {
const templateFiles = await getFolderFiles(path.join(folderPath, templateToRun));
const generateTemplateElements = (template: Template, startDirectory: string, templateType: string): Template => {
const files = fs.readdirSync(startDirectory);

// TODO: need a template validation function in the CLI to validate the template
let template: Template = JSON.parse(
fs.readFileSync(path.join(folderPath, templateToRun, 'config.json'), 'utf8')
) as Template;
if (!files) {
return template;
}

if (template) {
// Need to make dynamic
const jsFileContents = await fs.promises.readFile(path.join(folderPath, templateToRun, 'file.js'), 'utf8');
const filenames: string[] = files.filter((value) => value.match(/file.(js|html|css|ftl)/));

if (!jsFileContents) {
logline(chalk.red(`Decision template file missing - Skip Deploy`));
return;
if (filenames.length > 0) {
// If empty array, then initialize
if (template.templateElements == undefined || template.templateElements.length == 0) {
template.templateElements = [];
}

// Check to see if this template already exists
let templateFromService: Template = await templateService.GetByFriendlyId(template.friendlyId);
filenames.forEach((file, item) => {
let fileContents = fs.readFileSync(path.join(startDirectory, file), 'utf8');
let extension = file.split('.').pop();

logline(chalk.greenBright(`Deploying decision template ${template.friendlyId}`));
if (extension == 'ftl') {
extension = 'freemarker';
}

// This needs to be refactored :-)
if (template.templateElements !== undefined && template.templateElements.length > 0) {
let objIndexTemplateElement = template.templateElements.findIndex((obj) => obj.id === 'js');
if (fileContents && extension) {
let existingElement = template.templateElements.findIndex((element) => element.id == extension);

if (objIndexTemplateElement !== -1) {
template.templateElements[objIndexTemplateElement].template = jsFileContents;
if (existingElement > -1) {
template.templateElements[existingElement].template = fileContents;
} else {
template.templateElements.push({ id: extension, template: fileContents });
}
} else {
template.templateElements.push({ id: 'js', template: jsFileContents });
logline(`There was an issue attaching file ${file} to ${template.name}`);
}
} else {
template.templateElements = [];
template.templateElements.push({ id: 'js', template: jsFileContents });
}
});
}

return template;
};

const deployIndividualTemplates = async (
templateToRun: string,
folderPath: string,
templateType: string,
templateService: any
) => {
let template: Template | null = await getConfigFile(path.join(folderPath, templateToRun));

if (template != null) {
let result: Template | null = null;

const templateFromService: Template = await templateService.GetByFriendlyId(template.friendlyId);

template = generateTemplateElements(template, path.join(folderPath, templateToRun), templateType);

if (templateFromService) {
logline(chalk.greenBright(`Template ${template.friendlyId} already exists in tenant - will update values`));

Expand Down

0 comments on commit d4bac4a

Please sign in to comment.