Files
send-mail/node_modules/@actions/glob/lib/internal-path.js

87 lines
3.2 KiB
JavaScript
Raw Normal View History

import * as path from 'path';
import * as pathHelper from './internal-path-helper.js';
import assert from 'assert';
2021-06-22 19:18:21 +02:00
const IS_WINDOWS = process.platform === 'win32';
/**
* Helper class for parsing paths into segments
*/
export class Path {
2021-06-22 19:18:21 +02:00
/**
* Constructs a Path
* @param itemPath Path or array of segments
*/
constructor(itemPath) {
this.segments = [];
// String
if (typeof itemPath === 'string') {
assert(itemPath, `Parameter 'itemPath' must not be empty`);
2021-06-22 19:18:21 +02:00
// Normalize slashes and trim unnecessary trailing slash
itemPath = pathHelper.safeTrimTrailingSeparator(itemPath);
// Not rooted
if (!pathHelper.hasRoot(itemPath)) {
this.segments = itemPath.split(path.sep);
}
// Rooted
else {
// Add all segments, while not at the root
let remaining = itemPath;
let dir = pathHelper.dirname(remaining);
while (dir !== remaining) {
// Add the segment
const basename = path.basename(remaining);
this.segments.unshift(basename);
// Truncate the last segment
remaining = dir;
dir = pathHelper.dirname(remaining);
}
// Remainder is the root
this.segments.unshift(remaining);
}
}
// Array
else {
// Must not be empty
assert(itemPath.length > 0, `Parameter 'itemPath' must not be an empty array`);
2021-06-22 19:18:21 +02:00
// Each segment
for (let i = 0; i < itemPath.length; i++) {
let segment = itemPath[i];
// Must not be empty
assert(segment, `Parameter 'itemPath' must not contain any empty segments`);
2021-06-22 19:18:21 +02:00
// Normalize slashes
segment = pathHelper.normalizeSeparators(itemPath[i]);
// Root segment
if (i === 0 && pathHelper.hasRoot(segment)) {
segment = pathHelper.safeTrimTrailingSeparator(segment);
assert(segment === pathHelper.dirname(segment), `Parameter 'itemPath' root segment contains information for multiple segments`);
2021-06-22 19:18:21 +02:00
this.segments.push(segment);
}
// All other segments
else {
// Must not contain slash
assert(!segment.includes(path.sep), `Parameter 'itemPath' contains unexpected path separators`);
2021-06-22 19:18:21 +02:00
this.segments.push(segment);
}
}
}
}
/**
* Converts the path to it's string representation
*/
toString() {
// First segment
let result = this.segments[0];
// All others
let skipSlash = result.endsWith(path.sep) || (IS_WINDOWS && /^[A-Z]:$/i.test(result));
for (let i = 1; i < this.segments.length; i++) {
if (skipSlash) {
skipSlash = false;
}
else {
result += path.sep;
}
result += this.segments[i];
}
return result;
}
}
//# sourceMappingURL=internal-path.js.map