create-payload-app 3.0.0-canary.a78fca3 → 3.0.0-canary.aceaf5d
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.js.map +1 -1
- package/dist/lib/configure-payload-config.js +1 -1
- package/dist/lib/configure-payload-config.js.map +1 -1
- package/dist/lib/create-project.js.map +1 -1
- package/dist/lib/create-project.spec.js +20 -19
- package/dist/lib/create-project.spec.js.map +1 -1
- package/dist/lib/generate-secret.js.map +1 -1
- package/dist/lib/get-package-manager.d.ts.map +1 -1
- package/dist/lib/get-package-manager.js +12 -3
- package/dist/lib/get-package-manager.js.map +1 -1
- package/dist/lib/init-next.d.ts +2 -2
- package/dist/lib/init-next.d.ts.map +1 -1
- package/dist/lib/init-next.js +48 -8
- package/dist/lib/init-next.js.map +1 -1
- package/dist/lib/install-packages.js.map +1 -1
- package/dist/lib/parse-project-name.js.map +1 -1
- package/dist/lib/parse-template.js.map +1 -1
- package/dist/lib/replacements.d.ts.map +1 -1
- package/dist/lib/replacements.js +15 -2
- package/dist/lib/replacements.js.map +1 -1
- package/dist/lib/select-db.d.ts.map +1 -1
- package/dist/lib/select-db.js +11 -11
- package/dist/lib/select-db.js.map +1 -1
- package/dist/lib/templates.js +7 -1
- package/dist/lib/templates.js.map +1 -1
- package/dist/lib/update-payload-in-project.d.ts.map +1 -1
- package/dist/lib/update-payload-in-project.js +1 -1
- package/dist/lib/update-payload-in-project.js.map +1 -1
- package/dist/lib/wrap-next-config.d.ts +5 -5
- package/dist/lib/wrap-next-config.d.ts.map +1 -1
- package/dist/lib/wrap-next-config.js +122 -58
- package/dist/lib/wrap-next-config.js.map +1 -1
- package/dist/lib/wrap-next-config.spec.js +84 -34
- package/dist/lib/wrap-next-config.spec.js.map +1 -1
- package/dist/lib/write-env-file.d.ts.map +1 -1
- package/dist/lib/write-env-file.js +25 -25
- package/dist/lib/write-env-file.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +6 -1
- package/dist/main.js.map +1 -1
- package/dist/scripts/pack-template-files.js +3 -3
- package/dist/scripts/pack-template-files.js.map +1 -1
- package/dist/template/src/app/(payload)/admin/[[...segments]]/not-found.tsx +3 -1
- package/dist/template/src/app/(payload)/admin/[[...segments]]/page.tsx +3 -1
- package/dist/template/src/app/(payload)/admin/importMap.js +1 -0
- package/dist/template/src/app/(payload)/layout.tsx +6 -1
- package/dist/template/src/collections/Media.ts +1 -1
- package/dist/template/src/collections/Users.ts +1 -1
- package/dist/template/src/payload-types.ts +127 -0
- package/dist/template/src/payload.config.ts +4 -1
- package/dist/types.d.ts +4 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/copy-recursive-sync.js.map +1 -1
- package/dist/utils/git.js.map +1 -1
- package/dist/utils/log.d.ts.map +1 -1
- package/dist/utils/log.js +1 -1
- package/dist/utils/log.js.map +1 -1
- package/dist/utils/messages.js.map +1 -1
- package/package.json +4 -4
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/lib/replacements.ts"],"sourcesContent":["import type { DbType, StorageAdapterType } from '../types.js'\n\ntype DbAdapterReplacement = {\n configReplacement: (envName?: string) => string[]\n importReplacement: string\n packageName: string\n}\n\nconst mongodbReplacement: DbAdapterReplacement = {\n // Replacement between `// database-adapter-config-start` and `// database-adapter-config-end`\n configReplacement: (envName = 'DATABASE_URI') => [\n ' db: mongooseAdapter({',\n ` url: process.env.${envName} || '',`,\n ' }),',\n ],\n importReplacement: \"import { mongooseAdapter } from '@payloadcms/db-mongodb'\",\n packageName: '@payloadcms/db-mongodb',\n}\n\nconst postgresReplacement: DbAdapterReplacement = {\n configReplacement: (envName = 'DATABASE_URI') => [\n ' db: postgresAdapter({',\n ' pool: {',\n ` connectionString: process.env.${envName} || '',`,\n ' },',\n ' }),',\n ],\n importReplacement: \"import { postgresAdapter } from '@payloadcms/db-postgres'\",\n packageName: '@payloadcms/db-postgres',\n}\n\nexport const dbReplacements: Record<DbType, DbAdapterReplacement> = {\n mongodb: mongodbReplacement,\n postgres: postgresReplacement,\n}\n\ntype StorageAdapterReplacement = {\n configReplacement: string[]\n importReplacement?: string\n packageName?: string\n}\n\nconst vercelBlobStorageReplacement: StorageAdapterReplacement = {\n // Replacement of `// storage-adapter-placeholder`\n configReplacement: [\n ' vercelBlobStorage({',\n ' collections: {',\n ' [Media.slug]: true,',\n ' },',\n \" token: process.env.BLOB_READ_WRITE_TOKEN || '',\",\n ' }),',\n ],\n importReplacement: \"import { vercelBlobStorage } from '@payloadcms/storage-vercel-blob'\",\n packageName: '@payloadcms/storage-vercel-blob',\n}\n\nconst payloadCloudReplacement: StorageAdapterReplacement = {\n configReplacement: [' payloadCloudPlugin(),'],\n importReplacement: \"import { payloadCloudPlugin } from '@payloadcms/plugin-cloud'\",\n packageName: '@payloadcms/plugin-cloud',\n}\n\n// Removes placeholders\nconst diskReplacement: StorageAdapterReplacement = {\n configReplacement: [],\n}\n\nexport const storageReplacements: Record<StorageAdapterType, StorageAdapterReplacement> = {\n localDisk: diskReplacement,\n payloadCloud: payloadCloudReplacement,\n vercelBlobStorage: vercelBlobStorageReplacement,\n}\n\n/**\n * Generic config replacement\n */\ntype ConfigReplacement = {\n configReplacement: {\n match: string\n replacement: string\n }\n importReplacement: string\n packageName: string\n}\n\nexport const configReplacements: Record<string, ConfigReplacement> = {\n sharp: {\n // Replacement of `sharp, // Now optional`\n configReplacement: {\n match: 'sharp,',\n replacement: ' // sharp,',\n },\n importReplacement: \"import sharp from 'sharp'\",\n packageName: 'sharp',\n },\n}\n"],"names":["mongodbReplacement","configReplacement","envName","importReplacement","packageName","postgresReplacement","dbReplacements","mongodb","postgres","vercelBlobStorageReplacement","payloadCloudReplacement","diskReplacement","storageReplacements","localDisk","payloadCloud","vercelBlobStorage","configReplacements","sharp","match","replacement"],"
|
1
|
+
{"version":3,"sources":["../../src/lib/replacements.ts"],"sourcesContent":["import type { DbType, StorageAdapterType } from '../types.js'\n\ntype DbAdapterReplacement = {\n configReplacement: (envName?: string) => string[]\n importReplacement: string\n packageName: string\n}\n\nconst mongodbReplacement: DbAdapterReplacement = {\n // Replacement between `// database-adapter-config-start` and `// database-adapter-config-end`\n configReplacement: (envName = 'DATABASE_URI') => [\n ' db: mongooseAdapter({',\n ` url: process.env.${envName} || '',`,\n ' }),',\n ],\n importReplacement: \"import { mongooseAdapter } from '@payloadcms/db-mongodb'\",\n packageName: '@payloadcms/db-mongodb',\n}\n\nconst postgresReplacement: DbAdapterReplacement = {\n configReplacement: (envName = 'DATABASE_URI') => [\n ' db: postgresAdapter({',\n ' pool: {',\n ` connectionString: process.env.${envName} || '',`,\n ' },',\n ' }),',\n ],\n importReplacement: \"import { postgresAdapter } from '@payloadcms/db-postgres'\",\n packageName: '@payloadcms/db-postgres',\n}\n\nconst sqliteReplacement: DbAdapterReplacement = {\n configReplacement: (envName = 'DATABASE_URI') => [\n ' db: sqliteAdapter({',\n ' client: {',\n ` url: process.env.${envName} || '',`,\n ' },',\n ' }),',\n ],\n importReplacement: \"import { sqliteAdapter } from '@payloadcms/db-sqlite'\",\n packageName: '@payloadcms/db-sqlite',\n}\n\nexport const dbReplacements: Record<DbType, DbAdapterReplacement> = {\n mongodb: mongodbReplacement,\n postgres: postgresReplacement,\n sqlite: sqliteReplacement,\n}\n\ntype StorageAdapterReplacement = {\n configReplacement: string[]\n importReplacement?: string\n packageName?: string\n}\n\nconst vercelBlobStorageReplacement: StorageAdapterReplacement = {\n // Replacement of `// storage-adapter-placeholder`\n configReplacement: [\n ' vercelBlobStorage({',\n ' collections: {',\n ' [Media.slug]: true,',\n ' },',\n \" token: process.env.BLOB_READ_WRITE_TOKEN || '',\",\n ' }),',\n ],\n importReplacement: \"import { vercelBlobStorage } from '@payloadcms/storage-vercel-blob'\",\n packageName: '@payloadcms/storage-vercel-blob',\n}\n\nconst payloadCloudReplacement: StorageAdapterReplacement = {\n configReplacement: [' payloadCloudPlugin(),'],\n importReplacement: \"import { payloadCloudPlugin } from '@payloadcms/plugin-cloud'\",\n packageName: '@payloadcms/plugin-cloud',\n}\n\n// Removes placeholders\nconst diskReplacement: StorageAdapterReplacement = {\n configReplacement: [],\n importReplacement: '',\n}\n\nexport const storageReplacements: Record<StorageAdapterType, StorageAdapterReplacement> = {\n localDisk: diskReplacement,\n payloadCloud: payloadCloudReplacement,\n vercelBlobStorage: vercelBlobStorageReplacement,\n}\n\n/**\n * Generic config replacement\n */\ntype ConfigReplacement = {\n configReplacement: {\n match: string\n replacement: string\n }\n importReplacement: string\n packageName: string\n}\n\nexport const configReplacements: Record<string, ConfigReplacement> = {\n sharp: {\n // Replacement of `sharp, // Now optional`\n configReplacement: {\n match: 'sharp,',\n replacement: ' // sharp,',\n },\n importReplacement: \"import sharp from 'sharp'\",\n packageName: 'sharp',\n },\n}\n"],"names":["mongodbReplacement","configReplacement","envName","importReplacement","packageName","postgresReplacement","sqliteReplacement","dbReplacements","mongodb","postgres","sqlite","vercelBlobStorageReplacement","payloadCloudReplacement","diskReplacement","storageReplacements","localDisk","payloadCloud","vercelBlobStorage","configReplacements","sharp","match","replacement"],"mappings":"AAQA,MAAMA,qBAA2C;IAC/C,8FAA8F;IAC9FC,mBAAmB,CAACC,UAAU,cAAc,GAAK;YAC/C;YACA,CAAC,qBAAqB,EAAEA,QAAQ,OAAO,CAAC;YACxC;SACD;IACDC,mBAAmB;IACnBC,aAAa;AACf;AAEA,MAAMC,sBAA4C;IAChDJ,mBAAmB,CAACC,UAAU,cAAc,GAAK;YAC/C;YACA;YACA,CAAC,oCAAoC,EAAEA,QAAQ,OAAO,CAAC;YACvD;YACA;SACD;IACDC,mBAAmB;IACnBC,aAAa;AACf;AAEA,MAAME,oBAA0C;IAC9CL,mBAAmB,CAACC,UAAU,cAAc,GAAK;YAC/C;YACA;YACA,CAAC,uBAAuB,EAAEA,QAAQ,OAAO,CAAC;YAC1C;YACA;SACD;IACDC,mBAAmB;IACnBC,aAAa;AACf;AAEA,OAAO,MAAMG,iBAAuD;IAClEC,SAASR;IACTS,UAAUJ;IACVK,QAAQJ;AACV,EAAC;AAQD,MAAMK,+BAA0D;IAC9D,kDAAkD;IAClDV,mBAAmB;QACjB;QACA;QACA;QACA;QACA;QACA;KACD;IACDE,mBAAmB;IACnBC,aAAa;AACf;AAEA,MAAMQ,0BAAqD;IACzDX,mBAAmB;QAAC;KAA4B;IAChDE,mBAAmB;IACnBC,aAAa;AACf;AAEA,uBAAuB;AACvB,MAAMS,kBAA6C;IACjDZ,mBAAmB,EAAE;IACrBE,mBAAmB;AACrB;AAEA,OAAO,MAAMW,sBAA6E;IACxFC,WAAWF;IACXG,cAAcJ;IACdK,mBAAmBN;AACrB,EAAC;AAcD,OAAO,MAAMO,qBAAwD;IACnEC,OAAO;QACL,0CAA0C;QAC1ClB,mBAAmB;YACjBmB,OAAO;YACPC,aAAa;QACf;QACAlB,mBAAmB;QACnBC,aAAa;IACf;AACF,EAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"select-db.d.ts","sourceRoot":"","sources":["../../src/lib/select-db.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAU,MAAM,aAAa,CAAA;
|
1
|
+
{"version":3,"file":"select-db.d.ts","sourceRoot":"","sources":["../../src/lib/select-db.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAU,MAAM,aAAa,CAAA;AA4B7D,wBAAsB,QAAQ,CAAC,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,CA8CrF"}
|
package/dist/lib/select-db.js
CHANGED
@@ -10,6 +10,12 @@ const dbChoiceRecord = {
|
|
10
10
|
dbConnectionPrefix: 'postgres://postgres:<password>@127.0.0.1:5432/',
|
11
11
|
title: 'PostgreSQL (beta)',
|
12
12
|
value: 'postgres'
|
13
|
+
},
|
14
|
+
sqlite: {
|
15
|
+
dbConnectionPrefix: 'file:./',
|
16
|
+
dbConnectionSuffix: '.db',
|
17
|
+
title: 'SQLite (beta)',
|
18
|
+
value: 'sqlite'
|
13
19
|
}
|
14
20
|
};
|
15
21
|
export async function selectDb(args, projectName) {
|
@@ -23,22 +29,16 @@ export async function selectDb(args, projectName) {
|
|
23
29
|
dbType = await p.select({
|
24
30
|
initialValue: 'mongodb',
|
25
31
|
message: `Select a database`,
|
26
|
-
options:
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
},
|
31
|
-
{
|
32
|
-
label: 'Postgres',
|
33
|
-
value: 'postgres'
|
34
|
-
}
|
35
|
-
]
|
32
|
+
options: Object.values(dbChoiceRecord).map((dbChoice)=>({
|
33
|
+
label: dbChoice.title,
|
34
|
+
value: dbChoice.value
|
35
|
+
}))
|
36
36
|
});
|
37
37
|
if (p.isCancel(dbType)) process.exit(0);
|
38
38
|
}
|
39
39
|
const dbChoice = dbChoiceRecord[dbType];
|
40
40
|
let dbUri = undefined;
|
41
|
-
const initialDbUri = `${dbChoice.dbConnectionPrefix}${projectName === '.' ? `payload-${getRandomDigitSuffix()}` : slugify(projectName)}`;
|
41
|
+
const initialDbUri = `${dbChoice.dbConnectionPrefix}${projectName === '.' ? `payload-${getRandomDigitSuffix()}` : slugify(projectName)}${dbChoice.dbConnectionSuffix || ''}`;
|
42
42
|
if (args['--db-accept-recommended']) {
|
43
43
|
dbUri = initialDbUri;
|
44
44
|
} else if (args['--db-connection-string']) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/lib/select-db.ts"],"sourcesContent":["import * as p from '@clack/prompts'\nimport slugify from '@sindresorhus/slugify'\n\nimport type { CliArgs, DbDetails, DbType } from '../types.js'\n\ntype DbChoice = {\n dbConnectionPrefix: `${string}/`\n title: string\n value: DbType\n}\n\nconst dbChoiceRecord: Record<DbType, DbChoice> = {\n mongodb: {\n dbConnectionPrefix: 'mongodb://127.0.0.1/',\n title: 'MongoDB',\n value: 'mongodb',\n },\n postgres: {\n dbConnectionPrefix: 'postgres://postgres:<password>@127.0.0.1:5432/',\n title: 'PostgreSQL (beta)',\n value: 'postgres',\n },\n}\n\nexport async function selectDb(args: CliArgs, projectName: string): Promise<DbDetails> {\n let dbType: DbType | symbol | undefined = undefined\n if (args['--db']) {\n if (!Object.values(dbChoiceRecord).some((dbChoice) => dbChoice.value === args['--db'])) {\n throw new Error(\n `Invalid database type given. Valid types are: ${Object.values(dbChoiceRecord)\n .map((dbChoice) => dbChoice.value)\n .join(', ')}`,\n )\n }\n dbType = args['--db'] as DbType\n } else {\n dbType = await p.select<{ label: string; value: DbType }[], DbType>({\n initialValue: 'mongodb',\n message: `Select a database`,\n options:
|
1
|
+
{"version":3,"sources":["../../src/lib/select-db.ts"],"sourcesContent":["import * as p from '@clack/prompts'\nimport slugify from '@sindresorhus/slugify'\n\nimport type { CliArgs, DbDetails, DbType } from '../types.js'\n\ntype DbChoice = {\n dbConnectionPrefix: `${string}/`\n dbConnectionSuffix?: string\n title: string\n value: DbType\n}\n\nconst dbChoiceRecord: Record<DbType, DbChoice> = {\n mongodb: {\n dbConnectionPrefix: 'mongodb://127.0.0.1/',\n title: 'MongoDB',\n value: 'mongodb',\n },\n postgres: {\n dbConnectionPrefix: 'postgres://postgres:<password>@127.0.0.1:5432/',\n title: 'PostgreSQL (beta)',\n value: 'postgres',\n },\n sqlite: {\n dbConnectionPrefix: 'file:./',\n dbConnectionSuffix: '.db',\n title: 'SQLite (beta)',\n value: 'sqlite',\n },\n}\n\nexport async function selectDb(args: CliArgs, projectName: string): Promise<DbDetails> {\n let dbType: DbType | symbol | undefined = undefined\n if (args['--db']) {\n if (!Object.values(dbChoiceRecord).some((dbChoice) => dbChoice.value === args['--db'])) {\n throw new Error(\n `Invalid database type given. Valid types are: ${Object.values(dbChoiceRecord)\n .map((dbChoice) => dbChoice.value)\n .join(', ')}`,\n )\n }\n dbType = args['--db'] as DbType\n } else {\n dbType = await p.select<{ label: string; value: DbType }[], DbType>({\n initialValue: 'mongodb',\n message: `Select a database`,\n options: Object.values(dbChoiceRecord).map((dbChoice) => ({\n label: dbChoice.title,\n value: dbChoice.value,\n })),\n })\n if (p.isCancel(dbType)) process.exit(0)\n }\n\n const dbChoice = dbChoiceRecord[dbType]\n\n let dbUri: string | symbol | undefined = undefined\n const initialDbUri = `${dbChoice.dbConnectionPrefix}${\n projectName === '.' ? `payload-${getRandomDigitSuffix()}` : slugify(projectName)\n }${dbChoice.dbConnectionSuffix || ''}`\n\n if (args['--db-accept-recommended']) {\n dbUri = initialDbUri\n } else if (args['--db-connection-string']) {\n dbUri = args['--db-connection-string']\n } else {\n dbUri = await p.text({\n initialValue: initialDbUri,\n message: `Enter ${dbChoice.title.split(' ')[0]} connection string`, // strip beta from title\n })\n if (p.isCancel(dbUri)) process.exit(0)\n }\n\n return {\n type: dbChoice.value,\n dbUri,\n }\n}\n\nfunction getRandomDigitSuffix(): string {\n return (Math.random() * Math.pow(10, 6)).toFixed(0)\n}\n"],"names":["p","slugify","dbChoiceRecord","mongodb","dbConnectionPrefix","title","value","postgres","sqlite","dbConnectionSuffix","selectDb","args","projectName","dbType","undefined","Object","values","some","dbChoice","Error","map","join","select","initialValue","message","options","label","isCancel","process","exit","dbUri","initialDbUri","getRandomDigitSuffix","text","split","type","Math","random","pow","toFixed"],"mappings":"AAAA,YAAYA,OAAO,iBAAgB;AACnC,OAAOC,aAAa,wBAAuB;AAW3C,MAAMC,iBAA2C;IAC/CC,SAAS;QACPC,oBAAoB;QACpBC,OAAO;QACPC,OAAO;IACT;IACAC,UAAU;QACRH,oBAAoB;QACpBC,OAAO;QACPC,OAAO;IACT;IACAE,QAAQ;QACNJ,oBAAoB;QACpBK,oBAAoB;QACpBJ,OAAO;QACPC,OAAO;IACT;AACF;AAEA,OAAO,eAAeI,SAASC,IAAa,EAAEC,WAAmB;IAC/D,IAAIC,SAAsCC;IAC1C,IAAIH,IAAI,CAAC,OAAO,EAAE;QAChB,IAAI,CAACI,OAAOC,MAAM,CAACd,gBAAgBe,IAAI,CAAC,CAACC,WAAaA,SAASZ,KAAK,KAAKK,IAAI,CAAC,OAAO,GAA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,KAAKD,KAAKE,GAAG,CAAC,IAAI,EAAC,EAAGC,OAAO,CAAC;AACnD"}
|
package/dist/lib/templates.js
CHANGED
@@ -14,7 +14,13 @@ export function getValidTemplates() {
|
|
14
14
|
name: 'blank',
|
15
15
|
type: 'starter',
|
16
16
|
description: 'Blank 3.0 Template',
|
17
|
-
url: 'https://github.com/payloadcms/payload/templates/blank
|
17
|
+
url: 'https://github.com/payloadcms/payload/templates/blank#beta'
|
18
|
+
},
|
19
|
+
{
|
20
|
+
name: 'website',
|
21
|
+
type: 'starter',
|
22
|
+
description: 'Website Template',
|
23
|
+
url: 'https://github.com/payloadcms/payload/templates/website#beta'
|
18
24
|
}
|
19
25
|
];
|
20
26
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/lib/templates.ts"],"sourcesContent":["import type { ProjectTemplate } from '../types.js'\n\nimport { error, info } from '../utils/log.js'\n\nexport function validateTemplate(templateName: string): boolean {\n const validTemplates = getValidTemplates()\n if (!validTemplates.map((t) => t.name).includes(templateName)) {\n error(`'${templateName}' is not a valid template.`)\n info(`Valid templates: ${validTemplates.map((t) => t.name).join(', ')}`)\n return false\n }\n return true\n}\n\nexport function getValidTemplates(): ProjectTemplate[] {\n return [\n {\n name: 'blank',\n type: 'starter',\n description: 'Blank 3.0 Template',\n url: 'https://github.com/payloadcms/payload/templates/blank
|
1
|
+
{"version":3,"sources":["../../src/lib/templates.ts"],"sourcesContent":["import type { ProjectTemplate } from '../types.js'\n\nimport { error, info } from '../utils/log.js'\n\nexport function validateTemplate(templateName: string): boolean {\n const validTemplates = getValidTemplates()\n if (!validTemplates.map((t) => t.name).includes(templateName)) {\n error(`'${templateName}' is not a valid template.`)\n info(`Valid templates: ${validTemplates.map((t) => t.name).join(', ')}`)\n return false\n }\n return true\n}\n\nexport function getValidTemplates(): ProjectTemplate[] {\n return [\n {\n name: 'blank',\n type: 'starter',\n description: 'Blank 3.0 Template',\n url: 'https://github.com/payloadcms/payload/templates/blank#beta',\n },\n {\n name: 'website',\n type: 'starter',\n description: 'Website Template',\n url: 'https://github.com/payloadcms/payload/templates/website#beta',\n },\n\n // Remove these until they have been updated for 3.0\n\n // {\n // name: 'blank',\n // type: 'starter',\n // description: 'Blank Template',\n // url: 'https://github.com/payloadcms/payload/templates/blank',\n // },\n // {\n // name: 'ecommerce',\n // type: 'starter',\n // description: 'E-commerce Template',\n // url: 'https://github.com/payloadcms/payload/templates/ecommerce',\n // },\n // {\n // name: 'plugin',\n // type: 'plugin',\n // description: 'Template for creating a Payload plugin',\n // url: 'https://github.com/payloadcms/payload-plugin-template#beta',\n // },\n // {\n // name: 'payload-demo',\n // type: 'starter',\n // description: 'Payload demo site at https://demo.payloadcms.com',\n // url: 'https://github.com/payloadcms/public-demo',\n // },\n // {\n // name: 'payload-website',\n // type: 'starter',\n // description: 'Payload website CMS at https://payloadcms.com',\n // url: 'https://github.com/payloadcms/website-cms',\n // },\n ]\n}\n"],"names":["error","info","validateTemplate","templateName","validTemplates","getValidTemplates","map","t","name","includes","join","type","description","url"],"mappings":"AAEA,SAASA,KAAK,EAAEC,IAAI,QAAQ,kBAAiB;AAE7C,OAAO,SAASC,iBAAiBC,YAAoB;IACnD,MAAMC,iBAAiBC;IACvB,IAAI,CAACD,eAAeE,GAAG,CAAC,CAACC,IAAMA,EAAEC,IAAI,EAAEC,QAAQ,CAACN,eAAe;QAC7DH,MAAM,CAAC,CAAC,EAAEG,aAAa,0BAA0B,CAAC;QAClDF,KAAK,CAAC,iBAAiB,EAAEG,eAAeE,GAAG,CAAC,CAACC,IAAMA,EAAEC,IAAI,EAAEE,IAAI,CAAC,MAAM,CAAC;QACvE,OAAO;IACT;IACA,OAAO;AACT;AAEA,OAAO,SAASL;IACd,OAAO;QACL;YACEG,MAAM;YACNG,MAAM;YACNC,aAAa;YACbC,KAAK;QACP;QACA;YACEL,MAAM;YACNG,MAAM;YACNC,aAAa;YACbC,KAAK;QACP;KAkCD;AACH"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"update-payload-in-project.d.ts","sourceRoot":"","sources":["../../src/lib/update-payload-in-project.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAOjD,wBAAsB,sBAAsB,CAC1C,UAAU,EAAE,cAAc,GACzB,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,
|
1
|
+
{"version":3,"file":"update-payload-in-project.d.ts","sourceRoot":"","sources":["../../src/lib/update-payload-in-project.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAOjD,wBAAsB,sBAAsB,CAC1C,UAAU,EAAE,cAAc,GACzB,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAyEhD"}
|
@@ -59,7 +59,7 @@ export async function updatePayloadInProject(appDetails) {
|
|
59
59
|
}
|
60
60
|
info('Payload packages updated successfully.');
|
61
61
|
info(`Updating Payload Next.js files...`);
|
62
|
-
const templateFilesPath =
|
62
|
+
const templateFilesPath = process.env.JEST_WORKER_ID !== undefined ? path.resolve(dirname, '../../../../templates/blank') : path.resolve(dirname, '../..', 'dist/template');
|
63
63
|
const templateSrcDir = path.resolve(templateFilesPath, 'src/app/(payload)');
|
64
64
|
copyRecursiveSync(templateSrcDir, path.resolve(projectDir, appDetails.isSrcDir ? 'src/app' : 'app', '(payload)'));
|
65
65
|
return {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/lib/update-payload-in-project.ts"],"sourcesContent":["import execa from 'execa'\nimport fse from 'fs-extra'\nimport { fileURLToPath } from 'node:url'\nimport path from 'path'\n\nconst filename = fileURLToPath(import.meta.url)\nconst dirname = path.dirname(filename)\n\nimport type { NextAppDetails } from '../types.js'\n\nimport { copyRecursiveSync } from '../utils/copy-recursive-sync.js'\nimport { info } from '../utils/log.js'\nimport { getPackageManager } from './get-package-manager.js'\nimport { installPackages } from './install-packages.js'\n\nexport async function updatePayloadInProject(\n appDetails: NextAppDetails,\n): Promise<{ message: string; success: boolean }> {\n if (!appDetails.nextConfigPath) return { message: 'No Next.js config found', success: false }\n\n const projectDir = path.dirname(appDetails.nextConfigPath)\n\n const packageObj = (await fse.readJson(path.resolve(projectDir, 'package.json'))) as {\n dependencies?: Record<string, string>\n }\n if (!packageObj?.dependencies) {\n throw new Error('No package.json found in this project')\n }\n\n const payloadVersion = packageObj.dependencies?.payload\n if (!payloadVersion) {\n throw new Error('Payload is not installed in this project')\n }\n\n const packageManager = await getPackageManager({ projectDir })\n\n // Fetch latest Payload version from npm\n const { exitCode: getLatestVersionExitCode, stdout: latestPayloadVersion } = await execa('npm', [\n 'show',\n 'payload@beta',\n 'version',\n ])\n if (getLatestVersionExitCode !== 0) {\n throw new Error('Failed to fetch latest Payload version')\n }\n\n if (payloadVersion === latestPayloadVersion) {\n return { message: `Payload v${payloadVersion} is already up to date.`, success: true }\n }\n\n // Update all existing Payload packages\n const payloadPackages = Object.keys(packageObj.dependencies).filter((dep) =>\n dep.startsWith('@payloadcms/'),\n )\n\n const packageNames = ['payload', ...payloadPackages]\n\n const packagesToUpdate = packageNames.map((pkg) => `${pkg}@${latestPayloadVersion}`)\n\n info(`Using ${packageManager}.\\n`)\n info(\n `Updating ${packagesToUpdate.length} Payload packages to v${latestPayloadVersion}...\\n\\n${packageNames.map((p) => ` - ${p}`).join('\\n')}`,\n )\n\n const { success: updateSuccess } = await installPackages({\n packageManager,\n packagesToInstall: packagesToUpdate,\n projectDir,\n })\n\n if (!updateSuccess) {\n throw new Error('Failed to update Payload packages')\n }\n info('Payload packages updated successfully.')\n\n info(`Updating Payload Next.js files...`)\n const templateFilesPath
|
1
|
+
{"version":3,"sources":["../../src/lib/update-payload-in-project.ts"],"sourcesContent":["import execa from 'execa'\nimport fse from 'fs-extra'\nimport { fileURLToPath } from 'node:url'\nimport path from 'path'\n\nconst filename = fileURLToPath(import.meta.url)\nconst dirname = path.dirname(filename)\n\nimport type { NextAppDetails } from '../types.js'\n\nimport { copyRecursiveSync } from '../utils/copy-recursive-sync.js'\nimport { info } from '../utils/log.js'\nimport { getPackageManager } from './get-package-manager.js'\nimport { installPackages } from './install-packages.js'\n\nexport async function updatePayloadInProject(\n appDetails: NextAppDetails,\n): Promise<{ message: string; success: boolean }> {\n if (!appDetails.nextConfigPath) return { message: 'No Next.js config found', success: false }\n\n const projectDir = path.dirname(appDetails.nextConfigPath)\n\n const packageObj = (await fse.readJson(path.resolve(projectDir, 'package.json'))) as {\n dependencies?: Record<string, string>\n }\n if (!packageObj?.dependencies) {\n throw new Error('No package.json found in this project')\n }\n\n const payloadVersion = packageObj.dependencies?.payload\n if (!payloadVersion) {\n throw new Error('Payload is not installed in this project')\n }\n\n const packageManager = await getPackageManager({ projectDir })\n\n // Fetch latest Payload version from npm\n const { exitCode: getLatestVersionExitCode, stdout: latestPayloadVersion } = await execa('npm', [\n 'show',\n 'payload@beta',\n 'version',\n ])\n if (getLatestVersionExitCode !== 0) {\n throw new Error('Failed to fetch latest Payload version')\n }\n\n if (payloadVersion === latestPayloadVersion) {\n return { message: `Payload v${payloadVersion} is already up to date.`, success: true }\n }\n\n // Update all existing Payload packages\n const payloadPackages = Object.keys(packageObj.dependencies).filter((dep) =>\n dep.startsWith('@payloadcms/'),\n )\n\n const packageNames = ['payload', ...payloadPackages]\n\n const packagesToUpdate = packageNames.map((pkg) => `${pkg}@${latestPayloadVersion}`)\n\n info(`Using ${packageManager}.\\n`)\n info(\n `Updating ${packagesToUpdate.length} Payload packages to v${latestPayloadVersion}...\\n\\n${packageNames.map((p) => ` - ${p}`).join('\\n')}`,\n )\n\n const { success: updateSuccess } = await installPackages({\n packageManager,\n packagesToInstall: packagesToUpdate,\n projectDir,\n })\n\n if (!updateSuccess) {\n throw new Error('Failed to update Payload packages')\n }\n info('Payload packages updated successfully.')\n\n info(`Updating Payload Next.js files...`)\n\n const templateFilesPath =\n process.env.JEST_WORKER_ID !== undefined\n ? path.resolve(dirname, '../../../../templates/blank')\n : path.resolve(dirname, '../..', 'dist/template')\n\n const templateSrcDir = path.resolve(templateFilesPath, 'src/app/(payload)')\n\n copyRecursiveSync(\n templateSrcDir,\n path.resolve(projectDir, appDetails.isSrcDir ? 'src/app' : 'app', '(payload)'),\n )\n\n return { message: 'Payload updated successfully.', success: true }\n}\n"],"names":["execa","fse","fileURLToPath","path","filename","url","dirname","copyRecursiveSync","info","getPackageManager","installPackages","updatePayloadInProject","appDetails","nextConfigPath","message","success","projectDir","packageObj","readJson","resolve","dependencies","Error","payloadVersion","payload","packageManager","exitCode","getLatestVersionExitCode","stdout","latestPayloadVersion","payloadPackages","Object","keys","filter","dep","startsWith","packageNames","packagesToUpdate","map","pkg","length","p","join","updateSuccess","packagesToInstall","templateFilesPath","process","env","JEST_WORKER_ID","undefined","templateSrcDir","isSrcDir"],"mappings":"AAAA,OAAOA,WAAW,QAAO;AACzB,OAAOC,SAAS,WAAU;AAC1B,SAASC,aAAa,QAAQ,WAAU;AACxC,OAAOC,UAAU,OAAM;AAEvB,MAAMC,WAAWF,cAAc,YAAYG,GAAG;AAC9C,MAAMC,UAAUH,KAAKG,OAAO,CAACF;AAI7B,SAASG,iBAAiB,QAAQ,kCAAiC;AACnE,SAASC,IAAI,QAAQ,kBAAiB;AACtC,SAASC,iBAAiB,QAAQ,2BAA0B;AAC5D,SAASC,eAAe,QAAQ,wBAAuB;AAEvD,OAAO,eAAeC,uBACpBC,UAA0B;IAE1B,IAAI,CAACA,WAAWC,cAAc,EAAE,OAAO;QAAEC,SAAS;QAA2BC,SAAS;IAAM;IAE5F,MAAMC,aAAab,KAAKG,OAAO,CAACM,WAAWC,cAAc;IAEzD,MAAMI,aAAc,MAAMhB,IAAIiB,QAAQ,CAACf,KAAKgB,OAAO,CAACH,YAAY;IAGhE,IAAI,CAACC,YAAYG,cAAc;QAC7B,MAAM,IAAIC,M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KC,YAC3B7C,KAAKgB,OAAO,CAACb,SAAS,iCACtBH,KAAKgB,OAAO,CAACb,SAAS,SAAS;IAErC,MAAM2C,iBAAiB9C,KAAKgB,OAAO,CAACyB,mBAAmB;IAEvDrC,kBACE0C,gBACA9C,KAAKgB,OAAO,CAACH,YAAYJ,WAAWsC,QAAQ,GAAG,YAAY,OAAO;IAGpE,OAAO;QAAEpC,SAAS;QAAiCC,SAAS;IAAK;AACnE"}
|
@@ -1,18 +1,18 @@
|
|
1
|
+
import type { NextConfigType } from '../types.js';
|
1
2
|
export declare const withPayloadStatement: {
|
2
3
|
cjs: string;
|
3
4
|
esm: string;
|
5
|
+
ts: string;
|
4
6
|
};
|
5
|
-
type NextConfigType = 'cjs' | 'esm';
|
6
7
|
export declare const wrapNextConfig: (args: {
|
7
8
|
nextConfigPath: string;
|
8
9
|
nextConfigType: NextConfigType;
|
9
|
-
}) => void
|
10
|
+
}) => Promise<void>;
|
10
11
|
/**
|
11
12
|
* Parses config content with AST and wraps it with withPayload function
|
12
13
|
*/
|
13
|
-
export declare function parseAndModifyConfigContent(content: string, configType: NextConfigType): {
|
14
|
+
export declare function parseAndModifyConfigContent(content: string, configType: NextConfigType): Promise<{
|
14
15
|
modifiedConfigContent: string;
|
15
16
|
success: boolean;
|
16
|
-
}
|
17
|
-
export {};
|
17
|
+
}>;
|
18
18
|
//# sourceMappingURL=wrap-next-config.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"wrap-next-config.d.ts","sourceRoot":"","sources":["../../src/lib/wrap-next-config.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"wrap-next-config.d.ts","sourceRoot":"","sources":["../../src/lib/wrap-next-config.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAIjD,eAAO,MAAM,oBAAoB;;;;CAIhC,CAAA;AAED,eAAO,MAAM,cAAc,SAAgB;IACzC,cAAc,EAAE,MAAM,CAAA;IACtB,cAAc,EAAE,cAAc,CAAA;CAC/B,kBAaA,CAAA;AAED;;GAEG;AACH,wBAAsB,2BAA2B,CAC/C,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,cAAc,GACzB,OAAO,CAAC;IAAE,qBAAqB,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CA8H9D"}
|
@@ -1,15 +1,17 @@
|
|
1
|
+
import { parse } from '@swc/core';
|
1
2
|
import chalk from 'chalk';
|
2
3
|
import { Syntax, parseModule } from 'esprima-next';
|
3
4
|
import fs from 'fs';
|
4
5
|
import { log, warning } from '../utils/log.js';
|
5
6
|
export const withPayloadStatement = {
|
6
|
-
cjs: `const { withPayload } = require(
|
7
|
-
esm: `import { withPayload } from
|
7
|
+
cjs: `const { withPayload } = require("@payloadcms/next/withPayload");`,
|
8
|
+
esm: `import { withPayload } from "@payloadcms/next/withPayload";`,
|
9
|
+
ts: `import { withPayload } from "@payloadcms/next/withPayload";`
|
8
10
|
};
|
9
|
-
export const wrapNextConfig = (args)=>{
|
11
|
+
export const wrapNextConfig = async (args)=>{
|
10
12
|
const { nextConfigPath, nextConfigType: configType } = args;
|
11
13
|
const configContent = fs.readFileSync(nextConfigPath, 'utf8');
|
12
|
-
const { modifiedConfigContent: newConfig, success } = parseAndModifyConfigContent(configContent, configType);
|
14
|
+
const { modifiedConfigContent: newConfig, success } = await parseAndModifyConfigContent(configContent, configType);
|
13
15
|
if (!success) {
|
14
16
|
return;
|
15
17
|
}
|
@@ -17,80 +19,103 @@ export const wrapNextConfig = (args)=>{
|
|
17
19
|
};
|
18
20
|
/**
|
19
21
|
* Parses config content with AST and wraps it with withPayload function
|
20
|
-
*/ export function parseAndModifyConfigContent(content, configType) {
|
21
|
-
content = withPayloadStatement[configType] + content;
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
22
|
+
*/ export async function parseAndModifyConfigContent(content, configType) {
|
23
|
+
content = withPayloadStatement[configType] + '\n' + content;
|
24
|
+
console.log({
|
25
|
+
configType,
|
26
|
+
content
|
27
|
+
});
|
28
|
+
if (configType === 'cjs' || configType === 'esm') {
|
29
|
+
try {
|
30
|
+
const ast = parseModule(content, {
|
31
|
+
loc: true
|
32
|
+
});
|
33
|
+
if (configType === 'cjs') {
|
34
|
+
// Find `module.exports = X`
|
35
|
+
const moduleExports = ast.body.find((p)=>p.type === Syntax.ExpressionStatement && p.expression?.type === Syntax.AssignmentExpression && p.expression.left?.type === Syntax.MemberExpression && p.expression.left.object?.type === Syntax.Identifier && p.expression.left.object.name === 'module' && p.expression.left.property?.type === Syntax.Identifier && p.expression.left.property.name === 'exports');
|
36
|
+
if (moduleExports && moduleExports.expression.right?.loc) {
|
37
|
+
const modifiedConfigContent = insertBeforeAndAfter(content, moduleExports.expression.right.loc);
|
38
|
+
return {
|
39
|
+
modifiedConfigContent,
|
40
|
+
success: true
|
41
|
+
};
|
42
|
+
}
|
43
|
+
return Promise.resolve({
|
44
|
+
modifiedConfigContent: content,
|
45
|
+
success: false
|
46
|
+
});
|
47
|
+
} else if (configType === 'esm') {
|
48
|
+
const exportDefaultDeclaration = ast.body.find((p)=>p.type === Syntax.ExportDefaultDeclaration);
|
49
|
+
const exportNamedDeclaration = ast.body.find((p)=>p.type === Syntax.ExportNamedDeclaration);
|
50
|
+
if (!exportDefaultDeclaration && !exportNamedDeclaration) {
|
51
|
+
throw new Error('Could not find ExportDefaultDeclaration in next.config.js');
|
52
|
+
}
|
53
|
+
if (exportDefaultDeclaration && exportDefaultDeclaration.declaration?.loc) {
|
54
|
+
const modifiedConfigContent = insertBeforeAndAfter(content, exportDefaultDeclaration.declaration.loc);
|
55
|
+
return {
|
56
|
+
modifiedConfigContent,
|
57
|
+
success: true
|
58
|
+
};
|
59
|
+
} else if (exportNamedDeclaration) {
|
60
|
+
const exportSpecifier = exportNamedDeclaration.specifiers.find((s)=>s.type === 'ExportSpecifier' && s.exported?.name === 'default' && s.local?.type === 'Identifier' && s.local?.name);
|
61
|
+
if (exportSpecifier) {
|
62
|
+
warning('Could not automatically wrap next.config.js with withPayload.');
|
63
|
+
warning('Automatic wrapping of named exports as default not supported yet.');
|
64
|
+
warnUserWrapNotSuccessful(configType);
|
65
|
+
return {
|
66
|
+
modifiedConfigContent: content,
|
67
|
+
success: false
|
68
|
+
};
|
69
|
+
}
|
70
|
+
}
|
71
|
+
warning('Could not automatically wrap Next config with withPayload.');
|
72
|
+
warnUserWrapNotSuccessful(configType);
|
73
|
+
return Promise.resolve({
|
74
|
+
modifiedConfigContent: content,
|
75
|
+
success: false
|
76
|
+
});
|
77
|
+
}
|
78
|
+
} catch (error) {
|
79
|
+
if (error instanceof Error) {
|
80
|
+
warning(`Unable to parse Next config. Error: ${error.message} `);
|
81
|
+
warnUserWrapNotSuccessful(configType);
|
82
|
+
}
|
45
83
|
return {
|
46
|
-
modifiedConfigContent,
|
47
|
-
success:
|
84
|
+
modifiedConfigContent: content,
|
85
|
+
success: false
|
48
86
|
};
|
49
|
-
}
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
87
|
+
}
|
88
|
+
} else if (configType === 'ts') {
|
89
|
+
const { moduleItems, parseOffset } = await compileTypeScriptFileToAST(content);
|
90
|
+
const exportDefaultDeclaration = moduleItems.find((m)=>m.type === 'ExportDefaultExpression' && (m.expression.type === 'Identifier' || m.expression.type === 'CallExpression'));
|
91
|
+
if (exportDefaultDeclaration) {
|
92
|
+
if (!('span' in exportDefaultDeclaration.expression)) {
|
93
|
+
warning('Could not automatically wrap Next config with withPayload.');
|
54
94
|
warnUserWrapNotSuccessful(configType);
|
55
|
-
return {
|
95
|
+
return Promise.resolve({
|
56
96
|
modifiedConfigContent: content,
|
57
97
|
success: false
|
58
|
-
};
|
98
|
+
});
|
59
99
|
}
|
60
|
-
|
61
|
-
warning('Could not automatically wrap Next config with withPayload.');
|
62
|
-
warnUserWrapNotSuccessful(configType);
|
63
|
-
return {
|
64
|
-
modifiedConfigContent: content,
|
65
|
-
success: false
|
66
|
-
};
|
67
|
-
} else if (configType === 'cjs') {
|
68
|
-
// Find `module.exports = X`
|
69
|
-
const moduleExports = ast.body.find((p)=>p.type === Syntax.ExpressionStatement && p.expression?.type === Syntax.AssignmentExpression && p.expression.left?.type === Syntax.MemberExpression && p.expression.left.object?.type === Syntax.Identifier && p.expression.left.object.name === 'module' && p.expression.left.property?.type === Syntax.Identifier && p.expression.left.property.name === 'exports');
|
70
|
-
if (moduleExports && moduleExports.expression.right?.loc) {
|
71
|
-
const modifiedConfigContent = insertBeforeAndAfter(content, moduleExports.expression.right.loc);
|
100
|
+
const modifiedConfigContent = insertBeforeAndAfterSWC(content, exportDefaultDeclaration.expression.span, parseOffset);
|
72
101
|
return {
|
73
102
|
modifiedConfigContent,
|
74
103
|
success: true
|
75
104
|
};
|
76
105
|
}
|
77
|
-
return {
|
78
|
-
modifiedConfigContent: content,
|
79
|
-
success: false
|
80
|
-
};
|
81
106
|
}
|
82
107
|
warning('Could not automatically wrap Next config with withPayload.');
|
83
108
|
warnUserWrapNotSuccessful(configType);
|
84
|
-
return {
|
109
|
+
return Promise.resolve({
|
85
110
|
modifiedConfigContent: content,
|
86
111
|
success: false
|
87
|
-
};
|
112
|
+
});
|
88
113
|
}
|
89
114
|
function warnUserWrapNotSuccessful(configType) {
|
90
115
|
// Output directions for user to update next.config.js
|
91
116
|
const withPayloadMessage = `
|
92
117
|
|
93
|
-
${chalk.bold(`Please manually wrap your existing
|
118
|
+
${chalk.bold(`Please manually wrap your existing Next config with the withPayload function. Here is an example:`)}
|
94
119
|
|
95
120
|
${withPayloadStatement[configType]}
|
96
121
|
|
@@ -98,7 +123,7 @@ function warnUserWrapNotSuccessful(configType) {
|
|
98
123
|
// Your Next.js config here
|
99
124
|
}
|
100
125
|
|
101
|
-
${configType === '
|
126
|
+
${configType === 'cjs' ? 'module.exports = withPayload(nextConfig)' : 'export default withPayload(nextConfig)'}
|
102
127
|
|
103
128
|
`;
|
104
129
|
log(withPayloadMessage);
|
@@ -119,5 +144,44 @@ function insertBeforeAndAfter(content, loc) {
|
|
119
144
|
}
|
120
145
|
return lines.join('\n');
|
121
146
|
}
|
147
|
+
function insertBeforeAndAfterSWC(content, span, /**
|
148
|
+
* WARNING: This is ONLY for unit tests. Defaults to 0 otherwise.
|
149
|
+
*
|
150
|
+
* @see compileTypeScriptFileToAST
|
151
|
+
*/ parseOffset) {
|
152
|
+
const { end: preOffsetEnd, start: preOffsetStart } = span;
|
153
|
+
const start = preOffsetStart - parseOffset;
|
154
|
+
const end = preOffsetEnd - parseOffset;
|
155
|
+
const insert = (pos, text)=>{
|
156
|
+
return content.slice(0, pos) + text + content.slice(pos);
|
157
|
+
};
|
158
|
+
// insert ) after end
|
159
|
+
content = insert(end - 1, ')');
|
160
|
+
// insert withPayload before start
|
161
|
+
content = insert(start - 1, 'withPayload(');
|
162
|
+
return content;
|
163
|
+
}
|
164
|
+
/**
|
165
|
+
* Compile typescript to AST using the swc compiler
|
166
|
+
*/ async function compileTypeScriptFileToAST(fileContent) {
|
167
|
+
let parseOffset = 0;
|
168
|
+
/**
|
169
|
+
* WARNING: This is ONLY for unit tests.
|
170
|
+
*
|
171
|
+
* Multiple instances of swc DO NOT reset the .span.end value.
|
172
|
+
* During unit tests, the .spawn.end value is read and accounted for.
|
173
|
+
*
|
174
|
+
* https://github.com/swc-project/swc/issues/1366
|
175
|
+
*/ if (process.env.NODE_ENV === 'test') {
|
176
|
+
parseOffset = (await parse('')).span.end;
|
177
|
+
}
|
178
|
+
const module = await parse(fileContent, {
|
179
|
+
syntax: 'typescript'
|
180
|
+
});
|
181
|
+
return {
|
182
|
+
moduleItems: module.body,
|
183
|
+
parseOffset
|
184
|
+
};
|
185
|
+
}
|
122
186
|
|
123
187
|
//# sourceMappingURL=wrap-next-config.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/lib/wrap-next-config.ts"],"sourcesContent":["import type { Program } from 'esprima-next'\n\nimport chalk from 'chalk'\nimport { Syntax, parseModule } from 'esprima-next'\nimport fs from 'fs'\n\nimport { log, warning } from '../utils/log.js'\n\nexport const withPayloadStatement = {\n cjs: `const { withPayload } = require('@payloadcms/next/withPayload')\\n`,\n esm: `import { withPayload } from '@payloadcms/next/withPayload'\\n`,\n}\n\ntype NextConfigType = 'cjs' | 'esm'\n\nexport const wrapNextConfig = (args: {\n nextConfigPath: string\n nextConfigType: NextConfigType\n}) => {\n const { nextConfigPath, nextConfigType: configType } = args\n const configContent = fs.readFileSync(nextConfigPath, 'utf8')\n const { modifiedConfigContent: newConfig, success } = parseAndModifyConfigContent(\n configContent,\n configType,\n )\n\n if (!success) {\n return\n }\n\n fs.writeFileSync(nextConfigPath, newConfig)\n}\n\n/**\n * Parses config content with AST and wraps it with withPayload function\n */\nexport function parseAndModifyConfigContent(\n content: string,\n configType: NextConfigType,\n): { modifiedConfigContent: string; success: boolean } {\n content = withPayloadStatement[configType] + content\n\n let ast: Program | undefined\n try {\n ast = parseModule(content, { loc: true })\n } catch (error: unknown) {\n if (error instanceof Error) {\n warning(`Unable to parse Next config. Error: ${error.message} `)\n warnUserWrapNotSuccessful(configType)\n }\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n\n if (configType === 'esm') {\n const exportDefaultDeclaration = ast.body.find(\n (p) => p.type === Syntax.ExportDefaultDeclaration,\n ) as Directive | undefined\n\n const exportNamedDeclaration = ast.body.find(\n (p) => p.type === Syntax.ExportNamedDeclaration,\n ) as ExportNamedDeclaration | undefined\n\n if (!exportDefaultDeclaration && !exportNamedDeclaration) {\n throw new Error('Could not find ExportDefaultDeclaration in next.config.js')\n }\n\n if (exportDefaultDeclaration && exportDefaultDeclaration.declaration?.loc) {\n const modifiedConfigContent = insertBeforeAndAfter(\n content,\n exportDefaultDeclaration.declaration.loc,\n )\n return { modifiedConfigContent, success: true }\n } else if (exportNamedDeclaration) {\n const exportSpecifier = exportNamedDeclaration.specifiers.find(\n (s) =>\n s.type === 'ExportSpecifier' &&\n s.exported?.name === 'default' &&\n s.local?.type === 'Identifier' &&\n s.local?.name,\n )\n\n if (exportSpecifier) {\n warning('Could not automatically wrap next.config.js with withPayload.')\n warning('Automatic wrapping of named exports as default not supported yet.')\n\n warnUserWrapNotSuccessful(configType)\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n }\n\n warning('Could not automatically wrap Next config with withPayload.')\n warnUserWrapNotSuccessful(configType)\n return {\n modifiedConfigContent: content,\n success: false,\n }\n } else if (configType === 'cjs') {\n // Find `module.exports = X`\n const moduleExports = ast.body.find(\n (p) =>\n p.type === Syntax.ExpressionStatement &&\n p.expression?.type === Syntax.AssignmentExpression &&\n p.expression.left?.type === Syntax.MemberExpression &&\n p.expression.left.object?.type === Syntax.Identifier &&\n p.expression.left.object.name === 'module' &&\n p.expression.left.property?.type === Syntax.Identifier &&\n p.expression.left.property.name === 'exports',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ) as any\n\n if (moduleExports && moduleExports.expression.right?.loc) {\n const modifiedConfigContent = insertBeforeAndAfter(\n content,\n moduleExports.expression.right.loc,\n )\n return { modifiedConfigContent, success: true }\n }\n\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n\n warning('Could not automatically wrap Next config with withPayload.')\n warnUserWrapNotSuccessful(configType)\n return {\n modifiedConfigContent: content,\n success: false,\n }\n}\n\nfunction warnUserWrapNotSuccessful(configType: NextConfigType) {\n // Output directions for user to update next.config.js\n const withPayloadMessage = `\n\n ${chalk.bold(`Please manually wrap your existing next.config.js with the withPayload function. Here is an example:`)}\n\n ${withPayloadStatement[configType]}\n\n const nextConfig = {\n // Your Next.js config here\n }\n\n ${configType === 'esm' ? 'export default withPayload(nextConfig)' : 'module.exports = withPayload(nextConfig)'}\n\n`\n\n log(withPayloadMessage)\n}\n\ntype Directive = {\n declaration?: {\n loc: Loc\n }\n}\n\ntype ExportNamedDeclaration = {\n declaration: null\n loc: Loc\n specifiers: {\n exported: {\n loc: Loc\n name: string\n type: string\n }\n loc: Loc\n local: {\n loc: Loc\n name: string\n type: string\n }\n type: string\n }[]\n type: string\n}\n\ntype Loc = {\n end: { column: number; line: number }\n start: { column: number; line: number }\n}\n\nfunction insertBeforeAndAfter(content: string, loc: Loc) {\n const { end, start } = loc\n const lines = content.split('\\n')\n\n const insert = (line: string, column: number, text: string) => {\n return line.slice(0, column) + text + line.slice(column)\n }\n\n // insert ) after end\n lines[end.line - 1] = insert(lines[end.line - 1], end.column, ')')\n // insert withPayload before start\n if (start.line === end.line) {\n lines[end.line - 1] = insert(lines[end.line - 1], start.column, 'withPayload(')\n } else {\n lines[start.line - 1] = insert(lines[start.line - 1], start.column, 'withPayload(')\n }\n\n return lines.join('\\n')\n}\n"],"names":["chalk","Syntax","parseModule","fs","log","warning","withPayloadStatement","cjs","esm","wrapNextConfig","args","nextConfigPath","nextConfigType","configType","configContent","readFileSync","modifiedConfigContent","newConfig","success","parseAndModifyConfigContent","writeFileSync","content","ast","loc","error","Error","message","warnUserWrapNotSuccessful","exportDefaultDeclaration","body","find","p","type","ExportDefaultDeclaration","exportNamedDeclaration","ExportNamedDeclaration","declaration","insertBeforeAndAfter","exportSpecifier","specifiers","s","exported","name","local","moduleExports","ExpressionStatement","expression","AssignmentExpression","left","MemberExpression","object","Identifier","property","right","withPayloadMessage","bold","end","start","lines","split","insert","line","column","text","slice","join"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAEA,OAAOA,WAAW,QAAO;AACzB,SAASC,MAAM,EAAEC,WAAW,QAAQ,eAAc;AAClD,OAAOC,Q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|
1
|
+
{"version":3,"sources":["../../src/lib/wrap-next-config.ts"],"sourcesContent":["import type { ExportDefaultExpression, ModuleItem } from '@swc/core'\n\nimport { parse } from '@swc/core'\nimport chalk from 'chalk'\nimport { Syntax, parseModule } from 'esprima-next'\nimport fs from 'fs'\n\nimport type { NextConfigType } from '../types.js'\n\nimport { log, warning } from '../utils/log.js'\n\nexport const withPayloadStatement = {\n cjs: `const { withPayload } = require(\"@payloadcms/next/withPayload\");`,\n esm: `import { withPayload } from \"@payloadcms/next/withPayload\";`,\n ts: `import { withPayload } from \"@payloadcms/next/withPayload\";`,\n}\n\nexport const wrapNextConfig = async (args: {\n nextConfigPath: string\n nextConfigType: NextConfigType\n}) => {\n const { nextConfigPath, nextConfigType: configType } = args\n const configContent = fs.readFileSync(nextConfigPath, 'utf8')\n const { modifiedConfigContent: newConfig, success } = await parseAndModifyConfigContent(\n configContent,\n configType,\n )\n\n if (!success) {\n return\n }\n\n fs.writeFileSync(nextConfigPath, newConfig)\n}\n\n/**\n * Parses config content with AST and wraps it with withPayload function\n */\nexport async function parseAndModifyConfigContent(\n content: string,\n configType: NextConfigType,\n): Promise<{ modifiedConfigContent: string; success: boolean }> {\n content = withPayloadStatement[configType] + '\\n' + content\n\n console.log({ configType, content })\n\n if (configType === 'cjs' || configType === 'esm') {\n try {\n const ast = parseModule(content, { loc: true })\n\n if (configType === 'cjs') {\n // Find `module.exports = X`\n const moduleExports = ast.body.find(\n (p) =>\n p.type === Syntax.ExpressionStatement &&\n p.expression?.type === Syntax.AssignmentExpression &&\n p.expression.left?.type === Syntax.MemberExpression &&\n p.expression.left.object?.type === Syntax.Identifier &&\n p.expression.left.object.name === 'module' &&\n p.expression.left.property?.type === Syntax.Identifier &&\n p.expression.left.property.name === 'exports',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ) as any\n\n if (moduleExports && moduleExports.expression.right?.loc) {\n const modifiedConfigContent = insertBeforeAndAfter(\n content,\n moduleExports.expression.right.loc,\n )\n return { modifiedConfigContent, success: true }\n }\n\n return Promise.resolve({\n modifiedConfigContent: content,\n success: false,\n })\n } else if (configType === 'esm') {\n const exportDefaultDeclaration = ast.body.find(\n (p) => p.type === Syntax.ExportDefaultDeclaration,\n ) as Directive | undefined\n\n const exportNamedDeclaration = ast.body.find(\n (p) => p.type === Syntax.ExportNamedDeclaration,\n ) as ExportNamedDeclaration | undefined\n\n if (!exportDefaultDeclaration && !exportNamedDeclaration) {\n throw new Error('Could not find ExportDefaultDeclaration in next.config.js')\n }\n\n if (exportDefaultDeclaration && exportDefaultDeclaration.declaration?.loc) {\n const modifiedConfigContent = insertBeforeAndAfter(\n content,\n exportDefaultDeclaration.declaration.loc,\n )\n return { modifiedConfigContent, success: true }\n } else if (exportNamedDeclaration) {\n const exportSpecifier = exportNamedDeclaration.specifiers.find(\n (s) =>\n s.type === 'ExportSpecifier' &&\n s.exported?.name === 'default' &&\n s.local?.type === 'Identifier' &&\n s.local?.name,\n )\n\n if (exportSpecifier) {\n warning('Could not automatically wrap next.config.js with withPayload.')\n warning('Automatic wrapping of named exports as default not supported yet.')\n\n warnUserWrapNotSuccessful(configType)\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n }\n\n warning('Could not automatically wrap Next config with withPayload.')\n warnUserWrapNotSuccessful(configType)\n return Promise.resolve({\n modifiedConfigContent: content,\n success: false,\n })\n }\n } catch (error: unknown) {\n if (error instanceof Error) {\n warning(`Unable to parse Next config. Error: ${error.message} `)\n warnUserWrapNotSuccessful(configType)\n }\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n } else if (configType === 'ts') {\n const { moduleItems, parseOffset } = await compileTypeScriptFileToAST(content)\n\n const exportDefaultDeclaration = moduleItems.find(\n (m) =>\n m.type === 'ExportDefaultExpression' &&\n (m.expression.type === 'Identifier' || m.expression.type === 'CallExpression'),\n ) as ExportDefaultExpression | undefined\n\n if (exportDefaultDeclaration) {\n if (!('span' in exportDefaultDeclaration.expression)) {\n warning('Could not automatically wrap Next config with withPayload.')\n warnUserWrapNotSuccessful(configType)\n return Promise.resolve({\n modifiedConfigContent: content,\n success: false,\n })\n }\n\n const modifiedConfigContent = insertBeforeAndAfterSWC(\n content,\n exportDefaultDeclaration.expression.span,\n parseOffset,\n )\n return { modifiedConfigContent, success: true }\n }\n }\n\n warning('Could not automatically wrap Next config with withPayload.')\n warnUserWrapNotSuccessful(configType)\n return Promise.resolve({\n modifiedConfigContent: content,\n success: false,\n })\n}\n\nfunction warnUserWrapNotSuccessful(configType: NextConfigType) {\n // Output directions for user to update next.config.js\n const withPayloadMessage = `\n\n ${chalk.bold(`Please manually wrap your existing Next config with the withPayload function. Here is an example:`)}\n\n ${withPayloadStatement[configType]}\n\n const nextConfig = {\n // Your Next.js config here\n }\n\n ${configType === 'cjs' ? 'module.exports = withPayload(nextConfig)' : 'export default withPayload(nextConfig)'}\n\n`\n\n log(withPayloadMessage)\n}\n\ntype Directive = {\n declaration?: {\n loc: Loc\n }\n}\n\ntype ExportNamedDeclaration = {\n declaration: null\n loc: Loc\n specifiers: {\n exported: {\n loc: Loc\n name: string\n type: string\n }\n loc: Loc\n local: {\n loc: Loc\n name: string\n type: string\n }\n type: string\n }[]\n type: string\n}\n\ntype Loc = {\n end: { column: number; line: number }\n start: { column: number; line: number }\n}\n\nfunction insertBeforeAndAfter(content: string, loc: Loc): string {\n const { end, start } = loc\n const lines = content.split('\\n')\n\n const insert = (line: string, column: number, text: string) => {\n return line.slice(0, column) + text + line.slice(column)\n }\n\n // insert ) after end\n lines[end.line - 1] = insert(lines[end.line - 1], end.column, ')')\n // insert withPayload before start\n if (start.line === end.line) {\n lines[end.line - 1] = insert(lines[end.line - 1], start.column, 'withPayload(')\n } else {\n lines[start.line - 1] = insert(lines[start.line - 1], start.column, 'withPayload(')\n }\n\n return lines.join('\\n')\n}\n\nfunction insertBeforeAndAfterSWC(\n content: string,\n span: ModuleItem['span'],\n /**\n * WARNING: This is ONLY for unit tests. Defaults to 0 otherwise.\n *\n * @see compileTypeScriptFileToAST\n */\n parseOffset: number,\n): string {\n const { end: preOffsetEnd, start: preOffsetStart } = span\n\n const start = preOffsetStart - parseOffset\n const end = preOffsetEnd - parseOffset\n\n const insert = (pos: number, text: string): string => {\n return content.slice(0, pos) + text + content.slice(pos)\n }\n\n // insert ) after end\n content = insert(end - 1, ')')\n // insert withPayload before start\n content = insert(start - 1, 'withPayload(')\n\n return content\n}\n\n/**\n * Compile typescript to AST using the swc compiler\n */\nasync function compileTypeScriptFileToAST(\n fileContent: string,\n): Promise<{ moduleItems: ModuleItem[]; parseOffset: number }> {\n let parseOffset = 0\n\n /**\n * WARNING: This is ONLY for unit tests.\n *\n * Multiple instances of swc DO NOT reset the .span.end value.\n * During unit tests, the .spawn.end value is read and accounted for.\n *\n * https://github.com/swc-project/swc/issues/1366\n */\n if (process.env.NODE_ENV === 'test') {\n parseOffset = (await parse('')).span.end\n }\n\n const module = await parse(fileContent, {\n syntax: 'typescript',\n })\n\n return { moduleItems: module.body, parseOffset }\n}\n"],"names":["parse","chalk","Syntax","parseModule","fs","log","warning","withPayloadStatement","cjs","esm","ts","wrapNextConfig","args","nextConfigPath","nextConfigType","configType","configContent","readFileSync","modifiedConfigContent","newConfig","success","parseAndModifyConfigContent","writeFileSync","content","console","ast","loc","moduleExports","body","find","p","type","ExpressionStatement","expression","AssignmentExpression","left","MemberExpression","object","Identifier","name","property","right","insertBeforeAndAfter","Promise","resolve","exportDefaultDeclaration","ExportDefaultDeclaration","exportNamedDeclaration","ExportNamedDeclaration","Error","declaration","exportSpecifier","specifiers","s","exported","local","warnUserWrapNotSuccessful","error","message","moduleItems","parseOffset","compileTypeScriptFileToAST","m","insertBeforeAndAfterSWC","span","withPayloadMessage","bold","end","start","lines","split","insert","line","column","text","slice","join","preOffsetEnd","preOffsetStart","pos","fileContent","process","env","NODE_ENV","module","syntax"],"mappings":"AAEA,SAASA,KAAK,QAAQ,YAAW;AACjC,OAAOC,WAAW,QAAO;AACzB,SAASC,MAAM,EAAEC,WAAW,QAA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