UNPKG

4.85 kBPlain TextView Raw
1/**
2 * @license
3 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at
5 * http://polymer.github.io/LICENSE.txt
6 * The complete set of authors may be found at
7 * http://polymer.github.io/AUTHORS.txt
8 * The complete set of contributors may be found at
9 * http://polymer.github.io/CONTRIBUTORS.txt
10 * Code distributed by Google as part of the polymer project is also
11 * subject to an additional IP rights grant found at
12 * http://polymer.github.io/PATENTS.txt
13 */
14
15/**
16 * CODE ADAPTED FROM THE "SLASH" LIBRARY BY SINDRE SORHUS
17 * https://github.com/sindresorhus/slash
18 *
19 * ORIGINAL LICENSE:
20 * The MIT License (MIT)
21 *
22 * Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)*
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy*
25 * of this software and associated documentation files (the "Software"), to
26 * deal*
27 * in the Software without restriction, including without limitation the rights*
28 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell*
29 * copies of the Software, and to permit persons to whom the Software is*
30 * furnished to do so, subject to the following conditions:*
31 *
32 * The above copyright notice and this permission notice shall be included in*
33 * all copies or substantial portions of the Software.*
34 *
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE*
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
40 * FROM,*
41 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN*
42 * THE SOFTWARE.
43 */
44
45/**
46 * This module consists of functions for transformations to filesystem and url
47 * paths.
48 * TODO(usergenic): We should consider migrating the responsibility of
49 * path-related string transformation to a package like `upath`.
50 * Please see: https://www.npmjs.com/package/upath
51 */
52
53import * as path from 'path';
54import {PackageRelativeUrl} from 'polymer-analyzer';
55
56export declare class LocalFsPathBrand {
57 private LocalFsPathBrand: never;
58}
59export type LocalFsPath = string&LocalFsPathBrand;
60export declare class PosixPathBrand {
61 private PosixPathBrand: never;
62}
63export type PosixPath = string&PosixPathBrand;
64
65/**
66 * Returns a properly encoded URL representing the relative URL from the root
67 * to the target. This function will throw an error if the target is outside
68 * the root. We use this to map a file from the filesystem to the relative
69 * URL that represents it in the build.
70 */
71export function urlFromPath(
72 root: LocalFsPath, target: LocalFsPath): PackageRelativeUrl {
73 const targetPosix = posixifyPath(target);
74 const rootPosix = posixifyPath(root);
75
76 const relativePath = path.posix.relative(rootPosix, targetPosix);
77
78 // The startsWith(root) check is important on Windows because of the case
79 // where paths have different drive letters. The startsWith('../') will
80 // catch the general not-in-root case.
81 if (!targetPosix.startsWith(posixifyPath(root)) ||
82 relativePath.startsWith('../')) {
83 throw new Error(`target path is not in root: ${target} (${root})`);
84 }
85
86 return encodeURI(relativePath) as PackageRelativeUrl;
87}
88
89/**
90 * Returns a filesystem path for the url, relative to the root.
91 */
92export function pathFromUrl(
93 root: LocalFsPath,
94 // TODO(usergenic): PackageRelativeUrl are not *necessarily* always just a
95 // relative path from root. Maybe subclass as PackageRelativeUrlPath or
96 // something if this function doesn't disappear after
97 // https://github.com/Polymer/polymer-build/issues/324 is addressed.
98 url: PackageRelativeUrl): LocalFsPath {
99 return path.normalize(decodeURIComponent(path.posix.join(
100 posixifyPath(root), path.posix.join('/', url)))) as LocalFsPath;
101}
102
103/**
104 * Returns a string where all Windows path separators are converted to forward
105 * slashes.
106 * NOTE(usergenic): We will generate only canonical Windows paths, but this
107 * function is exported so that we can create a forward-slashed Windows root
108 * path when dealing with the `sw-precache` library, which uses `glob` npm
109 * module generates only forward-slash paths in building its `precacheConfig`
110 * map.
111 */
112export function posixifyPath(filepath: LocalFsPath): PosixPath {
113 // We don't want to change backslashes to forward-slashes in the case where
114 // we're already on posix environment, because they would be intentional in
115 // that case (albeit weird.)
116 if (path.sep === '\\') {
117 filepath = filepath.replace(/\\/g, '/') as string as LocalFsPath;
118 }
119 return filepath as string as PosixPath;
120}