crownpeak-dxm-sdk-core
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -10,2 +10,3 @@ import { CmsFieldTypes } from './cmsFieldTypes'; | ||
[Symbol.toPrimitive](_hint: string): any; | ||
data(): any; | ||
} |
@@ -30,5 +30,23 @@ "use strict"; | ||
_this.cmsIndexedField = cmsIndexedField; | ||
var handler = { | ||
get: function (target, prop) { | ||
if (typeof prop !== "string") | ||
return target[prop]; | ||
if (prop in target) | ||
return target[prop]; | ||
if (prop === "$$typeof") | ||
return undefined; // React uses this to determine if this is a React component or not | ||
if (prop === "_isVue") | ||
return undefined; // Vue.js uses this to determine if this is a Vue.js component or not | ||
return target.data()[prop]; | ||
} | ||
}; | ||
if (typeof Proxy !== "undefined") | ||
return new Proxy(_this, handler); | ||
return _this; | ||
} | ||
CmsField.prototype[Symbol.toPrimitive] = function (_hint) { | ||
return this.data(); | ||
}; | ||
CmsField.prototype.data = function () { | ||
if (typeof (this.value) !== "undefined" | ||
@@ -35,0 +53,0 @@ && this.value !== null |
@@ -190,2 +190,32 @@ const CrownpeakApi = require("crownpeak-dxm-accessapi-helper"); | ||
}; | ||
const ensurePath = async (siteRoot, path) => { | ||
// Paths come in with no leading / and one trailing / | ||
const pathSegments = path.substr(0, path.length - 1).split("/"); | ||
let workingPath = [siteRoot]; | ||
let lastExists = true; | ||
let lastFolder = await get(siteRoot); | ||
for (let i = 0, len = pathSegments.length; i < len; i++) { | ||
let mustCreate = false; | ||
workingPath.push(pathSegments[i]); | ||
if (!lastExists) { | ||
// If the parent didn't exist last time, this folder won't exist either | ||
mustCreate = true; | ||
} | ||
else { | ||
let folder = await getByPath(workingPath.join("/")); | ||
if (!folder || !folder.asset) { | ||
lastExists = false; | ||
mustCreate = true; | ||
} | ||
else { | ||
lastFolder = folder; | ||
} | ||
} | ||
if (mustCreate) { | ||
// We need to create a new folder here | ||
lastFolder = await createFolder(pathSegments[i], lastFolder.asset.id); | ||
} | ||
} | ||
return lastFolder; | ||
}; | ||
//#region Initialize site root | ||
@@ -355,3 +385,3 @@ /** | ||
continue; | ||
const folder = await getByPath(`${siteRootPath}${upload.destination}`); | ||
const folder = await ensurePath(siteRootPath, upload.destination); // await getByPath(`${siteRootPath}${upload.destination}`); | ||
if (!folder || !folder.asset) { | ||
@@ -358,0 +388,0 @@ console.error(`Unable to find folder [${siteRootPath}${upload.destination}] for upload`); |
@@ -0,1 +1,8 @@ | ||
const fs = require("fs"); | ||
const path = require('path'); | ||
const reAssets = [ | ||
/<(img|script|input)\s+(?:[^>]*?)src=((["']?)(.*?)\3)/ig, | ||
/<(link)\s+(?:[^>]*?)href=((["']?)(.*?)\3)/ig, | ||
/(url)\s*\(\s*((["']?)([^)]*)\3)\s*\)/ig | ||
]; | ||
const colouriseErrors = () => { | ||
@@ -16,5 +23,87 @@ // See https://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color | ||
}; | ||
const getPaths = (origin, url) => { | ||
const filename = path.basename(url); | ||
const root = getRoot(); | ||
let filepath = ""; | ||
if (url.indexOf("/") === 0) | ||
filepath = path.join(root, url); | ||
else if (url.indexOf("~/") === 0 || url.indexOf("@/") === 0) | ||
filepath = path.join(root, url.substr(1)); | ||
else | ||
filepath = path.resolve(path.dirname(origin), url); | ||
let folder = path.dirname(filepath).substr(root.length + 1).replace(/\\/g, "/") + "/"; | ||
if (folder === "/") | ||
folder = ""; | ||
return { | ||
path: filepath, | ||
folder: folder, | ||
filename: filename | ||
}; | ||
}; | ||
const getRecursive = function (dir, extn) { | ||
if (extn.substr(0, 1) !== ".") | ||
extn = "." + extn; | ||
var results = []; | ||
var list = fs.readdirSync(dir); | ||
list.forEach(function (file) { | ||
if (file !== "node_modules") { | ||
file = dir + '/' + file; | ||
var stat = fs.statSync(file); | ||
if (stat && stat.isDirectory()) { | ||
results = results.concat(getRecursive(file, extn)); | ||
} | ||
else { | ||
if (file.slice(extn.length * -1) === extn) { | ||
results.push(file); | ||
} | ||
} | ||
} | ||
}); | ||
return results; | ||
}; | ||
const replaceAssets = (file, content, cssParser) => { | ||
let result = content; | ||
let uploads = []; | ||
var matches; | ||
for (var i = 0, len = reAssets.length; i < len; i++) { | ||
while (matches = reAssets[i].exec(result)) { | ||
if (matches && matches.length > 4) { | ||
let url = matches[4]; | ||
if (url && url.indexOf("http") < 0 && url.indexOf("//") < 0) { | ||
//console.log(`Found candidate ${url}`); | ||
const { path: filepath, folder: dir, filename } = getPaths(file, url); | ||
if (fs.existsSync(filepath)) { | ||
let replacement = `"<%= Asset.Load(Asset.GetSiteRoot(asset).AssetPath + \"/${dir}${filename}\").GetLink() %>"`; | ||
//console.log(`Replacement is ${replacement}`); | ||
result = result.replace(matches[2], replacement); | ||
if (matches[1] === "link" && fs.lstatSync(filepath).isFile()) { | ||
// If this is CSS, it needs to be parsed separately | ||
const result = cssParser.parse(filepath, fs.readFileSync(filepath, "utf8"), ""); | ||
if (result.content && result.uploads && result.uploads.length) { | ||
uploads.push({ source: filepath, name: filename, destination: dir, content: result.content }); | ||
uploads = uploads.concat(result.uploads); | ||
} | ||
else { | ||
uploads.push({ source: filepath, name: filename, destination: dir }); | ||
} | ||
} | ||
else { | ||
uploads.push({ source: filepath, name: filename, destination: dir }); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return { content: result, uploads: uploads }; | ||
}; | ||
const getRoot = function () { | ||
return process.cwd(); | ||
}; | ||
module.exports = { | ||
colouriseErrors: colouriseErrors, | ||
colorizeErrors: colouriseErrors | ||
colorizeErrors: colouriseErrors, | ||
getPaths: getPaths, | ||
getRecursive: getRecursive, | ||
replaceAssets: replaceAssets | ||
}; |
{ | ||
"name": "crownpeak-dxm-sdk-core", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Crownpeak Digital Experience Management (DXM) Software Development Kits (SDK) have been constructed to assist the Single Page App developer in developing client-side applications that leverage DXM for content management purposes.", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/Crownpeak/DXM-SDK-Core", |
@@ -18,2 +18,3 @@ <a href="https://www.crownpeak.com" target="_blank"></a> | ||
| 2.2.0 | 2020SEP03 | Add support for indexed fields. | | ||
| 2.3.0 | 2020OCT01 | Preserve paths for uploads, support uploads from pages and wrappers. Bug fixes. | | ||
@@ -20,0 +21,0 @@ |
602948
1853
49
15