本文整理匯總了TypeScript中path.posix.relative方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript posix.relative方法的具體用法?TypeScript posix.relative怎麽用?TypeScript posix.relative使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類path.posix
的用法示例。
在下文中一共展示了posix.relative方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1:
.map(match => {
const relativePath = path.posix.relative(slashedDirname, match);
return path.posix.dirname(match) === slashedDirname
? `./${relativePath}`
: relativePath;
})
示例2: Buffer
return this._getBundles().then((bundles) => {
let sharedDepsBundle = (this.shell)
? this.urlFromPath(this.shell)
: this.sharedBundleUrl;
let sharedDeps = bundles.get(sharedDepsBundle) || [];
let promises = [];
if (this.shell) {
let shellFile = this.analyzer.getFile(this.shell);
console.assert(shellFile != null);
let newShellContent = this._addSharedImportsToShell(bundles);
shellFile.contents = new Buffer(newShellContent);
}
for (let fragment of this.allFragments) {
let fragmentUrl = this.urlFromPath(fragment);
let addedImports = (fragment === this.shell && this.shell)
? []
: [path.relative(path.dirname(fragmentUrl), sharedDepsBundle)];
let excludes = (fragment === this.shell && this.shell)
? []
: sharedDeps.concat(sharedDepsBundle);
promises.push(new Promise((resolve, reject) => {
let vulcanize = new Vulcanize({
abspath: null,
fsResolver: this.analyzer.resolver,
addedImports: addedImports,
stripExcludes: excludes,
inlineScripts: true,
inlineCss: true,
inputUrl: fragmentUrl,
});
vulcanize.process(null, (err, doc) => {
if (err) {
reject(err);
} else {
resolve({
url: fragment,
contents: doc,
});
}
});
}));
}
// vulcanize the shared bundle
if (!this.shell && sharedDeps && sharedDeps.length !== 0) {
logger.info(`generating shared bundle`);
promises.push(this._generateSharedBundle(sharedDeps));
}
return Promise.all(promises).then((bundles) => {
// convert {url,contents}[] into a Map
let contentsMap = new Map();
for (let bundle of bundles) {
contentsMap.set(bundle.url, bundle.contents);
}
return contentsMap;
});
});
示例3: relativeToRootDirs
export function relativeToRootDirs(filePath: string, rootDirs: string[]): string {
if (!filePath) return filePath;
// NB: the rootDirs should have been sorted longest-first
for (let i = 0; i < rootDirs.length; i++) {
const dir = rootDirs[i];
const rel = path.posix.relative(dir, filePath);
if (rel.indexOf('.') != 0) return rel;
}
return filePath;
}
示例4: resolveBazelFilePath
/**
* Resolves a given path to the associated relative path if the current process runs within
* Bazel. We need to use the wrapped NodeJS resolve logic in order to properly handle the given
* runfiles files which are only part of the runfile manifest on Windows.
*/
function resolveBazelFilePath(fileName: string): string {
// If the CLI has been launched through the NodeJS Bazel rules, we need to resolve the
// actual file paths because otherwise this script won't work on Windows where runfiles
// are not available in the working directory. In order to resolve the real path for the
// runfile, we need to use `require.resolve` which handles runfiles properly on Windows.
if (process.env['BAZEL_TARGET']) {
return path.posix.relative(process.cwd(), require.resolve(fileName));
}
return fileName;
}
示例5: relativePathBetween
export function relativePathBetween(from: string, to: string): string|null {
let relative = path.posix.relative(path.dirname(from), to).replace(TS_DTS_EXTENSION, '');
if (relative === '') {
return null;
}
// path.relative() does not include the leading './'.
if (!relative.startsWith('.')) {
relative = `./${relative}`;
}
return relative;
}
示例6: getRelativeUrl
export function getRelativeUrl(
fromUrl: ConvertedDocumentUrl, toUrl: ConvertedDocumentUrl): string {
// Error: Expects two package-root-relative URLs to compute a relative path
if (!fromUrl.startsWith('./') || !toUrl.startsWith('./')) {
throw new Error(
`paths relative to package root expected (actual: ` +
`from="${fromUrl}", to="${toUrl}")`);
}
let moduleJsUrl = path.relative(path.dirname(fromUrl), toUrl);
// Correct URL format to add './' preface if none exists
if (!moduleJsUrl.startsWith('.') && !moduleJsUrl.startsWith('/')) {
moduleJsUrl = './' + moduleJsUrl;
}
return moduleJsUrl;
}
示例7: urlFromPath
export function urlFromPath(root: string, target: string): string {
target = posixifyPath(target);
root = posixifyPath(root);
const relativePath = path.posix.relative(root, target);
// The startsWith(root) check is important on Windows because of the case
// where paths have different drive letters. The startsWith('../') will
// catch the general not-in-root case.
if (!target.startsWith(root) || relativePath.startsWith('../')) {
throw new Error(`target path is not in root: ${target} (${root})`);
}
return encodeURI(relativePath);
}
示例8: checkModuleDeps
export function checkModuleDeps(
sf: ts.SourceFile, tc: ts.TypeChecker, allowedDeps: string[],
rootDir: string, skipGoogSchemeDepsChecking: boolean,
ignoredFilesPrefixes: string[] = []): ts.Diagnostic[] {
function stripExt(fn: string) {
return fn.replace(/(\.d)?\.tsx?$/, '');
}
const allowedMap: {[fileName: string]: boolean} = {};
for (const d of allowedDeps) allowedMap[stripExt(d)] = true;
const result: ts.Diagnostic[] = [];
for (const stmt of sf.statements) {
if (stmt.kind !== ts.SyntaxKind.ImportDeclaration &&
stmt.kind !== ts.SyntaxKind.ExportDeclaration) {
continue;
}
const id = stmt as ts.ImportDeclaration | ts.ExportDeclaration;
const modSpec = id.moduleSpecifier;
if (!modSpec) continue; // E.g. a bare "export {x};"
if (ts.isStringLiteral(modSpec) && modSpec.text.startsWith('goog:') &&
skipGoogSchemeDepsChecking) {
continue;
}
const sym = tc.getSymbolAtLocation(modSpec);
if (!sym || !sym.declarations || sym.declarations.length < 1) {
continue;
}
// Module imports can only have one declaration location.
const declFileName = sym.declarations[0].getSourceFile().fileName;
if (allowedMap[stripExt(declFileName)]) continue;
if (ignoredFilesPrefixes.some(p => declFileName.startsWith(p))) continue;
const importName = path.posix.relative(rootDir, declFileName);
result.push({
file: sf,
start: modSpec.getStart(),
length: modSpec.getEnd() - modSpec.getStart(),
messageText: `transitive dependency on ${importName} not allowed. ` +
`Please add the BUILD target to your rule's deps.`,
category: ts.DiagnosticCategory.Error,
// semantics are close enough, needs taze.
code: TS_ERR_CANNOT_FIND_MODULE,
});
}
return result;
}
示例9: urlFromPath
export function urlFromPath(
root: LocalFsPath, target: LocalFsPath): PackageRelativeUrl {
const targetPosix = posixifyPath(target);
const rootPosix = posixifyPath(root);
const relativePath = path.posix.relative(rootPosix, targetPosix);
// The startsWith(root) check is important on Windows because of the case
// where paths have different drive letters. The startsWith('../') will
// catch the general not-in-root case.
if (!targetPosix.startsWith(posixifyPath(root)) ||
relativePath.startsWith('../')) {
throw new Error(`target path is not in root: ${target} (${root})`);
}
return encodeURI(relativePath) as PackageRelativeUrl;
}
示例10: generate
generate(): ts.SourceFile {
const relativeEntryPoint = './' +
path.posix.relative(path.posix.dirname(this.flatIndexPath), this.entryPoint)
.replace(/\.tsx?$/, '');
const contents = `/**
* Generated bundle index. Do not edit.
*/
export * from '${relativeEntryPoint}';
`;
const genFile = ts.createSourceFile(
this.flatIndexPath, contents, ts.ScriptTarget.ES2015, true, ts.ScriptKind.TS);
if (this.moduleName !== null) {
genFile.moduleName = this.moduleName;
}
return genFile;
}