@adobe/ccweb-add-on-core 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.
Files changed (94) hide show
  1. package/.c8rc.json +14 -0
  2. package/.mocharc.json +5 -0
  3. package/CODE_OF_CONDUCT.md +79 -0
  4. package/CONTRIBUTING.md +47 -0
  5. package/LICENSE +21 -0
  6. package/README.md +11 -0
  7. package/dist/config/index.d.ts +26 -0
  8. package/dist/config/index.d.ts.map +1 -0
  9. package/dist/config/index.js +26 -0
  10. package/dist/config/inversify.config.d.ts +28 -0
  11. package/dist/config/inversify.config.d.ts.map +1 -0
  12. package/dist/config/inversify.config.js +33 -0
  13. package/dist/config/inversify.types.d.ts +30 -0
  14. package/dist/config/inversify.types.d.ts.map +1 -0
  15. package/dist/config/inversify.types.js +30 -0
  16. package/dist/constants.d.ts +37 -0
  17. package/dist/constants.d.ts.map +1 -0
  18. package/dist/constants.js +38 -0
  19. package/dist/index.d.ts +28 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +28 -0
  22. package/dist/models/PackageJson.d.ts +61 -0
  23. package/dist/models/PackageJson.d.ts.map +1 -0
  24. package/dist/models/PackageJson.js +76 -0
  25. package/dist/models/PreferenceJson.d.ts +79 -0
  26. package/dist/models/PreferenceJson.d.ts.map +1 -0
  27. package/dist/models/PreferenceJson.js +88 -0
  28. package/dist/models/TemplateJson.d.ts +53 -0
  29. package/dist/models/TemplateJson.d.ts.map +1 -0
  30. package/dist/models/TemplateJson.js +112 -0
  31. package/dist/models/Types.d.ts +72 -0
  32. package/dist/models/Types.d.ts.map +1 -0
  33. package/dist/models/Types.js +25 -0
  34. package/dist/models/index.d.ts +28 -0
  35. package/dist/models/index.d.ts.map +1 -0
  36. package/dist/models/index.js +28 -0
  37. package/dist/tsconfig.tsbuildinfo +1 -0
  38. package/dist/utilities/CLIPreferences.d.ts +45 -0
  39. package/dist/utilities/CLIPreferences.d.ts.map +1 -0
  40. package/dist/utilities/CLIPreferences.js +80 -0
  41. package/dist/utilities/CLIProcess.d.ts +74 -0
  42. package/dist/utilities/CLIProcess.d.ts.map +1 -0
  43. package/dist/utilities/CLIProcess.js +190 -0
  44. package/dist/utilities/ConsoleLogger.d.ts +64 -0
  45. package/dist/utilities/ConsoleLogger.d.ts.map +1 -0
  46. package/dist/utilities/ConsoleLogger.js +103 -0
  47. package/dist/utilities/Extensions.d.ts +67 -0
  48. package/dist/utilities/Extensions.d.ts.map +1 -0
  49. package/dist/utilities/Extensions.js +92 -0
  50. package/dist/utilities/Logger.d.ts +65 -0
  51. package/dist/utilities/Logger.d.ts.map +1 -0
  52. package/dist/utilities/Logger.js +25 -0
  53. package/dist/utilities/Preferences.d.ts +41 -0
  54. package/dist/utilities/Preferences.d.ts.map +1 -0
  55. package/dist/utilities/Preferences.js +25 -0
  56. package/dist/utilities/Process.d.ts +63 -0
  57. package/dist/utilities/Process.d.ts.map +1 -0
  58. package/dist/utilities/Process.js +25 -0
  59. package/dist/utilities/UncaughtExceptionHandler.d.ts +40 -0
  60. package/dist/utilities/UncaughtExceptionHandler.d.ts.map +1 -0
  61. package/dist/utilities/UncaughtExceptionHandler.js +51 -0
  62. package/dist/utilities/index.d.ts +32 -0
  63. package/dist/utilities/index.d.ts.map +1 -0
  64. package/dist/utilities/index.js +32 -0
  65. package/package.json +51 -0
  66. package/src/config/index.ts +26 -0
  67. package/src/config/inversify.config.ts +39 -0
  68. package/src/config/inversify.types.ts +30 -0
  69. package/src/constants.ts +41 -0
  70. package/src/index.ts +28 -0
  71. package/src/models/PackageJson.ts +83 -0
  72. package/src/models/PreferenceJson.ts +112 -0
  73. package/src/models/TemplateJson.ts +121 -0
  74. package/src/models/Types.ts +80 -0
  75. package/src/models/index.ts +28 -0
  76. package/src/test/models/PackageJson.spec.ts +198 -0
  77. package/src/test/models/PreferenceJson.spec.ts +184 -0
  78. package/src/test/models/TemplateJson.spec.ts +179 -0
  79. package/src/test/utilities/CLIPreferences.spec.ts +208 -0
  80. package/src/test/utilities/CLIProcess.spec.ts +363 -0
  81. package/src/test/utilities/ConsoleLogger.spec.ts +165 -0
  82. package/src/test/utilities/Extensions.spec.ts +240 -0
  83. package/src/test/utilities/UncaughtExceptionHandler.spec.ts +83 -0
  84. package/src/types/applicationConfigPath.d.ts +1 -0
  85. package/src/utilities/CLIPreferences.ts +84 -0
  86. package/src/utilities/CLIProcess.ts +200 -0
  87. package/src/utilities/ConsoleLogger.ts +111 -0
  88. package/src/utilities/Extensions.ts +98 -0
  89. package/src/utilities/Logger.ts +70 -0
  90. package/src/utilities/Preferences.ts +43 -0
  91. package/src/utilities/Process.ts +66 -0
  92. package/src/utilities/UncaughtExceptionHandler.ts +55 -0
  93. package/src/utilities/index.ts +32 -0
  94. package/tsconfig.json +31 -0
