2019-04-05 11:08:41 +02:00
|
|
|
/**
|
|
|
|
* Helper functions that implement "directory" functionality in the Terminal.
|
2019-05-12 04:20:20 +02:00
|
|
|
* These aren't "real" directories, it's more of a pseudo-directory implementation
|
|
|
|
* that uses mainly string manipulation.
|
|
|
|
*
|
|
|
|
* This file contains functions that deal only with that string manipulation.
|
|
|
|
* Functions that need to access/process Server-related things can be
|
|
|
|
* found in ./DirectoryServerHelpers.ts
|
2019-04-05 11:08:41 +02:00
|
|
|
*/
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
/**
|
|
|
|
* Removes leading forward slash ("/") from a string.
|
|
|
|
*/
|
|
|
|
export function removeLeadingSlash(s: string): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (s.startsWith("/")) {
|
|
|
|
return s.slice(1);
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return s;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes trailing forward slash ("/") from a string.
|
|
|
|
* Note that this will also remove the slash if it is the leading slash (i.e. if s = "/")
|
|
|
|
*/
|
|
|
|
export function removeTrailingSlash(s: string): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (s.endsWith("/")) {
|
|
|
|
return s.slice(0, -1);
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return s;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
/**
|
|
|
|
* Checks whether a string is a valid filename. Only used for the filename itself,
|
|
|
|
* not the entire filepath
|
|
|
|
*/
|
2021-12-19 10:42:16 +01:00
|
|
|
export function isValidFilename(filename: string): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Allows alphanumerics, hyphens, underscores, and percentage signs
|
|
|
|
// Must have a file extension
|
|
|
|
const regex = /^[.a-zA-Z0-9_-]+[.][a-zA-Z0-9]+(?:-\d+(?:\.\d*)?%-INC)?$/;
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// match() returns null if no match is found
|
|
|
|
return filename.match(regex) != null;
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a string is a valid directory name. Only used for the directory itself,
|
|
|
|
* not an entire path
|
|
|
|
*/
|
2021-12-23 21:55:58 +01:00
|
|
|
export function isValidDirectoryName(name: string): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Allows alphanumerics, hyphens, underscores, and percentage signs.
|
|
|
|
// Name can begin with a single period, but otherwise cannot have any
|
|
|
|
const regex = /^.?[a-zA-Z0-9_-]+$/;
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// match() returns null if no match is found
|
|
|
|
return name.match(regex) != null;
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a string is a valid directory path.
|
|
|
|
* This only checks if it has the proper formatting. It does NOT check
|
|
|
|
* if the directories actually exist on Terminal
|
|
|
|
*/
|
|
|
|
export function isValidDirectoryPath(path: string): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
let t_path: string = path;
|
|
|
|
|
|
|
|
if (t_path.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (t_path.length === 1) {
|
|
|
|
return t_path === "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
// A full path must have a leading slash, but we'll ignore it for the checks
|
|
|
|
if (t_path.startsWith("/")) {
|
|
|
|
t_path = t_path.slice(1);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trailing slash does not matter
|
|
|
|
t_path = removeTrailingSlash(t_path);
|
|
|
|
|
|
|
|
// Check that every section of the path is a valid directory name
|
|
|
|
const dirs = t_path.split("/");
|
|
|
|
for (const dir of dirs) {
|
|
|
|
// Special case, "." and ".." are valid for path
|
|
|
|
if (dir === "." || dir === "..") {
|
|
|
|
continue;
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!isValidDirectoryName(dir)) {
|
|
|
|
return false;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a string is a valid file path. This only checks if it has the
|
|
|
|
* proper formatting. It dose NOT check if the file actually exists on Terminal
|
|
|
|
*/
|
|
|
|
export function isValidFilePath(path: string): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (path == null || typeof path !== "string") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const t_path = path;
|
|
|
|
|
|
|
|
// Impossible for filename to have less than length of 3
|
|
|
|
if (t_path.length < 3) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full filepath can't end with trailing slash because it must be a file
|
|
|
|
if (t_path.endsWith("/")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything after the last forward slash is the filename. Everything before
|
|
|
|
// it is the file path
|
|
|
|
const fnSeparator = t_path.lastIndexOf("/");
|
|
|
|
if (fnSeparator === -1) {
|
|
|
|
return isValidFilename(t_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn = t_path.slice(fnSeparator + 1);
|
|
|
|
const dirPath = t_path.slice(0, fnSeparator + 1);
|
|
|
|
|
|
|
|
return isValidDirectoryPath(dirPath) && isValidFilename(fn);
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-20 07:27:33 +02:00
|
|
|
* Returns a formatted string for the first parent directory in a filepath. For example:
|
2019-04-10 08:07:12 +02:00
|
|
|
* /home/var/test/ -> home/
|
|
|
|
* If there is no first parent directory, then it returns "/" for root
|
|
|
|
*/
|
|
|
|
export function getFirstParentDirectory(path: string): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
let t_path = path;
|
|
|
|
t_path = removeLeadingSlash(t_path);
|
|
|
|
t_path = removeTrailingSlash(t_path);
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (t_path.lastIndexOf("/") === -1) {
|
|
|
|
return "/";
|
|
|
|
}
|
2019-04-20 07:27:33 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const dirs = t_path.split("/");
|
|
|
|
if (dirs.length === 0) {
|
|
|
|
return "/";
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return dirs[0] + "/";
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 07:27:33 +02:00
|
|
|
/**
|
|
|
|
* Given a filepath, returns a formatted string for all of the parent directories
|
|
|
|
* in that filepath. For example:
|
|
|
|
* /home/var/tes -> home/var/
|
|
|
|
* /home/var/test/ -> home/var/test/
|
|
|
|
* If there are no parent directories, it returns the empty string
|
|
|
|
*/
|
|
|
|
export function getAllParentDirectories(path: string): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
const t_path = path;
|
|
|
|
const lastSlash = t_path.lastIndexOf("/");
|
|
|
|
if (lastSlash === -1) {
|
|
|
|
return "";
|
|
|
|
}
|
2019-04-20 07:27:33 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return t_path.slice(0, lastSlash + 1);
|
2019-04-20 07:27:33 +02:00
|
|
|
}
|
|
|
|
|
2021-11-19 04:15:26 +01:00
|
|
|
/**
|
|
|
|
* Given a destination that only contains a directory part, returns the
|
|
|
|
* path to the source filename inside the new destination directory.
|
|
|
|
* Otherwise, returns the path to the destination file.
|
|
|
|
* @param destination The destination path or file name
|
|
|
|
* @param source The source path
|
|
|
|
* @param cwd The current working directory
|
|
|
|
* @returns A file path which may be absolute or relative
|
|
|
|
*/
|
2021-12-19 19:04:34 +01:00
|
|
|
export function getDestinationFilepath(destination: string, source: string, cwd: string): string {
|
2021-11-19 04:15:26 +01:00
|
|
|
const dstDir = evaluateDirectoryPath(destination, cwd);
|
|
|
|
// If evaluating the directory for this destination fails, we have a filename or full path.
|
|
|
|
if (dstDir === null) {
|
|
|
|
return destination;
|
|
|
|
} else {
|
|
|
|
// Append the filename to the directory provided.
|
2021-12-19 19:04:34 +01:00
|
|
|
const t_path = removeTrailingSlash(dstDir);
|
2021-11-19 04:15:26 +01:00
|
|
|
const fileName = getFileName(source);
|
|
|
|
return t_path + "/" + fileName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a filepath, returns the file name (e.g. without directory parts)
|
|
|
|
* For example:
|
|
|
|
* /home/var/test.js -> test.js
|
|
|
|
* ./var/test.js -> test.js
|
|
|
|
* test.js -> test.js
|
|
|
|
*/
|
|
|
|
export function getFileName(path: string): string {
|
|
|
|
const t_path = path;
|
|
|
|
const lastSlash = t_path.lastIndexOf("/");
|
|
|
|
if (lastSlash === -1) {
|
|
|
|
return t_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t_path.slice(lastSlash + 1);
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
/**
|
|
|
|
* Checks if a file path refers to a file in the root directory.
|
|
|
|
*/
|
|
|
|
export function isInRootDirectory(path: string): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!isValidFilePath(path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (path == null || path.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.lastIndexOf("/") <= 0;
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates a directory path, including the processing of linux dots.
|
|
|
|
* Returns the full, proper path, or null if an invalid path is passed in
|
|
|
|
*/
|
2021-09-09 05:47:34 +02:00
|
|
|
export function evaluateDirectoryPath(path: string, currPath?: string): string | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
let t_path = path;
|
|
|
|
|
|
|
|
// If the path begins with a slash, then its an absolute path. Otherwise its relative
|
|
|
|
// For relative paths, we need to prepend the current directory
|
|
|
|
if (!t_path.startsWith("/") && currPath != null) {
|
|
|
|
t_path = currPath + (currPath.endsWith("/") ? "" : "/") + t_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidDirectoryPath(t_path)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim leading/trailing slashes
|
|
|
|
t_path = removeLeadingSlash(t_path);
|
|
|
|
t_path = removeTrailingSlash(t_path);
|
|
|
|
|
|
|
|
const dirs = t_path.split("/");
|
|
|
|
const reconstructedPath: string[] = [];
|
|
|
|
|
|
|
|
for (const dir of dirs) {
|
|
|
|
if (dir === ".") {
|
|
|
|
// Current directory, do nothing
|
|
|
|
continue;
|
|
|
|
} else if (dir === "..") {
|
|
|
|
// Parent directory
|
|
|
|
const res = reconstructedPath.pop();
|
|
|
|
if (res == null) {
|
|
|
|
return null; // Array was empty, invalid path
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reconstructedPath.push(dir);
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return "/" + reconstructedPath.join("/");
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates a file path, including the processing of linux dots.
|
|
|
|
* Returns the full, proper path, or null if an invalid path is passed in
|
|
|
|
*/
|
2021-09-09 05:47:34 +02:00
|
|
|
export function evaluateFilePath(path: string, currPath?: string): string | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
let t_path = path;
|
|
|
|
|
|
|
|
// If the path begins with a slash, then its an absolute path. Otherwise its relative
|
|
|
|
// For relative paths, we need to prepend the current directory
|
|
|
|
if (!t_path.startsWith("/") && currPath != null) {
|
|
|
|
t_path = currPath + (currPath.endsWith("/") ? "" : "/") + t_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidFilePath(t_path)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim leading/trailing slashes
|
|
|
|
t_path = removeLeadingSlash(t_path);
|
|
|
|
|
|
|
|
const dirs = t_path.split("/");
|
|
|
|
const reconstructedPath: string[] = [];
|
|
|
|
|
|
|
|
for (const dir of dirs) {
|
|
|
|
if (dir === ".") {
|
|
|
|
// Current directory, do nothing
|
|
|
|
continue;
|
|
|
|
} else if (dir === "..") {
|
|
|
|
// Parent directory
|
|
|
|
const res = reconstructedPath.pop();
|
|
|
|
if (res == null) {
|
|
|
|
return null; // Array was empty, invalid path
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reconstructedPath.push(dir);
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return "/" + reconstructedPath.join("/");
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
2021-11-06 02:01:23 +01:00
|
|
|
|
|
|
|
export function areFilesEqual(f0: string, f1: string): boolean {
|
|
|
|
if (!f0.startsWith("/")) f0 = "/" + f0;
|
|
|
|
if (!f1.startsWith("/")) f1 = "/" + f1;
|
|
|
|
return f0 === f1;
|
|
|
|
}
|
2021-12-23 21:55:58 +01:00
|
|
|
|
|
|
|
export function areImportsEquals(f0: string, f1: string): boolean {
|
|
|
|
if (!f0.endsWith(".ns") && !f0.endsWith(".js")) f0 = f0 + ".js";
|
|
|
|
if (!f1.endsWith(".ns") && !f1.endsWith(".js")) f1 = f1 + ".js";
|
|
|
|
return areFilesEqual(f0, f1);
|
|
|
|
}
|