Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug/1511 dont recognize code as SSJS if the script tag has "language=ampscript" in it #1542

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion @types/lib/metadataTypes/Journey.d.ts.map

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

2 changes: 1 addition & 1 deletion @types/lib/util/util.d.ts.map

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

8 changes: 5 additions & 3 deletions lib/metadataTypes/Journey.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ class Journey extends MetadataType {
let parsed;
if (retrieveDir) {
const searchName = mode === 'name' ? key.slice(5) : null;
let foundKey;
const foundKey = [];
// get extra details for saving this
const details = results.items
? await Promise.all(
results.items.map(async (a) => {
if (mode === 'name') {
// when filtering by name, the API in fact does a LIKE search with placeholders left and right of the search term - and also searches the description field.
if (searchName === a[this.definition.nameField]) {
foundKey = a[this.definition.keyField];
foundKey.push(a[this.definition.keyField]);
} else {
// skip because the name does not match
return null;
Expand Down Expand Up @@ -163,7 +163,9 @@ class Journey extends MetadataType {
const savedMetadata = await this.saveResults(parsed, retrieveDir, null, null);
Util.logger.info(
`Downloaded: ${this.definition.type} (${Object.keys(savedMetadata).length})` +
Util.getKeysString(mode === 'name' ? `${foundKey} (${key})` : key)
Util.getKeysString(
mode === 'name' ? `${foundKey.join(', ')} (${key})` : key
)
);
} else {
// limit to main details for caching
Expand Down
18 changes: 13 additions & 5 deletions lib/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,18 +878,26 @@ export const Util = {
// (.*) capture any character, zero or more times
// /s dotall flag
// ideally the code looks like <script runat="server">...</script>
const scriptRegex = /^<\s*script [^>]*?>(.*)<\/\s*script\s*>$/s;

// regex that matches <script runat="server">...</script>, <script runat="server" language="javascript">...</script> or <script language="JavaScript" runat="server">...</script>, but it may not match <script language="ampscript" runat="server">...</script> and it may also not match <script runat="server" language="ampscript">...</script>
const scriptRegex =
/^<\s*script\s*(language=["']{1}javascript["']{1})?\s?[^>]*?runat\s*=\s*["']{1}server["']{1}\s*?(language=["']{1}javascript["']{1})?\s*>(.*)<\/\s*script\s*>$/is;

code = code.trim();
const regexMatches = scriptRegex.exec(code);
if (regexMatches?.length > 1) {
// regexMatches indexes:
// 1: first optional language block
// 2: second optional language block
// 3: the actual code
if (regexMatches?.length > 1 && regexMatches[3]) {
// script found
/* eslint-disable unicorn/prefer-ternary */
if (regexMatches[1].includes('<script')) {
if (regexMatches[3].includes('<script')) {
// nested script found
return null;
} else {
// no nested script found
return regexMatches[1];
// no nested script found: return the assumed SSJS-code
return regexMatches[3];
}
/* eslint-enable unicorn/prefer-ternary */
}
Expand Down
Loading