Skip to content

Commit

Permalink
#747: SSJS support for codesnippets
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Feb 17, 2023
1 parent 7513f98 commit 4b9709c
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/metadataTypes/Asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,12 +1036,7 @@ class Asset extends MetadataType {
// metadata.content
subDirArr = [this.definition.type, subType];
readDirArr = [deployDir, ...subDirArr];
const fileExtArr = ['html']; // eslint-disable-line no-case-declarations
if (metadata.assetType.name === 'codesnippetblock') {
// extracted code snippets should end on the right extension
// we are making a few assumptions during retrieve to pick the right one
fileExtArr.push('amp', 'sjss');
}
const fileExtArr = ['html', 'ssjs', 'amp']; // eslint-disable-line no-case-declarations
for (const ext of fileExtArr) {
if (
await File.pathExists(
Expand All @@ -1058,6 +1053,9 @@ class Asset extends MetadataType {
(templateName || customerKey) + subtypeExtension,
ext
);
if (ext === 'ssjs') {
metadata.content = `<script runat="server">\n${metadata.content}</script>`;
}
}
if (templateName) {
// to use this method in templating, store a copy of the info in fileList
Expand Down Expand Up @@ -1279,20 +1277,27 @@ class Asset extends MetadataType {
case 'codesnippetblock': {
// other-codesnippetblock
// metadata.content
let fileExt = 'html'; // eslint-disable-line no-case-declarations
if (
let fileExt;
let content = metadata?.content;
const scriptRegex = /<\s*script .*?>(.+?)<\s*\/script>/gms;
const regexMatches = scriptRegex.exec(content);
if (content && regexMatches?.length > 1) {
fileExt = 'ssjs';
content = regexMatches[1];
} else if (
metadata.assetType.name === 'codesnippetblock' && // extracted code snippets should end on the right extension
// we are making a few assumptions during retrieve to pick the right one
metadata?.content?.includes('%%[')
content?.includes('%%[')
) {
fileExt = 'amp';
} else {
fileExt = 'html';
}
if (metadata?.content?.length) {
if (content?.length) {
codeArr.push({
subFolder: null,
fileName: customerKey,
fileExt: fileExt,
content: metadata.content,
content: content,
});
delete metadata.content;
}
Expand Down

0 comments on commit 4b9709c

Please sign in to comment.