@modern-js/utils 1.19.0 → 1.20.1

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # @modern-js/utils
2
2
 
3
+ ## 1.20.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 49515c5: fix(utils): failed to call logger.success method
8
+
9
+ fix(utils): 修复调用 logger.success 出现异常的问题
10
+
11
+ ## 1.20.0
12
+
13
+ ### Patch Changes
14
+
15
+ - d5d570b: feat: optimize the logger of @modern-js/utils, remove builder logger
16
+
17
+ feat: 优化 @modern-js/utils 的 logger 格式, 移除 builder 内置的 logger
18
+
19
+ - 4ddc185: chore(builder): bump webpack to 5.74.0
20
+
21
+ chore(builder): 升级 webpack 到 5.74.0 版本
22
+
23
+ - df8ee7e: fix(utils): failed to resolve execa
24
+
25
+ fix(utils): 修复找不到 execa 模块的问题
26
+
27
+ - 8c05089: fix: support monorepo deploy in pnpm 7
28
+ fix: 修复 monorepo deploy 命令在 pnpm 7 下的问题
29
+
3
30
  ## 1.19.0
4
31
 
5
32
  ## 1.18.1
@@ -18,8 +45,8 @@
18
45
 
19
46
  ### Minor Changes
20
47
 
21
- - 5227370: feat: add webpack-builder plugin subresource-integrity
22
- feat: 新增 webpack-builder 插件 subresource-integrity
48
+ - 5227370: feat: add builder plugin subresource-integrity
49
+ feat: 新增 builder 插件 subresource-integrity
23
50
 
24
51
  ### Patch Changes
25
52
 
@@ -124,7 +151,7 @@
124
151
 
125
152
  ### Minor Changes
126
153
 
127
- - 7b9067f: add babel plugin for webpack-builder
154
+ - 7b9067f: add babel plugin for builder
128
155
 
129
156
  ### Patch Changes
130
157
 
package/dist/getPort.d.ts CHANGED
@@ -2,6 +2,10 @@
2
2
  * Get available free port.
3
3
  * @param port - Current port want to use.
4
4
  * @param tryLimits - Maximum number of retries.
5
+ * @param strictPort - Whether to throw an error when the port is occupied.
5
6
  * @returns Available port number.
6
7
  */
7
- export declare const getPort: (port: string | number, tryLimits?: number) => Promise<number>;
8
+ export declare const getPort: (port: string | number, { tryLimits, strictPort, }?: {
9
+ tryLimits?: number | undefined;
10
+ strictPort?: boolean | undefined;
11
+ }) => Promise<number>;
package/dist/getPort.js CHANGED
@@ -11,13 +11,17 @@ const logger_1 = require("./logger");
11
11
  * Get available free port.
12
12
  * @param port - Current port want to use.
13
13
  * @param tryLimits - Maximum number of retries.
14
+ * @param strictPort - Whether to throw an error when the port is occupied.
14
15
  * @returns Available port number.
15
16
  */
16
17
  /* eslint-disable no-param-reassign, @typescript-eslint/no-loop-func */
17
- const getPort = async (port, tryLimits = 20) => {
18
+ const getPort = async (port, { tryLimits = 20, strictPort = false, } = {}) => {
18
19
  if (typeof port === 'string') {
19
20
  port = parseInt(port, 10);
20
21
  }
22
+ if (strictPort) {
23
+ tryLimits = 1;
24
+ }
21
25
  const original = port;
22
26
  let found = false;
23
27
  let attempts = 0;
@@ -45,7 +49,12 @@ const getPort = async (port, tryLimits = 20) => {
45
49
  }
46
50
  }
47
51
  if (port !== original) {
48
- logger_1.logger.info(compiled_1.chalk.red(`Something is already running on port ${original}. ${compiled_1.chalk.yellow(`Use port ${port} instead.`)}`));
52
+ if (strictPort) {
53
+ throw new Error(`Port "${original}" is occupied, please choose another one.`);
54
+ }
55
+ else {
56
+ logger_1.logger.info(`Something is already running on port ${original}. ${compiled_1.chalk.yellow(`Use port ${port} instead.`)}`);
57
+ }
49
58
  }
50
59
  return port;
51
60
  };
package/dist/index.d.ts CHANGED
@@ -34,4 +34,4 @@ export * from './ssr';
34
34
  export * from './tryResolve';
35
35
  export * from './analyzeProject';
36
36
  export * from './chainId';
37
- export * from './reactVersion';
37
+ export * from './version';
package/dist/index.js CHANGED
@@ -50,4 +50,4 @@ __exportStar(require("./ssr"), exports);
50
50
  __exportStar(require("./tryResolve"), exports);
51
51
  __exportStar(require("./analyzeProject"), exports);
52
52
  __exportStar(require("./chainId"), exports);
