This repository was archived by the owner on May 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathcli_support.ts
More file actions
64 lines (54 loc) · 1.91 KB
/
cli_support.ts
File metadata and controls
64 lines (54 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as path from './path';
/**
* asserts that the given fileName is an absolute path.
*
* The TypeScript API works in absolute paths, so we must be careful to resolve
* paths before handing them over to TypeScript.
*/
export function assertAbsolute(fileName: string) {
if (!path.isAbsolute(fileName)) {
throw new Error(`expected ${JSON.stringify(fileName)} to be absolute`);
}
}
/**
* Takes a context (ts.SourceFile.fileName of the current file) and the import URL of an ES6
* import and generates a googmodule module name for the imported module.
*/
export function pathToModuleName(
rootModulePath: string,
context: string,
fileName: string,
): string {
fileName = fileName.replace(/(\.d)?\.[tj]s$/, '');
if (fileName[0] === '.') {
// './foo' or '../foo'.
// Resolve the path against the dirname of the current module.
fileName = path.join(path.dirname(context), fileName);
}
// TODO(evanm): various tests assume they can import relative paths like
// 'foo/bar' and have them interpreted as root-relative; preserve that here.
// Fix this by removing the next line.
if (!path.isAbsolute(fileName)) {
fileName = path.join(rootModulePath, fileName);
}
// TODO(evanm): various tests assume they can pass in a 'fileName' like
// 'goog:foo.bar' and have this function do something reasonable.
// For correctness, the above must have produced an absolute path.
// assertAbsolute(fileName);
if (rootModulePath) {
fileName = path.relative(rootModulePath, fileName);
}
// Replace characters not supported by goog.module.
const moduleName = fileName
.replace(/\/|\\/g, '.')
.replace(/^[^a-zA-Z_$]/, '_')
.replace(/[^a-zA-Z0-9._$]/g, '_');
return moduleName;
}