Skip to content

Commit 94de400

Browse files
committed
ref: Create internal path utils
1 parent adbce67 commit 94de400

File tree

8 files changed

+190
-15
lines changed

8 files changed

+190
-15
lines changed

packages/browser/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"rollup": "^0.58.2",
4141
"rollup-plugin-commonjs": "^9.1.3",
4242
"rollup-plugin-license": "^0.6.0",
43-
"rollup-plugin-node-builtins": "^2.1.2",
4443
"rollup-plugin-node-resolve": "^3.3.0",
4544
"rollup-plugin-npm": "^2.0.0",
4645
"rollup-plugin-typescript2": "^0.13.0",

packages/browser/rollup.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import uglify from 'rollup-plugin-uglify';
33
import resolve from 'rollup-plugin-node-resolve';
44
import typescript from 'rollup-plugin-typescript2';
55
import license from 'rollup-plugin-license';
6-
import builtins from 'rollup-plugin-node-builtins';
76

87
const commitHash = require('child_process')
98
.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' })
@@ -18,7 +17,6 @@ const bundleConfig = {
1817
},
1918
context: 'window',
2019
plugins: [
21-
builtins(),
2220
typescript({
2321
tsconfig: 'tsconfig.build.json',
2422
tsconfigOverride: { compilerOptions: { declaration: false } },

packages/core/src/integrations/pluggable/rewriteframes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getCurrentHub, Scope } from '@sentry/hub';
22
import { Integration, SentryEvent, StackFrame } from '@sentry/types';
3-
import { basename, relative } from 'path';
3+
import { basename, relative } from '@sentry/utils/path';
44

55
type StackFrameIteratee = (frame: StackFrame) => Promise<StackFrame>;
66

packages/node/src/parsers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SentryEvent, SentryException, StackFrame } from '@sentry/types';
22
import { readFileAsync } from '@sentry/utils/fs';
3+
import { basename, dirname } from '@sentry/utils/path';
34
import { snipLine } from '@sentry/utils/string';
4-
import * as path from 'path';
55
import * as stacktrace from 'stack-trace';
66

77
const LINES_OF_CONTEXT: number = 7;
@@ -25,7 +25,7 @@ function getFunction(frame: stacktrace.StackFrame): string {
2525
}
2626
}
2727