53
- __exportStar(require("./reactVersion"), exports);
53
+ __exportStar(require("./version"), exports);
package/dist/logger.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Color } from '../compiled/chalk';
2
2
  declare type LogLevel = 'debug' | 'info' | 'warn' | 'error';
3
+ declare type LogMsg = number | string | Error | null;
3
4
  interface LoggerConfiguration {
4
5
  color?: typeof Color;
5
6
  label?: string;
@@ -7,7 +8,6 @@ interface LoggerConfiguration {
7
8
  }
8
9
  interface InstanceConfiguration {
9
10
  displayLabel?: boolean;
10
- underlineLabel?: boolean;
11
11
  uppercaseLabel?: boolean;
12
12
  }
13
13
  interface ConstructorOptions {
@@ -15,7 +15,7 @@ interface ConstructorOptions {
15
15
  level?: string;
16
16
  types?: Record<string, LoggerConfiguration>;
17
17
  }
18
- declare type LoggerFunction = (message?: number | string | Error, ...args: any[]) => void;
18
+ declare type LoggerFunction = (message?: LogMsg, ...args: any[]) => void;
19
19
  declare const LOG_TYPES: {
20
20
  error: {
21
21
  color: string;
@@ -27,6 +27,11 @@ declare const LOG_TYPES: {
27
27
  label: string;
28
28
  level: string;
29
29
  };
30
+ success: {
31
+ color: string;
32
+ label: string;
33
+ level: string;
34
+ };
30
35
  warn: {
31
36
  color: string;
32
37
  label: string;
@@ -42,20 +47,14 @@ declare const LOG_TYPES: {
42
47
  };
43
48
  };
44
49
  declare class Logger {
45
- private readonly logCount;
46
50
  private readonly level;
47
- private history;
48
51
  private readonly config;
49
52
  private readonly types;
50
53
  private readonly longestLabel;
51
54
  [key: string]: any;
52
55
  constructor(options?: ConstructorOptions);
53
- private retainLog;
54
56
  private _log;
55
57
  private getLongestLabel;
56
- private get longestUnderlinedLabel();
57
- getRetainedLogs(type: string): string[];
58
- clearRetainedLogs(type: string): void;
59
58
  }
60
59
  declare type LoggerInterface = {
61
60
  [key in keyof typeof LOG_TYPES]: LoggerFunction;
package/dist/logger.js CHANGED
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.logger = exports.Logger = void 0;
7
7
  const chalk_1 = __importDefault(require("../compiled/chalk"));
8
- const { grey, underline } = chalk_1.default;
9
8
  const LOG_LEVEL = {
10
9
  error: 0,
11
10
  warn: 1,
@@ -20,13 +19,18 @@ const LOG_TYPES = {
20
19
  level: 'error',
21
20
  },
22
21
  info: {
23
- color: 'blue',
22
+ color: 'cyan',
24
23
  label: 'info',
25
24
  level: 'info',
26
25
  },
26
+ success: {
27
+ color: 'green',
28
+ label: 'Success',
29
+ level: 'info',
30
+ },
27
31
  warn: {
28
32
  color: 'yellow',
29
- label: 'warning',
33
+ label: 'warn',
30
34
  level: 'warn',
31
35
  },
32
36
  debug: {
@@ -38,13 +42,10 @@ const LOG_TYPES = {
38
42
  };
39
43
  const DEFAULT_CONFIG = {
40
44
  displayLabel: true,
41
- underlineLabel: true,
42
45
  uppercaseLabel: false,
43
46
  };
44
47
  class Logger {
45
48
  constructor(options = {}) {
46
- this.logCount = 200;
47
- this.history = {};
48
49
  this.level = options.level || LOG_TYPES.log.level;
49
50
  this.config = { ...DEFAULT_CONFIG, ...(options.config || {}) };
50
51
  this.types = {
@@ -56,17 +57,8 @@ class Logger {
56
57
  this[type] = this._log.bind(this, type);
57
58
  });
58
59
  }
59
- retainLog(type, message) {
60
- if (!this.history[type]) {
61
- this.history[type] = [];
62
- }
63
- this.history[type].push(message);
64
- while (this.history[type].length > this.logCount) {
65
- this.history[type].shift();
66
- }
67
- }
68
60
  _log(type, message, ...args) {
69
- if (message === undefined) {
61
+ if (message === undefined || message === null) {
70
62
  // eslint-disable-next-line no-console
71
63
  console.log();
72
64
  return;
@@ -81,18 +73,13 @@ class Logger {
81
73
  label = this.config.uppercaseLabel
82
74
  ? logType.label.toUpperCase()
83
75
  : logType.label;
84
- if (this.config.underlineLabel) {
85
- label = underline(label).padEnd(this.longestUnderlinedLabel.length + 1);
86
- }
87
- else {
88
- label = label.padEnd(this.longestLabel.length + 1);
89
- }
90
- label = logType.color ? chalk_1.default[logType.color](label) : label;
76
+ label = label.padEnd(this.longestLabel.length);
77
+ label = chalk_1.default.bold(logType.color ? chalk_1.default[logType.color](label) : label);
91
78
  }
92
79
  if (message instanceof Error) {
93
80
  if (message.stack) {
94
81
  const [name, ...rest] = message.stack.split('\n');
95
- text = `${name}\n${grey(rest.join('\n'))}`;
82
+ text = `${name}\n${chalk_1.default.grey(rest.join('\n'))}`;
96
83
  }
97
84
  else {
98
85
  text = message.message;
@@ -101,12 +88,7 @@ class Logger {
101
88
  else {
102
89
  text = `${message}`;
103
90
  }
104
- // only retain logs of warn/error level
105
- if (logType.level === 'warn' || logType.level === 'error') {
106
- // retain log text without label
107
- this.retainLog(type, text);
108
- }
109
- const log = label.length > 0 ? `${label} ${text}` : text;
91
+ const log = label.length > 0 ? `${label} ${text}` : text;
110
92
  // eslint-disable-next-line no-console
111
93
  console.log(log, ...args);
112
94
  }
@@ -120,22 +102,6 @@ class Logger {
120
102
  });
121
103
  return longestLabel;
122
104
  }
123
- get longestUnderlinedLabel() {
124
- return underline(this.longestLabel);
125
- }
126
- getRetainedLogs(type) {
127
- return this.history[type] || [];
128
- }
129
- clearRetainedLogs(type) {
130
- if (type) {
131
- if (this.history[type]) {
132
- this.history[type] = [];
133
- }
134
- }
135
- else {
136
- this.history = {};
137
- }
138
- }
139
105
  }
140
106
  exports.Logger = Logger;
141
107
  const logger = new Logger();
@@ -0,0 +1,2 @@
1
+ export declare function getPnpmVersion(): Promise<string>;
2
+ export declare const isReact18: (cwd: string) => boolean;
@@ -3,9 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.isReact18 = void 0;
6
+ exports.isReact18 = exports.getPnpmVersion = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const compiled_1 = require("./compiled");
9
+ async function getPnpmVersion() {
10
+ const { stdout } = await (0, compiled_1.execa)('pnpm', ['--version']);
11
+ return stdout;
12
+ }
13
+ exports.getPnpmVersion = getPnpmVersion;
9
14
  const isReact18 = (cwd) => {
10
15
  const pkgPath = path_1.default.join(cwd, 'package.json');
11
16
  if (!compiled_1.fs.existsSync(pkgPath)) {
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.19.0",
14
+ "version": "1.20.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/index.d.ts",
17
17
  "main": "./dist/index.js",
@@ -20,9 +20,10 @@
20
20
  "_comment": "Provide ESM and CJS exports, ESM is used by runtime package, for treeshaking",
21
21
  "exports": {
22
22
  ".": "./dist/index.js",
23
+ "./ssr": "./dist/ssr.js",
23
24
  "./format": "./dist/format.js",
25
+ "./logger": "./dist/logger.js",
24
26
  "./constants": "./dist/constants.js",
25
- "./ssr": "./dist/ssr.js",
26
27
  "./ajv": "./compiled/ajv/index.js",
27
28
  "./glob": "./compiled/glob/index.js",
28
29
  "./chalk": "./compiled/chalk/index.js",
@@ -49,15 +50,18 @@
49
50
  },
50
51
  "typesVersions": {
51
52
  "*": {
53
+ "ssr": [
54
+ "./dist/ssr.d.ts"
55
+ ],
52
56
  "format": [
53
57
  "./dist/format.d.ts"
54
58
  ],
59
+ "logger": [
60
+ "./dist/logger.d.ts"
61
+ ],
55
62
  "constants": [
56
63
  "./dist/constants.d.ts"
57
64
  ],
58
- "ssr": [
59
- "./dist/ssr.d.ts"
60
- ],
61
65
  "ajv": [
62
66
  "./compiled/ajv/types/ajv.d.ts"
63
67
  ],
@@ -125,14 +129,14 @@
125
129
  "lodash": "^4.17.21"
126
130
  },
127
131
  "devDependencies": {
128
- "@modern-js/types": "1.19.0",
129
- "@scripts/build": "1.19.0",
130
- "@scripts/jest-config": "1.19.0",
132
+ "@modern-js/types": "1.20.1",
133
+ "@scripts/build": "1.20.1",
134
+ "@scripts/jest-config": "1.20.1",
131
135
  "@types/jest": "^27",
132
136
  "@types/node": "^14",
133
137
  "typescript": "^4",
134
138
  "jest": "^27",
135
- "webpack": "^5.71.0"
139
+ "webpack": "^5.74.0"
136
140
  },
137
141
  "sideEffects": false,
138
142
  "wireit": {
@@ -1 +0,0 @@
1
- export declare const isReact18: (cwd: string) => boolean;