Skip to main content
Version: v5.2

bimpkOperations

The bimpkOperations function defines the project's object model scripts as Script items and creates them in the Item Service for the project's namespace.

Bimpk file selection​

The bimpk file selection code differs for a one-click orchestrator setup and a VS code extension setup:

Orchestrator Bimpk file selection​

The script downloads the scripts directory:

//extracts the zip link for the scripts folder from the passed parameters
const { zipLink } = params.actualParams;

//downloads the project setup zip file
const response = await fetch(zipLink);
//uses response.unZippedFilePath for the directory path
const directoryPath = response.unZippedFilePath;
//constructs the path to the scripts folder
const filePath = `${directoryPath}/scripts`;

With the scripts folder downloaded, the readFilesRecursively function reads the content of each script in the folder and its subfolders to find the bimpk file:

let iaf_bimpk_name = "1801KS-INV-01-ZZ-M3-Z-0001_Federated.bimpk";
let bimpk_filePath;
await readFilesRecursively(filePath);

async function readFilesRecursively(dir) {
//uses fs.readdirSync to read the directory's contents
const files = await fs.readdirSync(dir);
for (const file of files) {
const filePath = `${dir}/${file}`;
//if the item does not have a file extension,
//it is a folder and the readFilesRecursively
//function is recursively called to read its files
if (!file.includes(".")) {
await readFilesRecursively(filePath); // Await the recursive call
} else {
try {
if (file === iaf_bimpk_name) {
bimpk_filePath = filePath;
}
} catch (error) {
throw error;
}
}
}
}

VS code Bimpk file selection​

The script uses the IafLocalFile.selectFiles function from the UiUtils package to open a local file system selector window for the user to select the Bimpk file to upload:

async uploadBIMPKFile(input, libraries, ctx) {

let { PlatformApi, UiUtils, IafScriptEngine } = libraries

let proj = await PlatformApi.IafProj.getCurrent(ctx)

let selectFiles = await UiUtils.IafLocalFile.selectFiles({ multiple: false, accept: ".bimpk" })

uploadBimpk​

Orchestrator upload​

Using the bimpk file path, the function uploads the bimpk file to the File Service using the ModelFileReader package function uploadLargeFile:

async function uploadBimpk() {
return new Promise(async (resolve, reject) => {
const filePath = bimpkMetadata.bimpkFilePath;
const filename = bimpkMetadata.bimpkFile;
const opts = { filename }
const tags = ['#invmodelfile#']
const uploadParams = { filePath, opts, tags, folder, context: params.context }
try {
const uploadedFile = await ModelFileReader.uploadLargeFile(uploadParams);
console.log("uploadBimpk uploadedFile: ", JSON.stringify(uploadedFile));
} catch (e) {
console.log("uploadLargeFile error", JSON.stringify(e))
throw e;
}
console.log("bimpkUploadAndImport ==== File Uploaded");
resolve(true)
});
}

Note: Import ModelFileReader from libraries:

const { IafScriptEngine, fs, PlatformApi, ModelFileReader } = libraries;

VS code extension upload​

With the file selected, the script uses IafScriptEngine.uploadFile to upload it to the File Service:

let uploadedFile = await IafScriptEngine.uploadFile(selectFiles[0], ctx)

return uploadedFile
},