@eagleoutice/flowr-dev
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
41 lines • 2.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.watchProtocol = void 0;
exports.handlePathLikeInput = handlePathLikeInput;
/**
* Handling of repl inputs that name a file, shared by every command that takes R code.
* @module
*/
const fs_1 = __importDefault(require("fs"));
const retriever_1 = require("../../r-bridge/retriever");
const ansi_1 = require("../../util/text/ansi");
/** Prefix of an input that is to be re-analyzed whenever the file changes, the counterpart of {@link fileProtocol}. */
exports.watchProtocol = 'watch://';
/** Whether `s` looks like a filesystem path the user likely meant to load via {@link fileProtocol}. */
function looksLikePath(s) {
if (s.startsWith(retriever_1.fileProtocol) || s.startsWith(exports.watchProtocol)) {
return false;
}
if (/^(~|\.{0,2}\/|[a-zA-Z]:[\\/])/.test(s)) {
return true;
}
// a single path-like token (a separator, no R-code punctuation) that actually exists on disk
return /^[^\s()]+\/[^\s()]+$/.test(s) && fs_1.default.existsSync(s);
}
/** The input to analyze: one that looks like a path gets the {@link fileProtocol}, unless the repl config says not to. */
function handlePathLikeInput(output, input, config) {
if (!looksLikePath(input)) {
return input;
}
const asFile = retriever_1.fileProtocol + input;
if (config.repl.autoUseFileProtocol) {
output.stdout((0, ansi_1.ansiInfo)(`'${input}' looks like a path, analyzing ${(0, ansi_1.bold)(asFile, output.formatter)} (${(0, ansi_1.bold)('repl.autoUseFileProtocol', output.formatter)} is set).`));
return asFile;
}
output.stdout((0, ansi_1.ansiInfo)(`'${input}' looks like a path. To analyze it, use ${(0, ansi_1.bold)(asFile, output.formatter)} (or ${(0, ansi_1.bold)(exports.watchProtocol + input, output.formatter)} to re-run on changes), or re-enable ${(0, ansi_1.bold)('repl.autoUseFileProtocol', output.formatter)} to have flowR do this for you. Use ${(0, ansi_1.bold)(':help', output.formatter)} for more.`));
return input;
}
//# sourceMappingURL=path-input.js.map