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

feat(quantic): added logic to include quantic version in analytic payloads #4685

Merged
merged 12 commits into from
Nov 26, 2024
159 changes: 159 additions & 0 deletions packages/quantic/build-static-resources.js
mmitiche marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
const {promisify} = require('util');
const ncp = promisify(require('ncp'));
const fs = require('fs').promises;
const path = require('path');

const STATIC_RESOURCES_PATH = './force-app/main/default/staticresources';
const TEMP_DIR = '.tmp/quantic-compiled';
const NODE_MODULES = '../../node_modules';

const LIBRARY_CONFIG = {
dompurify: {
directories: [`${STATIC_RESOURCES_PATH}/dompurify`],
files: [
{
src: `${NODE_MODULES}/dompurify/dist/purify.min.js`,
dest: `${STATIC_RESOURCES_PATH}/dompurify/purify.min.js`,
},
],
},
marked: {
directories: [`${STATIC_RESOURCES_PATH}/marked`],
files: [
{
src: `${NODE_MODULES}/marked/marked.min.js`,
dest: `${STATIC_RESOURCES_PATH}/marked/marked.min.js`,
},
],
},
bueno: {
directories: [
`${STATIC_RESOURCES_PATH}/coveobueno/browser`,
`${STATIC_RESOURCES_PATH}/coveobueno/definitions`,
],
files: [
{
src: `${NODE_MODULES}/@coveo/bueno/dist/browser/bueno.js`,
dest: `${STATIC_RESOURCES_PATH}/coveobueno/browser/bueno.js`,
},
{
src: `${NODE_MODULES}/@coveo/bueno/dist/definitions`,
dest: `${STATIC_RESOURCES_PATH}/coveobueno/definitions`,
},
],
},
headless: {
directories: [
`${STATIC_RESOURCES_PATH}/coveoheadless/case-assist`,
`${STATIC_RESOURCES_PATH}/coveoheadless/insight`,
`${STATIC_RESOURCES_PATH}/coveoheadless/recommendation`,
`${STATIC_RESOURCES_PATH}/coveoheadless/definitions`,
],
files: [
{
src: `${TEMP_DIR}/headless.js`,
dest: `${STATIC_RESOURCES_PATH}/coveoheadless/headless.js`,
},
{
src: `${TEMP_DIR}/case-assist/headless.js`,
dest: `${STATIC_RESOURCES_PATH}/coveoheadless/case-assist/headless.js`,
},
{
src: `${TEMP_DIR}/insight/headless.js`,
dest: `${STATIC_RESOURCES_PATH}/coveoheadless/insight/headless.js`,
},
{
src: `${TEMP_DIR}/recommendation/headless.js`,
dest: `${STATIC_RESOURCES_PATH}/coveoheadless/recommendation/headless.js`,
},
{
src: `${NODE_MODULES}/@coveo/headless/dist/definitions`,
dest: `${STATIC_RESOURCES_PATH}/coveoheadless/definitions`,
},
],
},
};

async function getPackageVersion() {
const packageJsonPath = path.join(__dirname, 'package.json');
const packageData = await fs.readFile(packageJsonPath, 'utf8');
return JSON.parse(packageData).version;
}

async function copy(src, dest) {
try {
await ncp(src, dest);
} catch (error) {
console.error(`Failed to copy: ${src}\nError: ${error.message}`);
process.exit(1);
}
}

async function createDirectories(directories) {
try {
for (const dir of directories) {
await fs.mkdir(dir, {recursive: true});
}
} catch (error) {
console.error(`Failed to create directories\nError: ${error.message}`);
process.exit(1);
}
}

async function writeQuanticVersion(filePath) {
try {
const existingContent = await fs.readFile(filePath, 'utf8');
const version = await getPackageVersion();
const contentToAppend = `\nwindow.coveoQuanticVersion = '${version}';`;
const endsWithNewline = existingContent.endsWith('\n');
const newContent = endsWithNewline
? existingContent + contentToAppend
: existingContent + '\n' + contentToAppend;
await fs.writeFile(filePath, newContent, 'utf8');
console.info(`Quantic version written to ${filePath}`);
} catch (error) {
console.error(
`Failed to write Quantic version in: ${filePath}\nError: ${error.message}`
);
process.exit(1);
}
}

async function copyLibrary(config) {
if (config.directories) {
await createDirectories(config.directories);
}
for (const {src, dest} of config.files) {
await copy(src, dest);
}
}

async function main() {
console.info('Begin building static resources');

await copyLibrary(LIBRARY_CONFIG.dompurify);
console.info('DOMPurify copied.');

await copyLibrary(LIBRARY_CONFIG.marked);
console.info('Marked copied.');

await copyLibrary(LIBRARY_CONFIG.bueno);
console.info('Bueno copied.');

await copyLibrary(LIBRARY_CONFIG.headless);
await fs.rm(TEMP_DIR, {recursive: true});
console.info('Headless copied.');

LIBRARY_CONFIG.headless.files.forEach(async ({dest}) => {
if (dest.includes('headless.js')) {
await writeQuanticVersion(dest);
}
});
console.info('Headless augmented with Quantic version.');

console.info('All resources built successfully!');
}

main().catch((error) => {
console.error('An error occurred during the build process:', error);
});
124 changes: 0 additions & 124 deletions packages/quantic/copy-static-resources.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/quantic/coveo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ declare global {
bundle: AnyHeadless;
};
};
coveoQuanticVersion: string
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export default class QuanticCaseAssistInterface extends LightningElement {
...(document.referrer && {
originLevel3: document.referrer.substring(0, 256),
}),
analyticsClientMiddleware: (_event, payload) => {
if (!payload.customData) {
payload.customData = {};
}
payload.customData.coveoQuanticVersion =
window.coveoQuanticVersion;
return payload;
},
},
...rest,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ export default class QuanticInsightInterface extends LightningElement {
...(document.referrer && {
originLevel3: document.referrer.substring(0, 256),
}),
analyticsClientMiddleware: (_event, payload) => {
if (!payload.customData) {
payload.customData = {};
}
payload.customData.coveoQuanticVersion =
window.coveoQuanticVersion;
return payload;
},
},
...rest,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ export default class QuanticRecommendationInterface extends LightningElement {
...(document.referrer && {
originLevel3: document.referrer.substring(0, 256),
}),
analyticsClientMiddleware: (_event, payload) => {
if (!payload.customData) {
payload.customData = {};
}
payload.customData.coveoQuanticVersion =
window.coveoQuanticVersion;
return payload;
},
},
...rest,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ export default class QuanticSearchInterface extends LightningElement {
...(document.referrer && {
originLevel3: document.referrer.substring(0, 256),
}),
analyticsClientMiddleware: (_event, payload) => {
if (!payload.customData) {
payload.customData = {};
}
payload.customData.coveoQuanticVersion =
window.coveoQuanticVersion;
return payload;
},
},
...rest,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/quantic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"lint:fix": "npm run lint:fix:js",
"lint:fix:js": "eslint --fix force-app/main/default/lwc/ && eslint --fix force-app/examples/main/lwc/ && prettier \"force-app/{,**}/*.js\" --write",
"lint:fix:apex": "prettier \"force-app/{,**}/*.{cls,trigger}\" --write",
"build:staticresources": "node copy-static-resources.js",
"build:staticresources": "node build-static-resources.js",
"build": "nx build",
"dev": "npx rimraf .localdevserver && npm run build:staticresources && npm run dev:sfdx",
"dev:sfdx": "sf project deploy start --source-dir force-app/main && sfdx force:lightning:lwc:start --port 3334",
Expand Down
Loading