28-
const mainModule: string = `${(require.main && require.main.filename && path.dirname(require.main.filename)) ||
28+
const mainModule: string = `${(require.main && require.main.filename && dirname(require.main.filename)) ||
2929
global.process.cwd()}/`;
3030

3131
/** JSDoc */
@@ -35,8 +35,8 @@ function getModule(filename: string, base?: string): string {
3535
}
3636

3737
// It's specifically a module
38-
const file = path.basename(filename, '.js');
39-
filename = path.dirname(filename); // tslint:disable-line:no-parameter-reassignment
38+
const file = basename(filename, '.js');
39+
filename = dirname(filename); // tslint:disable-line:no-parameter-reassignment
4040
let n = filename.lastIndexOf('/node_modules/');
4141
if (n > -1) {
4242
// /node_modules/ is 14 chars

packages/utils/src/fs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mkdir, mkdirSync, readFile, statSync } from 'fs';
2-
import { dirname, resolve } from 'path';
2+
import { dirname, resolve } from './path';
33

44
const _0777 = parseInt('0777', 8);
55

packages/utils/src/path.ts

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// Slightly modified (no IE8 support, ES6) and transcribed to TypeScript
2+
// https://raw.githubusercontent.com/calvinmetcalf/rollup-plugin-node-builtins/master/src/es6/path.js
3+
4+
/** JSDoc */
5+
function normalizeArray(parts: string[], allowAboveRoot?: boolean): string[] {
6+
// if the path tries to go above the root, `up` ends up > 0
7+
let up = 0;
8+
for (let i = parts.length - 1; i >= 0; i--) {
9+
const last = parts[i];
10+
if (last === '.') {
11+
parts.splice(i, 1);
12+
} else if (last === '..') {
13+
parts.splice(i, 1);
14+
up++;
15+
} else if (up) {
16+
parts.splice(i, 1);
17+
up--;
18+
}
19+
}
20+
21+
// if the path is allowed to go above the root, restore leading ..s
22+
if (allowAboveRoot) {
23+
for (; up--; up) {
24+
parts.unshift('..');
25+
}
26+
}
27+
28+
return parts;
29+
}
30+
31+
// Split a filename into [root, dir, basename, ext], unix version
32+
// 'root' is just a slash, or nothing.
33+
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
34+
/** JSDoc */
35+
function splitPath(filename: string): string[] {
36+
const parts = splitPathRe.exec(filename);
37+
return parts ? parts.slice(1) : [];
38+
}
39+
40+
// path.resolve([from ...], to)
41+
// posix version
42+
/** JSDoc */
43+
export function resolve(...args: string[]): string {
44+
let resolvedPath = '';
45+
let resolvedAbsolute = false;
46+
47+
for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
48+
const path = i >= 0 ? args[i] : '/';
49+
50+
// Skip empty entries
51+
if (!path) {
52+
continue;
53+
}
54+
55+
resolvedPath = `${path}/${resolvedPath}`;
56+
resolvedAbsolute = path.charAt(0) === '/';
57+
}
58+
59+
// At this point the path should be resolved to a full absolute path, but
60+
// handle relative paths to be safe (might happen when process.cwd() fails)
61+
62+
// Normalize the path
63+
resolvedPath = normalizeArray(resolvedPath.split('/').filter(p => !!p), !resolvedAbsolute).join('/');
64+
65+
return (resolvedAbsolute ? '/' : '') + resolvedPath || '.';
66+
}
67+
68+
/** JSDoc */
69+
function trim(arr: string[]): string[] {
70+
let start = 0;
71+
for (; start < arr.length; start++) {
72+
if (arr[start] !== '') {
73+
break;
74+
}
75+
}
76+
77+
let end = arr.length - 1;
78+
for (; end >= 0; end--) {
79+
if (arr[end] !== '') {
80+
break;
81+
}
82+
}
83+
84+
if (start > end) {
85+
return [];
86+
}
87+
return arr.slice(start, end - start + 1);
88+
}
89+
90+
// path.relative(from, to)
91+
// posix version
92+
/** JSDoc */
93+
export function relative(from: string, to: string): string {
94+
// tslint:disable:no-parameter-reassignment
95+
from = resolve(from).substr(1);
96+
to = resolve(to).substr(1);
97+
98+
const fromParts = trim(from.split('/'));
99+
const toParts = trim(to.split('/'));
100+
101+
const length = Math.min(fromParts.length, toParts.length);
102+
let samePartsLength = length;
103+
for (let i = 0; i < length; i++) {
104+
if (fromParts[i] !== toParts[i]) {
105+
samePartsLength = i;
106+
break;
107+
}
108+
}
109+
110+
let outputParts = [];
111+
for (let i = samePartsLength; i < fromParts.length; i++) {
112+
outputParts.push('..');
113+
}
114+
115+
outputParts = outputParts.concat(toParts.slice(samePartsLength));
116+
117+
return outputParts.join('/');
118+
}
119+
120+
// path.normalize(path)
121+
// posix version
122+
/** JSDoc */
123+
export function normalize(path: string): string {
124+
const isPathAbsolute = isAbsolute(path);
125+
const trailingSlash = path.substr(-1) === '/';
126+
127+
// Normalize the path
128+
let normalizedPath = normalizeArray(path.split('/').filter(p => !!p), !isPathAbsolute).join('/');
129+
130+
if (!normalizedPath && !isPathAbsolute) {
131+
normalizedPath = '.';
132+
}
133+
if (normalizedPath && trailingSlash) {
134+
normalizedPath += '/';
135+
}
136+
137+
return (isPathAbsolute ? '/' : '') + normalizedPath;
138+
}
139+
140+
// posix version
141+
/** JSDoc */
142+
export function isAbsolute(path: string): boolean {
143+
return path.charAt(0) === '/';
144+
}
145+
146+
// posix version
147+
/** JSDoc */
148+
export function join(...args: string[]): string {
149+
return normalize(args.join('/'));
150+
}
151+
152+
/** JSDoc */
153+
export function dirname(path: string): string {
154+
const result = splitPath(path);
155+
const root = result[0];
156+
let dir = result[1];
157+
158+
if (!root && !dir) {
159+
// No dirname whatsoever
160+
return '.';
161+
}
162+
163+
if (dir) {
164+
// It has a dirname, strip trailing slash
165+
dir = dir.substr(0, dir.length - 1);
166+
}
167+
168+
return root + dir;
169+
}
170+
171+
/** JSDoc */
172+
export function basename(path: string, ext?: string): string {
173+
let f = splitPath(path)[2];
174+
if (ext && f.substr(ext.length * -1) === ext) {
175+
f = f.substr(0, f.length - ext.length);
176+
}
177+
return f;
178+
}

packages/utils/src/store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { existsSync, readFileSync, writeFileSync } from 'fs';
2-
import { dirname, join } from 'path';
32
import { mkdirpSync } from './fs';
3+
import { dirname, join } from './path';
44

55
/**
66
* Lazily serializes data to a JSON file to persist. When created, it loads data

packages/utils/test/store.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { expect } from 'chai';
22
import * as fs from 'fs';
33
import * as os from 'os';
4-
import * as path from 'path';
4+
import { join } from '../path';
55

66
import { Store } from '../src/store';
77

88
jest.useFakeTimers();
99

10-
const folder = path.join(os.tmpdir(), 'test');
10+
const folder = join(os.tmpdir(), 'test');
1111
const filename = 'data';
12-
const finalFilename = path.join(folder, `${filename}.json`);
12+
const finalFilename = join(folder, `${filename}.json`);
1313
const inital = ['a', 'b'];
1414
let store: Store<string[]>;
1515

@@ -58,11 +58,11 @@ describe('Store', () => {
5858
});
5959

6060
it('should create all intermediate folders', async () => {
61-
const newPath = path.join(folder, 'test');
61+
const newPath = join(folder, 'test');
6262
store = new Store<string[]>(newPath, filename, inital);
6363
store.update(current => current.map(value => `${value}1`));
6464
jest.runAllTimers();
65-
const realFilename = path.join(newPath, `${filename}.json`);
65+
const realFilename = join(newPath, `${filename}.json`);
6666
expect(fs.existsSync(folder)).to.be.true;
6767
expect(fs.existsSync(newPath)).to.be.true;
6868
expect(fs.existsSync(realFilename)).to.be.true;

0 commit comments

Comments
 (0)