@@ -0,0 +1,208 @@
1
+ /********************************************************************************
2
+ * MIT License
3
+
4
+ * © Copyright 2023 Adobe. All rights reserved.
5
+
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ ********************************************************************************/
24
+
25
+ import applicationConfigPath from "application-config-path";
26
+ import { assert } from "chai";
27
+ import fs from "fs-extra";
28
+ import "mocha";
29
+ import os from "os";
30
+ import path from "path";
31
+ import type { SinonSandbox } from "sinon";
32
+ import sinon from "sinon";
33
+ import { ADD_ON_PREFERENCES_FILE, CCWEB_ADDON_DIRECTORY } from "../../constants.js";
34
+ import { PreferenceJson } from "../../models/PreferenceJson.js";
35
+ import type { Preferences } from "../../utilities/index.js";
36
+ import { CLIPreferences } from "../../utilities/index.js";
37
+
38
+ describe("CLIPreferences", () => {
39
+ let sandbox: SinonSandbox;
40
+ let cliPreferences: Preferences;
41
+
42
+ beforeEach(() => {
43
+ sandbox = sinon.createSandbox();
44
+ cliPreferences = new CLIPreferences();
45
+ });
46
+
47
+ afterEach(() => {
48
+ sandbox.restore();
49
+ });
50
+
51
+ describe("get", () => {
52
+ it(`should return empty preferences if '${ADD_ON_PREFERENCES_FILE}' does not exit.`, () => {
53
+ const preferenceFilePath = `/some-directory/${CCWEB_ADDON_DIRECTORY}/${ADD_ON_PREFERENCES_FILE}`;
54
+
55
+ sandbox
56
+ .stub(path, "join")
57
+ .withArgs(applicationConfigPath(CCWEB_ADDON_DIRECTORY), ADD_ON_PREFERENCES_FILE)
58
+ .returns(preferenceFilePath);
59
+
60
+ sandbox.stub(fs, "existsSync").withArgs(preferenceFilePath).returns(false);
61
+
62
+ const ensureFileStub = sandbox.stub(fs, "ensureFileSync");
63
+ ensureFileStub.withArgs(preferenceFilePath).returns();
64
+
65
+ const writeFileStub = sandbox.stub(fs, "writeFileSync");
66
+ writeFileStub.withArgs(preferenceFilePath, JSON.stringify({}, undefined, 4) + os.EOL).returns();
67
+
68
+ const preferences = cliPreferences.get();
69
+
70
+ assert.deepEqual(new PreferenceJson({}), preferences);
71
+ assert.equal(ensureFileStub.calledOnceWith(preferenceFilePath), true);
72
+ assert.equal(
73
+ writeFileStub.calledOnceWith(preferenceFilePath, JSON.stringify({}, undefined, 4) + os.EOL),
74
+ true
75
+ );
76
+ });
77
+
78
+ it(`should return empty preferences if '${ADD_ON_PREFERENCES_FILE}' exists but has no data.`, () => {
79
+ const preferenceFilePath = `/some-directory/${CCWEB_ADDON_DIRECTORY}/${ADD_ON_PREFERENCES_FILE}`;
80
+
81
+ sandbox
82
+ .stub(path, "join")
83
+ .withArgs(applicationConfigPath(CCWEB_ADDON_DIRECTORY), ADD_ON_PREFERENCES_FILE)
84
+ .returns(preferenceFilePath);
85
+
86
+ sandbox.stub(fs, "existsSync").withArgs(preferenceFilePath).returns(true);
87
+
88
+ sandbox.stub(fs, "readFileSync").withArgs(preferenceFilePath, "utf-8").returns("");
89
+
90
+ const preferences = cliPreferences.get();
91
+ assert.deepEqual(new PreferenceJson({}), preferences);
92
+ });
93
+
94
+ it(`should return empty preferences if any error is encountered while reading '${ADD_ON_PREFERENCES_FILE}'.`, () => {
95
+ const preferenceFilePath = `/some-directory/${CCWEB_ADDON_DIRECTORY}/${ADD_ON_PREFERENCES_FILE}`;
96
+
97
+ sandbox
98
+ .stub(path, "join")
99
+ .withArgs(applicationConfigPath(CCWEB_ADDON_DIRECTORY), ADD_ON_PREFERENCES_FILE)
100
+ .returns(preferenceFilePath);
101
+
102
+ sandbox.stub(fs, "existsSync").withArgs(preferenceFilePath).returns(true);
103
+
104
+ sandbox
105
+ .stub(fs, "readFileSync")
106
+ .withArgs(preferenceFilePath, "utf-8")
107
+ .throws(new Error("Unexpected error"));
108
+
109
+ const preferences = cliPreferences.get();
110
+ assert.deepEqual(new PreferenceJson({}), preferences);
111
+ });
112
+
113
+ it(`should return preferences with values if '${ADD_ON_PREFERENCES_FILE}' exits and has data.`, () => {
114
+ const preferenceFilePath = `/some-directory/${CCWEB_ADDON_DIRECTORY}/${ADD_ON_PREFERENCES_FILE}`;
115
+ const expectedPreference = {
116
+ hasTelemetryConsent: false,
117
+ ims: {
118
+ contexts: {
119
+ cli: {
120
+ "cli.bare-output": true
121
+ }
122
+ }
123
+ },
124
+ clientId: 1096361282655,
125
+ ssl: {
126
+ localhost: {
127
+ certificatePath: "/some-directory/localhost/ssl/certificate.cert",
128
+ keyPath: "/some-directory/localhost/ssl/private-key.key"
129
+ },
130
+ "localhost.adobe.com": {
131
+ certificatePath: "/some-directory/localhost.adobe.com/ssl/certificate.cert",
132
+ keyPath: "/some-directory/localhost.adobe.com/ssl/private-key.key"
133
+ }
134
+ }
135
+ };
136
+
137
+ sandbox
138
+ .stub(path, "join")
139
+ .withArgs(applicationConfigPath(CCWEB_ADDON_DIRECTORY), ADD_ON_PREFERENCES_FILE)
140
+ .returns(preferenceFilePath);
141
+
142
+ const fileExistsStub = sandbox.stub(fs, "existsSync");
143
+ fileExistsStub.withArgs(preferenceFilePath).returns(true);
144
+
145
+ const readFileStub = sandbox.stub(fs, "readFileSync");
146
+ readFileStub.withArgs(preferenceFilePath, "utf-8").returns(JSON.stringify(expectedPreference));
147
+
148
+ const actualPreference = cliPreferences.get();
149
+
150
+ assert.equal(actualPreference.hasTelemetryConsent, expectedPreference.hasTelemetryConsent);
151
+ assert.deepEqual(actualPreference.ims, expectedPreference.ims);
152
+ assert.equal(actualPreference.clientId, expectedPreference.clientId);
153
+ assert.deepEqual(actualPreference.ssl, new Map(Object.entries(expectedPreference.ssl)));
154
+
155
+ const cachedPreference = cliPreferences.get(true);
156
+
157
+ assert.deepEqual(cachedPreference, actualPreference);
158
+ assert.equal(fileExistsStub.callCount, 1);
159
+ assert.equal(readFileStub.callCount, 1);
160
+ });
161
+ });
162
+
163
+ describe("set", () => {
164
+ it(`should write the provided preferences to '${ADD_ON_PREFERENCES_FILE}'.`, () => {
165
+ const preferenceFilePath = `/some-directory/${CCWEB_ADDON_DIRECTORY}/${ADD_ON_PREFERENCES_FILE}`;
166
+ const preference = {
167
+ hasTelemetryConsent: false,
168
+ ims: {
169
+ contexts: {
170
+ cli: {
171
+ "cli.bare-output": true
172
+ }
173
+ }
174
+ },
175
+ clientId: 1096361282655,
176
+ ssl: {
177
+ localhost: {
178
+ certificatePath: "/some-directory/localhost/ssl/certificate.cert",
179
+ keyPath: "/some-directory/localhost/ssl/private-key.key"
180
+ },
181
+ "localhost.adobe.com": {
182
+ certificatePath: "/some-directory/localhost.adobe.com/ssl/certificate.cert",
183
+ keyPath: "/some-directory/localhost.adobe.com/ssl/private-key.key"
184
+ }
185
+ }
186
+ };
187
+
188
+ sandbox
189
+ .stub(path, "join")
190
+ .withArgs(applicationConfigPath(CCWEB_ADDON_DIRECTORY), ADD_ON_PREFERENCES_FILE)
191
+ .returns(preferenceFilePath);
192
+
193
+ sandbox.stub(fs, "existsSync").withArgs(preferenceFilePath).returns(true);
194
+
195
+ sandbox.stub(fs, "readFileSync").withArgs(preferenceFilePath, "utf-8").returns(JSON.stringify(preference));
196
+
197
+ const preferenceJson = cliPreferences.get();
198
+ preferenceJson.clientId = 123456789;
199
+
200
+ const writeFileStub = sandbox.stub(fs, "writeFileSync");
201
+ writeFileStub.withArgs(preferenceFilePath, preferenceJson.toJSON() + os.EOL).returns();
202
+
203
+ cliPreferences.set(preferenceJson);
204
+
205
+ assert.equal(writeFileStub.calledOnceWith(preferenceFilePath, preferenceJson.toJSON() + os.EOL), true);
206
+ });
207
+ });
208
+ });
@@ -0,0 +1,363 @@
1
+ /********************************************************************************
2
+ * MIT License
3
+
4
+ * © Copyright 2023 Adobe. All rights reserved.
5
+
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ ********************************************************************************/
24
+
25
+ /* eslint-disable camelcase -- child_process is the name of the module */
26
+ import { assert } from "chai";
27
+ import type { ChildProcess, ExecSyncOptions, SpawnSyncReturns } from "child_process";
28
+ import child_process from "cross-spawn";
29
+ import EventEmitter from "events";
30
+ import type { Dirent } from "fs-extra";
31
+ import fs from "fs-extra";
32
+ import "mocha";
33
+ import path from "path";
34
+ import process from "process";
35
+ import type { SinonSandbox } from "sinon";
36
+ import sinon from "sinon";
37
+ import type { StubbedInstance } from "ts-sinon";
38
+ import { stubInterface } from "ts-sinon";
39
+ import type { ExecutionResult } from "../../models/Types.js";
40
+ import type { Logger, Process } from "../../utilities/index.js";
41
+ import { CLIProcess } from "../../utilities/index.js";
42
+
43
+ describe("CLIProcess", () => {
44
+ let sandbox: SinonSandbox;
45
+ let logger: StubbedInstance<Logger>;
46
+ let cliProcess: Process;
47
+
48
+ beforeEach(() => {
49
+ sandbox = sinon.createSandbox();
50
+ logger = stubInterface<Logger>();
51
+ cliProcess = new CLIProcess(logger);
52
+ });
53
+
54
+ afterEach(() => {
55
+ sandbox.restore();
56
+ });
57
+
58
+ describe("execute ...", () => {
59
+ const runs = [
60
+ { command: "some command", args: [] },
61
+ { command: "some command", args: ["arg"] },
62
+ { command: "some command", args: ["arg-1", "arg-2"] },
63
+ { command: "some command", args: ["arg"], options: { stdio: "inherit" } }
64
+ ];
65
+ runs.forEach(run => {
66
+ it(`should call spawn() on ${run.command} with ${run.args?.length ?? 0} argument(s) and ${
67
+ run.options ? "" : "no "
68
+ }options.`, () => {
69
+ const spawnStub = sandbox.stub(child_process, "spawn");
70
+
71
+ const options = run.options as ExecSyncOptions;
72
+ if (options) {
73
+ cliProcess.execute(run.command, run.args, options);
74
+ } else {
75
+ cliProcess.execute(run.command, run.args);
76
+ }
77
+
78
+ assert.equal(spawnStub.callCount, 1);
79
+
80
+ if (options) {
81
+ assert.equal(spawnStub.calledWith(run.command, run.args, run.options as ExecSyncOptions), true);
82
+ } else {
83
+ assert.equal(spawnStub.calledWith(run.command, run.args), true);
84
+ }
85
+ });
86
+ });
87
+
88
+ it(`should return isSuccessful: true in ExecutionResult for successful execution of command.`, async () => {
89
+ const childProcess = <ChildProcess>new EventEmitter();
90
+ sandbox.stub(child_process, "spawn").returns(childProcess);
91
+
92
+ const command = "some-command";
93
+ const args = ["--some-option", "some-value"];
94
+ const expectedResult = {
95
+ command: "some-command --some-option some-value",
96
+ isSuccessful: true
97
+ } as ExecutionResult;
98
+
99
+ const executedPromise = cliProcess.execute(command, args);
100
+ childProcess.emit("close", 0);
101
+
102
+ const result = await executedPromise;
103
+ assert.equal(result.command, expectedResult.command);
104
+ assert.equal(result.isSuccessful, expectedResult.isSuccessful);
105
+ });
106
+
107
+ it(`should return isSuccessful: false in ExecutionResult for failed execution of command.`, async () => {
108
+ const childProcess = <ChildProcess>new EventEmitter();
109
+ sandbox.stub(child_process, "spawn").returns(childProcess);
110
+
111
+ const command = "some-command";
112
+ const args = ["--some-option", "some-value"];
113
+ const expectedError = {
114
+ command: "some-command --some-option some-value",
115
+ isSuccessful: false
116
+ } as ExecutionResult;
117
+
118
+ const executedPromise = cliProcess.execute(command, args);
119
+ childProcess.emit("close", 1);
120
+
121
+ const result = await executedPromise;
122
+ assert.equal(result === undefined, false);
123
+ assert.equal(result.command, expectedError.command);
124
+ assert.equal(result.isSuccessful, expectedError.isSuccessful);
125
+ });
126
+ });
127
+
128
+ describe("executeSync ...", () => {
129
+ const runs = [
130
+ { command: "some command", args: [] },
131
+ { command: "some command", args: ["arg"] },
132
+ { command: "some command", args: ["arg-1", "arg-2"] },
133
+ { command: "some command", args: ["arg"], options: { stdio: "inherit" } }
134
+ ];
135
+ runs.forEach(run => {
136
+ it(`should call sync() on ${run.command} with ${run.args?.length ?? 0} argument(s) and ${
137
+ run.options ? "" : "no "
138
+ }options.`, () => {
139
+ const syncStub = sandbox.stub(child_process, "sync").returns({
140
+ output: ["success"],
141
+ pid: 100,
142
+ signal: null,
143
+ stdout: "",
144
+ stderr: "",
145
+ status: 0
146
+ } as SpawnSyncReturns<string>);
147
+
148
+ const options = run.options as ExecSyncOptions;
149
+ if (options) {
150
+ cliProcess.executeSync(run.command, run.args, options);
151
+ } else {
152
+ cliProcess.executeSync(run.command, run.args);
153
+ }
154
+
155
+ assert.equal(syncStub.callCount, 1);
156
+
157
+ if (options) {
158
+ assert.equal(syncStub.calledWith(run.command, run.args, run.options as ExecSyncOptions), true);
159
+ } else {
160
+ assert.equal(syncStub.calledWith(run.command, run.args), true);
161
+ }
162
+ });
163
+ });
164
+
165
+ it(`should return isSuccessful: true in ExecutionResult for successful execution of command.`, () => {
166
+ sandbox.stub(child_process, "sync").returns({
167
+ output: ["successfully", "executed", "some-command"],
168
+ pid: 100,
169
+ signal: null,
170
+ stdout: "",
171
+ stderr: "",
172
+ status: 0
173
+ } as SpawnSyncReturns<string>);
174
+
175
+ const command = "some-command";
176
+ const args = ["--some-option", "some-value"];
177
+ const expectedResult = {
178
+ command: "some-command --some-option some-value",
179
+ isSuccessful: true,
180
+ data: "successfullyexecutedsome-command"
181
+ };
182
+
183
+ const result = cliProcess.executeSync(command, args);
184
+
185
+ assert.equal(result.command, expectedResult.command);
186
+ assert.equal(result.isSuccessful, expectedResult.isSuccessful);
187
+ assert.equal(result.data, expectedResult.data);
188
+ });
189
+
190
+ it(`should return isSuccessful: false in ExecutionResult for failed execution of command.`, () => {
191
+ const error = new Error("Injected failure.");
192
+ sandbox.stub(child_process, "sync").throws(error);
193
+
194
+ const command = "some-command";
195
+ const args = ["--some-option", "some-value"];
196
+ const expectedError = {
197
+ command: "some-command --some-option some-value",
198
+ isSuccessful: false,
199
+ error
200
+ };
201
+
202
+ try {
203
+ cliProcess.executeSync(command, args);
204
+ } catch (error: unknown) {
205
+ const executionError = error as ExecutionResult;
206
+ assert.equal(executionError === undefined, false);
207
+ assert.equal(executionError.command, expectedError.command);
208
+ assert.equal(executionError.isSuccessful, expectedError.isSuccessful);
209
+ assert.equal(executionError.error, expectedError.error);
210
+ }
211
+ });
212
+ });
213
+
214
+ describe("handleError ...", () => {
215
+ it(`should only log "Aborting installation" if error is undefined.`, () => {
216
+ cliProcess.handleError(undefined);
217
+
218
+ assert.equal(logger.error.callCount, 1);
219
+ assert.equal(logger.error.calledWith("Aborting installation.", { prefix: "\n" }), true);
220
+ });
221
+
222
+ it(`should log command if error has a non-empty command property.`, () => {
223
+ const command = "some-failing-command";
224
+ cliProcess.handleError({ command });
225
+
226
+ assert.equal(logger.error.callCount, 1);
227
+ assert.equal(logger.error.calledWith("Aborting installation.", { prefix: "\n" }), true);
228
+
229
+ assert.equal(logger.warning.callCount, 1);
230
+ assert.equal(
231
+ logger.warning.calledWith(`${command} has failed.`, {
232
+ prefix: " ",
233
+ postfix: "\n"
234
+ }),
235
+ true
236
+ );
237
+ });
238
+
239
+ it(`should log "Unexpected error" if error does not have a command property.`, () => {
240
+ const error = new Error("Injected failure.");
241
+ cliProcess.handleError(error);
242
+
243
+ assert.equal(logger.error.callCount, 3);
244
+ assert.equal(logger.error.getCall(0).calledWith("Aborting installation."), true);
245
+ assert.equal(logger.error.getCall(1).calledWith("Unexpected error. Please report it as a bug:"), true);
246
+ assert.equal(logger.error.getCall(2).calledWith(error), true);
247
+ });
248
+ });
249
+
250
+ describe("removeAddOn ...", () => {
251
+ const runs = [
252
+ {},
253
+ { addOnName: "hello-world", addOnDirectory: undefined },
254
+ { addOnName: "", addOnDirectory: "/../hello-world" },
255
+ { addOnName: undefined, addOnDirectory: undefined }
256
+ ];
257
+ runs.forEach(run => {
258
+ it(`should exit if addOnName or addOnDirectory is undefined.`, () => {
259
+ const processStub = sandbox.stub(process, "exit");
260
+
261
+ cliProcess.removeAddOn(run.addOnDirectory, run.addOnName);
262
+
263
+ assert.equal(processStub.callCount, 1);
264
+ assert.equal(processStub.calledWith(0), true);
265
+ });
266
+ });
267
+
268
+ it(`should remove known generated files along with addOnDirectory when no other files are present.`, () => {
269
+ const addOnName = "hello-world";
270
+ const addOnDirectory = "/../hello-world";
271
+
272
+ const pathJoinStub = sandbox.stub(path, "join");
273
+ const addOnDirectoryPath = "../../joined-path";
274
+ pathJoinStub.withArgs(addOnDirectory).returns(addOnDirectoryPath);
275
+
276
+ const readDirStub = sandbox.stub(fs, "readdirSync");
277
+ const addOnFiles = [
278
+ { name: "package.json" },
279
+ { name: "package-lock.json" },
280
+ { name: "node_modules" }
281
+ ] as Dirent[];
282
+ readDirStub.withArgs(addOnDirectoryPath, { withFileTypes: true }).returns(addOnFiles);
283
+
284
+ const removeStub = sandbox.stub(fs, "removeSync");
285
+
286
+ const pathResolveStub = sandbox.stub(path, "resolve");
287
+ pathResolveStub.returns("../../resolved-path");
288
+
289
+ const changeDirStub = sandbox.stub(process, "chdir");
290
+
291
+ cliProcess.removeAddOn(addOnDirectory, addOnName);
292
+
293
+ assert.equal(pathJoinStub.callCount, 4);
294
+ assert.equal(readDirStub.callCount, 1);
295
+ assert.equal(logger.information.callCount, 4);
296
+ assert.equal(logger.warning.callCount, 1);
297
+
298
+ assert.equal(logger.information.calledWith(`Deleting generated file/directory package.json ...`), true);
299
+
300
+ assert.equal(
301
+ logger.information.calledWith(`Deleting generated file/directory package-lock.json ...`),
302
+ true
303
+ );
304
+
305
+ assert.equal(logger.information.calledWith(`Deleting generated file/directory node_modules ...`), true);
306
+
307
+ assert.equal(logger.information.calledWith(`Deleting ${addOnName}/ from ../../resolved-path`), true);
308
+
309
+ assert.equal(logger.warning.calledWith("Done."), true);
310
+
311
+ assert.equal(removeStub.callCount, 4);
312
+ assert.equal(pathResolveStub.callCount, 1);
313
+ assert.equal(changeDirStub.callCount, 1);
314
+ });
315
+
316
+ it(`should only remove known generated files when other files are present.`, () => {
317
+ const addOnName = "hello-world";
318
+ const addOnDirectory = "/../hello-world";
319
+
320
+ const pathJoinStub = sandbox.stub(path, "join");
321
+ const addOnDirectoryPath = "../../joined-path";
322
+ pathJoinStub.withArgs(addOnDirectory).returns(addOnDirectoryPath);
323
+
324
+ const readDirStub = sandbox.stub(fs, "readdirSync");
325
+ const addOnFiles = [
326
+ { name: "package.json" },
327
+ { name: "package-lock.json" },
328
+ { name: "node_modules" },
329
+ { name: "xyz" }
330
+ ] as Dirent[];
331
+ readDirStub.withArgs(addOnDirectoryPath, { withFileTypes: true }).returns(addOnFiles);
332
+
333
+ const removeStub = sandbox.stub(fs, "removeSync");
334
+
335
+ const pathResolveStub = sandbox.stub(path, "resolve");
336
+ pathResolveStub.returns("../../resolved-path");
337
+
338
+ const changeDirStub = sandbox.stub(process, "chdir");
339
+
340
+ cliProcess.removeAddOn(addOnDirectory, addOnName);
341
+
342
+ assert.equal(pathJoinStub.callCount, 4);
343
+ assert.equal(readDirStub.callCount, 1);
344
+ assert.equal(logger.information.callCount, 3);
345
+ assert.equal(logger.warning.callCount, 1);
346
+
347
+ assert.equal(logger.information.calledWith(`Deleting generated file/directory package.json ...`), true);
348
+
349
+ assert.equal(
350
+ logger.information.calledWith(`Deleting generated file/directory package-lock.json ...`),
351
+ true
352
+ );
353
+
354
+ assert.equal(logger.information.calledWith(`Deleting generated file/directory node_modules ...`), true);
355
+
356
+ assert.equal(logger.warning.calledWith("Done."), true);
357
+
358
+ assert.equal(removeStub.callCount, 3);
359
+ assert.equal(pathResolveStub.callCount, 0);
360
+ assert.equal(changeDirStub.callCount, 0);
361
+ });
362
+ });
363
+ });