create-payload-app 3.0.0-alpha.59 → 3.0.0-alpha.61
Sign up to get free protection for your applications and to get access to all the features.
- package/bin/cli.js +3 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -9
- package/dist/index.js.map +1 -1
- package/dist/lib/configure-payload-config.js +12 -27
- package/dist/lib/configure-payload-config.js.map +1 -1
- package/dist/lib/create-project.js +32 -96
- package/dist/lib/create-project.js.map +1 -1
- package/dist/lib/create-project.spec.js +24 -33
- package/dist/lib/create-project.spec.js.map +1 -1
- package/dist/lib/generate-secret.js +3 -13
- package/dist/lib/generate-secret.js.map +1 -1
- package/dist/lib/init-next.js +40 -104
- package/dist/lib/init-next.js.map +1 -1
- package/dist/lib/packages.js +1 -11
- package/dist/lib/packages.js.map +1 -1
- package/dist/lib/parse-project-name.js +8 -64
- package/dist/lib/parse-project-name.js.map +1 -1
- package/dist/lib/parse-template.js +4 -55
- package/dist/lib/parse-template.js.map +1 -1
- package/dist/lib/select-db.js +8 -64
- package/dist/lib/select-db.js.map +1 -1
- package/dist/lib/templates.js +5 -23
- package/dist/lib/templates.js.map +1 -1
- package/dist/lib/wrap-next-config.js +18 -41
- package/dist/lib/wrap-next-config.js.map +1 -1
- package/dist/lib/wrap-next-config.spec.js +10 -55
- package/dist/lib/wrap-next-config.spec.js.map +1 -1
- package/dist/lib/write-env-file.js +11 -26
- package/dist/lib/write-env-file.js.map +1 -1
- package/dist/main.js +60 -115
- package/dist/main.js.map +1 -1
- package/dist/scripts/pack-template-files.js +15 -24
- package/dist/scripts/pack-template-files.js.map +1 -1
- package/dist/types.js +1 -4
- package/dist/types.js.map +1 -1
- package/dist/utils/copy-recursive-sync.js +11 -24
- package/dist/utils/copy-recursive-sync.js.map +1 -1
- package/dist/utils/log.js +12 -85
- package/dist/utils/log.js.map +1 -1
- package/dist/utils/messages.js +20 -55
- package/dist/utils/messages.js.map +1 -1
- package/package.json +1 -1
@@ -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://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: [\n { label: 'MongoDB', value: 'mongodb' },\n { label: 'Postgres', value: 'postgres' },\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 }`\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":["
|
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://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: [\n { label: 'MongoDB', value: 'mongodb' },\n { label: 'Postgres', value: 'postgres' },\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 }`\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","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"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,YAAYA,OAAO,iBAAgB;AACnC,OAAOC,aAAa,wBAAuB;AAU3C,MAAMC,iBAA2C;IAC/CC,SAAS;QACPC,oBAAoB;QACpBC,OAAO;QACPC,OAAO;IACT;IACAC,UAAU;QACRH,oBAAoB;QACpBC,OAAO;QACPC,OAAO;IACT;AACF;AAEA,OAAO,eAAeE,SAASC,IAAa,EAAEC,WAAmB;IAC/D,IAAIC,SAAsCC;IAC1C,IAAIH,IAAI,CAAC,OAAO,EAAE;QAChB,IAAI,CAACI,OAAOC,MAAM,CAACZ,gBAAgBa,IAAI,CAAC,CAACC,WAAaA,SAASV,KAAK,KAAKG,IAAI,CAAC,OAAO,GAAG;YACtF,MAAM,IAAIQ,MACR,CAAC,8CAA8C,EAAEJ,OAAOC,MAAM,CAACZ,gBAC5DgB,GAAG,CAAC,CAACF,WAAaA,SAASV,KAAK,EAChCa,IAAI,CAAC,MAAM,CAAC;QAEnB;QACAR,SAASF,IAAI,CAAC,OAAO;IACvB,OAAO;QACLE,SAAS,MAAMX,EAAEoB,MAAM,CAA6C;YAClEC,cAAc;YACdC,SAAS,CAAC,iBAAiB,CAAC;YAC5BC,SAAS;gBACP;oBAAEC,OAAO;oBAAWlB,OAAO;gBAAU;gBACrC;oBAAEkB,OAAO;oBAAYlB,OAAO;gBAAW;aACxC;QACH;QACA,IAAIN,EAAEyB,QAAQ,CAACd,SAASe,QAAQC,IAAI,CAAC;IACvC;IAEA,MAAMX,WAAWd,cAAc,CAACS,OAAO;IAEvC,IAAIiB,QAAqChB;IACzC,MAAMiB,eAAe,CAAC,EAAEb,SAASZ,kBAAkB,CAAC,EAClDM,gBAAgB,MAAM,CAAC,QAAQ,EAAEoB,uBAAuB,CAAC,GAAG7B,QAAQS,aACrE,CAAC;IAEF,IAAID,IAAI,CAAC,0BAA0B,EAAE;QACnCmB,QAAQC;IACV,OAAO,IAAIpB,IAAI,CAAC,yBAAyB,EAAE;QACzCmB,QAAQnB,IAAI,CAAC,yBAAyB;IACxC,OAAO;QACLmB,QAAQ,MAAM5B,EAAE+B,IAAI,CAAC;YACnBV,cAAcQ;YACdP,SAAS,CAAC,MAAM,EAAEN,SAASX,KAAK,CAAC2B,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,kBAAkB,CAAC;QACpE;QACA,IAAIhC,EAAEyB,QAAQ,CAACG,QAAQF,QAAQC,IAAI,CAAC;IACtC;IAEA,OAAO;QACLM,MAAMjB,SAASV,KAAK;QACpBsB;IACF;AACF;AAEA,SAASE;IACP,OAAO,AAACI,CAAAA,KAAKC,MAAM,KAAKD,KAAKE,GAAG,CAAC,IAAI,EAAC,EAAGC,OAAO,CAAC;AACnD"}
|
package/dist/lib/templates.js
CHANGED
@@ -1,32 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
value: true
|
4
|
-
});
|
5
|
-
function _export(target, all) {
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
7
|
-
enumerable: true,
|
8
|
-
get: all[name]
|
9
|
-
});
|
10
|
-
}
|
11
|
-
_export(exports, {
|
12
|
-
getValidTemplates: function() {
|
13
|
-
return getValidTemplates;
|
14
|
-
},
|
15
|
-
validateTemplate: function() {
|
16
|
-
return validateTemplate;
|
17
|
-
}
|
18
|
-
});
|
19
|
-
const _log = require("../utils/log.js");
|
20
|
-
function validateTemplate(templateName) {
|
1
|
+
import { error, info } from '../utils/log.js';
|
2
|
+
export function validateTemplate(templateName) {
|
21
3
|
const validTemplates = getValidTemplates();
|
22
4
|
if (!validTemplates.map((t)=>t.name).includes(templateName)) {
|
23
|
-
|
24
|
-
|
5
|
+
error(`'${templateName}' is not a valid template.`);
|
6
|
+
info(`Valid templates: ${validTemplates.map((t)=>t.name).join(', ')}`);
|
25
7
|
return false;
|
26
8
|
}
|
27
9
|
return true;
|
28
10
|
}
|
29
|
-
function getValidTemplates() {
|
11
|
+
export function getValidTemplates() {
|
30
12
|
return [
|
31
13
|
{
|
32
14
|
name: 'blank-3.0',
|
@@ -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-3.0',\n type: 'starter',\n description: 'Blank 3.0 Template',\n url: 'https://github.com/payloadcms/payload/templates/blank-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: 'website',\n type: 'starter',\n description: 'Website Template',\n url: 'https://github.com/payloadcms/payload/templates/website',\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',\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":["
|
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-3.0',\n type: 'starter',\n description: 'Blank 3.0 Template',\n url: 'https://github.com/payloadcms/payload/templates/blank-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: 'website',\n type: 'starter',\n description: 'Website Template',\n url: 'https://github.com/payloadcms/payload/templates/website',\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',\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"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","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;QACA;YACEL,MAAM;YACNG,MAAM;YACNC,aAAa;YACbC,KAAK;QACP;QACA;YACEL,MAAM;YACNG,MAAM;YACNC,aAAa;YACbC,KAAK;QACP;QACA;YACEL,MAAM;YACNG,MAAM;YACNC,aAAa;YACbC,KAAK;QACP;QACA;YACEL,MAAM;YACNG,MAAM;YACNC,aAAa;YACbC,KAAK;QACP;QACA;YACEL,MAAM;YACNG,MAAM;YACNC,aAAa;YACbC,KAAK;QACP;KACD;AACH"}
|
@@ -1,46 +1,23 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
}
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
get: all[name]
|
9
|
-
});
|
10
|
-
}
|
11
|
-
_export(exports, {
|
12
|
-
parseAndModifyConfigContent: function() {
|
13
|
-
return parseAndModifyConfigContent;
|
14
|
-
},
|
15
|
-
withPayloadImportStatement: function() {
|
16
|
-
return withPayloadImportStatement;
|
17
|
-
},
|
18
|
-
wrapNextConfig: function() {
|
19
|
-
return wrapNextConfig;
|
20
|
-
}
|
21
|
-
});
|
22
|
-
const _chalk = /*#__PURE__*/ _interop_require_default(require("chalk"));
|
23
|
-
const _esprima = require("esprima");
|
24
|
-
const _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
|
25
|
-
const _log = require("../utils/log.js");
|
26
|
-
function _interop_require_default(obj) {
|
27
|
-
return obj && obj.__esModule ? obj : {
|
28
|
-
default: obj
|
29
|
-
};
|
30
|
-
}
|
31
|
-
const withPayloadImportStatement = `import { withPayload } from '@payloadcms/next'\n`;
|
32
|
-
const wrapNextConfig = (args)=>{
|
1
|
+
import chalk from 'chalk';
|
2
|
+
import { parseModule } from 'esprima';
|
3
|
+
import fs from 'fs';
|
4
|
+
import { warning } from '../utils/log.js';
|
5
|
+
import { log } from '../utils/log.js';
|
6
|
+
export const withPayloadImportStatement = `import { withPayload } from '@payloadcms/next'\n`;
|
7
|
+
export const wrapNextConfig = (args)=>{
|
33
8
|
const { nextConfigPath } = args;
|
34
|
-
const configContent =
|
9
|
+
const configContent = fs.readFileSync(nextConfigPath, 'utf8');
|
35
10
|
const { modifiedConfigContent: newConfig, success } = parseAndModifyConfigContent(configContent);
|
36
11
|
if (!success) {
|
37
12
|
return;
|
38
13
|
}
|
39
|
-
|
14
|
+
fs.writeFileSync(nextConfigPath, newConfig);
|
40
15
|
};
|
41
|
-
|
16
|
+
/**
|
17
|
+
* Parses config content with AST and wraps it with withPayload function
|
18
|
+
*/ export function parseAndModifyConfigContent(content) {
|
42
19
|
content = withPayloadImportStatement + content;
|
43
|
-
const ast =
|
20
|
+
const ast = parseModule(content, {
|
44
21
|
loc: true
|
45
22
|
});
|
46
23
|
const exportDefaultDeclaration = ast.body.find((p)=>p.type === 'ExportDefaultDeclaration');
|
@@ -57,8 +34,8 @@ function parseAndModifyConfigContent(content) {
|
|
57
34
|
} else if (exportNamedDeclaration) {
|
58
35
|
const exportSpecifier = exportNamedDeclaration.specifiers.find((s)=>s.type === 'ExportSpecifier' && s.exported?.name === 'default' && s.local?.type === 'Identifier' && s.local?.name);
|
59
36
|
if (exportSpecifier) {
|
60
|
-
|
61
|
-
|
37
|
+
warning('Could not automatically wrap next.config.js with withPayload.');
|
38
|
+
warning('Automatic wrapping of named exports as default not supported yet.');
|
62
39
|
warnUserWrapNotSuccessful();
|
63
40
|
return {
|
64
41
|
modifiedConfigContent: content,
|
@@ -66,7 +43,7 @@ function parseAndModifyConfigContent(content) {
|
|
66
43
|
};
|
67
44
|
}
|
68
45
|
}
|
69
|
-
|
46
|
+
warning('Could not automatically wrap next.config.js with withPayload.');
|
70
47
|
warnUserWrapNotSuccessful();
|
71
48
|
return {
|
72
49
|
modifiedConfigContent: content,
|
@@ -77,7 +54,7 @@ function warnUserWrapNotSuccessful() {
|
|
77
54
|
// Output directions for user to update next.config.js
|
78
55
|
const withPayloadMessage = `
|
79
56
|
|
80
|
-
${
|
57
|
+
${chalk.bold(`Please manually wrap your existing next.config.js with the withPayload function. Here is an example:`)}
|
81
58
|
|
82
59
|
import withPayload from '@payloadcms/next/withPayload'
|
83
60
|
|
@@ -88,7 +65,7 @@ function warnUserWrapNotSuccessful() {
|
|
88
65
|
export default withPayload(nextConfig)
|
89
66
|
|
90
67
|
`;
|
91
|
-
|
68
|
+
log(withPayloadMessage);
|
92
69
|
}
|
93
70
|
function insertBeforeAndAfter(content, loc) {
|
94
71
|
const { end, start } = loc;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/lib/wrap-next-config.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { parseModule } from 'esprima'\nimport fs from 'fs'\n\nimport { warning } from '../utils/log.js'\nimport { log } from '../utils/log.js'\n\nexport const withPayloadImportStatement = `import { withPayload } from '@payloadcms/next'\\n`\n\nexport const wrapNextConfig = (args: { nextConfigPath: string }) => {\n const { nextConfigPath } = args\n const configContent = fs.readFileSync(nextConfigPath, 'utf8')\n const { modifiedConfigContent: newConfig, success } = parseAndModifyConfigContent(configContent)\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(content: string): {\n modifiedConfigContent: string\n success: boolean\n} {\n content = withPayloadImportStatement + content\n const ast = parseModule(content, { loc: true })\n const exportDefaultDeclaration = ast.body.find((p) => p.type === 'ExportDefaultDeclaration') as\n | Directive\n | undefined\n\n const exportNamedDeclaration = ast.body.find((p) => p.type === 'ExportNamedDeclaration') as\n | ExportNamedDeclaration\n | 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()\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n }\n\n warning('Could not automatically wrap next.config.js with withPayload.')\n warnUserWrapNotSuccessful()\n return {\n modifiedConfigContent: content,\n success: false,\n }\n}\n\nfunction warnUserWrapNotSuccessful() {\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 import withPayload from '@payloadcms/next/withPayload'\n\n const nextConfig = {\n // Your Next.js config here\n }\n\n 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) {\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":["
|
1
|
+
{"version":3,"sources":["../../src/lib/wrap-next-config.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { parseModule } from 'esprima'\nimport fs from 'fs'\n\nimport { warning } from '../utils/log.js'\nimport { log } from '../utils/log.js'\n\nexport const withPayloadImportStatement = `import { withPayload } from '@payloadcms/next'\\n`\n\nexport const wrapNextConfig = (args: { nextConfigPath: string }) => {\n const { nextConfigPath } = args\n const configContent = fs.readFileSync(nextConfigPath, 'utf8')\n const { modifiedConfigContent: newConfig, success } = parseAndModifyConfigContent(configContent)\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(content: string): {\n modifiedConfigContent: string\n success: boolean\n} {\n content = withPayloadImportStatement + content\n const ast = parseModule(content, { loc: true })\n const exportDefaultDeclaration = ast.body.find((p) => p.type === 'ExportDefaultDeclaration') as\n | Directive\n | undefined\n\n const exportNamedDeclaration = ast.body.find((p) => p.type === 'ExportNamedDeclaration') as\n | ExportNamedDeclaration\n | 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()\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n }\n\n warning('Could not automatically wrap next.config.js with withPayload.')\n warnUserWrapNotSuccessful()\n return {\n modifiedConfigContent: content,\n success: false,\n }\n}\n\nfunction warnUserWrapNotSuccessful() {\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 import withPayload from '@payloadcms/next/withPayload'\n\n const nextConfig = {\n // Your Next.js config here\n }\n\n 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) {\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","parseModule","fs","warning","log","withPayloadImportStatement","wrapNextConfig","args","nextConfigPath","configContent","readFileSync","modifiedConfigContent","newConfig","success","parseAndModifyConfigContent","writeFileSync","content","ast","loc","exportDefaultDeclaration","body","find","p","type","exportNamedDeclaration","Error","declaration","insertBeforeAndAfter","exportSpecifier","specifiers","s","exported","name","local","warnUserWrapNotSuccessful","withPayloadMessage","bold","end","start","lines","split","insert","line","column","text","slice","join"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"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"}
|
@@ -1,50 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
value: true
|
4
|
-
});
|
5
|
-
const _wrapnextconfig = require("./wrap-next-config.js");
|
6
|
-
const _prompts = /*#__PURE__*/ _interop_require_wildcard(require("@clack/prompts"));
|
7
|
-
function _getRequireWildcardCache(nodeInterop) {
|
8
|
-
if (typeof WeakMap !== "function") return null;
|
9
|
-
var cacheBabelInterop = new WeakMap();
|
10
|
-
var cacheNodeInterop = new WeakMap();
|
11
|
-
return (_getRequireWildcardCache = function(nodeInterop) {
|
12
|
-
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
13
|
-
})(nodeInterop);
|
14
|
-
}
|
15
|
-
function _interop_require_wildcard(obj, nodeInterop) {
|
16
|
-
if (!nodeInterop && obj && obj.__esModule) {
|
17
|
-
return obj;
|
18
|
-
}
|
19
|
-
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
20
|
-
return {
|
21
|
-
default: obj
|
22
|
-
};
|
23
|
-
}
|
24
|
-
var cache = _getRequireWildcardCache(nodeInterop);
|
25
|
-
if (cache && cache.has(obj)) {
|
26
|
-
return cache.get(obj);
|
27
|
-
}
|
28
|
-
var newObj = {
|
29
|
-
__proto__: null
|
30
|
-
};
|
31
|
-
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
32
|
-
for(var key in obj){
|
33
|
-
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
34
|
-
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
35
|
-
if (desc && (desc.get || desc.set)) {
|
36
|
-
Object.defineProperty(newObj, key, desc);
|
37
|
-
} else {
|
38
|
-
newObj[key] = obj[key];
|
39
|
-
}
|
40
|
-
}
|
41
|
-
}
|
42
|
-
newObj.default = obj;
|
43
|
-
if (cache) {
|
44
|
-
cache.set(obj, newObj);
|
45
|
-
}
|
46
|
-
return newObj;
|
47
|
-
}
|
1
|
+
import { parseAndModifyConfigContent, withPayloadImportStatement } from './wrap-next-config.js';
|
2
|
+
import * as p from '@clack/prompts';
|
48
3
|
const defaultNextConfig = `/** @type {import('next').NextConfig} */
|
49
4
|
const nextConfig = {};
|
50
5
|
|
@@ -72,24 +27,24 @@ export { wrapped as default }
|
|
72
27
|
`;
|
73
28
|
describe('parseAndInsertWithPayload', ()=>{
|
74
29
|
it('should parse the default next config', ()=>{
|
75
|
-
const { modifiedConfigContent } =
|
76
|
-
expect(modifiedConfigContent).toContain(
|
30
|
+
const { modifiedConfigContent } = parseAndModifyConfigContent(defaultNextConfig);
|
31
|
+
expect(modifiedConfigContent).toContain(withPayloadImportStatement);
|
77
32
|
expect(modifiedConfigContent).toContain('withPayload(nextConfig)');
|
78
33
|
});
|
79
34
|
it('should parse the config with a function', ()=>{
|
80
|
-
const { modifiedConfigContent } =
|
35
|
+
const { modifiedConfigContent } = parseAndModifyConfigContent(nextConfigWithFunc);
|
81
36
|
expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))');
|
82
37
|
});
|
83
38
|
it('should parse the config with a function on a new line', ()=>{
|
84
|
-
const { modifiedConfigContent } =
|
85
|
-
expect(modifiedConfigContent).toContain(
|
39
|
+
const { modifiedConfigContent } = parseAndModifyConfigContent(nextConfigWithFuncMultiline);
|
40
|
+
expect(modifiedConfigContent).toContain(withPayloadImportStatement);
|
86
41
|
expect(modifiedConfigContent).toMatch(/withPayload\(someFunc\(\n nextConfig\n\)\)/);
|
87
42
|
});
|
88
43
|
// Unsupported: export { wrapped as default }
|
89
44
|
it('should give warning with a named export as default', ()=>{
|
90
|
-
const warnLogSpy = jest.spyOn(
|
91
|
-
const { modifiedConfigContent, success } =
|
92
|
-
expect(modifiedConfigContent).toContain(
|
45
|
+
const warnLogSpy = jest.spyOn(p.log, 'warn').mockImplementation(()=>{});
|
46
|
+
const { modifiedConfigContent, success } = parseAndModifyConfigContent(nextConfigExportNamedDefault);
|
47
|
+
expect(modifiedConfigContent).toContain(withPayloadImportStatement);
|
93
48
|
expect(success).toBe(false);
|
94
49
|
expect(warnLogSpy).toHaveBeenCalledWith(expect.stringContaining('Could not automatically wrap'));
|
95
50
|
});
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/lib/wrap-next-config.spec.ts"],"sourcesContent":["import { parseAndModifyConfigContent, withPayloadImportStatement } from './wrap-next-config.js'\nimport * as p from '@clack/prompts'\n\nconst defaultNextConfig = `/** @type {import('next').NextConfig} */\nconst nextConfig = {};\n\nexport default nextConfig;\n`\n\nconst nextConfigWithFunc = `const nextConfig = {\n // Your Next.js config here\n}\n\nexport default someFunc(nextConfig)\n`\nconst nextConfigWithFuncMultiline = `const nextConfig = {\n // Your Next.js config here\n}\n\nexport default someFunc(\n nextConfig\n)\n`\n\nconst nextConfigExportNamedDefault = `const nextConfig = {\n // Your Next.js config here\n}\nconst wrapped = someFunc(asdf)\nexport { wrapped as default }\n`\n\ndescribe('parseAndInsertWithPayload', () => {\n it('should parse the default next config', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(defaultNextConfig)\n expect(modifiedConfigContent).toContain(withPayloadImportStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n it('should parse the config with a function', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(nextConfigWithFunc)\n expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))')\n })\n\n it('should parse the config with a function on a new line', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(nextConfigWithFuncMultiline)\n expect(modifiedConfigContent).toContain(withPayloadImportStatement)\n expect(modifiedConfigContent).toMatch(/withPayload\\(someFunc\\(\\n nextConfig\\n\\)\\)/)\n })\n\n // Unsupported: export { wrapped as default }\n it('should give warning with a named export as default', () => {\n const warnLogSpy = jest.spyOn(p.log, 'warn').mockImplementation(() => {})\n\n const { modifiedConfigContent, success } = parseAndModifyConfigContent(\n nextConfigExportNamedDefault,\n )\n expect(modifiedConfigContent).toContain(withPayloadImportStatement)\n expect(success).toBe(false)\n\n expect(warnLogSpy).toHaveBeenCalledWith(expect.stringContaining('Could not automatically wrap'))\n })\n})\n"],"names":["defaultNextConfig","nextConfigWithFunc","nextConfigWithFuncMultiline","nextConfigExportNamedDefault","describe","it","modifiedConfigContent","
|
1
|
+
{"version":3,"sources":["../../src/lib/wrap-next-config.spec.ts"],"sourcesContent":["import { parseAndModifyConfigContent, withPayloadImportStatement } from './wrap-next-config.js'\nimport * as p from '@clack/prompts'\n\nconst defaultNextConfig = `/** @type {import('next').NextConfig} */\nconst nextConfig = {};\n\nexport default nextConfig;\n`\n\nconst nextConfigWithFunc = `const nextConfig = {\n // Your Next.js config here\n}\n\nexport default someFunc(nextConfig)\n`\nconst nextConfigWithFuncMultiline = `const nextConfig = {\n // Your Next.js config here\n}\n\nexport default someFunc(\n nextConfig\n)\n`\n\nconst nextConfigExportNamedDefault = `const nextConfig = {\n // Your Next.js config here\n}\nconst wrapped = someFunc(asdf)\nexport { wrapped as default }\n`\n\ndescribe('parseAndInsertWithPayload', () => {\n it('should parse the default next config', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(defaultNextConfig)\n expect(modifiedConfigContent).toContain(withPayloadImportStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n it('should parse the config with a function', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(nextConfigWithFunc)\n expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))')\n })\n\n it('should parse the config with a function on a new line', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(nextConfigWithFuncMultiline)\n expect(modifiedConfigContent).toContain(withPayloadImportStatement)\n expect(modifiedConfigContent).toMatch(/withPayload\\(someFunc\\(\\n nextConfig\\n\\)\\)/)\n })\n\n // Unsupported: export { wrapped as default }\n it('should give warning with a named export as default', () => {\n const warnLogSpy = jest.spyOn(p.log, 'warn').mockImplementation(() => {})\n\n const { modifiedConfigContent, success } = parseAndModifyConfigContent(\n nextConfigExportNamedDefault,\n )\n expect(modifiedConfigContent).toContain(withPayloadImportStatement)\n expect(success).toBe(false)\n\n expect(warnLogSpy).toHaveBeenCalledWith(expect.stringContaining('Could not automatically wrap'))\n })\n})\n"],"names":["parseAndModifyConfigContent","withPayloadImportStatement","p","defaultNextConfig","nextConfigWithFunc","nextConfigWithFuncMultiline","nextConfigExportNamedDefault","describe","it","modifiedConfigContent","expect","toContain","toMatch","warnLogSpy","jest","spyOn","log","mockImplementation","success","toBe","toHaveBeenCalledWith","stringContaining"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,2BAA2B,EAAEC,0BAA0B,QAAQ,wBAAuB;AAC/F,YAAYC,OAAO,iBAAgB;AAEnC,MAAMC,oBAAoB,CAAC;;;;AAI3B,CAAC;AAED,MAAMC,qBAAqB,CAAC;;;;;AAK5B,CAAC;AACD,MAAMC,8BAA8B,CAAC;;;;;;;AAOrC,CAAC;AAED,MAAMC,+BAA+B,CAAC;;;;;AAKtC,CAAC;AAEDC,SAAS,6BAA6B;IACpCC,GAAG,wCAAwC;QACzC,MAAM,EAAEC,qBAAqB,EAAE,GAAGT,4BAA4BG;QAC9DO,OAAOD,uBAAuBE,SAAS,CAACV;QACxCS,OAAOD,uBAAuBE,SAAS,CAAC;IAC1C;IACAH,GAAG,2CAA2C;QAC5C,MAAM,EAAEC,qBAAqB,EAAE,GAAGT,4BAA4BI;QAC9DM,OAAOD,uBAAuBE,SAAS,CAAC;IAC1C;IAEAH,GAAG,yDAAyD;QAC1D,MAAM,EAAEC,qBAAqB,EAAE,GAAGT,4BAA4BK;QAC9DK,OAAOD,uBAAuBE,SAAS,CAACV;QACxCS,OAAOD,uBAAuBG,OAAO,CAAC;IACxC;IAEA,6CAA6C;IAC7CJ,GAAG,sDAAsD;QACvD,MAAMK,aAAaC,KAAKC,KAAK,CAACb,EAAEc,GAAG,EAAE,QAAQC,kBAAkB,CAAC,KAAO;QAEvE,MAAM,EAAER,qBAAqB,EAAES,OAAO,EAAE,GAAGlB,4BACzCM;QAEFI,OAAOD,uBAAuBE,SAAS,CAACV;QACxCS,OAAOQ,SAASC,IAAI,CAAC;QAErBT,OAAOG,YAAYO,oBAAoB,CAACV,OAAOW,gBAAgB,CAAC;IAClE;AACF"}
|
@@ -1,31 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
Object.defineProperty(exports, "writeEnvFile", {
|
6
|
-
enumerable: true,
|
7
|
-
get: function() {
|
8
|
-
return writeEnvFile;
|
9
|
-
}
|
10
|
-
});
|
11
|
-
const _fsextra = /*#__PURE__*/ _interop_require_default(require("fs-extra"));
|
12
|
-
const _path = /*#__PURE__*/ _interop_require_default(require("path"));
|
13
|
-
const _log = require("../utils/log.js");
|
14
|
-
function _interop_require_default(obj) {
|
15
|
-
return obj && obj.__esModule ? obj : {
|
16
|
-
default: obj
|
17
|
-
};
|
18
|
-
}
|
19
|
-
async function writeEnvFile(args) {
|
1
|
+
import fs from 'fs-extra';
|
2
|
+
import path from 'path';
|
3
|
+
import { debug, error } from '../utils/log.js';
|
4
|
+
/** Parse and swap .env.example values and write .env */ export async function writeEnvFile(args) {
|
20
5
|
const { cliArgs, databaseUri, payloadSecret, projectDir, template } = args;
|
21
6
|
if (cliArgs['--dry-run']) {
|
22
|
-
|
7
|
+
debug(`DRY RUN: .env file created`);
|
23
8
|
return;
|
24
9
|
}
|
25
10
|
try {
|
26
|
-
if (template?.type === 'starter' &&
|
11
|
+
if (template?.type === 'starter' && fs.existsSync(path.join(projectDir, '.env.example'))) {
|
27
12
|
// Parse .env file into key/value pairs
|
28
|
-
const envFile = await
|
13
|
+
const envFile = await fs.readFile(path.join(projectDir, '.env.example'), 'utf8');
|
29
14
|
const envWithValues = envFile.split('\n').filter((e)=>e).map((line)=>{
|
30
15
|
if (line.startsWith('#') || !line.includes('=')) return line;
|
31
16
|
const split = line.split('=');
|
@@ -40,15 +25,15 @@ async function writeEnvFile(args) {
|
|
40
25
|
return `${key}=${value}`;
|
41
26
|
});
|
42
27
|
// Write new .env file
|
43
|
-
await
|
28
|
+
await fs.writeFile(path.join(projectDir, '.env'), envWithValues.join('\n'));
|
44
29
|
} else {
|
45
30
|
const content = `DATABASE_URI=${databaseUri}\nPAYLOAD_SECRET=${payloadSecret}`;
|
46
|
-
await
|
31
|
+
await fs.outputFile(`${projectDir}/.env`, content);
|
47
32
|
}
|
48
33
|
} catch (err) {
|
49
|
-
|
34
|
+
error('Unable to write .env file');
|
50
35
|
if (err instanceof Error) {
|
51
|
-
|
36
|
+
error(err.message);
|
52
37
|
}
|
53
38
|
process.exit(1);
|
54
39
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/lib/write-env-file.ts"],"sourcesContent":["import fs from 'fs-extra'\nimport path from 'path'\n\nimport type { CliArgs, ProjectTemplate } from '../types.js'\n\nimport { debug, error } from '../utils/log.js'\n\n/** Parse and swap .env.example values and write .env */\nexport async function writeEnvFile(args: {\n cliArgs: CliArgs\n databaseUri: string\n payloadSecret: string\n projectDir: string\n template?: ProjectTemplate\n}): Promise<void> {\n const { cliArgs, databaseUri, payloadSecret, projectDir, template } = args\n\n if (cliArgs['--dry-run']) {\n debug(`DRY RUN: .env file created`)\n return\n }\n\n try {\n if (template?.type === 'starter' && fs.existsSync(path.join(projectDir, '.env.example'))) {\n // Parse .env file into key/value pairs\n const envFile = await fs.readFile(path.join(projectDir, '.env.example'), 'utf8')\n const envWithValues: string[] = envFile\n .split('\\n')\n .filter((e) => e)\n .map((line) => {\n if (line.startsWith('#') || !line.includes('=')) return line\n\n const split = line.split('=')\n const key = split[0]\n let value = split[1]\n\n if (key === 'MONGODB_URI' || key === 'MONGO_URL' || key === 'DATABASE_URI') {\n value = databaseUri\n }\n if (key === 'PAYLOAD_SECRET' || key === 'PAYLOAD_SECRET_KEY') {\n value = payloadSecret\n }\n\n return `${key}=${value}`\n })\n\n // Write new .env file\n await fs.writeFile(path.join(projectDir, '.env'), envWithValues.join('\\n'))\n } else {\n const content = `DATABASE_URI=${databaseUri}\\nPAYLOAD_SECRET=${payloadSecret}`\n await fs.outputFile(`${projectDir}/.env`, content)\n }\n } catch (err: unknown) {\n error('Unable to write .env file')\n if (err instanceof Error) {\n error(err.message)\n }\n process.exit(1)\n }\n}\n"],"names":["writeEnvFile","args","cliArgs","databaseUri","payloadSecret","projectDir","template","
|
1
|
+
{"version":3,"sources":["../../src/lib/write-env-file.ts"],"sourcesContent":["import fs from 'fs-extra'\nimport path from 'path'\n\nimport type { CliArgs, ProjectTemplate } from '../types.js'\n\nimport { debug, error } from '../utils/log.js'\n\n/** Parse and swap .env.example values and write .env */\nexport async function writeEnvFile(args: {\n cliArgs: CliArgs\n databaseUri: string\n payloadSecret: string\n projectDir: string\n template?: ProjectTemplate\n}): Promise<void> {\n const { cliArgs, databaseUri, payloadSecret, projectDir, template } = args\n\n if (cliArgs['--dry-run']) {\n debug(`DRY RUN: .env file created`)\n return\n }\n\n try {\n if (template?.type === 'starter' && fs.existsSync(path.join(projectDir, '.env.example'))) {\n // Parse .env file into key/value pairs\n const envFile = await fs.readFile(path.join(projectDir, '.env.example'), 'utf8')\n const envWithValues: string[] = envFile\n .split('\\n')\n .filter((e) => e)\n .map((line) => {\n if (line.startsWith('#') || !line.includes('=')) return line\n\n const split = line.split('=')\n const key = split[0]\n let value = split[1]\n\n if (key === 'MONGODB_URI' || key === 'MONGO_URL' || key === 'DATABASE_URI') {\n value = databaseUri\n }\n if (key === 'PAYLOAD_SECRET' || key === 'PAYLOAD_SECRET_KEY') {\n value = payloadSecret\n }\n\n return `${key}=${value}`\n })\n\n // Write new .env file\n await fs.writeFile(path.join(projectDir, '.env'), envWithValues.join('\\n'))\n } else {\n const content = `DATABASE_URI=${databaseUri}\\nPAYLOAD_SECRET=${payloadSecret}`\n await fs.outputFile(`${projectDir}/.env`, content)\n }\n } catch (err: unknown) {\n error('Unable to write .env file')\n if (err instanceof Error) {\n error(err.message)\n }\n process.exit(1)\n }\n}\n"],"names":["fs","path","debug","error","writeEnvFile","args","cliArgs","databaseUri","payloadSecret","projectDir","template","type","existsSync","join","envFile","readFile","envWithValues","split","filter","e","map","line","startsWith","includes","key","value","writeFile","content","outputFile","err","Error","message","process","exit"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,OAAOA,QAAQ,WAAU;AACzB,OAAOC,UAAU,OAAM;AAIvB,SAASC,KAAK,EAAEC,KAAK,QAAQ,kBAAiB;AAE9C,sDAAsD,GACtD,OAAO,eAAeC,aAAaC,IAMlC;IACC,MAAM,EAAEC,OAAO,EAAEC,WAAW,EAAEC,aAAa,EAAEC,UAAU,EAAEC,QAAQ,EAAE,GAAGL;IAEtE,IAAIC,OAAO,CAAC,YAAY,EAAE;QACxBJ,MAAM,CAAC,0BAA0B,CAAC;QAClC;IACF;IAEA,IAAI;QACF,IAAIQ,UAAUC,SAAS,aAAaX,GAAGY,UAAU,CAACX,KAAKY,IAAI,CAACJ,YAAY,kBAAkB;YACxF,uCAAuC;YACvC,MAAMK,UAAU,MAAMd,GAAGe,QAAQ,CAACd,KAAKY,IAAI,CAACJ,YAAY,iBAAiB;YACzE,MAAMO,gBAA0BF,QAC7BG,KAAK,CAAC,MACNC,MAAM,CAAC,CAACC,IAAMA,GACdC,GAAG,CAAC,CAACC;gBACJ,IAAIA,KAAKC,UAAU,CAAC,QAAQ,CAACD,KAAKE,QAAQ,CAAC,MAAM,OAAOF;gBAExD,MAAMJ,QAAQI,KAAKJ,KAAK,CAAC;gBACzB,MAAMO,MAAMP,KAAK,CAAC,EAAE;gBACpB,IAAIQ,QAAQR,KAAK,CAAC,EAAE;gBAEpB,IAAIO,QAAQ,iBAAiBA,QAAQ,eAAeA,QAAQ,gBAAgB;oBAC1EC,QAAQlB;gBACV;gBACA,IAAIiB,QAAQ,oBAAoBA,QAAQ,sBAAsB;oBAC5DC,QAAQjB;gBACV;gBAEA,OAAO,CAAC,EAAEgB,IAAI,CAAC,EAAEC,MAAM,CAAC;YAC1B;YAEF,sBAAsB;YACtB,MAAMzB,GAAG0B,SAAS,CAACzB,KAAKY,IAAI,CAACJ,YAAY,SAASO,cAAcH,IAAI,CAAC;QACvE,OAAO;YACL,MAAMc,UAAU,CAAC,aAAa,EAAEpB,YAAY,iBAAiB,EAAEC,cAAc,CAAC;YAC9E,MAAMR,GAAG4B,UAAU,CAAC,CAAC,EAAEnB,WAAW,KAAK,CAAC,EAAEkB;QAC5C;IACF,EAAE,OAAOE,KAAc;QACrB1B,MAAM;QACN,IAAI0B,eAAeC,OAAO;YACxB3B,MAAM0B,IAAIE,OAAO;QACnB;QACAC,QAAQC,IAAI,CAAC;IACf;AACF"}
|