🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

create-payload-app

Package Overview
Dependencies
Maintainers
3
Versions
714
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

create-payload-app - npm Package Compare versions

Comparing version

to
3.41.0-canary.1

dist/lib/manage-env-files.spec.js

19

dist/lib/create-project.js

@@ -106,13 +106,10 @@ import * as p from '@clack/prompts';

}
// Call manageEnvFiles before initializing Git
if (dbDetails) {
await manageEnvFiles({
cliArgs,
databaseType: dbDetails.type,
databaseUri: dbDetails.dbUri,
payloadSecret: generateSecret(),
projectDir,
template: 'template' in args ? args.template : undefined
});
}
await manageEnvFiles({
cliArgs,
databaseType: dbDetails?.type,
databaseUri: dbDetails?.dbUri,
payloadSecret: generateSecret(),
projectDir,
template: 'template' in args ? args.template : undefined
});
// Remove yarn.lock file. This is only desired in Payload Cloud.

@@ -119,0 +116,0 @@ const lockPath = path.resolve(projectDir, 'pnpm-lock.yaml');

@@ -10,3 +10,2 @@ import { jest } from '@jest/globals';

import { getValidTemplates } from './templates.js';
import { manageEnvFiles } from './manage-env-files.js';
describe('createProject', ()=>{

@@ -157,57 +156,2 @@ let projectDir;

});
describe('managing env files', ()=>{
it('updates .env files without overwriting existing data', async ()=>{
const envFilePath = path.join(projectDir, '.env');
const envExampleFilePath = path.join(projectDir, '.env.example');
fse.ensureDirSync(projectDir);
fse.ensureFileSync(envFilePath);
fse.ensureFileSync(envExampleFilePath);
const initialEnvContent = `CUSTOM_VAR=custom-value\nDATABASE_URI=old-connection\n`;
const initialEnvExampleContent = `CUSTOM_VAR=custom-value\nDATABASE_URI=old-connection\nPAYLOAD_SECRET=YOUR_SECRET_HERE\n`;
fse.writeFileSync(envFilePath, initialEnvContent);
fse.writeFileSync(envExampleFilePath, initialEnvExampleContent);
await manageEnvFiles({
cliArgs: {
'--debug': true
},
databaseType: 'mongodb',
databaseUri: 'mongodb://localhost:27017/test',
payloadSecret: 'test-secret',
projectDir,
template: undefined
});
const updatedEnvContent = fse.readFileSync(envFilePath, 'utf-8');
expect(updatedEnvContent).toContain('CUSTOM_VAR=custom-value');
expect(updatedEnvContent).toContain('DATABASE_URI=mongodb://localhost:27017/test');
expect(updatedEnvContent).toContain('PAYLOAD_SECRET=test-secret');
const updatedEnvExampleContent = fse.readFileSync(envExampleFilePath, 'utf-8');
expect(updatedEnvExampleContent).toContain('CUSTOM_VAR=custom-value');
expect(updatedEnvContent).toContain('DATABASE_URI=mongodb://localhost:27017/test');
expect(updatedEnvContent).toContain('PAYLOAD_SECRET=test-secret');
});
it('creates .env and .env.example if they do not exist', async ()=>{
const envFilePath = path.join(projectDir, '.env');
const envExampleFilePath = path.join(projectDir, '.env.example');
fse.ensureDirSync(projectDir);
if (fse.existsSync(envFilePath)) fse.removeSync(envFilePath);
if (fse.existsSync(envExampleFilePath)) fse.removeSync(envExampleFilePath);
await manageEnvFiles({
cliArgs: {
'--debug': true
},
databaseUri: '',
payloadSecret: '',
projectDir,
template: undefined
});
expect(fse.existsSync(envFilePath)).toBe(true);
expect(fse.existsSync(envExampleFilePath)).toBe(true);
const updatedEnvContent = fse.readFileSync(envFilePath, 'utf-8');
expect(updatedEnvContent).toContain('DATABASE_URI=your-connection-string-here');
expect(updatedEnvContent).toContain('PAYLOAD_SECRET=YOUR_SECRET_HERE');
const updatedEnvExampleContent = fse.readFileSync(envExampleFilePath, 'utf-8');
expect(updatedEnvExampleContent).toContain('DATABASE_URI=your-connection-string-here');
expect(updatedEnvExampleContent).toContain('PAYLOAD_SECRET=YOUR_SECRET_HERE');
});
});
});

@@ -214,0 +158,0 @@ });

@@ -6,3 +6,3 @@ import type { CliArgs, DbType, ProjectTemplate } from '../types.js';

