path-serializer 0.1.4 → 0.2.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # path-serializer
2
2
 
3
- <a href="https://www.npmjs.com/package/path-serializer"><img src="https://img.shields.io/npm/v/path-serializer?style=flat-square&color=98c379" alt="latest version" /></a>
3
+ [![npm latest version](https://img.shields.io/npm/v/path-serializer?style=flat-square&color=98c379)](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)
@@ -20,7 +20,7 @@ declare interface Features {
20
20
  /**
21
21
  * @default true
22
22
  */
23
- ansiDoubleQuotes?: boolean;
23
+ replaceHomeDir?: boolean;
24
24
  /**
25
25
  * @default true
26
26
  */
@@ -29,6 +29,14 @@ declare interface Features {
29
29
  * @default true
30
30
  */
31
31
  transformWin32Path?: boolean;
32
+ /**
33
+ * @default true
34
+ */
35
+ escapeDoubleQuotes?: boolean;
36
+ /**
37
+ * @default true
38
+ */
39
+ escapeEOL?: boolean;
32
40
  }
33
41
 
34
42
  declare interface PathMatcher {
@@ -44,9 +52,26 @@ declare interface SnapshotSerializer {
44
52
  }
45
53
 
46
54
  declare interface SnapshotSerializerOptions {
47
- cwd?: string;
55
+ /**
56
+ * repository root path
57
+ * @example '/Users/foo/codes/rsbuild/node_modules/.pnpm' -> '<ROOT>/node_modules/.pnpm'
58
+ * @default process.cwd()
59
+ */
60
+ root?: string;
61
+ /**
62
+ * workspace root path
63
+ * @example '/Users/foo/codes/rsbuild/packages/core/src' -> '<WORKSPACE>/src'
64
+ * @default ''
65
+ */
48
66
  workspace?: string;
67
+ /**
68
+ * @description replace -> workspace root pnpmInner temp home -> replacePost
69
+ */
49
70
  replace?: PathMatcher[];
71
+ /**
72
+ * @description replace -> workspace root pnpmInner temp home -> replacePost
73
+ */
74
+ replacePost?: PathMatcher[];
50
75
  features?: Features;
51
76
  }
52
77
 
@@ -425,8 +425,8 @@ function __webpack_require__(moduleId) {
425
425
  /** Detect free variable `global` from Node.js. */ var freeGlobal = 'object' == typeof global && global && global.Object === Object && global;
426
426
  /* harmony default export */ const _freeGlobal = freeGlobal;
427
427
  /** Detect free variable `self`. */ var freeSelf = 'object' == typeof self && self && self.Object === Object && self;
428
- /** Used as a reference to the global object. */ var root = _freeGlobal || freeSelf || Function('return this')();
429
- /* harmony default export */ const _root = root;
428
+ /** Used as a reference to the global object. */ var _root_root = _freeGlobal || freeSelf || Function('return this')();
429
+ /* harmony default export */ const _root = _root_root;
430
430
  /** Built-in value references. */ var Symbol1 = _root.Symbol;
431
431
  /* harmony default export */ const _Symbol = Symbol1;
432
432
  /**
@@ -834,17 +834,23 @@ function __webpack_require__(moduleId) {
834
834
  }
835
835
  const createTmpDirMatchers = ()=>{
836
836
  const ret = [];
837
- const tmpdir = getRealTemporaryDirectory();
838
- tmpdir && ret.push({
839
- match: tmpdir,
837
+ const realTmpDir = getRealTemporaryDirectory();
838
+ realTmpDir && ret.push({
839
+ match: realTmpDir,
840
840
  mark: 'temp'
841
841
  });
842
- ret.push({
843
- match: external_node_os_default().tmpdir(),
842
+ const tmpDir = external_node_os_default().tmpdir();
843
+ tmpDir && ret.push({
844
+ match: tmpDir,
844
845
  mark: 'temp'
845
846
  });
846
- ret.push({
847
- match: external_node_os_default().homedir(),
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,23 @@ 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 { cwd = process.cwd(), workspace = process.cwd(), replace: customMatchers = [], features = {} } = options || {};
870
- const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, ansiDoubleQuotes = true, addDoubleQuotes = true, transformWin32Path = 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
- if (replaceRoot) pathMatchers.push({
874
- mark: 'root',
875
- match: cwd
876
- });
877
- if (replaceWorkspace) pathMatchers.push({
879
+ pathMatchers.push(...customMatchers);
880
+ if (replaceWorkspace && workspace) pathMatchers.push({
878
881
  mark: 'workspace',
879
882
  match: workspace
880
883
  });
884
+ if (replaceRoot && root) pathMatchers.push({
885
+ mark: 'root',
886
+ match: root
887
+ });
881
888
  if (replacePnpmInner) pathMatchers.push(...createPnpmInnerMatchers());
882
- pathMatchers.push(...customMatchers);
883
889
  if (replaceTmpDir) pathMatchers.push(...createTmpDirMatchers());
890
+ if (replaceHomeDir) pathMatchers.push(...createHomeDirMatchers());
891
+ pathMatchers.push(...customPostMatchers);
884
892
  return pathMatchers;
885
893
  }
886
894
  const pathMatchers = createPathMatchers();
@@ -894,7 +902,8 @@ function __webpack_require__(moduleId) {
894
902
  let replaced = val;
895
903
  if (transformWin32Path) replaced = transformCodeToPosixPath(replaced);
896
904
  replaced = applyMatcherReplacement(pathMatchers, replaced);
897
- if (ansiDoubleQuotes) replaced = replaced.replace(/"/g, '\\"');
905
+ if (escapeDoubleQuotes) replaced = replaced.replace(/"/g, '\\"');
906
+ if (escapeEOL) replaced = replaced.replace(/\\r\\n/g, '\n');
898
907
  if (addDoubleQuotes) replaced = `"${replaced}"`;
899
908
  return replaced;
900
909
  }
@@ -20,7 +20,7 @@ declare interface Features {
20
20
  /**
21
21
  * @default true
22
22
  */
23
- ansiDoubleQuotes?: boolean;
23
+ replaceHomeDir?: boolean;
24
24
  /**
25
25
  * @default true
26
26
  */
@@ -29,6 +29,14 @@ declare interface Features {
29
29
  * @default true
30
30
  */
31
31
  transformWin32Path?: boolean;
32
+ /**
33
+ * @default true
34
+ */
35
+ escapeDoubleQuotes?: boolean;
36
+ /**
37
+ * @default true
38
+ */
39
+ escapeEOL?: boolean;
32
40
  }
33
41
 
34
42
  declare interface PathMatcher {
@@ -44,9 +52,26 @@ declare interface SnapshotSerializer {
44
52
  }
45
53
 
46
54
  declare interface SnapshotSerializerOptions {
47
- cwd?: string;
55
+ /**
56
+ * repository root path
57
+ * @example '/Users/foo/codes/rsbuild/node_modules/.pnpm' -> '<ROOT>/node_modules/.pnpm'
58
+ * @default process.cwd()
59
+ */
60
+ root?: string;
61
+ /**
62
+ * workspace root path
63
+ * @example '/Users/foo/codes/rsbuild/packages/core/src' -> '<WORKSPACE>/src'
64
+ * @default ''
65
+ */
48
66
  workspace?: string;
67
+ /**
68
+ * @description replace -> workspace root pnpmInner temp home -> replacePost
69
+ */
49
70
  replace?: PathMatcher[];
71
+ /**
72
+ * @description replace -> workspace root pnpmInner temp home -> replacePost
73
+ */
74
+ replacePost?: PathMatcher[];
50
75
  features?: Features;
51
76
  }
52
77
 
@@ -406,8 +406,8 @@ function __webpack_require__(moduleId) {
406
406
  /** Detect free variable `global` from Node.js. */ var freeGlobal = 'object' == typeof global && global && global.Object === Object && global;
407
407
  /* harmony default export */ const _freeGlobal = freeGlobal;
408
408
  /** Detect free variable `self`. */ var freeSelf = 'object' == typeof self && self && self.Object === Object && self;
409
- /** Used as a reference to the global object. */ var root = _freeGlobal || freeSelf || Function('return this')();
410
- /* harmony default export */ const _root = root;
409
+ /** Used as a reference to the global object. */ var _root_root = _freeGlobal || freeSelf || Function('return this')();
410
+ /* harmony default export */ const _root = _root_root;
411
411
  /** Built-in value references. */ var Symbol = _root.Symbol;
412
412
  /* harmony default export */ const _Symbol = Symbol;
413
413
  /**
@@ -809,17 +809,23 @@ function applyMatcherReplacement(matchers, str, options = {}) {
809
809
  }
810
810
  const createTmpDirMatchers = ()=>{
811
811
  const ret = [];
812
- const tmpdir = getRealTemporaryDirectory();
813
- tmpdir && ret.push({
814
- match: tmpdir,
812
+ const realTmpDir = getRealTemporaryDirectory();
813
+ realTmpDir && ret.push({
814
+ match: realTmpDir,
815
815
  mark: 'temp'
816
816
  });
817
- ret.push({
818
- match: __WEBPACK_EXTERNAL_MODULE_node_os__["default"].tmpdir(),
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.push({
822
- match: __WEBPACK_EXTERNAL_MODULE_node_os__["default"].homedir(),
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,23 @@ 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 { cwd = process.cwd(), workspace = process.cwd(), replace: customMatchers = [], features = {} } = options || {};
845
- const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, ansiDoubleQuotes = true, addDoubleQuotes = true, transformWin32Path = 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
- if (replaceRoot) pathMatchers.push({
849
- mark: 'root',
850
- match: cwd
851
- });
852
- if (replaceWorkspace) pathMatchers.push({
854
+ pathMatchers.push(...customMatchers);
855
+ if (replaceWorkspace && workspace) pathMatchers.push({
853
856
  mark: 'workspace',
854
857
  match: workspace
855
858
  });
859
+ if (replaceRoot && root) pathMatchers.push({
860
+ mark: 'root',
861
+ match: root
862
+ });
856
863
  if (replacePnpmInner) pathMatchers.push(...createPnpmInnerMatchers());
857
- pathMatchers.push(...customMatchers);
858
864
  if (replaceTmpDir) pathMatchers.push(...createTmpDirMatchers());
865
+ if (replaceHomeDir) pathMatchers.push(...createHomeDirMatchers());
866
+ pathMatchers.push(...customPostMatchers);
859
867
  return pathMatchers;
860
868
  }
861
869
  const pathMatchers = createPathMatchers();
@@ -869,7 +877,8 @@ function createSnapshotSerializer(options) {
869
877
  let replaced = val;
870
878
  if (transformWin32Path) replaced = transformCodeToPosixPath(replaced);
871
879
  replaced = applyMatcherReplacement(pathMatchers, replaced);
872
- if (ansiDoubleQuotes) replaced = replaced.replace(/"/g, '\\"');
880
+ if (escapeDoubleQuotes) replaced = replaced.replace(/"/g, '\\"');
881
+ if (escapeEOL) replaced = replaced.replace(/\\r\\n/g, '\n');
873
882
  if (addDoubleQuotes) replaced = `"${replaced}"`;
874
883
  return replaced;
875
884
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "path-serializer",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
4
4
  "description": "path-serializer",
5
5
  "keywords": [
6
6
  "snapshot",
@@ -9,7 +9,6 @@
9
9
  "ci",
10
10
  "test"
11
11
  ],
12
- "type": "module",
13
12
  "repository": {
14
13
  "type": "git",
15
14
  "url": "https://github.com/rspack-contrib/path-serializer.git"
@@ -19,13 +18,13 @@
19
18
  "exports": {
20
19
  ".": {
21
20
  "types": "./dist/esm/index.d.ts",
22
- "import": "./dist/esm/index.js",
23
- "require": "./dist/cjs/index.cjs"
21
+ "import": "./dist/esm/index.mjs",
22
+ "require": "./dist/cjs/index.js"
24
23
  },
25
24
  "./package.json": "./package.json"
26
25
  },
27
- "main": "./dist/cjs/index.cjs",
28
- "module": "./dist/esm/index.js",
26
+ "main": "./dist/cjs/index.js",
27
+ "module": "./dist/esm/index.mjs",
29
28
  "types": "./dist/esm/index.d.ts",
30
29
  "files": [
31
30
  "dist",