create-payload-app 4.0.0-internal.af6aad0 → 4.0.0-internal.b8a92d2
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/lib/agent-config.d.ts +2 -5
- package/dist/lib/agent-config.d.ts.map +1 -1
- package/dist/lib/agent-config.js +1 -8
- package/dist/lib/agent-config.js.map +1 -1
- package/dist/lib/create-project.d.ts.map +1 -1
- package/dist/lib/create-project.js +51 -8
- package/dist/lib/create-project.js.map +1 -1
- package/dist/lib/get-package-manager.d.ts +9 -0
- package/dist/lib/get-package-manager.d.ts.map +1 -1
- package/dist/lib/get-package-manager.js +20 -0
- package/dist/lib/get-package-manager.js.map +1 -1
- package/dist/lib/tanstack/template-files.d.ts +2 -2
- package/dist/lib/tanstack/template-files.d.ts.map +1 -1
- package/dist/lib/tanstack/template-files.js +2 -2
- package/dist/lib/tanstack/template-files.js.map +1 -1
- package/dist/lib/update-payload-in-project.d.ts.map +1 -1
- package/dist/lib/update-payload-in-project.js +7 -2
- package/dist/lib/update-payload-in-project.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +0 -1
- package/dist/main.js.map +1 -1
- package/dist/template/src/app/(frontend)/styles.css +7 -0
- package/dist/template/src/app/(payload)/admin/importMap.js +17 -0
- package/dist/template/src/payload-types.ts +72 -1
- package/dist/template/src/payload.config.ts +8 -0
- package/dist/template-tanstack/routes/_payload.tsx +22 -5
- package/dist/template-tanstack/src/payload.css +1 -0
- package/dist/utils/messages.js +1 -1
- package/dist/utils/messages.js.map +1 -1
- package/package.json +2 -2
- package/dist/ast-integration.spec.js +0 -132
- package/dist/ast-integration.spec.js.map +0 -1
- package/dist/lib/agent-config.spec.js +0 -23
- package/dist/lib/agent-config.spec.js.map +0 -1
- package/dist/lib/ast/package-json.spec.js +0 -139
- package/dist/lib/ast/package-json.spec.js.map +0 -1
- package/dist/lib/ast/payload-config.spec.js +0 -365
- package/dist/lib/ast/payload-config.spec.js.map +0 -1
- package/dist/lib/ast/utils.spec.js +0 -226
- package/dist/lib/ast/utils.spec.js.map +0 -1
- package/dist/lib/configure-payload-tsconfig.spec.js +0 -135
- package/dist/lib/configure-payload-tsconfig.spec.js.map +0 -1
- package/dist/lib/create-project.spec.js +0 -199
- package/dist/lib/create-project.spec.js.map +0 -1
- package/dist/lib/init-tanstack-default-template.spec.js +0 -119
- package/dist/lib/init-tanstack-default-template.spec.js.map +0 -1
- package/dist/lib/init-tanstack.spec.js +0 -394
- package/dist/lib/init-tanstack.spec.js.map +0 -1
- package/dist/lib/manage-env-files.spec.js +0 -118
- package/dist/lib/manage-env-files.spec.js.map +0 -1
- package/dist/lib/tanstack/detect.spec.js +0 -227
- package/dist/lib/tanstack/detect.spec.js.map +0 -1
- package/dist/lib/tanstack/prepare.spec.js +0 -288
- package/dist/lib/tanstack/prepare.spec.js.map +0 -1
- package/dist/lib/tanstack/transform-root-route.spec.js +0 -484
- package/dist/lib/tanstack/transform-root-route.spec.js.map +0 -1
- package/dist/lib/tanstack/transform-router.spec.js +0 -264
- package/dist/lib/tanstack/transform-router.spec.js.map +0 -1
- package/dist/lib/tanstack/transform-vite-config.spec.js +0 -456
- package/dist/lib/tanstack/transform-vite-config.spec.js.map +0 -1
- package/dist/lib/update-payload-in-project.spec.js +0 -258
- package/dist/lib/update-payload-in-project.spec.js.map +0 -1
- package/dist/lib/wrap-next-config.spec.js +0 -167
- package/dist/lib/wrap-next-config.spec.js.map +0 -1
- package/dist/main.spec.js +0 -102
- package/dist/main.spec.js.map +0 -1
- package/dist/template-tanstack/src/payload-foundation.css +0 -10
- package/dist/utils/resolvePackageVersion.spec.js +0 -77
- package/dist/utils/resolvePackageVersion.spec.js.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/ast/package-json.spec.ts"],"sourcesContent":["import { describe, it, expect, beforeEach, afterEach } from 'vitest'\nimport { preparePackageJson, updatePackageJson } from './package-json'\nimport * as fs from 'fs'\nimport * as path from 'path'\nimport * as os from 'os'\n\ndescribe('updatePackageJson', () => {\n let tempDir: string\n\n beforeEach(() => {\n tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'payload-test-'))\n })\n\n afterEach(() => {\n fs.rmSync(tempDir, { recursive: true, force: true })\n })\n\n it('updates database dependencies', () => {\n const pkgPath = path.join(tempDir, 'package.json')\n const originalPkg = {\n name: 'test-app',\n dependencies: {\n '@payloadcms/db-mongodb': '^3.0.0',\n payload: '^3.0.0',\n },\n }\n fs.writeFileSync(pkgPath, JSON.stringify(originalPkg, null, 2))\n\n updatePackageJson(pkgPath, {\n databaseAdapter: 'postgres',\n })\n\n const updated = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))\n expect(updated.dependencies['@payloadcms/db-postgres']).toBeDefined()\n expect(updated.dependencies['@payloadcms/db-mongodb']).toBeUndefined()\n })\n\n it('removes sharp when requested', () => {\n const pkgPath = path.join(tempDir, 'package.json')\n const originalPkg = {\n name: 'test-app',\n dependencies: {\n payload: '^3.0.0',\n sharp: '^0.33.0',\n },\n }\n fs.writeFileSync(pkgPath, JSON.stringify(originalPkg, null, 2))\n\n updatePackageJson(pkgPath, {\n removeSharp: true,\n })\n\n const updated = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))\n expect(updated.dependencies.sharp).toBeUndefined()\n })\n\n it('should remove selected packages from dependencies and devDependencies', () => {\n const pkgPath = path.join(tempDir, 'package.json')\n const originalPkg = {\n name: 'test-app',\n scripts: {\n dev: 'vite',\n },\n dependencies: {\n '@tanstack/router-plugin': '^1.0.0',\n payload: '^4.0.0',\n },\n devDependencies: {\n '@tanstack/router-plugin': '^1.0.0',\n typescript: '^6.0.0',\n },\n }\n fs.writeFileSync(pkgPath, JSON.stringify(originalPkg, null, 2))\n\n updatePackageJson(pkgPath, {\n removeDependencies: ['@tanstack/router-plugin'],\n })\n\n const updated = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))\n\n expect(updated).toEqual({\n name: 'test-app',\n scripts: {\n dev: 'vite',\n },\n dependencies: {\n payload: '^4.0.0',\n },\n devDependencies: {\n typescript: '^6.0.0',\n },\n })\n })\n\n it('should prepare exact package content without writing to disk', () => {\n const pkgPath = path.join(tempDir, 'package.json')\n const original = JSON.stringify(\n {\n dependencies: {\n '@tanstack/router-plugin': '^1.0.0',\n payload: '^4.0.0',\n },\n devDependencies: {\n '@tanstack/router-plugin': '^1.0.0',\n typescript: '^6.0.0',\n },\n name: 'router-app',\n },\n null,\n 2,\n )\n fs.writeFileSync(pkgPath, original)\n\n const prepared = preparePackageJson({\n filePath: pkgPath,\n options: { removeDependencies: ['@tanstack/router-plugin'] },\n })\n\n expect(prepared).toEqual({\n content: `${JSON.stringify(\n {\n dependencies: {\n payload: '^4.0.0',\n },\n devDependencies: {\n typescript: '^6.0.0',\n },\n name: 'router-app',\n },\n null,\n 2,\n )}\\n`,\n filePath: pkgPath,\n })\n expect(fs.readFileSync(pkgPath, 'utf8')).toBe(original)\n })\n\n it('updates package name', () => {\n const pkgPath = path.join(tempDir, 'package.json')\n const originalPkg = {\n name: 'template-name',\n dependencies: {\n payload: '^3.0.0',\n },\n }\n fs.writeFileSync(pkgPath, JSON.stringify(originalPkg, null, 2))\n\n updatePackageJson(pkgPath, {\n packageName: 'my-new-app',\n })\n\n const updated = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))\n expect(updated.name).toBe('my-new-app')\n })\n})\n"],"names":["describe","it","expect","beforeEach","afterEach","preparePackageJson","updatePackageJson","fs","path","os","tempDir","mkdtempSync","join","tmpdir","rmSync","recursive","force","pkgPath","originalPkg","name","dependencies","payload","writeFileSync","JSON","stringify","databaseAdapter","updated","parse","readFileSync","toBeDefined","toBeUndefined","sharp","removeSharp","scripts","dev","devDependencies","typescript","removeDependencies","toEqual","original","prepared","filePath","options","content","toBe","packageName"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,EAAEC,MAAM,EAAEC,UAAU,EAAEC,SAAS,QAAQ,SAAQ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UT,KAAKI,IAAI,CAACF,SAAS;QACnC,MAAMQ,cAAc;YAClBC,MAAM;YACNC,cAAc;gBACZC,SAAS;YACX;QACF;QACAd,GAAGe,aAAa,CAACL,SAASM,KAAKC,SAAS,CAACN,aAAa,MAAM;QAE5DZ,kBAAkBW,SAAS;YACzB4B,aAAa;QACf;QAEA,MAAMnB,UAAUH,KAAKI,KAAK,CAACpB,GAAGqB,YAAY,CAACX,SAAS;QACpDf,OAAOwB,QAAQP,IAAI,EAAEyB,IAAI,CAAC;IAC5B;AACF"}
|
|
@@ -1,365 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
-
import { Project } from 'ts-morph';
|
|
3
|
-
import { addDatabaseAdapter, addStorageAdapter, detectPayloadConfigStructure, removeSharp } from './payload-config';
|
|
4
|
-
import * as fs from 'fs';
|
|
5
|
-
import * as path from 'path';
|
|
6
|
-
import * as os from 'os';
|
|
7
|
-
describe('detectPayloadConfigStructure', ()=>{
|
|
8
|
-
it('successfully detects buildConfig call', ()=>{
|
|
9
|
-
const project = new Project({
|
|
10
|
-
useInMemoryFileSystem: true
|
|
11
|
-
});
|
|
12
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig } from 'payload'
|
|
13
|
-
|
|
14
|
-
export default buildConfig({
|
|
15
|
-
db: mongooseAdapter({ url: '' }),
|
|
16
|
-
plugins: []
|
|
17
|
-
})`);
|
|
18
|
-
const result = detectPayloadConfigStructure(sourceFile);
|
|
19
|
-
expect(result.success).toBe(true);
|
|
20
|
-
expect(result.structures?.buildConfigCall).toBeDefined();
|
|
21
|
-
});
|
|
22
|
-
it('fails when buildConfig call not found', ()=>{
|
|
23
|
-
const project = new Project({
|
|
24
|
-
useInMemoryFileSystem: true
|
|
25
|
-
});
|
|
26
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `export default {}`);
|
|
27
|
-
const result = detectPayloadConfigStructure(sourceFile);
|
|
28
|
-
expect(result.success).toBe(false);
|
|
29
|
-
expect(result.error?.userMessage).toContain('buildConfig');
|
|
30
|
-
expect(result.error?.technicalDetails).toContain('CallExpression');
|
|
31
|
-
});
|
|
32
|
-
it('detects buildConfig in variable declaration', ()=>{
|
|
33
|
-
const project = new Project({
|
|
34
|
-
useInMemoryFileSystem: true
|
|
35
|
-
});
|
|
36
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig } from 'payload'
|
|
37
|
-
|
|
38
|
-
const config = buildConfig({
|
|
39
|
-
db: mongooseAdapter({ url: '' })
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
export default config`);
|
|
43
|
-
const result = detectPayloadConfigStructure(sourceFile);
|
|
44
|
-
expect(result.success).toBe(true);
|
|
45
|
-
expect(result.structures?.buildConfigCall).toBeDefined();
|
|
46
|
-
});
|
|
47
|
-
it('detects import alias edge case', ()=>{
|
|
48
|
-
const project = new Project({
|
|
49
|
-
useInMemoryFileSystem: true
|
|
50
|
-
});
|
|
51
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig as createConfig } from 'payload'
|
|
52
|
-
import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
|
53
|
-
|
|
54
|
-
export default createConfig({
|
|
55
|
-
db: mongooseAdapter({ url: '' }),
|
|
56
|
-
collections: [],
|
|
57
|
-
})`);
|
|
58
|
-
const result = detectPayloadConfigStructure(sourceFile);
|
|
59
|
-
expect(result.success).toBe(true);
|
|
60
|
-
expect(result.edgeCases?.hasImportAlias).toBe(true);
|
|
61
|
-
expect(result.structures?.buildConfigCall).toBeDefined();
|
|
62
|
-
});
|
|
63
|
-
it('detects multiple buildConfig calls', ()=>{
|
|
64
|
-
const project = new Project({
|
|
65
|
-
useInMemoryFileSystem: true
|
|
66
|
-
});
|
|
67
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig } from 'payload'
|
|
68
|
-
|
|
69
|
-
const helper = buildConfig({ collections: [] })
|
|
70
|
-
|
|
71
|
-
export default buildConfig({
|
|
72
|
-
collections: [],
|
|
73
|
-
})`);
|
|
74
|
-
const result = detectPayloadConfigStructure(sourceFile);
|
|
75
|
-
expect(result.success).toBe(true);
|
|
76
|
-
expect(result.edgeCases?.multipleBuildConfigCalls).toBe(true);
|
|
77
|
-
});
|
|
78
|
-
it('detects other Payload imports', ()=>{
|
|
79
|
-
const project = new Project({
|
|
80
|
-
useInMemoryFileSystem: true
|
|
81
|
-
});
|
|
82
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig, CollectionConfig } from 'payload'
|
|
83
|
-
|
|
84
|
-
export default buildConfig({
|
|
85
|
-
collections: [],
|
|
86
|
-
})`);
|
|
87
|
-
const result = detectPayloadConfigStructure(sourceFile);
|
|
88
|
-
expect(result.success).toBe(true);
|
|
89
|
-
expect(result.edgeCases?.hasOtherPayloadImports).toBe(true);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
describe('addDatabaseAdapter', ()=>{
|
|
93
|
-
it.each([
|
|
94
|
-
{
|
|
95
|
-
adapter: 'mongodb',
|
|
96
|
-
adapterName: 'mongooseAdapter',
|
|
97
|
-
packageName: '@payloadcms/db-mongodb'
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
adapter: 'postgres',
|
|
101
|
-
adapterName: 'postgresAdapter',
|
|
102
|
-
packageName: '@payloadcms/db-postgres'
|
|
103
|
-
}
|
|
104
|
-
])('adds $adapter adapter with import and config', ({ adapter, adapterName, packageName })=>{
|
|
105
|
-
const project = new Project({
|
|
106
|
-
useInMemoryFileSystem: true
|
|
107
|
-
});
|
|
108
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig } from 'payload'
|
|
109
|
-
|
|
110
|
-
export default buildConfig({
|
|
111
|
-
collections: []
|
|
112
|
-
})`);
|
|
113
|
-
const result = addDatabaseAdapter({
|
|
114
|
-
sourceFile,
|
|
115
|
-
adapter,
|
|
116
|
-
envVarName: 'DATABASE_URL'
|
|
117
|
-
});
|
|
118
|
-
expect(result.success).toBe(true);
|
|
119
|
-
expect(result.modified).toBe(true);
|
|
120
|
-
expect(result.modifications.length).toBeGreaterThan(0);
|
|
121
|
-
const text = sourceFile.getText();
|
|
122
|
-
expect(text).toMatch(new RegExp(`import.*${adapterName}.*from.*${packageName.replace('/', '\\/')}`));
|
|
123
|
-
expect(text).toContain(`db: ${adapterName}`);
|
|
124
|
-
expect(text).toContain('process.env.DATABASE_URL');
|
|
125
|
-
});
|
|
126
|
-
it('replaces existing db adapter', ()=>{
|
|
127
|
-
const project = new Project({
|
|
128
|
-
useInMemoryFileSystem: true
|
|
129
|
-
});
|
|
130
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig } from 'payload'
|
|
131
|
-
import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
|
132
|
-
|
|
133
|
-
export default buildConfig({
|
|
134
|
-
db: mongooseAdapter({ url: '' }),
|
|
135
|
-
collections: []
|
|
136
|
-
})`);
|
|
137
|
-
const result = addDatabaseAdapter({
|
|
138
|
-
sourceFile,
|
|
139
|
-
adapter: 'postgres',
|
|
140
|
-
envVarName: 'DATABASE_URL'
|
|
141
|
-
});
|
|
142
|
-
expect(result.success).toBe(true);
|
|
143
|
-
const text = sourceFile.getText();
|
|
144
|
-
expect(text).toMatch(/import.*postgresAdapter.*from.*@payloadcms\/db-postgres/);
|
|
145
|
-
expect(text).toContain('db: postgresAdapter');
|
|
146
|
-
expect(text).not.toContain('mongooseAdapter');
|
|
147
|
-
expect(text).not.toContain('@payloadcms/db-mongodb');
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
describe('addStorageAdapter', ()=>{
|
|
151
|
-
it('adds vercelBlobStorage adapter to plugins array', ()=>{
|
|
152
|
-
const project = new Project({
|
|
153
|
-
useInMemoryFileSystem: true
|
|
154
|
-
});
|
|
155
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig } from 'payload'
|
|
156
|
-
|
|
157
|
-
export default buildConfig({
|
|
158
|
-
plugins: []
|
|
159
|
-
})`);
|
|
160
|
-
const result = addStorageAdapter({
|
|
161
|
-
sourceFile,
|
|
162
|
-
adapter: 'vercelBlobStorage'
|
|
163
|
-
});
|
|
164
|
-
expect(result.success).toBe(true);
|
|
165
|
-
expect(result.modified).toBe(true);
|
|
166
|
-
const text = sourceFile.getText();
|
|
167
|
-
expect(text).toMatch(/import.*vercelBlobStorage.*from.*@payloadcms\/storage-vercel-blob/);
|
|
168
|
-
expect(text).toContain('vercelBlobStorage(');
|
|
169
|
-
});
|
|
170
|
-
it('creates plugins array if missing', ()=>{
|
|
171
|
-
const project = new Project({
|
|
172
|
-
useInMemoryFileSystem: true
|
|
173
|
-
});
|
|
174
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig } from 'payload'
|
|
175
|
-
|
|
176
|
-
export default buildConfig({
|
|
177
|
-
collections: []
|
|
178
|
-
})`);
|
|
179
|
-
const result = addStorageAdapter({
|
|
180
|
-
sourceFile,
|
|
181
|
-
adapter: 'r2Storage'
|
|
182
|
-
});
|
|
183
|
-
expect(result.success).toBe(true);
|
|
184
|
-
const text = sourceFile.getText();
|
|
185
|
-
expect(text).toContain('plugins: [');
|
|
186
|
-
expect(text).toContain('r2Storage(');
|
|
187
|
-
});
|
|
188
|
-
it('adds to existing plugins array', ()=>{
|
|
189
|
-
const project = new Project({
|
|
190
|
-
useInMemoryFileSystem: true
|
|
191
|
-
});
|
|
192
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig } from 'payload'
|
|
193
|
-
|
|
194
|
-
export default buildConfig({
|
|
195
|
-
plugins: [
|
|
196
|
-
someOtherPlugin()
|
|
197
|
-
]
|
|
198
|
-
})`);
|
|
199
|
-
const result = addStorageAdapter({
|
|
200
|
-
sourceFile,
|
|
201
|
-
adapter: 's3Storage'
|
|
202
|
-
});
|
|
203
|
-
expect(result.success).toBe(true);
|
|
204
|
-
const text = sourceFile.getText();
|
|
205
|
-
expect(text).toContain('someOtherPlugin()');
|
|
206
|
-
expect(text).toContain('s3Storage(');
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
describe('removeSharp', ()=>{
|
|
210
|
-
it('removes sharp import and property', ()=>{
|
|
211
|
-
const project = new Project({
|
|
212
|
-
useInMemoryFileSystem: true
|
|
213
|
-
});
|
|
214
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig } from 'payload'
|
|
215
|
-
import sharp from 'sharp'
|
|
216
|
-
|
|
217
|
-
export default buildConfig({
|
|
218
|
-
sharp,
|
|
219
|
-
collections: []
|
|
220
|
-
})`);
|
|
221
|
-
const result = removeSharp(sourceFile);
|
|
222
|
-
expect(result.success).toBe(true);
|
|
223
|
-
expect(result.modified).toBe(true);
|
|
224
|
-
expect(result.modifications.length).toBeGreaterThan(0);
|
|
225
|
-
const text = sourceFile.getText();
|
|
226
|
-
expect(text).not.toContain("import sharp from 'sharp'");
|
|
227
|
-
expect(text).not.toContain('sharp,');
|
|
228
|
-
});
|
|
229
|
-
it('does nothing if sharp not present', ()=>{
|
|
230
|
-
const project = new Project({
|
|
231
|
-
useInMemoryFileSystem: true
|
|
232
|
-
});
|
|
233
|
-
const sourceFile = project.createSourceFile('payload.config.ts', `import { buildConfig } from 'payload'
|
|
234
|
-
|
|
235
|
-
export default buildConfig({
|
|
236
|
-
collections: []
|
|
237
|
-
})`);
|
|
238
|
-
const result = removeSharp(sourceFile);
|
|
239
|
-
expect(result.success).toBe(true);
|
|
240
|
-
expect(result.modified).toBe(false);
|
|
241
|
-
});
|
|
242
|
-
});
|
|
243
|
-
describe('configurePayloadConfig', ()=>{
|
|
244
|
-
let tempDir;
|
|
245
|
-
beforeEach(()=>{
|
|
246
|
-
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'payload-test-'));
|
|
247
|
-
});
|
|
248
|
-
afterEach(()=>{
|
|
249
|
-
fs.rmSync(tempDir, {
|
|
250
|
-
recursive: true,
|
|
251
|
-
force: true
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
|
-
it('applies all transformations in one call (db + storage)', async ()=>{
|
|
255
|
-
const filePath = path.join(tempDir, 'payload.config.ts');
|
|
256
|
-
fs.writeFileSync(filePath, `import { buildConfig } from 'payload'
|
|
257
|
-
|
|
258
|
-
export default buildConfig({
|
|
259
|
-
collections: []
|
|
260
|
-
})`);
|
|
261
|
-
const { configurePayloadConfig } = await import('./payload-config');
|
|
262
|
-
const result = await configurePayloadConfig(filePath, {
|
|
263
|
-
db: {
|
|
264
|
-
type: 'postgres',
|
|
265
|
-
envVarName: 'DATABASE_URL'
|
|
266
|
-
},
|
|
267
|
-
storage: 'vercelBlobStorage'
|
|
268
|
-
});
|
|
269
|
-
expect(result.success).toBe(true);
|
|
270
|
-
const content = fs.readFileSync(filePath, 'utf-8');
|
|
271
|
-
expect(content).toContain('postgresAdapter');
|
|
272
|
-
expect(content).toContain('vercelBlobStorage');
|
|
273
|
-
expect(content).toContain('DATABASE_URL');
|
|
274
|
-
});
|
|
275
|
-
it('applies db transformation only', async ()=>{
|
|
276
|
-
const filePath = path.join(tempDir, 'payload.config.ts');
|
|
277
|
-
fs.writeFileSync(filePath, `import { buildConfig } from 'payload'
|
|
278
|
-
|
|
279
|
-
export default buildConfig({
|
|
280
|
-
collections: []
|
|
281
|
-
})`);
|
|
282
|
-
const { configurePayloadConfig } = await import('./payload-config');
|
|
283
|
-
const result = await configurePayloadConfig(filePath, {
|
|
284
|
-
db: {
|
|
285
|
-
type: 'mongodb',
|
|
286
|
-
envVarName: 'MONGO_URL'
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
expect(result.success).toBe(true);
|
|
290
|
-
const content = fs.readFileSync(filePath, 'utf-8');
|
|
291
|
-
expect(content).toContain('mongooseAdapter');
|
|
292
|
-
expect(content).toContain('MONGO_URL');
|
|
293
|
-
});
|
|
294
|
-
it('applies removeSharp transformation', async ()=>{
|
|
295
|
-
const filePath = path.join(tempDir, 'payload.config.ts');
|
|
296
|
-
fs.writeFileSync(filePath, `import { buildConfig } from 'payload'
|
|
297
|
-
import sharp from 'sharp'
|
|
298
|
-
import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
|
299
|
-
|
|
300
|
-
export default buildConfig({
|
|
301
|
-
db: mongooseAdapter({ url: '' }),
|
|
302
|
-
sharp,
|
|
303
|
-
collections: []
|
|
304
|
-
})`);
|
|
305
|
-
const { configurePayloadConfig } = await import('./payload-config');
|
|
306
|
-
const result = await configurePayloadConfig(filePath, {
|
|
307
|
-
removeSharp: true
|
|
308
|
-
});
|
|
309
|
-
expect(result.success).toBe(true);
|
|
310
|
-
const content = fs.readFileSync(filePath, 'utf-8');
|
|
311
|
-
expect(content).not.toContain("import sharp from 'sharp'");
|
|
312
|
-
expect(content).not.toContain('sharp,');
|
|
313
|
-
});
|
|
314
|
-
it('handles transformation errors gracefully', async ()=>{
|
|
315
|
-
const filePath = path.join(tempDir, 'payload.config.ts');
|
|
316
|
-
fs.writeFileSync(filePath, `export default {}`);
|
|
317
|
-
const { configurePayloadConfig } = await import('./payload-config');
|
|
318
|
-
const result = await configurePayloadConfig(filePath, {
|
|
319
|
-
db: {
|
|
320
|
-
type: 'postgres',
|
|
321
|
-
envVarName: 'DATABASE_URL'
|
|
322
|
-
}
|
|
323
|
-
});
|
|
324
|
-
expect(result.success).toBe(false);
|
|
325
|
-
expect(result.error?.userMessage).toContain('buildConfig');
|
|
326
|
-
});
|
|
327
|
-
it('handles file not found error', async ()=>{
|
|
328
|
-
const filePath = path.join(tempDir, 'nonexistent.ts');
|
|
329
|
-
const { configurePayloadConfig } = await import('./payload-config');
|
|
330
|
-
const result = await configurePayloadConfig(filePath, {
|
|
331
|
-
db: {
|
|
332
|
-
type: 'postgres',
|
|
333
|
-
envVarName: 'DATABASE_URL'
|
|
334
|
-
}
|
|
335
|
-
});
|
|
336
|
-
expect(result.success).toBe(false);
|
|
337
|
-
expect(result.error).toBeDefined();
|
|
338
|
-
});
|
|
339
|
-
it('replaces adapter in config file', async ()=>{
|
|
340
|
-
const filePath = path.join(tempDir, 'payload.config.ts');
|
|
341
|
-
fs.writeFileSync(filePath, `import { buildConfig } from 'payload'
|
|
342
|
-
import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
|
343
|
-
|
|
344
|
-
export default buildConfig({
|
|
345
|
-
db: mongooseAdapter({ url: process.env.DATABASE_URL || '' }),
|
|
346
|
-
collections: []
|
|
347
|
-
})`);
|
|
348
|
-
const { configurePayloadConfig } = await import('./payload-config');
|
|
349
|
-
const result = await configurePayloadConfig(filePath, {
|
|
350
|
-
db: {
|
|
351
|
-
type: 'postgres',
|
|
352
|
-
envVarName: 'DATABASE_URL'
|
|
353
|
-
}
|
|
354
|
-
});
|
|
355
|
-
expect(result.success).toBe(true);
|
|
356
|
-
// Verify config was updated
|
|
357
|
-
const content = fs.readFileSync(filePath, 'utf-8');
|
|
358
|
-
expect(content).toContain('postgresAdapter');
|
|
359
|
-
expect(content).toContain('@payloadcms/db-postgres');
|
|
360
|
-
expect(content).not.toContain('mongooseAdapter');
|
|
361
|
-
expect(content).not.toContain('@payloadcms/db-mongodb');
|
|
362
|
-
});
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
//# sourceMappingURL=payload-config.spec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/ast/payload-config.spec.ts"],"sourcesContent":["import { describe, it, expect, beforeEach, afterEach } from 'vitest'\nimport { Project } from 'ts-morph'\nimport {\n addDatabaseAdapter,\n addStorageAdapter,\n detectPayloadConfigStructure,\n removeSharp,\n} from './payload-config'\nimport * as fs from 'fs'\nimport * as path from 'path'\nimport * as os from 'os'\n\ndescribe('detectPayloadConfigStructure', () => {\n it('successfully detects buildConfig call', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig } from 'payload'\n\nexport default buildConfig({\n db: mongooseAdapter({ url: '' }),\n plugins: []\n})`,\n )\n\n const result = detectPayloadConfigStructure(sourceFile)\n\n expect(result.success).toBe(true)\n expect(result.structures?.buildConfigCall).toBeDefined()\n })\n\n it('fails when buildConfig call not found', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile('payload.config.ts', `export default {}`)\n\n const result = detectPayloadConfigStructure(sourceFile)\n\n expect(result.success).toBe(false)\n expect(result.error?.userMessage).toContain('buildConfig')\n expect(result.error?.technicalDetails).toContain('CallExpression')\n })\n\n it('detects buildConfig in variable declaration', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig } from 'payload'\n\nconst config = buildConfig({\n db: mongooseAdapter({ url: '' })\n})\n\nexport default config`,\n )\n\n const result = detectPayloadConfigStructure(sourceFile)\n\n expect(result.success).toBe(true)\n expect(result.structures?.buildConfigCall).toBeDefined()\n })\n\n it('detects import alias edge case', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig as createConfig } from 'payload'\nimport { mongooseAdapter } from '@payloadcms/db-mongodb'\n\nexport default createConfig({\n db: mongooseAdapter({ url: '' }),\n collections: [],\n})`,\n )\n\n const result = detectPayloadConfigStructure(sourceFile)\n\n expect(result.success).toBe(true)\n expect(result.edgeCases?.hasImportAlias).toBe(true)\n expect(result.structures?.buildConfigCall).toBeDefined()\n })\n\n it('detects multiple buildConfig calls', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig } from 'payload'\n\nconst helper = buildConfig({ collections: [] })\n\nexport default buildConfig({\n collections: [],\n})`,\n )\n\n const result = detectPayloadConfigStructure(sourceFile)\n\n expect(result.success).toBe(true)\n expect(result.edgeCases?.multipleBuildConfigCalls).toBe(true)\n })\n\n it('detects other Payload imports', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig, CollectionConfig } from 'payload'\n\nexport default buildConfig({\n collections: [],\n})`,\n )\n\n const result = detectPayloadConfigStructure(sourceFile)\n\n expect(result.success).toBe(true)\n expect(result.edgeCases?.hasOtherPayloadImports).toBe(true)\n })\n})\n\ndescribe('addDatabaseAdapter', () => {\n it.each([\n {\n adapter: 'mongodb' as const,\n adapterName: 'mongooseAdapter',\n packageName: '@payloadcms/db-mongodb',\n },\n {\n adapter: 'postgres' as const,\n adapterName: 'postgresAdapter',\n packageName: '@payloadcms/db-postgres',\n },\n ])('adds $adapter adapter with import and config', ({ adapter, adapterName, packageName }) => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig } from 'payload'\n\nexport default buildConfig({\n collections: []\n})`,\n )\n\n const result = addDatabaseAdapter({\n sourceFile,\n adapter,\n envVarName: 'DATABASE_URL',\n })\n\n expect(result.success).toBe(true)\n expect(result.modified).toBe(true)\n expect(result.modifications.length).toBeGreaterThan(0)\n\n const text = sourceFile.getText()\n expect(text).toMatch(\n new RegExp(`import.*${adapterName}.*from.*${packageName.replace('/', '\\\\/')}`),\n )\n expect(text).toContain(`db: ${adapterName}`)\n expect(text).toContain('process.env.DATABASE_URL')\n })\n\n it('replaces existing db adapter', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig } from 'payload'\nimport { mongooseAdapter } from '@payloadcms/db-mongodb'\n\nexport default buildConfig({\n db: mongooseAdapter({ url: '' }),\n collections: []\n})`,\n )\n\n const result = addDatabaseAdapter({\n sourceFile,\n adapter: 'postgres',\n envVarName: 'DATABASE_URL',\n })\n\n expect(result.success).toBe(true)\n const text = sourceFile.getText()\n expect(text).toMatch(/import.*postgresAdapter.*from.*@payloadcms\\/db-postgres/)\n expect(text).toContain('db: postgresAdapter')\n expect(text).not.toContain('mongooseAdapter')\n expect(text).not.toContain('@payloadcms/db-mongodb')\n })\n})\n\ndescribe('addStorageAdapter', () => {\n it('adds vercelBlobStorage adapter to plugins array', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig } from 'payload'\n\nexport default buildConfig({\n plugins: []\n})`,\n )\n\n const result = addStorageAdapter({ sourceFile, adapter: 'vercelBlobStorage' })\n\n expect(result.success).toBe(true)\n expect(result.modified).toBe(true)\n const text = sourceFile.getText()\n expect(text).toMatch(/import.*vercelBlobStorage.*from.*@payloadcms\\/storage-vercel-blob/)\n expect(text).toContain('vercelBlobStorage(')\n })\n\n it('creates plugins array if missing', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig } from 'payload'\n\nexport default buildConfig({\n collections: []\n})`,\n )\n\n const result = addStorageAdapter({ sourceFile, adapter: 'r2Storage' })\n\n expect(result.success).toBe(true)\n const text = sourceFile.getText()\n expect(text).toContain('plugins: [')\n expect(text).toContain('r2Storage(')\n })\n\n it('adds to existing plugins array', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig } from 'payload'\n\nexport default buildConfig({\n plugins: [\n someOtherPlugin()\n ]\n})`,\n )\n\n const result = addStorageAdapter({ sourceFile, adapter: 's3Storage' })\n\n expect(result.success).toBe(true)\n const text = sourceFile.getText()\n expect(text).toContain('someOtherPlugin()')\n expect(text).toContain('s3Storage(')\n })\n})\n\ndescribe('removeSharp', () => {\n it('removes sharp import and property', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig } from 'payload'\nimport sharp from 'sharp'\n\nexport default buildConfig({\n sharp,\n collections: []\n})`,\n )\n\n const result = removeSharp(sourceFile)\n\n expect(result.success).toBe(true)\n expect(result.modified).toBe(true)\n expect(result.modifications.length).toBeGreaterThan(0)\n const text = sourceFile.getText()\n expect(text).not.toContain(\"import sharp from 'sharp'\")\n expect(text).not.toContain('sharp,')\n })\n\n it('does nothing if sharp not present', () => {\n const project = new Project({ useInMemoryFileSystem: true })\n const sourceFile = project.createSourceFile(\n 'payload.config.ts',\n `import { buildConfig } from 'payload'\n\nexport default buildConfig({\n collections: []\n})`,\n )\n\n const result = removeSharp(sourceFile)\n\n expect(result.success).toBe(true)\n expect(result.modified).toBe(false)\n })\n})\n\ndescribe('configurePayloadConfig', () => {\n let tempDir: string\n\n beforeEach(() => {\n tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'payload-test-'))\n })\n\n afterEach(() => {\n fs.rmSync(tempDir, { recursive: true, force: true })\n })\n\n it('applies all transformations in one call (db + storage)', async () => {\n const filePath = path.join(tempDir, 'payload.config.ts')\n fs.writeFileSync(\n filePath,\n `import { buildConfig } from 'payload'\n\nexport default buildConfig({\n collections: []\n})`,\n )\n\n const { configurePayloadConfig } = await import('./payload-config')\n const result = await configurePayloadConfig(filePath, {\n db: { type: 'postgres', envVarName: 'DATABASE_URL' },\n storage: 'vercelBlobStorage',\n })\n\n expect(result.success).toBe(true)\n\n const content = fs.readFileSync(filePath, 'utf-8')\n expect(content).toContain('postgresAdapter')\n expect(content).toContain('vercelBlobStorage')\n expect(content).toContain('DATABASE_URL')\n })\n\n it('applies db transformation only', async () => {\n const filePath = path.join(tempDir, 'payload.config.ts')\n fs.writeFileSync(\n filePath,\n `import { buildConfig } from 'payload'\n\nexport default buildConfig({\n collections: []\n})`,\n )\n\n const { configurePayloadConfig } = await import('./payload-config')\n const result = await configurePayloadConfig(filePath, {\n db: { type: 'mongodb', envVarName: 'MONGO_URL' },\n })\n\n expect(result.success).toBe(true)\n\n const content = fs.readFileSync(filePath, 'utf-8')\n expect(content).toContain('mongooseAdapter')\n expect(content).toContain('MONGO_URL')\n })\n\n it('applies removeSharp transformation', async () => {\n const filePath = path.join(tempDir, 'payload.config.ts')\n fs.writeFileSync(\n filePath,\n `import { buildConfig } from 'payload'\nimport sharp from 'sharp'\nimport { mongooseAdapter } from '@payloadcms/db-mongodb'\n\nexport default buildConfig({\n db: mongooseAdapter({ url: '' }),\n sharp,\n collections: []\n})`,\n )\n\n const { configurePayloadConfig } = await import('./payload-config')\n const result = await configurePayloadConfig(filePath, {\n removeSharp: true,\n })\n\n expect(result.success).toBe(true)\n\n const content = fs.readFileSync(filePath, 'utf-8')\n expect(content).not.toContain(\"import sharp from 'sharp'\")\n expect(content).not.toContain('sharp,')\n })\n\n it('handles transformation errors gracefully', async () => {\n const filePath = path.join(tempDir, 'payload.config.ts')\n fs.writeFileSync(\n filePath,\n `export default {}`, // Invalid structure\n )\n\n const { configurePayloadConfig } = await import('./payload-config')\n const result = await configurePayloadConfig(filePath, {\n db: { type: 'postgres', envVarName: 'DATABASE_URL' },\n })\n\n expect(result.success).toBe(false)\n expect(result.error?.userMessage).toContain('buildConfig')\n })\n\n it('handles file not found error', async () => {\n const filePath = path.join(tempDir, 'nonexistent.ts')\n\n const { configurePayloadConfig } = await import('./payload-config')\n const result = await configurePayloadConfig(filePath, {\n db: { type: 'postgres', envVarName: 'DATABASE_URL' },\n })\n\n expect(result.success).toBe(false)\n expect(result.error).toBeDefined()\n })\n\n it('replaces adapter in config file', async () => {\n const filePath = path.join(tempDir, 'payload.config.ts')\n fs.writeFileSync(\n filePath,\n `import { buildConfig } from 'payload'\nimport { mongooseAdapter } from '@payloadcms/db-mongodb'\n\nexport default buildConfig({\n db: mongooseAdapter({ url: process.env.DATABASE_URL || '' }),\n collections: []\n})`,\n )\n\n const { configurePayloadConfig } = await import('./payload-config')\n const result = await configurePayloadConfig(filePath, {\n db: { type: 'postgres', envVarName: 'DATABASE_URL' },\n })\n\n expect(result.success).toBe(true)\n\n // Verify config was updated\n const content = fs.readFileSync(filePath, 'utf-8')\n expect(content).toContain('postgresAdapter')\n expect(content).toContain('@payloadcms/db-postgres')\n expect(content).not.toContain('mongooseAdapter')\n expect(content).not.toContain('@payloadcms/db-mongodb')\n })\n})\n"],"names":["describe","it","expect","beforeEach","afterEach","Project","addDatabaseAdapter","addStorageAdapter","detectPayloadConfigStructure","removeSharp","fs","path","os","project","useInMemoryFileSystem","sourceFile","createSourceFile","result","success","toBe","structures","buildConfigCall","toBeDefined","error","userMessage","toContain","technicalDetails","edgeCases","hasImportAlias","multipleBuildConfigCalls","hasOtherPayloadImports","each","adapter","adapterName","packageName","envVarName","modified","modifications","length","toBeGreaterThan","text","getText","toMatch","RegExp","replace","not","tempDir","mkdtempSync","join","tmpdir","rmSync","recursive","force","filePath","writeFileSync","configurePayloadConfig","db","type","storage","content","readFileSync"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,EAAEC,MAAM,EAAEC,UAAU,EAAEC,SAAS,QAAQ,SAAQ;AACpE,SAASC,OAAO,QAAQ,WAAU;AAClC,SACEC,kBAAkB,EAClBC,iBAAiB,EACjBC,4BAA4B,EAC5BC,WAAW,QACN,mBAAkB;AACzB,YAAYC,QAAQ,KAAI;AACxB,YAAYC,UAAU,OAAM;AAC5B,YAAYC,QAAQ,KAAI;AAExBZ,SAAS,gCAAgC;IACvCC,GAAG,yCAAyC;QAC1C,MAAMY,UAAU,IAAIR,QAAQ;YAAES,uBAAuB;QAAK;QAC1D,MAAMC,aAAaF,QAAQG,gBAAgB,CACzC,qBACA,CAAC;;;;;EAKL,CAAC;QAGC,MAAMC,SAAST,6BAA6BO;QAE5Cb,OAAOe,OAAOC,OAAO,EAAEC,IAAI,CAAC;QAC5BjB,OAAOe,OAAOG,UAAU,EAAEC,iBAAiBC,WAAW;IACxD;IAEArB,GAAG,yCAAyC;QAC1C,MAAMY,UAAU,IAAIR,QAAQ;YAAES,uBAAuB;QAAK;QAC1D,MAAMC,aAAaF,QAAQG,gBAAgB,CAAC,qBAAqB,CAAC,iBAAiB,CAAC;QAEpF,MAAMC,SAAST,6BAA6BO;QAE5Cb,OAAOe,OAAOC,OAAO,EAAEC,IAAI,CAAC;QAC5BjB,OAAOe,OAAOM,KAAK,EAAEC,aAAaC,SAAS,CAAC;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AOyD,SAASlC,SAAS,CAAC;QAC1BvB,OAAOyD,SAASlC,SAAS,CAAC;QAC1BvB,OAAOyD,SAASd,GAAG,CAACpB,SAAS,CAAC;QAC9BvB,OAAOyD,SAASd,GAAG,CAACpB,SAAS,CAAC;IAChC;AACF"}
|
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { Project } from 'ts-morph';
|
|
3
|
-
import { addImportDeclaration, cleanupOrphanedImports, findImportDeclaration, isNamedImportUsed, removeImportDeclaration, removeNamedImports } from './utils';
|
|
4
|
-
describe('findImportDeclaration', ()=>{
|
|
5
|
-
it('finds import by module specifier', ()=>{
|
|
6
|
-
const project = new Project({
|
|
7
|
-
useInMemoryFileSystem: true
|
|
8
|
-
});
|
|
9
|
-
const sourceFile = project.createSourceFile('test.ts', `import { buildConfig } from 'payload'
|
|
10
|
-
import { mongooseAdapter } from '@payloadcms/db-mongodb'`);
|
|
11
|
-
const result = findImportDeclaration({
|
|
12
|
-
sourceFile,
|
|
13
|
-
moduleSpecifier: '@payloadcms/db-mongodb'
|
|
14
|
-
});
|
|
15
|
-
expect(result).toBeDefined();
|
|
16
|
-
expect(result?.getModuleSpecifierValue()).toBe('@payloadcms/db-mongodb');
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
describe('addImportDeclaration', ()=>{
|
|
20
|
-
it('adds new import when not present', ()=>{
|
|
21
|
-
const project = new Project({
|
|
22
|
-
useInMemoryFileSystem: true
|
|
23
|
-
});
|
|
24
|
-
const sourceFile = project.createSourceFile('test.ts', `import { buildConfig } from 'payload'`);
|
|
25
|
-
const result = addImportDeclaration({
|
|
26
|
-
sourceFile,
|
|
27
|
-
moduleSpecifier: '@payloadcms/db-postgres',
|
|
28
|
-
namedImports: [
|
|
29
|
-
'postgresAdapter'
|
|
30
|
-
]
|
|
31
|
-
});
|
|
32
|
-
const imports = result.getImportDeclarations();
|
|
33
|
-
expect(imports).toHaveLength(2);
|
|
34
|
-
expect(imports[1].getModuleSpecifierValue()).toBe('@payloadcms/db-postgres');
|
|
35
|
-
expect(imports[1].getNamedImports()[0].getName()).toBe('postgresAdapter');
|
|
36
|
-
});
|
|
37
|
-
it('does not duplicate existing import', ()=>{
|
|
38
|
-
const project = new Project({
|
|
39
|
-
useInMemoryFileSystem: true
|
|
40
|
-
});
|
|
41
|
-
const sourceFile = project.createSourceFile('test.ts', `import { mongooseAdapter } from '@payloadcms/db-mongodb'`);
|
|
42
|
-
const result = addImportDeclaration({
|
|
43
|
-
sourceFile,
|
|
44
|
-
moduleSpecifier: '@payloadcms/db-mongodb',
|
|
45
|
-
namedImports: [
|
|
46
|
-
'mongooseAdapter'
|
|
47
|
-
]
|
|
48
|
-
});
|
|
49
|
-
const imports = result.getImportDeclarations();
|
|
50
|
-
expect(imports).toHaveLength(1);
|
|
51
|
-
});
|
|
52
|
-
it('adds named import to existing module import', ()=>{
|
|
53
|
-
const project = new Project({
|
|
54
|
-
useInMemoryFileSystem: true
|
|
55
|
-
});
|
|
56
|
-
const sourceFile = project.createSourceFile('test.ts', `import { buildConfig } from 'payload'`);
|
|
57
|
-
const result = addImportDeclaration({
|
|
58
|
-
sourceFile,
|
|
59
|
-
moduleSpecifier: 'payload',
|
|
60
|
-
namedImports: [
|
|
61
|
-
'Field'
|
|
62
|
-
]
|
|
63
|
-
});
|
|
64
|
-
const imports = result.getImportDeclarations();
|
|
65
|
-
expect(imports).toHaveLength(1);
|
|
66
|
-
const namedImports = imports[0].getNamedImports().map((ni)=>ni.getName());
|
|
67
|
-
expect(namedImports).toContain('buildConfig');
|
|
68
|
-
expect(namedImports).toContain('Field');
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
describe('removeImportDeclaration', ()=>{
|
|
72
|
-
it('removes import by module specifier', ()=>{
|
|
73
|
-
const project = new Project({
|
|
74
|
-
useInMemoryFileSystem: true
|
|
75
|
-
});
|
|
76
|
-
const sourceFile = project.createSourceFile('test.ts', `import { buildConfig } from 'payload'
|
|
77
|
-
import sharp from 'sharp'`);
|
|
78
|
-
const result = removeImportDeclaration({
|
|
79
|
-
sourceFile,
|
|
80
|
-
moduleSpecifier: 'sharp'
|
|
81
|
-
});
|
|
82
|
-
expect(result.removedIndex).toBe(1);
|
|
83
|
-
const imports = result.sourceFile.getImportDeclarations();
|
|
84
|
-
expect(imports).toHaveLength(1);
|
|
85
|
-
expect(imports[0].getModuleSpecifierValue()).toBe('payload');
|
|
86
|
-
});
|
|
87
|
-
it('returns undefined removedIndex when import not found', ()=>{
|
|
88
|
-
const project = new Project({
|
|
89
|
-
useInMemoryFileSystem: true
|
|
90
|
-
});
|
|
91
|
-
const sourceFile = project.createSourceFile('test.ts', `import { buildConfig } from 'payload'`);
|
|
92
|
-
const result = removeImportDeclaration({
|
|
93
|
-
sourceFile,
|
|
94
|
-
moduleSpecifier: 'sharp'
|
|
95
|
-
});
|
|
96
|
-
expect(result.removedIndex).toBeUndefined();
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
describe('removeNamedImports', ()=>{
|
|
100
|
-
it('removes specific named imports', ()=>{
|
|
101
|
-
const project = new Project({
|
|
102
|
-
useInMemoryFileSystem: true
|
|
103
|
-
});
|
|
104
|
-
const sourceFile = project.createSourceFile('test.ts', `import { mongooseAdapter, SomeOtherType } from '@payloadcms/db-mongodb'`);
|
|
105
|
-
const importDecl = findImportDeclaration({
|
|
106
|
-
sourceFile,
|
|
107
|
-
moduleSpecifier: '@payloadcms/db-mongodb'
|
|
108
|
-
});
|
|
109
|
-
const result = removeNamedImports({
|
|
110
|
-
sourceFile,
|
|
111
|
-
importDeclaration: importDecl,
|
|
112
|
-
namedImportsToRemove: [
|
|
113
|
-
'mongooseAdapter'
|
|
114
|
-
]
|
|
115
|
-
});
|
|
116
|
-
expect(result.fullyRemoved).toBe(false);
|
|
117
|
-
const imports = result.sourceFile.getImportDeclarations();
|
|
118
|
-
expect(imports).toHaveLength(1);
|
|
119
|
-
const namedImports = imports[0].getNamedImports().map((ni)=>ni.getName());
|
|
120
|
-
expect(namedImports).toEqual([
|
|
121
|
-
'SomeOtherType'
|
|
122
|
-
]);
|
|
123
|
-
});
|
|
124
|
-
it('removes entire import when no named imports remain', ()=>{
|
|
125
|
-
const project = new Project({
|
|
126
|
-
useInMemoryFileSystem: true
|
|
127
|
-
});
|
|
128
|
-
const sourceFile = project.createSourceFile('test.ts', `import { buildConfig } from 'payload'
|
|
129
|
-
import { mongooseAdapter } from '@payloadcms/db-mongodb'`);
|
|
130
|
-
const importDecl = findImportDeclaration({
|
|
131
|
-
sourceFile,
|
|
132
|
-
moduleSpecifier: '@payloadcms/db-mongodb'
|
|
133
|
-
});
|
|
134
|
-
const result = removeNamedImports({
|
|
135
|
-
sourceFile,
|
|
136
|
-
importDeclaration: importDecl,
|
|
137
|
-
namedImportsToRemove: [
|
|
138
|
-
'mongooseAdapter'
|
|
139
|
-
]
|
|
140
|
-
});
|
|
141
|
-
expect(result.fullyRemoved).toBe(true);
|
|
142
|
-
expect(result.index).toBe(1);
|
|
143
|
-
const imports = result.sourceFile.getImportDeclarations();
|
|
144
|
-
expect(imports).toHaveLength(1);
|
|
145
|
-
expect(imports[0].getModuleSpecifierValue()).toBe('payload');
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
describe('isNamedImportUsed', ()=>{
|
|
149
|
-
it('detects used import in code', ()=>{
|
|
150
|
-
const project = new Project({
|
|
151
|
-
useInMemoryFileSystem: true
|
|
152
|
-
});
|
|
153
|
-
const sourceFile = project.createSourceFile('test.ts', `import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
|
154
|
-
|
|
155
|
-
export default buildConfig({
|
|
156
|
-
db: mongooseAdapter({ url: '' })
|
|
157
|
-
})`);
|
|
158
|
-
const isUsed = isNamedImportUsed(sourceFile, 'mongooseAdapter');
|
|
159
|
-
expect(isUsed).toBe(true);
|
|
160
|
-
});
|
|
161
|
-
it('detects unused import', ()=>{
|
|
162
|
-
const project = new Project({
|
|
163
|
-
useInMemoryFileSystem: true
|
|
164
|
-
});
|
|
165
|
-
const sourceFile = project.createSourceFile('test.ts', `import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
|
166
|
-
import { postgresAdapter } from '@payloadcms/db-postgres'
|
|
167
|
-
|
|
168
|
-
export default buildConfig({
|
|
169
|
-
db: postgresAdapter({ url: '' })
|
|
170
|
-
})`);
|
|
171
|
-
const isUsed = isNamedImportUsed(sourceFile, 'mongooseAdapter');
|
|
172
|
-
expect(isUsed).toBe(false);
|
|
173
|
-
});
|
|
174
|
-
});
|
|
175
|
-
describe('cleanupOrphanedImports', ()=>{
|
|
176
|
-
it('removes unused imports', ()=>{
|
|
177
|
-
const project = new Project({
|
|
178
|
-
useInMemoryFileSystem: true
|
|
179
|
-
});
|
|
180
|
-
const sourceFile = project.createSourceFile('test.ts', `import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
|
181
|
-
import { postgresAdapter } from '@payloadcms/db-postgres'
|
|
182
|
-
|
|
183
|
-
export default buildConfig({
|
|
184
|
-
db: postgresAdapter({ url: '' })
|
|
185
|
-
})`);
|
|
186
|
-
const result = cleanupOrphanedImports({
|
|
187
|
-
sourceFile,
|
|
188
|
-
moduleSpecifier: '@payloadcms/db-mongodb',
|
|
189
|
-
importNames: [
|
|
190
|
-
'mongooseAdapter'
|
|
191
|
-
]
|
|
192
|
-
});
|
|
193
|
-
expect(result.removed).toEqual([
|
|
194
|
-
'mongooseAdapter'
|
|
195
|
-
]);
|
|
196
|
-
expect(result.kept).toEqual([]);
|
|
197
|
-
const imports = result.sourceFile.getImportDeclarations();
|
|
198
|
-
expect(imports).toHaveLength(1);
|
|
199
|
-
expect(imports[0].getModuleSpecifierValue()).toBe('@payloadcms/db-postgres');
|
|
200
|
-
});
|
|
201
|
-
it('keeps used imports', ()=>{
|
|
202
|
-
const project = new Project({
|
|
203
|
-
useInMemoryFileSystem: true
|
|
204
|
-
});
|
|
205
|
-
const sourceFile = project.createSourceFile('test.ts', `import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
|
206
|
-
|
|
207
|
-
export default buildConfig({
|
|
208
|
-
db: mongooseAdapter({ url: '' })
|
|
209
|
-
})`);
|
|
210
|
-
const result = cleanupOrphanedImports({
|
|
211
|
-
sourceFile,
|
|
212
|
-
moduleSpecifier: '@payloadcms/db-mongodb',
|
|
213
|
-
importNames: [
|
|
214
|
-
'mongooseAdapter'
|
|
215
|
-
]
|
|
216
|
-
});
|
|
217
|
-
expect(result.removed).toEqual([]);
|
|
218
|
-
expect(result.kept).toEqual([
|
|
219
|
-
'mongooseAdapter'
|
|
220
|
-
]);
|
|
221
|
-
const imports = result.sourceFile.getImportDeclarations();
|
|
222
|
-
expect(imports).toHaveLength(1);
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
//# sourceMappingURL=utils.spec.js.map
|