@salesforce/core
Version:
Core libraries to interact with SFDX projects, orgs, and APIs.
152 lines • 6.2 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pollUntilUnlockSync = exports.pollUntilUnlock = exports.lockInitSync = exports.lockInit = void 0;
/*
* Copyright (c) 2023, 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 fs = __importStar(require("node:fs"));
const node_path_1 = require("node:path");
const proper_lockfile_1 = require("proper-lockfile");
const kit_1 = require("@salesforce/kit");
const ts_retry_promise_1 = require("ts-retry-promise");
const sfError_1 = require("../sfError");
const logger_1 = require("../logger/logger");
const lockRetryOptions_1 = require("./lockRetryOptions");
/**
*
*This method exists as a separate function so it can be used by ConfigFile OR outside of ConfigFile.
*
* @param filePath where to save the file
* @returns 2 functions:
* - writeAndUnlock: a function that takes the data to write and writes it to the file, then unlocks the file whether write succeeded or not
* - unlock: a function that unlocks the file (in case you don't end up calling writeAndUnlock)
*/
const lockInit = async (filePath) => {
// make sure we can write to the directory
try {
await fs.promises.mkdir((0, node_path_1.dirname)(filePath), { recursive: true });
}
catch (err) {
throw sfError_1.SfError.wrap(err);
}
const [unlock] = await Promise.all(fs.existsSync(filePath)
? // if the file exists, wait for it to be unlocked
[(0, proper_lockfile_1.lock)(filePath, lockRetryOptions_1.lockRetryOptions)]
: // lock the entire directory to keep others from trying to create the file while we are
[
(0, proper_lockfile_1.lock)((0, node_path_1.dirname)(filePath), lockRetryOptions_1.lockRetryOptions),
(await logger_1.Logger.child('fileLocking.lockInit')).debug(`No file found at ${filePath}. Write will create it. Locking the entire directory until file is written.`),
]);
return {
writeAndUnlock: async (data) => {
const logger = await logger_1.Logger.child('fileLocking.writeAndUnlock');
logger.debug(`Writing to file: ${filePath}`);
try {
await fs.promises.writeFile(filePath, data);
}
finally {
await unlock();
}
},
unlock,
};
};
exports.lockInit = lockInit;
/**
* prefer async {@link lockInit}.
* See its documentation for details.
*/
const lockInitSync = (filePath) => {
// make sure we can write to the directory
try {
fs.mkdirSync((0, node_path_1.dirname)(filePath), { recursive: true });
}
catch (err) {
throw sfError_1.SfError.wrap(err);
}
const [unlock] = fs.existsSync(filePath)
? // if the file exists, wait for it to be unlocked
[(0, proper_lockfile_1.lockSync)(filePath, lockRetryOptions_1.lockOptions)]
: // lock the entire directory to keep others from trying to create the file while we are
[
(0, proper_lockfile_1.lockSync)((0, node_path_1.dirname)(filePath), lockRetryOptions_1.lockOptions),
logger_1.Logger.childFromRoot('fileLocking.lockInit').debug(`No file found at ${filePath}. Write will create it. Locking the entire directory until file is written.`),
];
return {
writeAndUnlock: (data) => {
const logger = logger_1.Logger.childFromRoot('fileLocking.writeAndUnlock');
logger.debug(`Writing to file: ${filePath}`);
try {
fs.writeFileSync(filePath, data);
}
finally {
unlock();
}
},
unlock,
};
};
exports.lockInitSync = lockInitSync;
/**
* Poll until the file is unlocked.
*
* @param filePath file path to check
*/
const pollUntilUnlock = async (filePath) => {
try {
await (0, ts_retry_promise_1.retryDecorator)(proper_lockfile_1.check, {
timeout: kit_1.Duration.minutes(1).milliseconds,
delay: 10,
until: (locked) => locked === false,
// don't retry errors (typically enoent or access on the lockfile, therefore not locked)
retryIf: () => false,
})(filePath, lockRetryOptions_1.lockRetryOptions);
}
catch (e) {
// intentionally swallow the error, same reason as above
}
};
exports.pollUntilUnlock = pollUntilUnlock;
const pollUntilUnlockSync = (filePath) => {
// Set a counter to ensure that the while loop does not run indefinitely
let counter = 0;
let locked = true;
while (locked && counter < 100) {
try {
locked = (0, proper_lockfile_1.checkSync)(filePath, lockRetryOptions_1.lockOptions);
counter++;
}
catch {
// Likely a file not found error, which means the file is not locked
locked = false;
}
}
};
exports.pollUntilUnlockSync = pollUntilUnlockSync;
//# sourceMappingURL=fileLocking.js.map
;