@neurodevs/ndx-cli 0.1.55 → 0.1.58

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 (40) hide show
  1. package/build/__tests__/AbstractCommandRunnerTest.d.ts +66 -0
  2. package/build/__tests__/AbstractCommandRunnerTest.js +264 -0
  3. package/build/__tests__/AbstractCommandRunnerTest.js.map +1 -0
  4. package/build/__tests__/modules/CliCommandRunner.test.d.ts +2 -94
  5. package/build/__tests__/modules/CliCommandRunner.test.js +2 -675
  6. package/build/__tests__/modules/CliCommandRunner.test.js.map +1 -1
  7. package/build/__tests__/modules/commands/BindSnippetCommand.test.d.ts +20 -0
  8. package/build/__tests__/modules/commands/BindSnippetCommand.test.js +111 -0
  9. package/build/__tests__/modules/commands/BindSnippetCommand.test.js.map +1 -0
  10. package/build/__tests__/modules/commands/CreateImplCommand.test.d.ts +13 -0
  11. package/build/__tests__/modules/commands/CreateImplCommand.test.js +112 -0
  12. package/build/__tests__/modules/commands/CreateImplCommand.test.js.map +1 -0
  13. package/build/__tests__/modules/commands/CreatePackageCommand.test.d.ts +10 -0
  14. package/build/__tests__/modules/commands/CreatePackageCommand.test.js +98 -0
  15. package/build/__tests__/modules/commands/CreatePackageCommand.test.js.map +1 -0
  16. package/build/__tests__/modules/commands/CreateUiCommand.test.d.ts +22 -0
  17. package/build/__tests__/modules/commands/CreateUiCommand.test.js +220 -0
  18. package/build/__tests__/modules/commands/CreateUiCommand.test.js.map +1 -0
  19. package/build/__tests__/modules/commands/UpgradePackageCommand.test.d.ts +11 -0
  20. package/build/__tests__/modules/commands/UpgradePackageCommand.test.js +90 -0
  21. package/build/__tests__/modules/commands/UpgradePackageCommand.test.js.map +1 -0
  22. package/build/modules/CliCommandRunner.d.ts +4 -2
  23. package/build/modules/CliCommandRunner.js +18 -7
  24. package/build/modules/CliCommandRunner.js.map +1 -1
  25. package/build/modules/commands/BindSnippetCommand.d.ts +16 -0
  26. package/build/modules/commands/BindSnippetCommand.js +67 -0
  27. package/build/modules/commands/BindSnippetCommand.js.map +1 -0
  28. package/build/scripts/runCommandRunner.js +1 -1
  29. package/build/scripts/runCommandRunner.js.map +1 -1
  30. package/package.json +2 -2
  31. package/src/__tests__/AbstractCommandRunnerTest.ts +321 -0
  32. package/src/__tests__/modules/CliCommandRunner.test.ts +3 -910
  33. package/src/__tests__/modules/commands/BindSnippetCommand.test.ts +129 -0
  34. package/src/__tests__/modules/commands/CreateImplCommand.test.ts +137 -0
  35. package/src/__tests__/modules/commands/CreatePackageCommand.test.ts +113 -0
  36. package/src/__tests__/modules/commands/CreateUiCommand.test.ts +297 -0
  37. package/src/__tests__/modules/commands/UpgradePackageCommand.test.ts +105 -0
  38. package/src/modules/CliCommandRunner.ts +19 -7
  39. package/src/modules/commands/BindSnippetCommand.ts +75 -0
  40. package/src/scripts/runCommandRunner.ts +1 -1
@@ -0,0 +1,321 @@
1
+ import { exec as execSync } from 'child_process'
2
+ import { mkdir, readFile, writeFile } from 'fs/promises'
3
+ import os from 'os'
4
+ import path from 'path'
5
+ import { promisify } from 'util'
6
+ import { generateId } from '@sprucelabs/test-utils'
7
+ import {
8
+ fakeExec,
9
+ fakeLog,
10
+ fakeMkdir,
11
+ fakeReadFile,
12
+ fakeWriteFile,
13
+ resetCallsToExec,
14
+ resetCallsToLog,
15
+ resetCallsToMkdir,
16
+ resetCallsToReadFile,
17
+ resetCallsToWriteFile,
18
+ setFakeReadFileResult,
19
+ } from '@neurodevs/fake-node-core'
20
+ import {
21
+ FakeAutomodule,
22
+ FakeAutopackage,
23
+ FakeSnippetKeybinder,
24
+ ImplAutomodule,
25
+ NpmAutopackage,
26
+ UiAutomodule,
27
+ VscodeSnippetKeybinder,
28
+ } from '@neurodevs/meta-node'
29
+ import prompts from 'prompts'
30
+ import CliCommandRunner from '../modules/CliCommandRunner'
31
+ import fakePrompts, {
32
+ resetCallsToFakePrompts,
33
+ } from '../testDoubles/prompts/fakePrompts'
34
+ import AbstractPackageTest from './AbstractPackageTest'
35
+
36
+ const exec = promisify(execSync)
37
+
38
+ export default class AbstractCommandRunnerTest extends AbstractPackageTest {
39
+ protected static readonly bindSnippetCommand = 'bind.snippet'
40
+
41
+ protected static readonly createImplCommand = 'create.impl'
42
+ protected static readonly interfaceName = generateId()
43
+ protected static readonly implName = generateId()
44
+
45
+ protected static readonly createPackageCommand = 'create.package'
46
+ protected static readonly packageName = generateId()
47
+ protected static readonly description = generateId()
48
+ protected static readonly keywords = [generateId(), generateId()]
49
+ protected static readonly githubToken = generateId()
50
+
51
+ protected static readonly defaultKeywords = ['nodejs', 'typescript', 'tdd']
52
+
53
+ protected static get keywordsWithDefaults() {
54
+ return [...this.defaultKeywords, ...this.keywords]
55
+ }
56
+
57
+ protected static readonly createUiCommand = 'create.ui'
58
+ protected static readonly componentName = generateId()
59
+
60
+ protected static readonly upgradePackageCommand = 'upgrade.package'
61
+
62
+ protected static async beforeEach() {
63
+ await super.beforeEach()
64
+
65
+ this.setFakeAutopackage()
66
+ this.setFakeImplAutomodule()
67
+ this.setFakeUiAutomodule()
68
+ this.setFakeSnippetKeybinder()
69
+
70
+ this.setFakeExec()
71
+ this.setFakeLog()
72
+ this.setFakeMkdir()
73
+ this.setFakePrompts()
74
+ this.setFakeReadFile()
75
+ this.setFakeWriteFile()
76
+
77
+ process.env.GITHUB_TOKEN = this.githubToken
78
+ }
79
+
80
+ protected static setFakeReadFileResultToTsconfig() {
81
+ setFakeReadFileResult(this.tsconfigPath, this.originalTsconfigFile)
82
+ }
83
+
84
+ protected static setFakeReadToEmptyPackageJson() {
85
+ setFakeReadFileResult('package.json', '{}')
86
+ }
87
+
88
+ protected static setFakeReadToAllInstalledExcept(dep: string) {
89
+ setFakeReadFileResult(
90
+ 'package.json',
91
+ this.allInstalled.replace(dep, '')
92
+ )
93
+ }
94
+
95
+ protected static setFakeReadToAllInstalled() {
96
+ setFakeReadFileResult('package.json', this.allInstalled)
97
+ }
98
+
99
+ protected static setFakePackageJson(
100
+ responses?: Record<string, string | string[]>
101
+ ) {
102
+ const infoFromPackageJson = {
103
+ ...this.infoFromPackageJson,
104
+ ...responses,
105
+ }
106
+
107
+ setFakeReadFileResult(
108
+ 'package.json',
109
+ JSON.stringify(infoFromPackageJson)
110
+ )
111
+
112
+ return infoFromPackageJson
113
+ }
114
+
115
+ protected static readonly infoFromPackageJson = {
116
+ name: this.packageName,
117
+ description: this.description,
118
+ keywords: this.keywordsWithDefaults,
119
+ }
120
+
121
+ protected static readonly allInstalled = `
122
+ {
123
+ "dependencies": {
124
+ "react": "^...",
125
+ "react-dom": "^..."
126
+ },
127
+ "devDependencies": {
128
+ "@types/react": "^...",
129
+ "@types/react-dom": "^...",
130
+ "@types/jsdom": "^...",
131
+ "@testing-library/react": "^...",
132
+ "@testing-library/dom": "^...",
133
+ "@testing-library/jest-dom": "^...",
134
+ "jsdom": "^..."
135
+ }
136
+ }
137
+ `
138
+
139
+ protected static readonly allRequiredDependencies = [
140
+ 'react',
141
+ 'react-dom',
142
+ '@types/react',
143
+ '@types/react-dom',
144
+ '@types/jsdom',
145
+ '@testing-library/react',
146
+ '@testing-library/dom',
147
+ '@testing-library/jest-dom',
148
+ 'jsdom',
149
+ ]
150
+
151
+ protected static readonly installDependenciesCommand =
152
+ 'yarn add react react-dom'
153
+
154
+ protected static readonly installDevDependenciesCommand =
155
+ 'yarn add -D @types/react @types/react-dom @types/jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom jsdom'
156
+
157
+ protected static expandHomeDir(inputPath: string): string {
158
+ return inputPath.startsWith('~')
159
+ ? path.join(os.homedir(), inputPath.slice(1))
160
+ : inputPath
161
+ }
162
+
163
+ protected static splitOnCommaOrWhitespace(value: string) {
164
+ return value
165
+ .split(/[\s,]+/)
166
+ .map((v: string) => v.trim())
167
+ .filter(Boolean)
168
+ }
169
+
170
+ protected static readonly implTestSaveDir = 'src/__tests__/modules'
171
+ protected static readonly implModuleSaveDir = 'src/modules'
172
+
173
+ protected static get implFakeSaveDir() {
174
+ return `src/testDoubles/${this.interfaceName}`
175
+ }
176
+
177
+ protected static readonly uiTestSaveDir = 'src/__tests__/ui'
178
+ protected static readonly uiModuleSaveDir = 'src/ui'
179
+
180
+ protected static get uiFakeSaveDir() {
181
+ return `src/testDoubles/${this.componentName}`
182
+ }
183
+
184
+ protected static readonly tsconfigPath = 'tsconfig.json'
185
+ protected static readonly randomId = generateId()
186
+
187
+ protected static readonly originalTsconfigFile = JSON.stringify(
188
+ {
189
+ [this.randomId]: this.randomId,
190
+ compilerOptions: {
191
+ [this.randomId]: this.randomId,
192
+ },
193
+ },
194
+ null,
195
+ 4
196
+ )
197
+
198
+ protected static readonly updatedTsconfigFile = JSON.stringify(
199
+ {
200
+ [this.randomId]: this.randomId,
201
+ compilerOptions: {
202
+ jsx: 'react-jsx',
203
+ [this.randomId]: this.randomId,
204
+ },
205
+ include: ['src'],
206
+ },
207
+ null,
208
+ 4
209
+ )
210
+
211
+ protected static setFakeAutopackage() {
212
+ NpmAutopackage.Class = FakeAutopackage
213
+ FakeAutopackage.resetTestDouble()
214
+ }
215
+
216
+ protected static setFakeImplAutomodule() {
217
+ ImplAutomodule.Class = FakeAutomodule
218
+ FakeAutomodule.resetTestDouble()
219
+ }
220
+
221
+ protected static setFakeUiAutomodule() {
222
+ UiAutomodule.Class = FakeAutomodule
223
+ FakeAutomodule.resetTestDouble()
224
+ }
225
+
226
+ protected static setFakeSnippetKeybinder() {
227
+ VscodeSnippetKeybinder.Class = FakeSnippetKeybinder
228
+ FakeSnippetKeybinder.resetTestDouble()
229
+ }
230
+
231
+ protected static setFakeExec() {
232
+ CliCommandRunner.exec = fakeExec as unknown as typeof exec
233
+ resetCallsToExec()
234
+ }
235
+
236
+ protected static setFakeLog() {
237
+ CliCommandRunner.log = fakeLog as unknown as typeof CliCommandRunner.log
238
+ resetCallsToLog()
239
+ }
240
+
241
+ protected static setFakeMkdir() {
242
+ CliCommandRunner.mkdir = fakeMkdir as unknown as typeof mkdir
243
+ resetCallsToMkdir()
244
+ }
245
+
246
+ protected static setFakePrompts() {
247
+ CliCommandRunner.prompts = fakePrompts as unknown as typeof prompts
248
+ resetCallsToFakePrompts()
249
+ }
250
+
251
+ protected static setFakeReadFile() {
252
+ CliCommandRunner.readFile = fakeReadFile as unknown as typeof readFile
253
+ resetCallsToReadFile()
254
+
255
+ this.setFakeReadToAllInstalled()
256
+ this.setFakeReadFileResultToTsconfig()
257
+ }
258
+
259
+ protected static setFakeWriteFile() {
260
+ CliCommandRunner.writeFile =
261
+ fakeWriteFile as unknown as typeof writeFile
262
+ resetCallsToWriteFile()
263
+ }
264
+
265
+ protected static readonly interfaceNameMessage =
266
+ 'What should the interface be called? Example: YourInterface'
267
+
268
+ protected static readonly implNameMessage =
269
+ 'What should the implementation class be called? Example: YourInterfaceImpl'
270
+
271
+ protected static readonly packageNameMessage =
272
+ 'What should the package be called? Example: useful-package'
273
+
274
+ protected static readonly packageDescriptionMessage =
275
+ 'What should the package description be? Example: A useful package.'
276
+
277
+ protected static readonly componentNameMessage =
278
+ 'What should the component be called? Example: YourComponent'
279
+
280
+ protected static readonly setupTestsFile = `
281
+ import { JSDOM } from 'jsdom'
282
+
283
+ const jsdom = new JSDOM('<!doctype html><html><body></body></html>', {
284
+ url: 'http://localhost',
285
+ })
286
+
287
+ global.window = jsdom.window as unknown as Window & typeof globalThis
288
+ global.document = jsdom.window.document
289
+ global.navigator = jsdom.window.navigator
290
+ global.HTMLElement = jsdom.window.HTMLElement
291
+ global.getComputedStyle = jsdom.window.getComputedStyle
292
+
293
+ global.ResizeObserver = class {
294
+ public observe() {}
295
+ public unobserve() {}
296
+ public disconnect() {}
297
+ }
298
+
299
+ global.SVGElement = jsdom.window.SVGElement
300
+ `
301
+
302
+ protected static readonly helpText = `ndx CLI (Command Line Interface)
303
+
304
+ Available commands:
305
+
306
+ - bind.snippet Bind a text snippet to a keyboard shortcut in vscode.
307
+ - create.impl Create implementation for interface with test and fake.
308
+ - create.package Create npm package using latest template.
309
+ - create.ui Create React component with test and fake.
310
+ - upgrade.package Upgrade existing npm package to latest template.
311
+ - help, --help, -h Show this help text.
312
+
313
+ Usage:
314
+
315
+ - ndx <command>
316
+ `
317
+
318
+ protected static CliCommandRunner(args: string[]) {
319
+ return CliCommandRunner.Create(args)
320
+ }
321
+ }