path-serializer 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/cjs/index.d.ts +20 -0
- package/dist/cjs/index.js +22 -15
- package/dist/esm/index.d.ts +20 -0
- package/dist/esm/index.mjs +22 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# path-serializer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/path-serializer)
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
6
|
// __snapshots__/index.test.ts.snap
|
|
@@ -22,6 +22,8 @@ expect.addSnapshotSerializer(
|
|
|
22
22
|
);
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
The specific usage can be found in [./src/types.ts](https://github.com/rspack-contrib/path-serializer/blob/main/src/types.ts)
|
|
26
|
+
|
|
25
27
|
## Showcases
|
|
26
28
|
|
|
27
29
|
[Rslib](https://github.com/web-infra-dev/rslib/blob/3ff6859eb38171c731e447a1364afc021f8c501a/tests/setupVitestTests.ts)
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -17,6 +17,10 @@ declare interface Features {
|
|
|
17
17
|
* @default true
|
|
18
18
|
*/
|
|
19
19
|
replaceTmpDir?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* @default true
|
|
22
|
+
*/
|
|
23
|
+
replaceHomeDir?: boolean;
|
|
20
24
|
/**
|
|
21
25
|
* @default true
|
|
22
26
|
*/
|
|
@@ -48,9 +52,25 @@ declare interface SnapshotSerializer {
|
|
|
48
52
|
}
|
|
49
53
|
|
|
50
54
|
declare interface SnapshotSerializerOptions {
|
|
55
|
+
/**
|
|
56
|
+
* repository root path
|
|
57
|
+
* @example '/Users/foo/codes/rsbuild/node_modules/.pnpm' -> '<ROOT>/node_modules/.pnpm'
|
|
58
|
+
* @default process.cwd()
|
|
59
|
+
*/
|
|
51
60
|
root?: string;
|
|
61
|
+
/**
|
|
62
|
+
* workspace root path
|
|
63
|
+
* @example '/Users/foo/codes/rsbuild/packages/core/src' -> '<WORKSPACE>/src'
|
|
64
|
+
* @default ''
|
|
65
|
+
*/
|
|
52
66
|
workspace?: string;
|
|
67
|
+
/**
|
|
68
|
+
* @description replace -> workspace root pnpmInner temp home -> replacePost
|
|
69
|
+
*/
|
|
53
70
|
replace?: PathMatcher[];
|
|
71
|
+
/**
|
|
72
|
+
* @description replace -> workspace root pnpmInner temp home -> replacePost
|
|
73
|
+
*/
|
|
54
74
|
replacePost?: PathMatcher[];
|
|
55
75
|
features?: Features;
|
|
56
76
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -834,17 +834,23 @@ function __webpack_require__(moduleId) {
|
|
|
834
834
|
}
|
|
835
835
|
const createTmpDirMatchers = ()=>{
|
|
836
836
|
const ret = [];
|
|
837
|
-
const
|
|
838
|
-
|
|
839
|
-
match:
|
|
837
|
+
const realTmpDir = getRealTemporaryDirectory();
|
|
838
|
+
realTmpDir && ret.push({
|
|
839
|
+
match: realTmpDir,
|
|
840
840
|
mark: 'temp'
|
|
841
841
|
});
|
|
842
|
-
|
|
843
|
-
|
|
842
|
+
const tmpDir = external_node_os_default().tmpdir();
|
|
843
|
+
tmpDir && ret.push({
|
|
844
|
+
match: tmpDir,
|
|
844
845
|
mark: 'temp'
|
|
845
846
|
});
|
|
846
|
-
ret
|
|
847
|
-
|
|
847
|
+
return ret;
|
|
848
|
+
};
|
|
849
|
+
const createHomeDirMatchers = ()=>{
|
|
850
|
+
const ret = [];
|
|
851
|
+
const homedir = external_node_os_default().homedir();
|
|
852
|
+
homedir && ret.push({
|
|
853
|
+
match: homedir,
|
|
848
854
|
mark: 'home'
|
|
849
855
|
});
|
|
850
856
|
return ret;
|
|
@@ -866,21 +872,22 @@ function __webpack_require__(moduleId) {
|
|
|
866
872
|
/(?<![a-zA-Z])([a-zA-Z]:[\\/])([-\u4e00-\u9fa5\w\s.()~!@#$%^&()\[\]{}+=]+[\\/])*/g, (match, _diskName)=>normalizeToPosixPath(match));
|
|
867
873
|
}
|
|
868
874
|
function createSnapshotSerializer(options) {
|
|
869
|
-
const { root = process.cwd(), workspace =
|
|
870
|
-
const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, addDoubleQuotes = true, transformWin32Path = true, escapeDoubleQuotes = true, escapeEOL = true } = features;
|
|
875
|
+
const { root = process.cwd(), workspace = '', replace: customMatchers = [], replacePost: customPostMatchers = [], features = {} } = options || {};
|
|
876
|
+
const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, replaceHomeDir = true, addDoubleQuotes = true, transformWin32Path = true, escapeDoubleQuotes = true, escapeEOL = true } = features;
|
|
871
877
|
function createPathMatchers() {
|
|
872
878
|
const pathMatchers = [];
|
|
873
879
|
pathMatchers.push(...customMatchers);
|
|
874
|
-
if (
|
|
875
|
-
mark: 'root',
|
|
876
|
-
match: root
|
|
877
|
-
});
|
|
878
|
-
if (replaceWorkspace) pathMatchers.push({
|
|
880
|
+
if (replaceWorkspace && workspace) pathMatchers.push({
|
|
879
881
|
mark: 'workspace',
|
|
880
882
|
match: workspace
|
|
881
883
|
});
|
|
884
|
+
if (replaceRoot && root) pathMatchers.push({
|
|
885
|
+
mark: 'root',
|
|
886
|
+
match: root
|
|
887
|
+
});
|
|
882
888
|
if (replacePnpmInner) pathMatchers.push(...createPnpmInnerMatchers());
|
|
883
889
|
if (replaceTmpDir) pathMatchers.push(...createTmpDirMatchers());
|
|
890
|
+
if (replaceHomeDir) pathMatchers.push(...createHomeDirMatchers());
|
|
884
891
|
pathMatchers.push(...customPostMatchers);
|
|
885
892
|
return pathMatchers;
|
|
886
893
|
}
|
|
@@ -896,7 +903,7 @@ function __webpack_require__(moduleId) {
|
|
|
896
903
|
if (transformWin32Path) replaced = transformCodeToPosixPath(replaced);
|
|
897
904
|
replaced = applyMatcherReplacement(pathMatchers, replaced);
|
|
898
905
|
if (escapeDoubleQuotes) replaced = replaced.replace(/"/g, '\\"');
|
|
899
|
-
if (escapeEOL) replaced = replaced.replace(/\\r\\n/g, '
|
|
906
|
+
if (escapeEOL) replaced = replaced.replace(/\\r\\n/g, '\\n');
|
|
900
907
|
if (addDoubleQuotes) replaced = `"${replaced}"`;
|
|
901
908
|
return replaced;
|
|
902
909
|
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -17,6 +17,10 @@ declare interface Features {
|
|
|
17
17
|
* @default true
|
|
18
18
|
*/
|
|
19
19
|
replaceTmpDir?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* @default true
|
|
22
|
+
*/
|
|
23
|
+
replaceHomeDir?: boolean;
|
|
20
24
|
/**
|
|
21
25
|
* @default true
|
|
22
26
|
*/
|
|
@@ -48,9 +52,25 @@ declare interface SnapshotSerializer {
|
|
|
48
52
|
}
|
|
49
53
|
|
|
50
54
|
declare interface SnapshotSerializerOptions {
|
|
55
|
+
/**
|
|
56
|
+
* repository root path
|
|
57
|
+
* @example '/Users/foo/codes/rsbuild/node_modules/.pnpm' -> '<ROOT>/node_modules/.pnpm'
|
|
58
|
+
* @default process.cwd()
|
|
59
|
+
*/
|
|
51
60
|
root?: string;
|
|
61
|
+
/**
|
|
62
|
+
* workspace root path
|
|
63
|
+
* @example '/Users/foo/codes/rsbuild/packages/core/src' -> '<WORKSPACE>/src'
|
|
64
|
+
* @default ''
|
|
65
|
+
*/
|
|
52
66
|
workspace?: string;
|
|
67
|
+
/**
|
|
68
|
+
* @description replace -> workspace root pnpmInner temp home -> replacePost
|
|
69
|
+
*/
|
|
53
70
|
replace?: PathMatcher[];
|
|
71
|
+
/**
|
|
72
|
+
* @description replace -> workspace root pnpmInner temp home -> replacePost
|
|
73
|
+
*/
|
|
54
74
|
replacePost?: PathMatcher[];
|
|
55
75
|
features?: Features;
|
|
56
76
|
}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -809,17 +809,23 @@ function applyMatcherReplacement(matchers, str, options = {}) {
|
|
|
809
809
|
}
|
|
810
810
|
const createTmpDirMatchers = ()=>{
|
|
811
811
|
const ret = [];
|
|
812
|
-
const
|
|
813
|
-
|
|
814
|
-
match:
|
|
812
|
+
const realTmpDir = getRealTemporaryDirectory();
|
|
813
|
+
realTmpDir && ret.push({
|
|
814
|
+
match: realTmpDir,
|
|
815
815
|
mark: 'temp'
|
|
816
816
|
});
|
|
817
|
-
|
|
818
|
-
|
|
817
|
+
const tmpDir = __WEBPACK_EXTERNAL_MODULE_node_os__["default"].tmpdir();
|
|
818
|
+
tmpDir && ret.push({
|
|
819
|
+
match: tmpDir,
|
|
819
820
|
mark: 'temp'
|
|
820
821
|
});
|
|
821
|
-
ret
|
|
822
|
-
|
|
822
|
+
return ret;
|
|
823
|
+
};
|
|
824
|
+
const createHomeDirMatchers = ()=>{
|
|
825
|
+
const ret = [];
|
|
826
|
+
const homedir = __WEBPACK_EXTERNAL_MODULE_node_os__["default"].homedir();
|
|
827
|
+
homedir && ret.push({
|
|
828
|
+
match: homedir,
|
|
823
829
|
mark: 'home'
|
|
824
830
|
});
|
|
825
831
|
return ret;
|
|
@@ -841,21 +847,22 @@ function transformCodeToPosixPath(code) {
|
|
|
841
847
|
/(?<![a-zA-Z])([a-zA-Z]:[\\/])([-\u4e00-\u9fa5\w\s.()~!@#$%^&()\[\]{}+=]+[\\/])*/g, (match, _diskName)=>normalizeToPosixPath(match));
|
|
842
848
|
}
|
|
843
849
|
function createSnapshotSerializer(options) {
|
|
844
|
-
const { root = process.cwd(), workspace =
|
|
845
|
-
const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, addDoubleQuotes = true, transformWin32Path = true, escapeDoubleQuotes = true, escapeEOL = true } = features;
|
|
850
|
+
const { root = process.cwd(), workspace = '', replace: customMatchers = [], replacePost: customPostMatchers = [], features = {} } = options || {};
|
|
851
|
+
const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, replaceHomeDir = true, addDoubleQuotes = true, transformWin32Path = true, escapeDoubleQuotes = true, escapeEOL = true } = features;
|
|
846
852
|
function createPathMatchers() {
|
|
847
853
|
const pathMatchers = [];
|
|
848
854
|
pathMatchers.push(...customMatchers);
|
|
849
|
-
if (
|
|
850
|
-
mark: 'root',
|
|
851
|
-
match: root
|
|
852
|
-
});
|
|
853
|
-
if (replaceWorkspace) pathMatchers.push({
|
|
855
|
+
if (replaceWorkspace && workspace) pathMatchers.push({
|
|
854
856
|
mark: 'workspace',
|
|
855
857
|
match: workspace
|
|
856
858
|
});
|
|
859
|
+
if (replaceRoot && root) pathMatchers.push({
|
|
860
|
+
mark: 'root',
|
|
861
|
+
match: root
|
|
862
|
+
});
|
|
857
863
|
if (replacePnpmInner) pathMatchers.push(...createPnpmInnerMatchers());
|
|
858
864
|
if (replaceTmpDir) pathMatchers.push(...createTmpDirMatchers());
|
|
865
|
+
if (replaceHomeDir) pathMatchers.push(...createHomeDirMatchers());
|
|
859
866
|
pathMatchers.push(...customPostMatchers);
|
|
860
867
|
return pathMatchers;
|
|
861
868
|
}
|
|
@@ -871,7 +878,7 @@ function createSnapshotSerializer(options) {
|
|
|
871
878
|
if (transformWin32Path) replaced = transformCodeToPosixPath(replaced);
|
|
872
879
|
replaced = applyMatcherReplacement(pathMatchers, replaced);
|
|
873
880
|
if (escapeDoubleQuotes) replaced = replaced.replace(/"/g, '\\"');
|
|
874
|
-
if (escapeEOL) replaced = replaced.replace(/\\r\\n/g, '
|
|
881
|
+
if (escapeEOL) replaced = replaced.replace(/\\r\\n/g, '\\n');
|
|
875
882
|
if (addDoubleQuotes) replaced = `"${replaced}"`;
|
|
876
883
|
return replaced;
|
|
877
884
|
}
|