Skip to content

Commit

Permalink
simplified the way of storing the Quantic version
Browse files Browse the repository at this point in the history
  • Loading branch information
mmitiche committed Nov 22, 2024
1 parent 2293fb3 commit 5025d9b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 68 deletions.
1 change: 0 additions & 1 deletion packages/quantic/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ force-app/main/default/staticresources/coveoheadless
force-app/main/default/staticresources/coveobueno
force-app/main/default/staticresources/marked
force-app/main/default/staticresources/dompurify
force-app/main/default/staticresources/quanticMetadata.json

# Test Coverage & Report
coverage
Expand Down
34 changes: 23 additions & 11 deletions packages/quantic/build-static-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const main = async () => {
await copyBueno();
await copyMarked();
await copyDompurify();
await setupQuanticMetadata();
};

const copyDompurify = async () => {
Expand Down Expand Up @@ -73,22 +72,34 @@ const copyHeadless = async () => {
await fs.mkdir(`${staticResourcesPath}/coveoheadless/definitions/`, {
recursive: true,
});

await copy(
'.tmp/quantic-compiled/headless.js',
`${staticResourcesPath}/coveoheadless/headless.js`
);
await writeQuanticVersion(`${staticResourcesPath}/coveoheadless/headless.js`);
await copy(
'.tmp/quantic-compiled/case-assist/headless.js',
`${staticResourcesPath}/coveoheadless/case-assist/headless.js`
);
await writeQuanticVersion(
`${staticResourcesPath}/coveoheadless/case-assist/headless.js`
);

await copy(
'.tmp/quantic-compiled/insight/headless.js',
`${staticResourcesPath}/coveoheadless/insight/headless.js`
);
await writeQuanticVersion(
`${staticResourcesPath}/coveoheadless/insight/headless.js`
);
await copy(
'.tmp/quantic-compiled/recommendation/headless.js',
`${staticResourcesPath}/coveoheadless/recommendation/headless.js`
);
await writeQuanticVersion(
`${staticResourcesPath}/coveoheadless/recommendation/headless.js`
);
await copy(
'../../node_modules/@coveo/headless/dist/definitions',
`${staticResourcesPath}/coveoheadless/definitions`
Expand Down Expand Up @@ -120,19 +131,20 @@ const copyBueno = async () => {
console.info('Bueno copied.');
};

const setupQuanticMetadata = async () => {
const writeQuanticVersion = async (filePath) => {
try {
console.info('Begin Quantic metadata creation');
const outputJsonPath = `${staticResourcesPath}/quanticMetadata.json`;

console.info('Begin Quantic version writing');
const existingContent = await fs.readFile(filePath, 'utf8');
const version = await getPackageVersion();
const metadata = {
version: version,
};
fs.writeFile(outputJsonPath, JSON.stringify(metadata, null, 2), 'utf8');
console.info('Quantic metadata created.');
const contentToAppend = `\nwindow.quanticVersion = '${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.');
} catch (error) {
console.error('Error occurred Quantic metadata creation: ', error);
console.error('Error occurred Quantic version writing: ', error);
}
};

Expand Down
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 @@ -8,7 +8,6 @@ import {
HeadlessBundleNames,
} from 'c/quanticHeadlessLoader';
import {LightningElement, api} from 'lwc';
import quanticMetadata from '@salesforce/resourceUrl/quanticMetadata';

/** @typedef {import("coveo").CaseAssistEngine} CaseAssistEngine */
/** @typedef {import("coveo").CaseAssistEngineOptions} CaseAssistEngineOptions */
Expand Down Expand Up @@ -50,19 +49,10 @@ export default class QuanticCaseAssistInterface extends LightningElement {
loadDependencies(this, HeadlessBundleNames.caseAssist)
.then(() => {
if (!getHeadlessBindings(this.engineId)?.engine) {
const beforeInitPromises = [
getHeadlessConfiguration(),
fetch(quanticMetadata).then((response) => response.json()),
];

Promise.all(beforeInitPromises)
getHeadlessConfiguration()
.then((data) => {
if (data) {
const {organizationId, accessToken, ...rest} = JSON.parse(
data[0]
);
const {version: quanticVersion} = data[1];

const {organizationId, accessToken, ...rest} = JSON.parse(data);
this.engineOptions = {
configuration: {
organizationId,
Expand All @@ -78,7 +68,8 @@ export default class QuanticCaseAssistInterface extends LightningElement {
if (!payload.customData) {
payload.customData = {};
}
payload.customData.coveoQuanticVersion = quanticVersion;
payload.customData.coveoQuanticVersion =
window.coveoQuanticVersion;
return payload;
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
setInitializedCallback,
} from 'c/quanticHeadlessLoader';
import {LightningElement, api} from 'lwc';
import quanticMetadata from '@salesforce/resourceUrl/quanticMetadata';

/** @typedef {import("coveo").InsightEngine} InsightEngine */
/** @typedef {import("coveo").InsightEngineOptions} InsightEngineOptions */
Expand Down Expand Up @@ -64,19 +63,10 @@ export default class QuanticInsightInterface extends LightningElement {
loadDependencies(this, HeadlessBundleNames.insight)
.then(() => {
if (!getHeadlessBindings(this.engineId)?.engine) {
const beforeInitPromises = [
getHeadlessConfiguration(),
fetch(quanticMetadata).then((response) => response.json()),
];

Promise.all(beforeInitPromises)
getHeadlessConfiguration()
.then((data) => {
if (data) {
const {organizationId, accessToken, ...rest} = JSON.parse(
data[0]
);
const {version: quanticVersion} = data[1];

const {organizationId, accessToken, ...rest} = JSON.parse(data);
this.engineOptions = {
configuration: {
organizationId,
Expand All @@ -94,7 +84,8 @@ export default class QuanticInsightInterface extends LightningElement {
if (!payload.customData) {
payload.customData = {};
}
payload.customData.coveoQuanticVersion = quanticVersion;
payload.customData.coveoQuanticVersion =
window.coveoQuanticVersion;
return payload;
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
HeadlessBundleNames,
} from 'c/quanticHeadlessLoader';
import {LightningElement, api} from 'lwc';
import quanticMetadata from '@salesforce/resourceUrl/quanticMetadata';

/** @typedef {import("coveo").RecommendationEngine} RecommendationEngine */
/** @typedef {import("coveo").RecommendationEngineOptions} RecommendationEngineOptions */
Expand Down Expand Up @@ -66,16 +65,9 @@ export default class QuanticRecommendationInterface extends LightningElement {
connectedCallback() {
loadDependencies(this, HeadlessBundleNames.recommendation).then(() => {
if (!getHeadlessBindings(this.engineId)?.engine) {
const beforeInitPromises = [
getHeadlessConfiguration(),
fetch(quanticMetadata).then((response) => response.json()),
];

Promise.all(beforeInitPromises).then((data) => {
getHeadlessConfiguration().then((data) => {
if (data) {
const {organizationId, accessToken, ...rest} = JSON.parse(data[0]);
const {version: quanticVersion} = data[1];

const {organizationId, accessToken, ...rest} = JSON.parse(data);
this.engineOptions = {
configuration: {
organizationId,
Expand All @@ -94,7 +86,8 @@ export default class QuanticRecommendationInterface extends LightningElement {
if (!payload.customData) {
payload.customData = {};
}
payload.customData.coveoQuanticVersion = quanticVersion;
payload.customData.coveoQuanticVersion =
window.coveoQuanticVersion;
return payload;
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import getHeadlessConfiguration from '@salesforce/apex/HeadlessController.getHeadlessConfiguration';
import LOCALE from '@salesforce/i18n/locale';
import TIMEZONE from '@salesforce/i18n/timeZone';
import quanticMetadata from '@salesforce/resourceUrl/quanticMetadata';
import {
getHeadlessBindings,
loadDependencies,
Expand Down Expand Up @@ -90,18 +89,9 @@ export default class QuanticSearchInterface extends LightningElement {
loadDependencies(this)
.then(() => {
if (!getHeadlessBindings(this.engineId)?.engine) {
const beforeInitPromises = [
getHeadlessConfiguration(),
fetch(quanticMetadata).then((response) => response.json()),
];

Promise.all(beforeInitPromises)
getHeadlessConfiguration()
.then((data) => {
const {organizationId, accessToken, ...rest} = JSON.parse(
data[0]
);
const {version: quanticVersion} = data[1];

const {organizationId, accessToken, ...rest} = JSON.parse(data);
this.engineOptions = {
configuration: {
organizationId,
Expand All @@ -121,7 +111,8 @@ export default class QuanticSearchInterface extends LightningElement {
if (!payload.customData) {
payload.customData = {};
}
payload.customData.coveoQuanticVersion = quanticVersion;
payload.customData.coveoQuanticVersion =
window.coveoQuanticVersion;
return payload;
},
},
Expand Down

This file was deleted.

0 comments on commit 5025d9b

Please sign in to comment.