@atlaspack/package-manager 2.14.5-canary.16 → 2.14.5-canary.160

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.
Files changed (49) hide show
  1. package/CHANGELOG.md +228 -0
  2. package/lib/JSONParseStream.js +52 -0
  3. package/lib/MockPackageInstaller.js +79 -0
  4. package/lib/NodePackageManager.js +602 -0
  5. package/lib/Npm.js +106 -0
  6. package/lib/Pnpm.js +185 -0
  7. package/lib/Yarn.js +160 -0
  8. package/lib/getCurrentPackageManager.js +20 -0
  9. package/lib/index.js +64 -5211
  10. package/lib/installPackage.js +223 -0
  11. package/lib/nodejsConditions.js +41 -0
  12. package/lib/promiseFromProcess.js +20 -0
  13. package/lib/types/JSONParseStream.d.ts +6 -0
  14. package/lib/types/MockPackageInstaller.d.ts +14 -0
  15. package/lib/types/NodePackageManager.d.ts +36 -0
  16. package/lib/types/Npm.d.ts +4 -0
  17. package/lib/types/Pnpm.d.ts +5 -0
  18. package/lib/types/Yarn.d.ts +5 -0
  19. package/lib/types/getCurrentPackageManager.d.ts +4 -0
  20. package/lib/types/index.d.ts +10 -0
  21. package/lib/types/installPackage.d.ts +5 -0
  22. package/lib/types/nodejsConditions.d.ts +3 -0
  23. package/lib/types/promiseFromProcess.d.ts +2 -0
  24. package/lib/types/utils.d.ts +15 -0
  25. package/lib/types/validateModuleSpecifier.d.ts +1 -0
  26. package/lib/utils.js +101 -0
  27. package/lib/validateModuleSpecifier.js +14 -0
  28. package/package.json +17 -19
  29. package/src/{JSONParseStream.js → JSONParseStream.ts} +8 -7
  30. package/src/{MockPackageInstaller.js → MockPackageInstaller.ts} +4 -6
  31. package/src/{NodePackageManager.js → NodePackageManager.ts} +88 -57
  32. package/src/{Npm.js → Npm.ts} +9 -9
  33. package/src/{Pnpm.js → Pnpm.ts} +68 -50
  34. package/src/{Yarn.js → Yarn.ts} +38 -25
  35. package/src/{getCurrentPackageManager.js → getCurrentPackageManager.ts} +9 -4
  36. package/src/{index.js → index.ts} +0 -2
  37. package/src/{installPackage.js → installPackage.ts} +5 -6
  38. package/src/{nodejsConditions.js → nodejsConditions.ts} +6 -3
  39. package/src/promiseFromProcess.ts +23 -0
  40. package/src/{utils.js → utils.ts} +21 -11
  41. package/src/{validateModuleSpecifier.js → validateModuleSpecifier.ts} +0 -2
  42. package/test/{NodePackageManager.test.js → NodePackageManager.test.ts} +19 -16
  43. package/test/{getCurrentPackageManager.test.js → getCurrentPackageManager.test.ts} +0 -1
  44. package/test/{validateModuleSpecifiers.test.js → validateModuleSpecifiers.test.ts} +2 -3
  45. package/tsconfig.json +4 -0
  46. package/index.d.ts +0 -40
  47. package/lib/index.d.ts +0 -10
  48. package/lib/index.js.map +0 -1
  49. package/src/promiseFromProcess.js +0 -19
package/lib/Pnpm.js ADDED
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Pnpm = void 0;
7
+ function _path() {
8
+ const data = _interopRequireDefault(require("path"));
9
+ _path = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _fs() {
15
+ const data = _interopRequireDefault(require("fs"));
16
+ _fs = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _commandExists() {
22
+ const data = _interopRequireDefault(require("command-exists"));
23
+ _commandExists = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _crossSpawn() {
29
+ const data = _interopRequireDefault(require("cross-spawn"));
30
+ _crossSpawn = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _buildCache() {
36
+ const data = require("@atlaspack/build-cache");
37
+ _buildCache = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ function _logger() {
43
+ const data = _interopRequireDefault(require("@atlaspack/logger"));
44
+ _logger = function () {
45
+ return data;
46
+ };
47
+ return data;
48
+ }
49
+ function _split() {
50
+ const data = _interopRequireDefault(require("split2"));
51
+ _split = function () {
52
+ return data;
53
+ };
54
+ return data;
55
+ }
56
+ var _JSONParseStream = _interopRequireDefault(require("./JSONParseStream"));
57
+ var _promiseFromProcess = _interopRequireDefault(require("./promiseFromProcess"));
58
+ var _utils = require("./utils");
59
+ var _package = _interopRequireDefault(require("../package.json"));
60
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
61
+ // @ts-expect-error TS7016
62
+
63
+ // @ts-expect-error TS7016
64
+
65
+ // @ts-expect-error TS7016
66
+
67
+ const PNPM_CMD = 'pnpm';
68
+ let hasPnpm;
69
+ let pnpmVersion;
70
+ class Pnpm {
71
+ static async exists() {
72
+ if (hasPnpm != null) {
73
+ return hasPnpm;
74
+ }
75
+ try {
76
+ hasPnpm = Boolean(await (0, _commandExists().default)('pnpm'));
77
+ } catch (err) {
78
+ hasPnpm = false;
79
+ }
80
+ return hasPnpm;
81
+ }
82
+ async install({
83
+ modules,
84
+ cwd,
85
+ saveDev = true
86
+ }) {
87
+ if (pnpmVersion == null) {
88
+ let version = await (0, _utils.exec)('pnpm --version');
89
+ // @ts-expect-error TS2345
90
+ pnpmVersion = parseInt(version.stdout, 10);
91
+ }
92
+ let args = ['add', '--reporter', 'ndjson'];
93
+ if (saveDev) {
94
+ args.push('-D');
95
+ }
96
+ if (pnpmVersion >= 7) {
97
+ if (_fs().default.existsSync(_path().default.join(cwd, 'pnpm-workspace.yaml'))) {
98
+ // installs in workspace root (regardless of cwd)
99
+ args.push('-w');
100
+ }
101
+ } else {
102
+ // ignores workspace root check
103
+ args.push('-W');
104
+ }
105
+ args = args.concat(modules.map(_utils.npmSpecifierFromModuleRequest));
106
+ let env = {};
107
+ for (let key in process.env) {
108
+ if (!key.startsWith('npm_') && key !== 'INIT_CWD' && key !== 'NODE_ENV') {
109
+ env[key] = process.env[key];
110
+ }
111
+ }
112
+ let addedCount = 0,
113
+ removedCount = 0;
114
+ let installProcess = (0, _crossSpawn().default)(PNPM_CMD, args, {
115
+ cwd,
116
+ env
117
+ });
118
+ installProcess.stdout.pipe((0, _split().default)())
119
+ // @ts-expect-error TS2554
120
+ .pipe(new _JSONParseStream.default())
121
+ // @ts-expect-error TS7006
122
+ .on('error', e => {
123
+ _logger().default.warn({
124
+ origin: '@atlaspack/package-manager',
125
+ message: e.chunk,
126
+ stack: e.stack
127
+ });
128
+ }).on('data', json => {
129
+ if (json.level === 'error') {
130
+ _logger().default.error({
131
+ origin: '@atlaspack/package-manager',
132
+ message: json.err.message,
133
+ stack: json.err.stack
134
+ });
135
+ } else if (json.level === 'info' && typeof json.message === 'string') {
136
+ _logger().default.info({
137
+ origin: '@atlaspack/package-manager',
138
+ message: prefix(json.message)
139
+ });
140
+ } else if (json.name === 'pnpm:stats') {
141
+ addedCount += json.added ?? 0;
142
+ removedCount += json.removed ?? 0;
143
+ }
144
+ });
145
+ let stderr = [];
146
+ installProcess.stderr
147
+ // @ts-expect-error TS7006
148
+ .on('data', str => {
149
+ stderr.push(str.toString());
150
+ })
151
+ // @ts-expect-error TS7006
152
+ .on('error', e => {
153
+ _logger().default.warn({
154
+ origin: '@atlaspack/package-manager',
155
+ message: e.message
156
+ });
157
+ });
158
+ try {
159
+ await (0, _promiseFromProcess.default)(installProcess);
160
+ if (addedCount > 0 || removedCount > 0) {
161
+ _logger().default.log({
162
+ origin: '@atlaspack/package-manager',
163
+ message: `Added ${addedCount} ${removedCount > 0 ? `and removed ${removedCount} ` : ''}packages via pnpm`
164
+ });
165
+ }
166
+
167
+ // Since we succeeded, stderr might have useful information not included
168
+ // in the json written to stdout. It's also not necessary to log these as
169
+ // errors as they often aren't.
170
+ for (let message of stderr) {
171
+ _logger().default.log({
172
+ origin: '@atlaspack/package-manager',
173
+ message
174
+ });
175
+ }
176
+ } catch (e) {
177
+ throw new Error('pnpm failed to install modules');
178
+ }
179
+ }
180
+ }
181
+ exports.Pnpm = Pnpm;
182
+ function prefix(message) {
183
+ return 'pnpm: ' + message;
184
+ }
185
+ (0, _buildCache().registerSerializableClass)(`${_package.default.version}:Pnpm`, Pnpm);
package/lib/Yarn.js ADDED
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Yarn = void 0;
7
+ function _commandExists() {
8
+ const data = _interopRequireDefault(require("command-exists"));
9
+ _commandExists = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _crossSpawn() {
15
+ const data = _interopRequireDefault(require("cross-spawn"));
16
+ _crossSpawn = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _buildCache() {
22
+ const data = require("@atlaspack/build-cache");
23
+ _buildCache = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _logger() {
29
+ const data = _interopRequireDefault(require("@atlaspack/logger"));
30
+ _logger = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _split() {
36
+ const data = _interopRequireDefault(require("split2"));
37
+ _split = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ var _JSONParseStream = _interopRequireDefault(require("./JSONParseStream"));
43
+ var _promiseFromProcess = _interopRequireDefault(require("./promiseFromProcess"));
44
+ var _utils = require("./utils");
45
+ var _package = _interopRequireDefault(require("../package.json"));
46
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
47
+ // @ts-expect-error TS7016
48
+
49
+ // @ts-expect-error TS7016
50
+
51
+ // @ts-expect-error TS7016
52
+
53
+ const YARN_CMD = 'yarn';
54
+ let hasYarn;
55
+ let yarnVersion;
56
+ class Yarn {
57
+ static async exists() {
58
+ if (hasYarn != null) {
59
+ return hasYarn;
60
+ }
61
+ try {
62
+ hasYarn = Boolean(await (0, _commandExists().default)('yarn'));
63
+ } catch (err) {
64
+ hasYarn = false;
65
+ }
66
+ return hasYarn;
67
+ }
68
+ async install({
69
+ modules,
70
+ cwd,
71
+ saveDev = true
72
+ }) {
73
+ if (yarnVersion == null) {
74
+ let version = await (0, _utils.exec)('yarn --version');
75
+ // @ts-expect-error TS2345
76
+ yarnVersion = parseInt(version.stdout, 10);
77
+ }
78
+ let args = ['add', '--json'].concat(modules.map(_utils.npmSpecifierFromModuleRequest));
79
+ if (saveDev) {
80
+ args.push('-D');
81
+ if (yarnVersion < 2) {
82
+ args.push('-W');
83
+ }
84
+ }
85
+
86
+ // When Parcel is run by Yarn (e.g. via package.json scripts), several environment variables are
87
+ // added. When parcel in turn calls Yarn again, these can cause Yarn to behave stragely, so we
88
+ // filter them out when installing packages.
89
+ let env = {};
90
+ for (let key in process.env) {
91
+ if (!key.startsWith('npm_') && key !== 'YARN_WRAP_OUTPUT' && key !== 'INIT_CWD' && key !== 'NODE_ENV') {
92
+ env[key] = process.env[key];
93
+ }
94
+ }
95
+ let installProcess = (0, _crossSpawn().default)(YARN_CMD, args, {
96
+ cwd,
97
+ env
98
+ });
99
+ installProcess.stdout
100
+ // Invoking yarn with --json provides streaming, newline-delimited JSON output.
101
+ .pipe((0, _split().default)())
102
+ // @ts-expect-error TS2554
103
+ .pipe(new _JSONParseStream.default())
104
+ // @ts-expect-error TS7006
105
+ .on('error', e => {
106
+ _logger().default.error(e, '@atlaspack/package-manager');
107
+ }).on('data', message => {
108
+ switch (message.type) {
109
+ case 'step':
110
+ _logger().default.progress(prefix(`[${message.data.current}/${message.data.total}] ${message.data.message}`));
111
+ return;
112
+ case 'success':
113
+ case 'info':
114
+ _logger().default.info({
115
+ origin: '@atlaspack/package-manager',
116
+ message: prefix(message.data)
117
+ });
118
+ return;
119
+ default:
120
+ // ignore
121
+ }
122
+ });
123
+
124
+ installProcess.stderr.pipe((0, _split().default)())
125
+ // @ts-expect-error TS2554
126
+ .pipe(new _JSONParseStream.default())
127
+ // @ts-expect-error TS7006
128
+ .on('error', e => {
129
+ _logger().default.error(e, '@atlaspack/package-manager');
130
+ }).on('data', message => {
131
+ switch (message.type) {
132
+ case 'warning':
133
+ _logger().default.warn({
134
+ origin: '@atlaspack/package-manager',
135
+ message: prefix(message.data)
136
+ });
137
+ return;
138
+ case 'error':
139
+ _logger().default.error({
140
+ origin: '@atlaspack/package-manager',
141
+ message: prefix(message.data)
142
+ });
143
+ return;
144
+ default:
145
+ // ignore
146
+ }
147
+ });
148
+
149
+ try {
150
+ return await (0, _promiseFromProcess.default)(installProcess);
151
+ } catch (e) {
152
+ throw new Error('Yarn failed to install modules:' + e.message);
153
+ }
154
+ }
155
+ }
156
+ exports.Yarn = Yarn;
157
+ function prefix(message) {
158
+ return 'yarn: ' + message;
159
+ }
160
+ (0, _buildCache().registerSerializableClass)(`${_package.default.version}:Yarn`, Yarn);
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getCurrentPackageManager;
7
+ function getCurrentPackageManager(
8
+ // @ts-expect-error TS2322
9
+ userAgent = process.env.npm_config_user_agent) {
10
+ if (!userAgent) {
11
+ return undefined;
12
+ }
13
+ const pmSpec = userAgent.split(' ')[0];
14
+ const separatorPos = pmSpec.lastIndexOf('/');
15
+ const name = pmSpec.substring(0, separatorPos);
16
+ return {
17
+ name: name,
18
+ version: pmSpec.substring(separatorPos + 1)
19
+ };
20
+ }