databaseType?: DbType;
databaseUri: string;
databaseUri?: string;
payloadSecret: string;

@@ -9,0 +9,0 @@ projectDir: string;

@@ -5,9 +5,17 @@ import fs from 'fs-extra';

import { dbChoiceRecord } from './select-db.js';
const updateEnvExampleVariables = (contents, databaseType, payloadSecret, databaseUri)=>{
const sanitizeEnv = ({ contents, databaseType, databaseUri, payloadSecret })=>{
const seenKeys = new Set();
const updatedEnv = contents.split('\n').map((line)=>{
// add defaults
let withDefaults = contents;
if (!contents.includes('DATABASE_URI') && !contents.includes('POSTGRES_URL') && !contents.includes('MONGODB_URI')) {
withDefaults += '\nDATABASE_URI=your-connection-string-here';
}
if (!contents.includes('PAYLOAD_SECRET')) {
withDefaults += '\nPAYLOAD_SECRET=YOUR_SECRET_HERE';
}
let updatedEnv = withDefaults.split('\n').map((line)=>{
if (line.startsWith('#') || !line.includes('=')) {
return line;
}
const [key] = line.split('=');
const [key, value] = line.split('=');
if (!key) {

@@ -21,2 +29,4 @@ return;

line = databaseType === 'vercel-postgres' ? `POSTGRES_URL=${placeholderUri}` : `DATABASE_URI=${placeholderUri}`;
} else {
line = `${key}=${value}`;
}

@@ -34,2 +44,5 @@ }

}).filter(Boolean).reverse().join('\n');
if (!updatedEnv.includes('# Added by Payload')) {
updatedEnv = `# Added by Payload\n${updatedEnv}`;
}
return updatedEnv;

@@ -44,7 +57,6 @@ };

}
const envExamplePath = path.join(projectDir, '.env.example');
const pathToEnvExample = path.join(projectDir, '.env.example');
const envPath = path.join(projectDir, '.env');
const emptyEnvContent = `# Added by Payload\nDATABASE_URI=your-connection-string-here\nPAYLOAD_SECRET=YOUR_SECRET_HERE\n`;
let exampleEnv = '';
try {
let updatedExampleContents;
if (template?.type === 'plugin') {

@@ -56,19 +68,23 @@ if (debugFlag) {

}
if (!fs.existsSync(envExamplePath)) {
updatedExampleContents = updateEnvExampleVariables(emptyEnvContent, databaseType, payloadSecret, databaseUri);
await fs.writeFile(envExamplePath, updatedExampleContents);
// If there's a .env.example file, use it to create or update the .env file
if (fs.existsSync(pathToEnvExample)) {
const envExampleContents = await fs.readFile(pathToEnvExample, 'utf8');
exampleEnv = sanitizeEnv({
contents: envExampleContents,
databaseType,
databaseUri,
payloadSecret
});
if (debugFlag) {
debug(`.env.example file successfully created`);
debug(`.env.example file successfully read`);
}
} else {
const envExampleContents = await fs.readFile(envExamplePath, 'utf8');
const mergedEnvs = envExampleContents + '\n' + emptyEnvContent;
updatedExampleContents = updateEnvExampleVariables(mergedEnvs, databaseType, payloadSecret, databaseUri);
await fs.writeFile(envExamplePath, updatedExampleContents);
if (debugFlag) {
debug(`.env.example file successfully updated`);
}
}
// If there's no .env file, create it using the .env.example content (if it exists)
if (!fs.existsSync(envPath)) {
const envContent = updateEnvExampleVariables(emptyEnvContent, databaseType, payloadSecret, databaseUri);
const envContent = sanitizeEnv({
contents: exampleEnv,
databaseType,
databaseUri,
payloadSecret
});
await fs.writeFile(envPath, envContent);

@@ -79,5 +95,10 @@ if (debugFlag) {

} else {
// If the .env file already exists, sanitize it as-is
const envContents = await fs.readFile(envPath, 'utf8');
const mergedEnvs = envContents + '\n' + emptyEnvContent;
const updatedEnvContents = updateEnvExampleVariables(mergedEnvs, databaseType, payloadSecret, databaseUri);
const updatedEnvContents = sanitizeEnv({
contents: envContents,
databaseType,
databaseUri,
payloadSecret
});
await fs.writeFile(envPath, updatedEnvContents);

@@ -84,0 +105,0 @@ if (debugFlag) {

{
"name": "create-payload-app",
"version": "3.41.0-canary.0",
"version": "3.41.0-canary.1",
"homepage": "https://payloadcms.com",

@@ -5,0 +5,0 @@ "repository": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet