@lipemat/js-boilerplate-shared 0.0.4 → 0.1.0

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 +1,6 @@
1
1
  "# js-boilerplate-shared"
2
+
3
+
4
+ ## Scripts
5
+
6
+ * `js-boilerplate-shared__fix-pnp`: Fixes PNP compatibility issues
package/bin/fix-pnp.ts ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * When using PNP loose mode, we get warnings for every module
5
+ * we access, not strictly declared.
6
+ *
7
+ * No built-in way in Yarn to disable the warnings.
8
+ * This script modifies to generate .pnp.js file to suppress
9
+ * all loose module warnings unless the environmental variable
10
+ * it set to display all warnings.
11
+ *
12
+ * @example
13
+ * ```json
14
+ * {
15
+ * "scripts": {
16
+ * "postinstall": "lipemat-js-boilerplate fix-pnp"
17
+ * }
18
+ * }
19
+ * ```
20
+ */
21
+
22
+ import fs from 'fs';
23
+
24
+ const PNP_FILES = [
25
+ './.pnp.js',
26
+ './.pnp.cjs',
27
+ './.pnp.mjs',
28
+ ];
29
+
30
+ PNP_FILES.forEach( PNP_FILE => {
31
+ if ( fs.existsSync( PNP_FILE ) ) {
32
+ fs.readFile( PNP_FILE, 'utf8', ( readError, data ) => {
33
+ if ( readError ) {
34
+ return console.error( readError );
35
+ }
36
+
37
+ const result = data.replace( /if \(reference != null\) {/, '// # Warnings suppressed via @lipemat/js-boilerplate/fix-pnp script. \n' +
38
+ 'if (! alwaysWarnOnFallback && reference != null) { \n' +
39
+ 'dependencyReference = reference; \n' +
40
+ '} else if (alwaysWarnOnFallback && reference != null) {' );
41
+
42
+ fs.writeFile( PNP_FILE, result, 'utf8', writeError => {
43
+ if ( writeError ) {
44
+ return console.error( writeError );
45
+ }
46
+ console.debug( `The ${PNP_FILE} file has been adjusted to no longer display warnings for loose modules.` );
47
+ } );
48
+ } );
49
+ }
50
+ } );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lipemat/js-boilerplate-shared",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
4
4
  "license": "MIT",
5
5
  "description": "Shared utilities for all @lipemat boilerplate packages",
6
6
  "engines": {
@@ -21,6 +21,9 @@
21
21
  "url": "https://github.com/lipemat/js-boilerplate-shared/issues"
22
22
  },
23
23
  "homepage": "https://github.com/lipemat/js-boilerplate-shared#readme",
24
+ "bin": {
25
+ "js-boilerplate-shared__fix-pnp": "bin/fix-pnp.ts"
26
+ },
24
27
  "scripts": {
25
28
  "build": "tsc --project ./bin",
26
29
  "lint": "eslint --fix --cache",
@@ -4,3 +4,91 @@
4
4
  * @example AtLeast<Post, 'author' | 'post_title'> - post_title and author will be required, the rest optional.
5
5
  */
6
6
  export type AtLeast<T, K extends keyof T> = Partial<T> & Required<Pick<T, K>>;
7
+
8
+ /**
9
+ * Same as `keyof` except only keys whose value of a specified
10
+ * type will be returned.
11
+ *
12
+ * @example ```ts
13
+ * type Post = {id: number, title: string, url: string}
14
+ * KeysOfType<Post, string> - 'id' will not be included because is not string = 'title'|'url'
15
+ * ```
16
+ */
17
+ export type KeysOfType<T, K> = { [I in keyof T]: T[I] extends K ? I : never }[keyof T]
18
+
19
+ /**
20
+ * Removes all the properties of type never, even the deeply nested ones.
21
+ *
22
+ * Used to provider different types based on context by assigning
23
+ * some properties to never.
24
+ *
25
+ * @notice Will not work properly of sub items are an `interface` they
26
+ * must be a `type`.
27
+ *
28
+ * @example OmitNever<{foo: number; bar: never}> - {foo: number}
29
+ */
30
+ export type OmitNever<T, Converted = {
31
+ [K in keyof T]:
32
+ // If the type of this property (excluding | undefined) is never set it to never.
33
+ Exclude<T[ K ], undefined> extends never ? never :
34
+ // If the type of this property is an object, send it through again.
35
+ // eslint-disable-next-line @typescript-eslint/no-restricted-types
36
+ T[ K ] extends Record<string, unknown> ? OmitNever<T[ K ]> : T[ K ];
37
+ }> = Pick<Converted, {
38
+ // List all properties which were not set to never above.
39
+ [K in keyof Converted]: Converted[ K ] extends never ? never : K;
40
+ }[ keyof Converted ]>;
41
+
42
+ /**
43
+ * Make specified properties on a type optional.
44
+ *
45
+ * @example `Optional<Post, 'post_title'|'author'>` - `post_title` and `author` will be optional.
46
+ */
47
+ export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
48
+
49
+ /**
50
+ * Make specified properties on a type required.
51
+ * Leave the rest of the properties as is.
52
+ *
53
+ * @example Require<UserUpdate, 'id'> - `id` will be required, the rest left as is.
54
+ */
55
+ export type Require<T, K extends keyof T> = T & Required<Pick<T, K>>;
56
+
57
+ /**
58
+ * Subtract one types properties from another.
59
+ *
60
+ * @example Subtract<Post, {id: number, post_title: string}> - id and post_title
61
+ * will be removed from the Post definition.
62
+ */
63
+ export type Subtract<T extends K, K> = Omit<T, keyof K>;
64
+
65
+ /**
66
+ * Exclude all top-level properties in an object that are of a type.
67
+ *
68
+ * @example ExcludeOfType<{y:'false':x:false}, boolean> = {y:'false'}
69
+ */
70
+ export type ExcludeOfType<T, Type> = Pick<T, {
71
+ [P in keyof T]: T[P] extends Type ? never : P
72
+ }[keyof T]>
73
+
74
+ /**
75
+ * Union from array elements.
76
+ *
77
+ * Useful for passing to `Pick`.
78
+ *
79
+ * @example UnionOfArray<['one', 2, 'three']> - `'one' | 2 | 'three'`.
80
+ */
81
+ // eslint-disable-next-line @typescript-eslint/no-restricted-types
82
+ export type UnionOfArray<A extends Readonly<unknown[]>> = A[number];
83
+
84
+ /**
85
+ * Combine two types, making all properties optional which
86
+ * do not intersect.
87
+ *
88
+ * @example OptionalNonIntersect<{foo: string, bar: number}, {foo: string, waz: boolean}> - `{foo: string, bar?: number, waz?: boolean}`.
89
+ */
90
+ export type OptionalNonIntersect<T, U> =
91
+ Pick<T, Extract<keyof T, keyof U>> &
92
+ {
93
+ [P in Exclude<keyof T | keyof U, Extract<keyof T, keyof U>>]?: P extends keyof T ? T[P] : P extends keyof U ? U[P] : never;
94
+ };