UNPKG

@salesforce/salesforcedx-vscode-test-tools

Version:
152 lines 5.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createFolder = createFolder; exports.createOrOverwriteFile = createOrOverwriteFile; exports.removeFolder = removeFolder; exports.createCustomObjects = createCustomObjects; exports.createGlobalSnippetsFile = createGlobalSnippetsFile; exports.getVsixFilesFromDir = getVsixFilesFromDir; exports.getFolderName = getFolderName; /* * Copyright (c) 2025, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const miscellaneous_1 = require("../core/miscellaneous"); const fast_glob_1 = __importDefault(require("fast-glob")); /** * Creates a directory at the specified path * @param folderPath - The file system path where the folder should be created */ function createFolder(folderPath) { fs_1.default.mkdirSync(folderPath, { recursive: true }); } /** * Creates a file at the specified path, overwriting the file if it already exists * @param filePath - The file system path where the file should be created * @param content - The content to be written to the file */ function createOrOverwriteFile(filePath, content) { fs_1.default.writeFileSync(filePath, content); } /** * Removes a directory and all its contents at the specified path * @param folderPath - The file system path of the folder to be removed */ function removeFolder(folderPath) { fs_1.default.rmdirSync(folderPath, { recursive: true }); } /** * Creates custom Salesforce objects in the project's force-app directory * Copies object definitions from testSetup.testDataFolderPath to the project * @param testSetup - The test setup object containing project paths * @throws Error if copying fails or paths are undefined */ async function createCustomObjects(testSetup) { const projectPath = testSetup.projectFolderPath; const testDataFolderPath = testSetup.testDataFolderPath; if (!testDataFolderPath) { throw new Error('testDataFolderPath is undefined'); } if (!projectPath) { throw new Error('projectPath is undefined'); } const source = testDataFolderPath; const destination = path_1.default.join(projectPath, 'force-app', 'main', 'default', 'objects'); // Ensure the project path has been created fs_1.default.mkdirSync(path_1.default.dirname(destination), { recursive: true }); const copyRecursive = (src, dest) => { if (fs_1.default.statSync(src).isDirectory()) { fs_1.default.mkdirSync(dest, { recursive: true }); fs_1.default.readdirSync(src).forEach(child => { copyRecursive(path_1.default.join(src, child), path_1.default.join(dest, child)); }); } else { fs_1.default.copyFileSync(src, dest); } }; try { copyRecursive(source, destination); } catch (error) { if (error instanceof Error) { (0, miscellaneous_1.log)(`Failed in copying custom objects ${error.message}`); } (0, miscellaneous_1.log)(`source was: '${source}'`); (0, miscellaneous_1.log)(`destination was: '${destination}'`); await testSetup?.tearDown(); throw error; } } /** * Creates a global Apex code snippets file in the project's .vscode directory * @param testSetup - The test setup object containing project paths * @throws Error if file creation fails or paths are undefined */ async function createGlobalSnippetsFile(testSetup) { const projectPath = testSetup.projectFolderPath; if (!projectPath) { throw new Error('projectPath is undefined'); } const destination = path_1.default.join(projectPath, '.vscode', 'apex.json.code-snippets'); const apexSnippet = [ `{`, `"SOQL": {`, `"prefix": "soql",`, `"body": [`, ` "[SELECT \${1:field1, field2} FROM \${2:SobjectName} WHERE \${3:clause}];"`, `],`, `"description": "Apex SOQL query"`, `}`, `}` ].join('\n'); try { fs_1.default.writeFileSync(destination, apexSnippet); } catch (error) { if (error instanceof Error) { (0, miscellaneous_1.log)(`Failed in creating apex snippets file ${error.message}`); } (0, miscellaneous_1.log)(`destination was: '${destination}'`); await testSetup?.tearDown(); throw error; } } /** * Scans the directory for vsix files and returns the full path to each file * @param vsixDir * @returns */ function getVsixFilesFromDir(vsixDir) { return fast_glob_1.default.sync('**/*.vsix', { cwd: vsixDir }).map(vsixFile => path_1.default.join(vsixDir, vsixFile)); } /** * Return folder name if given path is a directory, otherwise return null * @param folderPath * @returns folder name */ function getFolderName(folderPath) { try { // Check if the given path exists and if it is a directory const stats = fs_1.default.statSync(folderPath); if (stats.isDirectory()) { // Extract and return the folder name return path_1.default.basename(folderPath); } else { return null; // It's not a directory } } catch (err) { console.error('Error checking path:', err); return null; // The path doesn't exist or isn't accessible } } //# sourceMappingURL=fileSystem.js.map