@ms-cloudpack/path-utilities
Advanced tools
Comparing version 2.7.39 to 2.7.40
/** | ||
* Takes a relative intermediate content path (e.g. "lib/foo/bar.js") and resolves it to a source content path | ||
* (e.g. "/src/foo/foo.tsx"). Note: source files must exist so that the utility can resolve the source. | ||
* (e.g. "./src/foo/foo.tsx"). Note: source files must exist so that the utility can resolve the source. | ||
*/ | ||
export declare function intermediateToSourcePath(relativeIntermediatePath: string, rootPath: string): string | undefined; | ||
//# sourceMappingURL=intermediateToSourcePath.d.ts.map |
import path from 'path'; | ||
import { isFileSync } from './isFileSync.js'; | ||
import { slash } from '@ms-cloudpack/path-string-parsing'; | ||
import { normalizeRelativePath } from '@ms-cloudpack/path-string-parsing'; | ||
import { sourceExtensions } from './sourceExtensions.js'; | ||
// TODO: this assumption won't work for all repos (tracked by https://github.com/microsoft/cloudpack/issues/2366) | ||
const intermediateFolderNames = ['lib', 'lib-amd', 'lib-esm', 'lib-commonjs', 'dist']; | ||
@@ -9,44 +10,37 @@ const sourceFolderName = 'src'; | ||
* Takes a relative intermediate content path (e.g. "lib/foo/bar.js") and resolves it to a source content path | ||
* (e.g. "/src/foo/foo.tsx"). Note: source files must exist so that the utility can resolve the source. | ||
* (e.g. "./src/foo/foo.tsx"). Note: source files must exist so that the utility can resolve the source. | ||
*/ | ||
export function intermediateToSourcePath(relativeIntermediatePath, rootPath) { | ||
const candidates = []; | ||
const originalExt = path.extname(relativeIntermediatePath); | ||
const originalPath = path.dirname(relativeIntermediatePath); | ||
const originalDir = path.dirname(relativeIntermediatePath); | ||
const basename = path.basename(relativeIntermediatePath, sourceExtensions.includes(originalExt) ? originalExt : undefined); | ||
// Find first non "." or ".." folder in relative path | ||
const parts = slash(originalPath).split('/'); | ||
// Iterate through parts until we find the first folder name. If the folder name | ||
// is a known intermediate folder name, then we can replace it with the source folder name | ||
// and push all candidates to the candidates array. | ||
for (let i = 0; i < parts.length; i++) { | ||
const part = parts[i]; | ||
if (part !== '.' && part !== '..') { | ||
if (intermediateFolderNames.includes(part)) { | ||
// Join the parts back together, replacing the part with the source folder name. | ||
const newPath = parts.map((v, j) => (j === i ? sourceFolderName : v)).join('/'); | ||
// Push all possible source extensions. | ||
candidates.push(...sourceExtensions.map((ext) => path.join(newPath, basename + ext))); | ||
// Push the original. | ||
candidates.push(path.join(newPath, basename)); | ||
} | ||
break; | ||
} | ||
// Normalize to get rid of leading ./ or any other extra segments, then split into parts. | ||
const dirParts = path.normalize(originalDir).split(/[\\/]/g); | ||
if (dirParts[0] === sourceFolderName && isFileSync(path.resolve(rootPath, relativeIntermediatePath))) { | ||
// This is already a complete source path. Use it as-is. | ||
return normalizeRelativePath(relativeIntermediatePath); | ||
} | ||
const candidates = []; | ||
// Find the first non-'..' folder name. If it's a known intermediate folder name, then we can | ||
// replace it with the source folder name and add candidate paths for all extensions. | ||
const firstDirIndex = dirParts.findIndex((part) => part !== '..'); | ||
if (firstDirIndex !== -1 && intermediateFolderNames.includes(dirParts[firstDirIndex])) { | ||
// Join the parts back together, replacing the part with the source folder name. | ||
dirParts[firstDirIndex] = sourceFolderName; | ||
const newDir = dirParts.join('/'); | ||
// Push all possible source extensions. | ||
candidates.push(...sourceExtensions.map((ext) => path.join(newDir, basename + ext))); | ||
// Push the original. | ||
candidates.push(path.join(newDir, basename)); | ||
} | ||
// Push the original path with all supported source extensions. | ||
candidates.push(...sourceExtensions.map((ext) => path.join(originalPath, basename + ext))); | ||
// And finally push the original path with the original extension in case it wasn't | ||
// covered. | ||
candidates.push(...sourceExtensions.map((ext) => path.join(originalDir, basename + ext))); | ||
// And finally push the original path with the original extension in case it wasn't covered. | ||
candidates.push(relativeIntermediatePath); | ||
let match = candidates.find((candidate) => isFileSync(path.resolve(rootPath, candidate))); | ||
// Ensure correct path direction. | ||
const match = candidates.find((candidate) => isFileSync(path.resolve(rootPath, candidate))); | ||
if (match) { | ||
match = slash(match); | ||
return normalizeRelativePath(match); | ||
} | ||
// Re-add the leading period. | ||
if (match && !match.startsWith('./')) { | ||
match = './' + match; | ||
} | ||
return match; | ||
return undefined; | ||
} | ||
//# sourceMappingURL=intermediateToSourcePath.js.map |
/** | ||
* Takes a relative source path (e.g. "src/foo/bar.ts") and resolves it to an intermediate content path | ||
* (e.g. "/lib/foo/bar.js"). | ||
* (e.g. "./lib/foo/bar.js"). | ||
*/ | ||
export declare function sourceToIntermediatePath(sourcePath: string): string; | ||
//# sourceMappingURL=sourceToIntermediatePath.d.ts.map |
import path from 'path'; | ||
import { slash } from '@ms-cloudpack/path-string-parsing'; | ||
import { normalizeRelativePath } from '@ms-cloudpack/path-string-parsing'; | ||
/** | ||
* Takes a relative source path (e.g. "src/foo/bar.ts") and resolves it to an intermediate content path | ||
* (e.g. "/lib/foo/bar.js"). | ||
* (e.g. "./lib/foo/bar.js"). | ||
*/ | ||
@@ -10,16 +10,9 @@ export function sourceToIntermediatePath(sourcePath) { | ||
const basename = path.basename(sourcePath, ext); | ||
const pathParts = slash(path.dirname(sourcePath)).split('/'); | ||
if (pathParts[0] === '.') { | ||
pathParts.shift(); | ||
// TODO: this assumption won't work for all repos (tracked by https://github.com/microsoft/cloudpack/issues/2366) | ||
const newDir = path.normalize(path.dirname(sourcePath)).replace(/^src([\\/]|$)/, 'lib/'); | ||
// Handle js replacement extensions (keep the c or m if present) | ||
const jsMatch = ext.match(/\.([cm])?[jt]sx?$/); | ||
if (jsMatch) { | ||
ext = `.${jsMatch[1] || ''}js`; | ||
} | ||
if (pathParts[0] === 'src') { | ||
pathParts[0] = 'lib'; | ||
} | ||
if (['.ts', '.tsx', '.jsx'].includes(ext)) { | ||
// Handle js replacement extensions | ||
ext = '.js'; | ||
} | ||
else if (ext === '.mts') { | ||
ext = '.mjs'; | ||
} | ||
else if (ext === '.scss') { | ||
@@ -29,4 +22,4 @@ // Handle js appends | ||
} | ||
return './' + slash(path.join(...pathParts, basename + ext)); | ||
return normalizeRelativePath(path.join(newDir, basename + ext)); | ||
} | ||
//# sourceMappingURL=sourceToIntermediatePath.js.map |
{ | ||
"name": "@ms-cloudpack/path-utilities", | ||
"version": "2.7.39", | ||
"version": "2.7.40", | ||
"description": "Utilities for resolving paths between source/intermediate/output locations in Cloudpack.", | ||
@@ -31,3 +31,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@ms-cloudpack/common-types": "^0.19.2", | ||
"@ms-cloudpack/common-types": "^0.19.3", | ||
"@ms-cloudpack/eslint-plugin-internal": "^0.0.1", | ||
@@ -34,0 +34,0 @@ "@ms-cloudpack/scripts": "^0.0.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38127
308