@modern-js/utils 1.1.4-rc.0 → 1.1.6-rc.0

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,18 @@
1
1
  # @modern-js/utils
2
2
 
3
- ## 1.1.4-rc.0
3
+ ## 1.1.6-rc.0
4
+
5
+ ### Patch Changes
6
+
7
+ - b7fb82ec: fix: get package manager function
8
+
9
+ ## 1.1.5
10
+
11
+ ### Patch Changes
12
+
13
+ - ca7dcb32: change watch logic
14
+
15
+ ## 1.1.4
4
16
 
5
17
  ### Patch Changes
6
18
 
@@ -1,10 +1,15 @@
1
1
  import os from 'os';
2
2
  import path from 'path';
3
3
  import fs from 'fs-extra';
4
- export function getPackageManager(cwd = process.cwd()) {
4
+ import { canUsePnpm, canUseYarn } from "./nodeEnv";
5
+ const MAX_TIMES = 5;
6
+ export async function getPackageManager(cwd = process.cwd()) {
5
7
  let appDirectory = cwd;
8
+ let times = 0;
9
+
10
+ while (os.homedir() !== appDirectory && times < MAX_TIMES) {
11
+ times++;
6
12
 
7
- while (os.homedir() !== appDirectory) {
8
13
  if (fs.existsSync(path.resolve(appDirectory, 'pnpm-lock.yaml'))) {
9
14
  return 'pnpm';
10
15
  }
@@ -20,5 +25,13 @@ export function getPackageManager(cwd = process.cwd()) {
20
25
  appDirectory = path.join(appDirectory, '..');
21
26
  }
22
27
 
28
+ if (await canUsePnpm()) {
29
+ return 'pnpm';
30
+ }
31
+
32
+ if (await canUseYarn()) {
33
+ return 'yarn';
34
+ }
35
+
23
36
  return 'npm';
24
37
  }
@@ -28,4 +28,5 @@ export * from "./generateMetaTags";
28
28
  export * from "./prettyInstructions";
29
29
  export * from "./alias";
30
30
  export * from "./import";
31
- export * from "./watch";
31
+ export * from "./watch";
32
+ export * from "./nodeEnv";
@@ -0,0 +1,31 @@
1
+ import execa from 'execa';
2
+ export async function canUseNpm() {
3
+ try {
4
+ await execa('npm', ['--version'], {
5
+ env: process.env
6
+ });
7
+ return true;
8
+ } catch (e) {
9
+ return false;
10
+ }
11
+ }
12
+ export async function canUseYarn() {
13
+ try {
14
+ await execa('yarn', ['--version'], {
15
+ env: process.env
16
+ });
17
+ return true;
18
+ } catch (e) {
19
+ return false;
20
+ }
21
+ }
22
+ export async function canUsePnpm() {
23
+ try {
24
+ await execa('pnpm', ['--version'], {
25
+ env: process.env
26
+ });
27
+ return true;
28
+ } catch (e) {
29
+ return false;
30
+ }
31
+ }
@@ -8,7 +8,7 @@ export const WatchChangeType = {
8
8
  };
9
9
  export const watch = (watchDir, runTask, ignored = []) => {
10
10
  let ready = false;
11
- const watcher = chokidar.watch(`${watchDir}/**/*.{js,jsx,ts,tsx}`, {
11
+ const watcher = chokidar.watch(watchDir, {
12
12
  ignored
13
13
  });
14
14
  watcher.on('ready', () => ready = true);
@@ -11,12 +11,19 @@ var _path = _interopRequireDefault(require("path"));
11
11
 
12
12
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
13
13
 
14
+ var _nodeEnv = require("./nodeEnv");
15
+
14
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
17
 
16
- function getPackageManager(cwd = process.cwd()) {
18
+ const MAX_TIMES = 5;
19
+
20
+ async function getPackageManager(cwd = process.cwd()) {
17
21
  let appDirectory = cwd;
22
+ let times = 0;
23
+
24
+ while (_os.default.homedir() !== appDirectory && times < MAX_TIMES) {
25
+ times++;
18
26
 
19
- while (_os.default.homedir() !== appDirectory) {
20
27
  if (_fsExtra.default.existsSync(_path.default.resolve(appDirectory, 'pnpm-lock.yaml'))) {
21
28
  return 'pnpm';
22
29
  }
@@ -32,5 +39,13 @@ function getPackageManager(cwd = process.cwd()) {
32
39
  appDirectory = _path.default.join(appDirectory, '..');
33
40
  }
34
41
 
42
+ if (await (0, _nodeEnv.canUsePnpm)()) {
43
+ return 'pnpm';
44
+ }
45
+
46
+ if (await (0, _nodeEnv.canUseYarn)()) {
47
+ return 'yarn';
48
+ }
49
+
35
50
  return 'npm';
36
51
  }
@@ -418,6 +418,20 @@ Object.keys(_watch).forEach(function (key) {
418
418
  });
419
419
  });
420
420
 
421
+ var _nodeEnv = require("./nodeEnv");
422
+
423
+ Object.keys(_nodeEnv).forEach(function (key) {
424
+ if (key === "default" || key === "__esModule") return;
425
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
426
+ if (key in exports && exports[key] === _nodeEnv[key]) return;
427
+ Object.defineProperty(exports, key, {
428
+ enumerable: true,
429
+ get: function () {
430
+ return _nodeEnv[key];
431
+ }
432
+ });
433
+ });
434
+
421
435
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
422
436
 
423
437
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.canUseNpm = canUseNpm;
7
+ exports.canUsePnpm = canUsePnpm;
8
+ exports.canUseYarn = canUseYarn;
9
+
10
+ var _execa = _interopRequireDefault(require("execa"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ async function canUseNpm() {
15
+ try {
16
+ await (0, _execa.default)('npm', ['--version'], {
17
+ env: process.env
18
+ });
19
+ return true;
20
+ } catch (e) {
21
+ return false;
22
+ }
23
+ }
24
+
25
+ async function canUseYarn() {
26
+ try {
27
+ await (0, _execa.default)('yarn', ['--version'], {
28
+ env: process.env
29
+ });
30
+ return true;
31
+ } catch (e) {
32
+ return false;
33
+ }
34
+ }
35
+
36
+ async function canUsePnpm() {
37
+ try {
38
+ await (0, _execa.default)('pnpm', ['--version'], {
39
+ env: process.env
40
+ });
41
+ return true;
42
+ } catch (e) {
43
+ return false;
44
+ }
45
+ }
@@ -24,7 +24,7 @@ exports.WatchChangeType = WatchChangeType;
24
24
 
25
25
  const watch = (watchDir, runTask, ignored = []) => {
26
26
  let ready = false;
27
- const watcher = chokidar.watch(`${watchDir}/**/*.{js,jsx,ts,tsx}`, {
27
+ const watcher = chokidar.watch(watchDir, {
28
28
  ignored
29
29
  });
30
30
  watcher.on('ready', () => ready = true);
@@ -1,25 +1,101 @@
1
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
+
3
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
+
5
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
6
+
1
7
  import os from 'os';
2
8
  import path from 'path';
3
9
  import fs from 'fs-extra';
10
+ import { canUsePnpm, canUseYarn } from "./nodeEnv";
11
+ var MAX_TIMES = 5;
4
12
  export function getPackageManager() {
5
- var cwd = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : process.cwd();
6
- var appDirectory = cwd;
13
+ return _getPackageManager.apply(this, arguments);
14
+ }
15
+
16
+ function _getPackageManager() {
17
+ _getPackageManager = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
18
+ var cwd,
19
+ appDirectory,
20
+ times,
21
+ _args = arguments;
22
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
23
+ while (1) {
24
+ switch (_context.prev = _context.next) {
25
+ case 0:
26
+ cwd = _args.length > 0 && _args[0] !== undefined ? _args[0] : process.cwd();
27
+ appDirectory = cwd;
28
+ times = 0;
29
+
30
+ case 3:
31
+ if (!(os.homedir() !== appDirectory && times < MAX_TIMES)) {
32
+ _context.next = 14;
33
+ break;
34
+ }
35
+
36
+ times++;
37
+
38
+ if (!fs.existsSync(path.resolve(appDirectory, 'pnpm-lock.yaml'))) {
39
+ _context.next = 7;
40
+ break;
41
+ }
42
+
43
+ return _context.abrupt("return", 'pnpm');
44
+
45
+ case 7:
46
+ if (!fs.existsSync(path.resolve(appDirectory, 'yarn.lock'))) {
47
+ _context.next = 9;
48
+ break;
49
+ }
50
+
51
+ return _context.abrupt("return", 'yarn');
52
+
53
+ case 9:
54
+ if (!fs.existsSync(path.resolve(appDirectory, 'package-lock.json'))) {
55
+ _context.next = 11;
56
+ break;
57
+ }
58
+
59
+ return _context.abrupt("return", 'npm');
60
+
61
+ case 11:
62
+ appDirectory = path.join(appDirectory, '..');
63
+ _context.next = 3;
64
+ break;
65
+
66
+ case 14:
67
+ _context.next = 16;
68
+ return canUsePnpm();
69
+
70
+ case 16:
71
+ if (!_context.sent) {
72
+ _context.next = 18;
73
+ break;
74
+ }
75
+
76
+ return _context.abrupt("return", 'pnpm');
7
77
 
8
- while (os.homedir() !== appDirectory) {
9
- if (fs.existsSync(path.resolve(appDirectory, 'pnpm-lock.yaml'))) {
10
- return 'pnpm';
11
- }
78
+ case 18:
79
+ _context.next = 20;
80
+ return canUseYarn();
12
81
 
13
- if (fs.existsSync(path.resolve(appDirectory, 'yarn.lock'))) {
14
- return 'yarn';
15
- }
82
+ case 20:
83
+ if (!_context.sent) {
84
+ _context.next = 22;
85
+ break;
86
+ }
16
87
 
17
- if (fs.existsSync(path.resolve(appDirectory, 'package-lock.json'))) {
18
- return 'npm';
19
- }
88
+ return _context.abrupt("return", 'yarn');
20
89
 
21
- appDirectory = path.join(appDirectory, '..');
22
- }
90
+ case 22:
91
+ return _context.abrupt("return", 'npm');
23
92
 
24
- return 'npm';
93
+ case 23:
94
+ case "end":
95
+ return _context.stop();
96
+ }
97
+ }
98
+ }, _callee);
99
+ }));
100
+ return _getPackageManager.apply(this, arguments);
25
101
  }
@@ -28,4 +28,5 @@ export * from "./generateMetaTags";
28
28
  export * from "./prettyInstructions";
29
29
  export * from "./alias";
30
30
  export * from "./import";
31
- export * from "./watch";
31
+ export * from "./watch";
32
+ export * from "./nodeEnv";
@@ -0,0 +1,108 @@
1
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
+
3
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
+
5
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
6
+
7
+ import execa from 'execa';
8
+ export function canUseNpm() {
9
+ return _canUseNpm.apply(this, arguments);
10
+ }
11
+
12
+ function _canUseNpm() {
13
+ _canUseNpm = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
14
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
15
+ while (1) {
16
+ switch (_context.prev = _context.next) {
17
+ case 0:
18
+ _context.prev = 0;
19
+ _context.next = 3;
20
+ return execa('npm', ['--version'], {
21
+ env: process.env
22
+ });
23
+
24
+ case 3:
25
+ return _context.abrupt("return", true);
26
+
27
+ case 6:
28
+ _context.prev = 6;
29
+ _context.t0 = _context["catch"](0);
30
+ return _context.abrupt("return", false);
31
+
32
+ case 9:
33
+ case "end":
34
+ return _context.stop();
35
+ }
36
+ }
37
+ }, _callee, null, [[0, 6]]);
38
+ }));
39
+ return _canUseNpm.apply(this, arguments);
40
+ }
41
+
42
+ export function canUseYarn() {
43
+ return _canUseYarn.apply(this, arguments);
44
+ }
45
+
46
+ function _canUseYarn() {
47
+ _canUseYarn = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
48
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
49
+ while (1) {
50
+ switch (_context2.prev = _context2.next) {
51
+ case 0:
52
+ _context2.prev = 0;
53
+ _context2.next = 3;
54
+ return execa('yarn', ['--version'], {
55
+ env: process.env
56
+ });
57
+
58
+ case 3:
59
+ return _context2.abrupt("return", true);
60
+
61
+ case 6:
62
+ _context2.prev = 6;
63
+ _context2.t0 = _context2["catch"](0);
64
+ return _context2.abrupt("return", false);
65
+
66
+ case 9:
67
+ case "end":
68
+ return _context2.stop();
69
+ }
70
+ }
71
+ }, _callee2, null, [[0, 6]]);
72
+ }));
73
+ return _canUseYarn.apply(this, arguments);
74
+ }
75
+
76
+ export function canUsePnpm() {
77
+ return _canUsePnpm.apply(this, arguments);
78
+ }
79
+
80
+ function _canUsePnpm() {
81
+ _canUsePnpm = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
82
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
83
+ while (1) {
84
+ switch (_context3.prev = _context3.next) {
85
+ case 0:
86
+ _context3.prev = 0;
87
+ _context3.next = 3;
88
+ return execa('pnpm', ['--version'], {
89
+ env: process.env
90
+ });
91
+
92
+ case 3:
93
+ return _context3.abrupt("return", true);
94
+
95
+ case 6:
96
+ _context3.prev = 6;
97
+ _context3.t0 = _context3["catch"](0);
98
+ return _context3.abrupt("return", false);
99
+
100
+ case 9:
101
+ case "end":
102
+ return _context3.stop();
103
+ }
104
+ }
105
+ }, _callee3, null, [[0, 6]]);
106
+ }));
107
+ return _canUsePnpm.apply(this, arguments);
108
+ }
@@ -15,7 +15,7 @@ export var WatchChangeType = {
15
15
  export var watch = function watch(watchDir, runTask) {
16
16
  var ignored = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
17
17
  var ready = false;
18
- var watcher = chokidar.watch("".concat(watchDir, "/**/*.{js,jsx,ts,tsx}"), {
18
+ var watcher = chokidar.watch(watchDir, {
19
19
  ignored: ignored
20
20
  });
21
21
  watcher.on('ready', function () {
@@ -1 +1 @@
1
- export declare function getPackageManager(cwd?: string): "pnpm" | "yarn" | "npm";
1
+ export declare function getPackageManager(cwd?: string): Promise<"npm" | "yarn" | "pnpm">;
@@ -27,4 +27,5 @@ export * from './generateMetaTags';
27
27
  export * from './prettyInstructions';
28
28
  export * from './alias';
29
29
  export * from './import';
30
- export * from './watch';
30
+ export * from './watch';
31
+ export * from './nodeEnv';
@@ -0,0 +1,3 @@
1
+ export declare function canUseNpm(): Promise<boolean>;
2
+ export declare function canUseYarn(): Promise<boolean>;
3
+ export declare function canUsePnpm(): Promise<boolean>;
@@ -4,5 +4,5 @@ declare type RunTaskType = (option: {
4
4
  changedFilePath: string;
5
5
  changeType: WatchChangeTypeValueT;
6
6
  }) => void | Promise<void>;
7
- export declare const watch: (watchDir: string, runTask: RunTaskType, ignored?: string[]) => import("chokidar").FSWatcher;
7
+ export declare const watch: (watchDir: string | string[], runTask: RunTaskType, ignored?: string[]) => import("chokidar").FSWatcher;
8
8
  export {};
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.1.4-rc.0",
14
+ "version": "1.1.6-rc.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -54,7 +54,8 @@
54
54
  "recursive-readdir": "^2.2.2",
55
55
  "strip-ansi": "6.0.0",
56
56
  "upath": "^2.0.1",
57
- "yaml": "^1.10.2"
57
+ "yaml": "^1.10.2",
58
+ "execa": "5"
58
59
  },
59
60
  "devDependencies": {
60
61
  "@types/debug": "^4.1.7",
@@ -65,8 +66,8 @@
65
66
  "@types/recursive-readdir": "^2.2.0",
66
67
  "typescript": "^4",
67
68
  "webpack": "^5.54.0",
68
- "@modern-js/plugin-testing": "^1.1.1",
69
- "@modern-js/module-tools": "^1.1.1"
69
+ "@modern-js/plugin-testing": "^1.2.2",
70
+ "@modern-js/module-tools": "^1.1.4"
70
71
  },
71
72
  "peerDependencies": {
72
73
  "typescript": "^4.4.3"
@@ -1,10 +1,14 @@
1
1
  import os from 'os';
2
2
  import path from 'path';
3
3
  import fs from 'fs-extra';
4
+ import { canUsePnpm, canUseYarn } from './nodeEnv';
4
5
 
5
- export function getPackageManager(cwd: string = process.cwd()) {
6
+ const MAX_TIMES = 5;
7
+ export async function getPackageManager(cwd: string = process.cwd()) {
6
8
  let appDirectory = cwd;
7
- while (os.homedir() !== appDirectory) {
9
+ let times = 0;
10
+ while (os.homedir() !== appDirectory && times < MAX_TIMES) {
11
+ times++;
8
12
  if (fs.existsSync(path.resolve(appDirectory, 'pnpm-lock.yaml'))) {
9
13
  return 'pnpm';
10
14
  }
@@ -16,5 +20,11 @@ export function getPackageManager(cwd: string = process.cwd()) {
16
20
  }
17
21
  appDirectory = path.join(appDirectory, '..');
18
22
  }
23
+ if (await canUsePnpm()) {
24
+ return 'pnpm';
25
+ }
26
+ if (await canUseYarn()) {
27
+ return 'yarn';
28
+ }
19
29
  return 'npm';
20
30
  }
package/src/index.ts CHANGED
@@ -28,3 +28,4 @@ export * from './prettyInstructions';
28
28
  export * from './alias';
29
29
  export * from './import';
30
30
  export * from './watch';
31
+ export * from './nodeEnv';
package/src/nodeEnv.ts ADDED
@@ -0,0 +1,28 @@
1
+ import execa from 'execa';
2
+
3
+ export async function canUseNpm() {
4
+ try {
5
+ await execa('npm', ['--version'], { env: process.env });
6
+ return true;
7
+ } catch (e) {
8
+ return false;
9
+ }
10
+ }
11
+
12
+ export async function canUseYarn() {
13
+ try {
14
+ await execa('yarn', ['--version'], { env: process.env });
15
+ return true;
16
+ } catch (e) {
17
+ return false;
18
+ }
19
+ }
20
+
21
+ export async function canUsePnpm() {
22
+ try {
23
+ await execa('pnpm', ['--version'], { env: process.env });
24
+ return true;
25
+ } catch (e) {
26
+ return false;
27
+ }
28
+ }
package/src/watch.ts CHANGED
@@ -20,12 +20,12 @@ type RunTaskType = (option: {
20
20
  }) => void | Promise<void>;
21
21
 
22
22
  export const watch = (
23
- watchDir: string,
23
+ watchDir: string | string[],
24
24
  runTask: RunTaskType,
25
25
  ignored: string[] = [],
26
26
  ) => {
27
27
  let ready = false;
28
- const watcher = chokidar.watch(`${watchDir}/**/*.{js,jsx,ts,tsx}`, {
28
+ const watcher = chokidar.watch(watchDir, {
29
29
  ignored,
30
30
  });
31
31
 
@@ -0,0 +1,16 @@
1
+ import { canUseNpm, canUsePnpm, canUseYarn } from '@/index';
2
+
3
+ describe('test generator utils', () => {
4
+ test('test canUseNpm', async () => {
5
+ const npmAbility = await canUseNpm();
6
+ expect(typeof npmAbility === 'boolean').toBe(true);
7
+ });
8
+ test('test canUsePnpm', async () => {
9
+ const pnpmAbility = await canUsePnpm();
10
+ expect(typeof pnpmAbility === 'boolean').toBe(true);
11
+ });
12
+ test('test canUseYarn', async () => {
13
+ const yarnAbility = await canUseYarn();
14
+ expect(typeof yarnAbility === 'boolean').toBe(true);
15
+ });
16
+ });