@neurodevs/meta-node 0.7.0 → 0.9.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 (50) hide show
  1. package/build/__tests__/AbstractAutomoduleTest.d.ts +17 -0
  2. package/build/__tests__/AbstractAutomoduleTest.js +56 -0
  3. package/build/__tests__/AbstractAutomoduleTest.js.map +1 -0
  4. package/build/__tests__/modules/ImplAutomodule.test.d.ts +3 -13
  5. package/build/__tests__/modules/ImplAutomodule.test.js +15 -50
  6. package/build/__tests__/modules/ImplAutomodule.test.js.map +1 -1
  7. package/build/__tests__/modules/UiAutomodule.test.d.ts +16 -0
  8. package/build/__tests__/modules/UiAutomodule.test.js +168 -0
  9. package/build/__tests__/modules/UiAutomodule.test.js.map +1 -0
  10. package/build/index.d.ts +4 -0
  11. package/build/index.js +7 -1
  12. package/build/index.js.map +1 -1
  13. package/build/modules/AbstractAutomodule.d.ts +44 -0
  14. package/build/modules/AbstractAutomodule.js +79 -0
  15. package/build/modules/AbstractAutomodule.js.map +1 -0
  16. package/build/modules/ImplAutomodule.d.ts +10 -32
  17. package/build/modules/ImplAutomodule.js +20 -64
  18. package/build/modules/ImplAutomodule.js.map +1 -1
  19. package/build/modules/NpmAutopackage.d.ts +3 -3
  20. package/build/modules/NpmAutopackage.js +8 -7
  21. package/build/modules/NpmAutopackage.js.map +1 -1
  22. package/build/modules/UiAutomodule.d.ts +20 -0
  23. package/build/modules/UiAutomodule.js +123 -0
  24. package/build/modules/UiAutomodule.js.map +1 -0
  25. package/build/scripts/{runAutomodule.js → runImplAutomodule.js} +1 -1
  26. package/build/scripts/runImplAutomodule.js.map +1 -0
  27. package/build/scripts/runUiAutomodule.d.ts +1 -0
  28. package/build/scripts/runUiAutomodule.js +22 -0
  29. package/build/scripts/runUiAutomodule.js.map +1 -0
  30. package/build/testDoubles/Automodule/FakeAutomodule.d.ts +7 -4
  31. package/build/testDoubles/Automodule/FakeAutomodule.js +6 -6
  32. package/build/testDoubles/Automodule/FakeAutomodule.js.map +1 -1
  33. package/build/types.d.ts +8 -0
  34. package/build/types.js +3 -0
  35. package/build/types.js.map +1 -0
  36. package/package.json +2 -3
  37. package/src/__tests__/AbstractAutomoduleTest.ts +83 -0
  38. package/src/__tests__/modules/ImplAutomodule.test.ts +17 -78
  39. package/src/__tests__/modules/UiAutomodule.test.ts +174 -0
  40. package/src/index.ts +6 -0
  41. package/src/modules/AbstractAutomodule.ts +133 -0
  42. package/src/modules/ImplAutomodule.ts +32 -98
  43. package/src/modules/NpmAutopackage.ts +8 -7
  44. package/src/modules/UiAutomodule.ts +143 -0
  45. package/src/scripts/runUiAutomodule.ts +23 -0
  46. package/src/testDoubles/Automodule/FakeAutomodule.ts +10 -6
  47. package/src/types.ts +9 -0
  48. package/build/scripts/runAutomodule.js.map +0 -1
  49. /package/build/scripts/{runAutomodule.d.ts → runImplAutomodule.d.ts} +0 -0
  50. /package/src/scripts/{runAutomodule.ts → runImplAutomodule.ts} +0 -0
@@ -1,25 +1,23 @@
1
1
  import { exec as execSync } from 'child_process'
2
- import { readFile, writeFile } from 'fs/promises'
3
- import path from 'path'
2
+ import { readFile } from 'fs/promises'
4
3
  import { promisify } from 'util'
