path-serializer 0.2.0 → 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)
@@ -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 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,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 = process.cwd(), replace: customMatchers = [], replacePost: customPostMatchers = [], features = {} } = options || {};
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 (replaceRoot) pathMatchers.push({
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
  }
@@ -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
  }
@@ -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,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 = process.cwd(), replace: customMatchers = [], replacePost: customPostMatchers = [], features = {} } = options || {};
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 (replaceRoot) pathMatchers.push({
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "path-serializer",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "path-serializer",
5
5
  "keywords": [
6
6
  "snapshot",