@optimizely/ocp-cli 1.2.6 → 1.2.8
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/dist/commands/app/BaseBuildCommand.js +1 -1
- package/dist/commands/app/BaseBuildCommand.js.map +1 -1
- package/dist/commands/app/Init.js +1 -1
- package/dist/commands/app/Init.js.map +1 -1
- package/dist/lib/AppUpdater.d.ts +1 -0
- package/dist/lib/AppUpdater.js +10 -10
- package/dist/lib/AppUpdater.js.map +1 -1
- package/dist/lib/checkForUpdate.js +15 -9
- package/dist/lib/checkForUpdate.js.map +1 -1
- package/dist/lib/packageManagerHandler.d.ts +53 -0
- package/dist/lib/packageManagerHandler.js +162 -0
- package/dist/lib/packageManagerHandler.js.map +1 -0
- package/dist/oo-cli.manifest.json +1 -1
- package/dist/test/AppUpdater.test.d.ts +1 -0
- package/dist/test/AppUpdater.test.js +155 -0
- package/dist/test/AppUpdater.test.js.map +1 -0
- package/dist/test/packageManagerHandler.test.d.ts +1 -0
- package/dist/test/packageManagerHandler.test.js +169 -0
- package/dist/test/packageManagerHandler.test.js.map +1 -0
- package/package.json +2 -2
- package/src/commands/app/BaseBuildCommand.ts +1 -1
- package/src/commands/app/Init.ts +1 -1
- package/src/lib/AppUpdater.ts +14 -15
- package/src/lib/checkForUpdate.ts +24 -13
- package/src/lib/packageManagerHandler.ts +178 -0
- package/src/test/AppUpdater.test.ts +196 -0
- package/src/test/packageManagerHandler.test.ts +190 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { TerminalOutput } from './TerminalOutput';
|
|
2
|
+
|
|
3
|
+
class PackageManagerHandler {
|
|
4
|
+
public static isLegacyYarnAvailable(): boolean {
|
|
5
|
+
const [success, output] =
|
|
6
|
+
PackageManagerHandler.executeCommand('yarn --version');
|
|
7
|
+
return success && output.startsWith('1.');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves package dependencies based on a scope pattern and production flag.
|
|
12
|
+
*
|
|
13
|
+
* @param scope - Regular expression pattern to filter package names
|
|
14
|
+
* @param prod - When true, only returns production dependencies. Defaults to false
|
|
15
|
+
* @returns A tuple where:
|
|
16
|
+
* - First element is a boolean indicating success/failure
|
|
17
|
+
* - Second element is either:
|
|
18
|
+
* - On success: An object containing dependency tree data
|
|
19
|
+
* - On failure: An error message string
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* - It uses NPM `ls` and transforms the dependency tree to match Yarn's format
|
|
23
|
+
*
|
|
24
|
+
* @throws Will not throw errors directly, but catches and returns them as failure results
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const [success, dependencies] = getPackageDependencies(/^@scope\//, true);
|
|
29
|
+
* if (success) {
|
|
30
|
+
* // Process dependency tree
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
public static getPackageDependencies(
|
|
35
|
+
scope: RegExp,
|
|
36
|
+
prod: boolean = false
|
|
37
|
+
): [boolean, any] {
|
|
38
|
+
const command = `npm ls --json ${prod ? '--prod ' : ''}`;
|
|
39
|
+
try {
|
|
40
|
+
const [success, output] = PackageManagerHandler.executeCommand(command);
|
|
41
|
+
if (!success) {
|
|
42
|
+
return [false, output];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// if we're using npm, then we need to parse the tree to have a similar output to yarn
|
|
46
|
+
const tree = JSON.parse(output);
|
|
47
|
+
const trees: any[] = [];
|
|
48
|
+
function collect(deps: Record<string, any> = {}, depth = 0) {
|
|
49
|
+
for (const [name, info] of Object.entries(deps)) {
|
|
50
|
+
if (scope.test(name)) {
|
|
51
|
+
trees.push({
|
|
52
|
+
name: `${name}@${info.version ?? 'unknown'}`,
|
|
53
|
+
children: [],
|
|
54
|
+
hint: null,
|
|
55
|
+
color: depth === 0 ? 'bold' : null,
|
|
56
|
+
depth,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (info.dependencies) {
|
|
60
|
+
collect(info.dependencies, depth + 1);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
collect(tree.dependencies);
|
|
66
|
+
|
|
67
|
+
return [
|
|
68
|
+
true,
|
|
69
|
+
{
|
|
70
|
+
type: 'tree',
|
|
71
|
+
data: {
|
|
72
|
+
type: 'list',
|
|
73
|
+
trees,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
];
|
|
77
|
+
} catch (e) {
|
|
78
|
+
console.error('Unable to get package dependencies:', e);
|
|
79
|
+
return [false, 'Unable to get package dependencies.'];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private static executeCommand(command: string): [boolean, string] {
|
|
84
|
+
const result = TerminalOutput.exec(command);
|
|
85
|
+
if (result.status === 0) {
|
|
86
|
+
const output = result.stdout.trim();
|
|
87
|
+
return [true, output];
|
|
88
|
+
} else {
|
|
89
|
+
return [false, result.stderr.trim()];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private targetPackageManager: 'yarn' | 'npm' | 'none' = 'none';
|
|
94
|
+
constructor(private packageName: string, cli: string) {
|
|
95
|
+
this.targetPackageManager = this.cliInstalledWith(cli);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public getPackageUpgradeCommand(): [boolean, string] {
|
|
99
|
+
const [possible, error] = this.isUpgradePossible();
|
|
100
|
+
if (!possible) {
|
|
101
|
+
return [false, error];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return [
|
|
105
|
+
true,
|
|
106
|
+
this.useYarn()
|
|
107
|
+
? `yarn global upgrade ${this.packageName} --latest`
|
|
108
|
+
: `npm install --global ${this.packageName}@latest`,
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Upgrades the specified package to its latest version globally.
|
|
114
|
+
* The function determines whether to use Yarn or NPM based on internal configuration.
|
|
115
|
+
*
|
|
116
|
+
* @returns {[boolean, string]} A tuple where:
|
|
117
|
+
* - First element (boolean) indicates if the upgrade was successful
|
|
118
|
+
* - Second element (string) contains error message if upgrade failed, or success message if upgrade succeeded
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* const [success, message] = packageManager.upgradeGlobalPackageToLatest();
|
|
122
|
+
* if (!success) {
|
|
123
|
+
* console.error(`Failed to upgrade package: ${message}`);
|
|
124
|
+
* }
|
|
125
|
+
*/
|
|
126
|
+
public upgradeGlobalPackageToLatest(): [boolean, string] {
|
|
127
|
+
const [possible, error] = this.isUpgradePossible();
|
|
128
|
+
if (!possible) {
|
|
129
|
+
return [false, error];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const command = this.useYarn()
|
|
133
|
+
? `yarn global upgrade ${this.packageName} --latest`
|
|
134
|
+
: `npm install --global ${this.packageName}@latest`;
|
|
135
|
+
return PackageManagerHandler.executeCommand(command);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public fetchLatestPackage(): [boolean, string] {
|
|
139
|
+
return PackageManagerHandler.executeCommand(
|
|
140
|
+
`npm show ${this.packageName} dist-tags.latest`
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private isUpgradePossible(): [boolean, string] {
|
|
145
|
+
if (this.useYarn() && !PackageManagerHandler.isLegacyYarnAvailable()) {
|
|
146
|
+
return [
|
|
147
|
+
false,
|
|
148
|
+
`${this.packageName} is installed using yarn 1.x, but the current version of yarn is not compatible with the upgrade command. Please use npm to install ${this.packageName} instead`,
|
|
149
|
+
];
|
|
150
|
+
}
|
|
151
|
+
return [true, ''];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private cliInstalledWith(cli: string): 'yarn' | 'npm' | 'none' {
|
|
155
|
+
const whichCommand = process.platform === 'win32' ? 'where' : 'which';
|
|
156
|
+
const [success, output] = PackageManagerHandler.executeCommand(
|
|
157
|
+
`${whichCommand} ${cli}`
|
|
158
|
+
);
|
|
159
|
+
if (success) {
|
|
160
|
+
// just in case we have multiple paths, we just take the first one
|
|
161
|
+
const cliPath = output.split('\n')[0].trim();
|
|
162
|
+
if (cliPath.includes('yarn')) {
|
|
163
|
+
// assuming only legacy yarn (1.x) is able to install the cli globally
|
|
164
|
+
return 'yarn';
|
|
165
|
+
} else {
|
|
166
|
+
// else we assume it's npm as this is how we recommend installing the cli
|
|
167
|
+
return 'npm';
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return 'none';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private useYarn(): boolean {
|
|
174
|
+
return this.targetPackageManager === 'yarn';
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export default PackageManagerHandler;
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { AppUpdater } from '../lib/AppUpdater';
|
|
2
|
+
import PackageManagerHandler from '../lib/packageManagerHandler';
|
|
3
|
+
|
|
4
|
+
jest.mock('../lib/packageManagerHandler');
|
|
5
|
+
|
|
6
|
+
describe('AppUpdater', () => {
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
jest.clearAllMocks();
|
|
9
|
+
});
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
jest.restoreAllMocks();
|
|
12
|
+
});
|
|
13
|
+
describe('getPackageDependencies', () => {
|
|
14
|
+
it('should parse dependencies correctly', () => {
|
|
15
|
+
const mockDeps = {
|
|
16
|
+
type: 'tree',
|
|
17
|
+
data: {
|
|
18
|
+
type: 'list',
|
|
19
|
+
trees: [
|
|
20
|
+
{
|
|
21
|
+
name: '@zaiusinc/package1@1.0.0',
|
|
22
|
+
children: [],
|
|
23
|
+
hint: null,
|
|
24
|
+
color: 'bold',
|
|
25
|
+
depth: 0,
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: '@zaiusinc/package2@2.0.0',
|
|
29
|
+
children: [],
|
|
30
|
+
hint: null,
|
|
31
|
+
color: 'bold',
|
|
32
|
+
depth: 0,
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
(
|
|
39
|
+
PackageManagerHandler.getPackageDependencies as jest.Mock
|
|
40
|
+
).mockReturnValueOnce([true, mockDeps]);
|
|
41
|
+
|
|
42
|
+
const result = AppUpdater.getPackageDependencies();
|
|
43
|
+
|
|
44
|
+
expect(result).toEqual({
|
|
45
|
+
'@zaiusinc/package1': '1.0.0',
|
|
46
|
+
'@zaiusinc/package2': '2.0.0',
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should handle prod dependencies flag', () => {
|
|
51
|
+
const mockDeps = {
|
|
52
|
+
type: 'tree',
|
|
53
|
+
data: {
|
|
54
|
+
type: 'list',
|
|
55
|
+
trees: [
|
|
56
|
+
{
|
|
57
|
+
name: '@zaiusinc/package1@1.0.0',
|
|
58
|
+
children: [],
|
|
59
|
+
hint: null,
|
|
60
|
+
color: 'bold',
|
|
61
|
+
depth: 0,
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const mockHandler = (
|
|
68
|
+
PackageManagerHandler.getPackageDependencies as jest.Mock
|
|
69
|
+
).mockReturnValueOnce([true, mockDeps]);
|
|
70
|
+
|
|
71
|
+
AppUpdater.getPackageDependencies(true);
|
|
72
|
+
|
|
73
|
+
expect(mockHandler).toHaveBeenCalledWith(/@zaiusinc\//, true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('error handling', () => {
|
|
77
|
+
let mockExit: jest.SpyInstance;
|
|
78
|
+
let mockConsole: jest.SpyInstance;
|
|
79
|
+
let mockConsoleError: jest.SpyInstance;
|
|
80
|
+
beforeEach(() => {
|
|
81
|
+
mockExit = jest
|
|
82
|
+
.spyOn(process, 'exit')
|
|
83
|
+
.mockImplementation(() => undefined as never);
|
|
84
|
+
mockConsole = jest
|
|
85
|
+
.spyOn(console, 'log')
|
|
86
|
+
.mockImplementation(() => undefined as never);
|
|
87
|
+
mockConsoleError = jest
|
|
88
|
+
.spyOn(console, 'error')
|
|
89
|
+
.mockImplementation(() => undefined as never);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
afterEach(() => {
|
|
93
|
+
mockExit.mockRestore();
|
|
94
|
+
mockConsole.mockRestore();
|
|
95
|
+
mockConsoleError.mockRestore();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should exit with error on package manager failure', () => {
|
|
99
|
+
(
|
|
100
|
+
PackageManagerHandler.getPackageDependencies as jest.Mock
|
|
101
|
+
).mockReturnValueOnce([false, 'error']);
|
|
102
|
+
|
|
103
|
+
AppUpdater.getPackageDependencies();
|
|
104
|
+
|
|
105
|
+
expect(mockExit).toHaveBeenCalledWith(1);
|
|
106
|
+
expect(mockConsoleError).toHaveBeenCalledWith('error');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should exit with error on exception', () => {
|
|
110
|
+
(
|
|
111
|
+
PackageManagerHandler.getPackageDependencies as jest.Mock
|
|
112
|
+
).mockImplementation(() => {
|
|
113
|
+
throw new Error('test error');
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
AppUpdater.getPackageDependencies();
|
|
117
|
+
|
|
118
|
+
expect(mockExit).toHaveBeenCalledWith(1);
|
|
119
|
+
expect(mockConsoleError).toHaveBeenCalledWith(new Error('test error'));
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('ensurePublicPackageUsage', () => {
|
|
125
|
+
let mockExit: jest.SpyInstance;
|
|
126
|
+
let mockConsoleLog: jest.SpyInstance;
|
|
127
|
+
let mockConsoleError: jest.SpyInstance;
|
|
128
|
+
|
|
129
|
+
beforeEach(() => {
|
|
130
|
+
mockExit = jest
|
|
131
|
+
.spyOn(process, 'exit')
|
|
132
|
+
.mockImplementation(() => undefined as never);
|
|
133
|
+
mockConsoleLog = jest
|
|
134
|
+
.spyOn(console, 'log')
|
|
135
|
+
.mockImplementation(() => undefined as never);
|
|
136
|
+
mockConsoleError = jest
|
|
137
|
+
.spyOn(console, 'error')
|
|
138
|
+
.mockImplementation(() => undefined as never);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
afterEach(() => {
|
|
142
|
+
mockExit.mockRestore();
|
|
143
|
+
mockConsoleLog.mockRestore();
|
|
144
|
+
mockConsoleError.mockRestore();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should allow execution when no deprecated packages are found', () => {
|
|
148
|
+
const mockDeps = {
|
|
149
|
+
type: 'tree',
|
|
150
|
+
data: {
|
|
151
|
+
type: 'list',
|
|
152
|
+
trees: [],
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
(
|
|
157
|
+
PackageManagerHandler.getPackageDependencies as jest.Mock
|
|
158
|
+
).mockReturnValueOnce([true, mockDeps]);
|
|
159
|
+
|
|
160
|
+
AppUpdater.ensurePublicPackageUsage();
|
|
161
|
+
|
|
162
|
+
expect(mockExit).not.toHaveBeenCalled();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('should exit when deprecated @zaius packages are found', () => {
|
|
166
|
+
const mockDeps = {
|
|
167
|
+
type: 'tree',
|
|
168
|
+
data: {
|
|
169
|
+
type: 'list',
|
|
170
|
+
trees: [
|
|
171
|
+
{
|
|
172
|
+
name: '@zaius/app-sdk@1.0.0',
|
|
173
|
+
children: [],
|
|
174
|
+
hint: null,
|
|
175
|
+
color: 'bold',
|
|
176
|
+
depth: 0,
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
(
|
|
183
|
+
PackageManagerHandler.getPackageDependencies as jest.Mock
|
|
184
|
+
).mockReturnValueOnce([true, mockDeps]);
|
|
185
|
+
|
|
186
|
+
AppUpdater.ensurePublicPackageUsage();
|
|
187
|
+
|
|
188
|
+
expect(mockExit).toHaveBeenCalledWith(1);
|
|
189
|
+
expect(mockConsoleLog).toHaveBeenCalledWith(
|
|
190
|
+
expect.stringContaining(
|
|
191
|
+
'@zaius/app-sdk & @zaius/node-sdk are no longer supported'
|
|
192
|
+
)
|
|
193
|
+
);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
});
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/* tslint:disable:max-line-length */
|
|
2
|
+
import PackageManagerHandler from '../lib/packageManagerHandler';
|
|
3
|
+
import { TerminalOutput } from '../lib/TerminalOutput';
|
|
4
|
+
|
|
5
|
+
jest.mock('../lib/TerminalOutput', () => ({
|
|
6
|
+
TerminalOutput: {
|
|
7
|
+
exec: jest.fn(),
|
|
8
|
+
},
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
describe('PackageManagerHandler', () => {
|
|
12
|
+
let packageManager: PackageManagerHandler;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
jest.clearAllMocks();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
jest.restoreAllMocks();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('isLegacyYarnAvailable', () => {
|
|
23
|
+
it('should return true when yarn 1.x is available', () => {
|
|
24
|
+
(TerminalOutput.exec as jest.Mock).mockReturnValue({
|
|
25
|
+
status: 0,
|
|
26
|
+
stdout: '1.22.19\n',
|
|
27
|
+
stderr: '',
|
|
28
|
+
});
|
|
29
|
+
expect(PackageManagerHandler.isLegacyYarnAvailable()).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should return false when yarn 2.x is available', () => {
|
|
33
|
+
(TerminalOutput.exec as jest.Mock).mockReturnValue({
|
|
34
|
+
status: 0,
|
|
35
|
+
stdout: '2.0.0\n',
|
|
36
|
+
stderr: '',
|
|
37
|
+
});
|
|
38
|
+
expect(PackageManagerHandler.isLegacyYarnAvailable()).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('upgradeGlobalPackageToLatest', () => {
|
|
43
|
+
it('should use yarn when the cli is installed using yarn', () => {
|
|
44
|
+
(TerminalOutput.exec as jest.Mock)
|
|
45
|
+
.mockReturnValueOnce({
|
|
46
|
+
status: 0,
|
|
47
|
+
stdout: '/foo/bar/yarn/bin/test-cli\n',
|
|
48
|
+
stderr: '',
|
|
49
|
+
}) // yarn check
|
|
50
|
+
.mockReturnValueOnce({ status: 0, stdout: '1.22.0', stderr: '' }) // check for yarn 1.x
|
|
51
|
+
.mockReturnValueOnce({ status: 0, stdout: 'success', stderr: '' }); // upgrade command
|
|
52
|
+
|
|
53
|
+
packageManager = new PackageManagerHandler('test-package', 'test-cli');
|
|
54
|
+
const [success, _] = packageManager.upgradeGlobalPackageToLatest();
|
|
55
|
+
expect(success).toBe(true);
|
|
56
|
+
expect(TerminalOutput.exec).toHaveBeenCalledWith(
|
|
57
|
+
'yarn global upgrade test-package --latest'
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should notify failure when the cli is installed using yarn 1.x but current yarn version is newer than 1.x', () => {
|
|
62
|
+
(TerminalOutput.exec as jest.Mock)
|
|
63
|
+
.mockReturnValueOnce({
|
|
64
|
+
status: 0,
|
|
65
|
+
stdout: '/foo/bar/yarn/bin/test-cli\n',
|
|
66
|
+
stderr: '',
|
|
67
|
+
}) // yarn check
|
|
68
|
+
.mockReturnValueOnce({ status: 0, stdout: '2.22.0', stderr: '' }); // check for yarn 1.x
|
|
69
|
+
|
|
70
|
+
packageManager = new PackageManagerHandler('test-package', 'test-cli');
|
|
71
|
+
const [success, message] = packageManager.upgradeGlobalPackageToLatest();
|
|
72
|
+
expect(success).toBe(false);
|
|
73
|
+
expect(message).toContain('not compatible');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('fetchLatestPackage', () => {
|
|
78
|
+
it('should fetch latest version using yarn when cli is installed with it', () => {
|
|
79
|
+
(TerminalOutput.exec as jest.Mock)
|
|
80
|
+
.mockReturnValueOnce({
|
|
81
|
+
status: 0,
|
|
82
|
+
stdout: '/foo/bar/yarn/bin/test-cli\n',
|
|
83
|
+
stderr: '',
|
|
84
|
+
}) // yarn check
|
|
85
|
+
.mockReturnValueOnce({ status: 0, stdout: '1.0.0', stderr: '' }); // fetch command
|
|
86
|
+
|
|
87
|
+
packageManager = new PackageManagerHandler('test-package', 'test-cli');
|
|
88
|
+
const [success, version] = packageManager.fetchLatestPackage();
|
|
89
|
+
expect(success).toBe(true);
|
|
90
|
+
expect(version).toBe('1.0.0');
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('getPackageDependencies', () => {
|
|
95
|
+
it('should parse npm dependencies into yarn-like format', () => {
|
|
96
|
+
const mockNpmOutput = JSON.stringify({
|
|
97
|
+
dependencies: {
|
|
98
|
+
'test-pkg': {
|
|
99
|
+
version: '1.0.0',
|
|
100
|
+
dependencies: {
|
|
101
|
+
'nested-test': {
|
|
102
|
+
version: '2.0.0',
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
'other-pkg': {
|
|
107
|
+
version: '3.0.0',
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
(TerminalOutput.exec as jest.Mock).mockReturnValueOnce({
|
|
113
|
+
status: 0,
|
|
114
|
+
stdout: mockNpmOutput,
|
|
115
|
+
stderr: '',
|
|
116
|
+
}); // list command
|
|
117
|
+
|
|
118
|
+
const [success, deps] =
|
|
119
|
+
PackageManagerHandler.getPackageDependencies(/test/);
|
|
120
|
+
|
|
121
|
+
expect(success).toBe(true);
|
|
122
|
+
expect(deps).toEqual({
|
|
123
|
+
type: 'tree',
|
|
124
|
+
data: {
|
|
125
|
+
type: 'list',
|
|
126
|
+
trees: [
|
|
127
|
+
{
|
|
128
|
+
name: 'test-pkg@1.0.0',
|
|
129
|
+
children: [],
|
|
130
|
+
hint: null,
|
|
131
|
+
color: 'bold',
|
|
132
|
+
depth: 0,
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
name: 'nested-test@2.0.0',
|
|
136
|
+
children: [],
|
|
137
|
+
hint: null,
|
|
138
|
+
color: null,
|
|
139
|
+
depth: 1,
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
describe('getPackageUpgradeCommand', () => {
|
|
147
|
+
it('should return yarn upgrade command when cli is installed with yarn', () => {
|
|
148
|
+
(TerminalOutput.exec as jest.Mock)
|
|
149
|
+
.mockReturnValueOnce({
|
|
150
|
+
status: 0,
|
|
151
|
+
stdout: '/foo/bar/yarn/bin/test-cli\n',
|
|
152
|
+
stderr: '',
|
|
153
|
+
}) // yarn check
|
|
154
|
+
.mockReturnValueOnce({ status: 0, stdout: '1.22.0', stderr: '' }); // check for yarn 1.x
|
|
155
|
+
|
|
156
|
+
packageManager = new PackageManagerHandler('test-package', 'test-cli');
|
|
157
|
+
const [success, command] = packageManager.getPackageUpgradeCommand();
|
|
158
|
+
expect(success).toBe(true);
|
|
159
|
+
expect(command).toBe('yarn global upgrade test-package --latest');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should return npm upgrade command when cli is installed with npm', () => {
|
|
163
|
+
(TerminalOutput.exec as jest.Mock).mockReturnValueOnce({
|
|
164
|
+
status: 0,
|
|
165
|
+
stdout: '/usr/local/bin/test-cli\n',
|
|
166
|
+
stderr: '',
|
|
167
|
+
}); // npm check
|
|
168
|
+
|
|
169
|
+
packageManager = new PackageManagerHandler('test-package', 'test-cli');
|
|
170
|
+
const [success, command] = packageManager.getPackageUpgradeCommand();
|
|
171
|
+
expect(success).toBe(true);
|
|
172
|
+
expect(command).toBe('npm install --global test-package@latest');
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('should return error when yarn version is not 1.x', () => {
|
|
176
|
+
(TerminalOutput.exec as jest.Mock)
|
|
177
|
+
.mockReturnValueOnce({
|
|
178
|
+
status: 0,
|
|
179
|
+
stdout: '/foo/bar/yarn/bin/test-cli\n',
|
|
180
|
+
stderr: '',
|
|
181
|
+
}) // yarn check
|
|
182
|
+
.mockReturnValueOnce({ status: 0, stdout: '2.22.0', stderr: '' }); // check for yarn 1.x
|
|
183
|
+
|
|
184
|
+
packageManager = new PackageManagerHandler('test-package', 'test-cli');
|
|
185
|
+
const [success, message] = packageManager.getPackageUpgradeCommand();
|
|
186
|
+
expect(success).toBe(false);
|
|
187
|
+
expect(message).toContain('not compatible');
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
});
|