5
- import { pathExists } from 'fs-extra'
6
-
7
- export default class ImplAutomodule implements Automodule {
8
- public static Class?: AutomoduleConstructor
4
+ import { Automodule, BaseAutomoduleOptions } from '../types'
5
+ import AbstractAutomodule from './AbstractAutomodule'
6
+
7
+ export default class ImplAutomodule
8
+ extends AbstractAutomodule
9
+ implements Automodule
10
+ {
11
+ public static Class?: ImplAutomoduleConstructor
9
12
  public static exec = promisify(execSync)
10
- public static pathExists = pathExists
11
13
  public static readFile = readFile
12
- public static writeFile = writeFile
13
14
 
14
- private testSaveDir: string
15
- private moduleSaveDir: string
16
- private fakeSaveDir: string
17
15
  private interfaceName: string
18
16
  private implName: string
19
17
 
20
18
  private originalIndexFile!: string
21
19
 
22
- protected constructor(options: AutomoduleOptions) {
20
+ protected constructor(options: ImplAutomoduleOptions) {
23
21
  const {
24
22
  testSaveDir,
25
23
  moduleSaveDir,
@@ -28,84 +26,35 @@ export default class ImplAutomodule implements Automodule {
28
26
  implName,
29
27
  } = options
30
28
 
31
- this.testSaveDir = testSaveDir
32
- this.moduleSaveDir = moduleSaveDir
33
- this.fakeSaveDir = fakeSaveDir
29
+ super({
30
+ testSaveDir,
31
+ moduleSaveDir,
32
+ fakeSaveDir,
33
+ })
34
+
34
35
  this.interfaceName = interfaceName
35
36
  this.implName = implName
36
37
  }
37
38
 
38
- public static Create(options: AutomoduleOptions) {
39
+ public static Create(options: ImplAutomoduleOptions) {
39
40
  return new (this.Class ?? this)(options)
40
41
  }
41
42
 
42
43
  public async run() {
43
- await this.throwIfDirectoriesDoNotExist()
44
-
45
- await this.createTestFile()
46
- await this.createModuleFile()
47
- await this.createFakeFile()
48
-
44
+ await this.generateFiles()
49
45
  await this.updateIndexFileExports()
50
46
  await this.bumpMinorVersion()
51
47
  }
52
48
 
53
- private async throwIfDirectoriesDoNotExist() {
54
- await this.throwIfTestDirDoesNotExist()
55
- await this.throwIfModuleDirDoesNotExist()
56
- await this.throwIfFakeDirDoesNotExist()
57
- }
58
-
59
- private async throwIfTestDirDoesNotExist() {
60
- const testDirExists = await this.pathExists(this.testSaveDir)
61
-
62
- if (!testDirExists) {
63
- this.throw(`testSaveDir does not exist: ${this.testSaveDir}!`)
64
- }
65
- }
66
-
67
- private async throwIfModuleDirDoesNotExist() {
68
- const moduleDirExists = await this.pathExists(this.moduleSaveDir)
69
-
70
- if (!moduleDirExists) {
71
- this.throw(`moduleSaveDir does not exist: ${this.moduleSaveDir}!`)
72
- }
73
- }
74
-
75
- private async throwIfFakeDirDoesNotExist() {
76
- const fakeDirExists = await this.pathExists(this.fakeSaveDir)
77
-
78
- if (!fakeDirExists) {
79
- this.throw(`fakeSaveDir does not exist: ${this.fakeSaveDir}!`)
80
- }
81
- }
82
-
83
- private throw(err: string) {
84
- throw new Error(err)
85
- }
86
-
87
- private async createTestFile() {
88
- await this.writeFile(this.testFileName, this.testFilePattern)
89
- }
90
-
91
- private get testFileName() {
92
- return path.join(this.testSaveDir, `${this.implName}.test.ts`)
93
- }
94
-
95
- private async createModuleFile() {
96
- await this.writeFile(this.moduleFileName, this.moduleFilePattern)
97
- }
98
-
99
- private get moduleFileName() {
100
- return path.join(this.moduleSaveDir, `${this.implName}.ts`)
101
- }
102
-
103
- private async createFakeFile() {
104
- await this.writeFile(this.fakeFileName, this.fakeFilePattern)
105
- }
106
-
107
- private get fakeFileName() {
108
- return path.join(this.fakeSaveDir, `Fake${this.interfaceName}.ts`)
49
+ private async generateFiles() {
50
+ await this.runAbstract({
51
+ testFileName: `${this.implName}.test.ts`,
52
+ testFileContent: this.testFilePattern,
53
+ moduleFileName: `${this.implName}.ts`,
54
+ moduleFileContent: this.moduleFilePattern,
55
+ fakeFileName: `Fake${this.interfaceName}.ts`,
56
+ fakeFileContent: this.fakeFilePattern,
57
+ })
109
58
  }
110
59
 
111
60
  private async updateIndexFileExports() {
@@ -142,18 +91,10 @@ export default class ImplAutomodule implements Automodule {
142
91
  return ImplAutomodule.exec
143
92
  }
144
93
 
145
- private get pathExists() {
146
- return ImplAutomodule.pathExists
147
- }
148
-
149
94
  private get readFile() {
150
95
  return ImplAutomodule.readFile
151
96
  }
152
97
 
153
- private get writeFile() {
154
- return ImplAutomodule.writeFile
155
- }
156
-
157
98
  private get testFilePattern() {
158
99
  return `
159
100
  import AbstractSpruceTest, { test, assert } from '@sprucelabs/test-utils'
@@ -161,10 +102,10 @@ export default class ImplAutomodule implements Automodule {
161
102
 
162
103
  export default class ${this.implName}Test extends AbstractSpruceTest {
163
104
  private static instance: ${this.interfaceName}
164
-
105
+
165
106
  protected static async beforeEach() {
166
107
  await super.beforeEach()
167
-
108
+
168
109
  this.instance = this.${this.implName}()
169
110
  }
170
111
 
@@ -231,18 +172,11 @@ export default class ImplAutomodule implements Automodule {
231
172
  }
232
173
  }
233
174
 
234
- export interface Automodule {
235
- run(): Promise<void>
236
- }
237
-
238
- export type AutomoduleConstructor = new (
239
- options: AutomoduleOptions
175
+ export type ImplAutomoduleConstructor = new (
176
+ options: ImplAutomoduleOptions
240
177
  ) => Automodule
241
178
 
242
- export interface AutomoduleOptions {
243
- testSaveDir: string
244
- moduleSaveDir: string
245
- fakeSaveDir: string
179
+ export interface ImplAutomoduleOptions extends BaseAutomoduleOptions {
246
180
  interfaceName: string
247
181
  implName: string
248
182
  }
@@ -1,11 +1,12 @@
1
1
  import { exec as execSync } from 'child_process'
2
2
  import { readFile, writeFile } from 'fs/promises'
3
+ import { chdir } from 'process'
3
4
  import { promisify } from 'util'
4
5
  import { pathExists } from 'fs-extra'
5
6
 
6
7
  export default class NpmAutopackage implements Autopackage {
7
8
  public static Class?: AutopackageConstructor
8
- public static chdir = process.chdir
9
+ public static chdir = chdir
9
10
  public static exec = promisify(execSync)
10
11
  public static fetch = fetch
11
12
  public static pathExists = pathExists
@@ -57,7 +58,7 @@ export default class NpmAutopackage implements Autopackage {
57
58
 
58
59
  this.chdirToPackageDir()
59
60
  await this.spruceCreateModule()
60
- await this.updatePackage()
61
+ await this.updatePackageJson()
61
62
  await this.updateGitignore()
62
63
  await this.setupVscode()
63
64
  }
@@ -165,12 +166,12 @@ export default class NpmAutopackage implements Autopackage {
165
166
  await this.exec('git push')
166
167
  }
167
168
 
168
- private async updatePackage() {
169
+ private async updatePackageJson() {
169
170
  this.originalJsonFile = await this.loadPackageJsonFile()
170
171
 
171
172
  if (!this.isPackageUpToDate) {
172
173
  console.log('Updating package.json...')
173
- await this.updatePackageJson()
174
+ await this.updatePackageJsonFile()
174
175
  await this.commitUpdatePackage()
175
176
  }
176
177
  }
@@ -189,7 +190,7 @@ export default class NpmAutopackage implements Autopackage {
189
190
  )
190
191
  }
191
192
 
192
- private async updatePackageJson() {
193
+ private async updatePackageJsonFile() {
193
194
  const ordered = this.orderJsonKeys(this.updatedJsonFile, [
194
195
  'name',
195
196
  'version',
@@ -278,7 +279,7 @@ export default class NpmAutopackage implements Autopackage {
278
279
  if (!this.isGitignoreUpdated) {
279
280
  console.log('Updating .gitignore...')
280
281
 
281
- await this.addBuildDirToGitignore()
282
+ await this.updateGitignoreFile()
282
283
  await this.commitUpdateGitignore()
283
284
  }
284
285
  }
@@ -301,7 +302,7 @@ export default class NpmAutopackage implements Autopackage {
301
302
  return lines.includes('build/')
302
303
  }
303
304
 
304
- private async addBuildDirToGitignore() {
305
+ private async updateGitignoreFile() {
305
306
  await this.writeFile(this.gitignorePath, '\nbuild/\n', {
306
307
  encoding: 'utf-8',
307
308
  flag: 'a',
@@ -0,0 +1,143 @@
1
+ import { pathExists } from 'fs-extra'
2
+ import { Automodule, BaseAutomoduleOptions } from '../types'
3
+ import AbstractAutomodule from './AbstractAutomodule'
4
+
5
+ export default class UiAutomodule
6
+ extends AbstractAutomodule
7
+ implements Automodule
8
+ {
9
+ public static Class?: UiAutomoduleConstructor
10
+ public static pathExists = pathExists
11
+
12
+ protected componentName: string
13
+ protected componentNameKebabCase: string
14
+
15
+ protected constructor(options: UiAutomoduleOptions) {
16
+ const { testSaveDir, moduleSaveDir, fakeSaveDir, componentName } =
17
+ options
18
+
19
+ super({
20
+ testSaveDir,
21
+ moduleSaveDir,
22
+ fakeSaveDir,
23
+ })
24
+
25
+ this.componentName = componentName
26
+ this.componentNameKebabCase = this.toKebabCase(componentName)
27
+ }
28
+
29
+ public static Create(options: UiAutomoduleOptions) {
30
+ return new (this.Class ?? this)(options)
31
+ }
32
+
33
+ private toKebabCase(str: string): string {
34
+ return str
35
+ .replace(/([a-z])([A-Z])/g, '$1-$2')
36
+ .replace(/[\s_]+/g, '-')
37
+ .toLowerCase()
38
+ }
39
+
40
+ public async run() {
41
+ await this.runAbstract({
42
+ testFileName: `${this.componentName}.test.tsx`,
43
+ testFileContent: this.testFileTemplate,
44
+ moduleFileName: `${this.componentName}.tsx`,
45
+ moduleFileContent: this.moduleFilePattern,
46
+ fakeFileName: `Fake${this.componentName}.tsx`,
47
+ fakeFileContent: this.fakeFilePattern,
48
+ })
49
+ }
50
+
51
+ private get testFileTemplate() {
52
+ return `
53
+ import { test, assert } from '@sprucelabs/test-utils'
54
+ import { render, RenderResult } from '@testing-library/react'
55
+ import ${this.componentName} from '../../ui/${this.componentName}'
56
+ import AbstractPackageTest from '../AbstractPackageTest'
57
+
58
+ export default class ${this.componentName}Test extends AbstractPackageTest {
59
+ private static result: RenderResult
60
+
61
+ protected static async beforeEach() {
62
+ await super.beforeEach()
63
+
64
+ this.result = this.render()
65
+ }
66
+
67
+ @test()
68
+ protected static async rendersComponent() {
69
+ assert.isTruthy(this.result, 'Failed to render component!')
70
+ }
71
+
72
+ @test()
73
+ protected static async rendersTopLevelDivWithExpectedClass() {
74
+ assert.isEqual(
75
+ this.div.className,
76
+ this.className,
77
+ 'Incorrect class for top-level div!'
78
+ )
79
+ }
80
+
81
+ private static get div() {
82
+ return this.getByTestId(this.className)
83
+ }
84
+
85
+ private static get getByTestId() {
86
+ return this.result.getByTestId
87
+ }
88
+
89
+ private static readonly className = '${this.toKebabCase(this.componentName)}'
90
+
91
+ protected static render() {
92
+ return render(<${this.componentName} />)
93
+ }
94
+ }
95
+ `
96
+ }
97
+
98
+ private get moduleFilePattern() {
99
+ return `
100
+ import React from 'react'
101
+
102
+ const ${this.componentName}: React.FC = () => {
103
+ return (
104
+ <div
105
+ className="${this.componentNameKebabCase}"
106
+ data-testid="${this.componentNameKebabCase}"
107
+ />
108
+ )
109
+ }
110
+
111
+ export default ${this.componentName}
112
+ `
113
+ }
114
+
115
+ private get fakeFilePattern() {
116
+ return `
117
+ import React from 'react'
118
+ import { ${this.componentName}Props } from '../ui/${this.componentName}'
119
+
120
+ export let last${this.componentName}Props: ${this.componentName}Props | undefined
121
+
122
+ const Fake${this.componentName}: React.FC<${this.componentName}Props> = (
123
+ props: ${this.componentName}Props
124
+ ) => {
125
+ last${this.componentName}Props = props
126
+
127
+ return (
128
+ <div data-testid="${this.componentNameKebabCase}" />
129
+ )
130
+ }
131
+
132
+ export default Fake${this.componentName}
133
+ `
134
+ }
135
+ }
136
+
137
+ export type UiAutomoduleConstructor = new (
138
+ options: UiAutomoduleOptions
139
+ ) => Automodule
140
+
141
+ export interface UiAutomoduleOptions extends BaseAutomoduleOptions {
142
+ componentName: string
143
+ }
@@ -0,0 +1,23 @@
1
+ import UiAutomodule from '../modules/UiAutomodule'
2
+
3
+ async function main() {
4
+ console.log('\nRunning automodule...')
5
+
6
+ const instance = UiAutomodule.Create({
7
+ testSaveDir:
8
+ '/Users/ericthecurious/dev/meta-node/src/__tests__/modules',
9
+ moduleSaveDir: '/Users/ericthecurious/dev/meta-node/src/modules',
10
+ fakeSaveDir:
11
+ '/Users/ericthecurious/dev/meta-node/src/testDoubles/Autothingy',
12
+ componentName: 'Autothingy',
13
+ })
14
+
15
+ await instance.run()
16
+
17
+ console.log('Finished running automodule!\n')
18
+ }
19
+
20
+ main().catch((err) => {
21
+ console.error(err)
22
+ process.exit(1)
23
+ })
@@ -1,15 +1,17 @@
1
- import { Automodule, AutomoduleOptions } from '../../modules/ImplAutomodule'
1
+ import { ImplAutomoduleOptions } from '../../modules/ImplAutomodule'
2
+ import { UiAutomoduleOptions } from '../../modules/UiAutomodule'
3
+ import { Automodule } from '../../types'
2
4
 
3
- export default class FakeAutomodule implements Automodule {
4
- public static callsToConstructor: AutomoduleOptions[] = []
5
+ export default class FakeImplAutomodule implements Automodule {
6
+ public static callsToConstructor: FakeAutomoduleOptions[] = []
5
7
  public static numCallsToRun = 0
6
8
 
7
- public constructor(options: AutomoduleOptions) {
8
- FakeAutomodule.callsToConstructor.push(options)
9
+ public constructor(options: FakeAutomoduleOptions) {
10
+ FakeImplAutomodule.callsToConstructor.push(options)
9
11
  }
10
12
 
11
13
  public async run() {
12
- FakeAutomodule.numCallsToRun++
14
+ FakeImplAutomodule.numCallsToRun++
13
15
  }
14
16
 
15
17
  public static resetTestDouble() {
@@ -17,3 +19,5 @@ export default class FakeAutomodule implements Automodule {
17
19
  this.numCallsToRun = 0
18
20
  }
19
21
  }
22
+
23
+ export type FakeAutomoduleOptions = ImplAutomoduleOptions | UiAutomoduleOptions
package/src/types.ts ADDED
@@ -0,0 +1,9 @@
1
+ export interface Automodule {
2
+ run(): Promise<void>
3
+ }
4
+
5
+ export interface BaseAutomoduleOptions {
6
+ testSaveDir: string
7
+ moduleSaveDir: string
8
+ fakeSaveDir: string
9
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"runAutomodule.js","sourceRoot":"","sources":["../../src/scripts/runAutomodule.ts"],"names":[],"mappings":";;;;;AAAA,+EAAsD;AAEtD,KAAK,UAAU,IAAI;IACf,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAA;IAEtC,MAAM,QAAQ,GAAG,wBAAc,CAAC,MAAM,CAAC;QACnC,WAAW,EACP,2DAA2D;QAC/D,aAAa,EAAE,iDAAiD;QAChE,WAAW,EACP,gEAAgE;QACpE,aAAa,EAAE,YAAY;QAC3B,QAAQ,EAAE,mBAAmB;KAChC,CAAC,CAAA;IAEF,MAAM,QAAQ,CAAC,GAAG,EAAE,CAAA;IAEpB,OAAO,CAAC,GAAG,CAAC,gCAAgC,CAAC,CAAA;AACjD,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACjB,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAClB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;AACnB,CAAC,CAAC,CAAA"}