Skip to content

Commit 0faece7

Browse files
committed
wrapped scripts in function to revent esm return errors
1 parent 3ec741e commit 0faece7

3 files changed

Lines changed: 42 additions & 49 deletions

File tree

scripts/database-setup.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
import fs from 'fs';
1+
import fs from 'fs/promises';
22

33
const filePath = './src/backend/.env';
44
const lineToAdd = 'DATABASE_URL="postgresql://postgres:docker@localhost:5432/nerpm?schema=public"';
55

6-
if (!fs.existsSync(filePath)) {
7-
fs.writeFileSync(filePath, lineToAdd, 'utf8');
8-
return;
9-
}
10-
11-
fs.readFile(filePath, 'utf8', (err, data) => {
12-
if (err) {
13-
console.error('Error reading file:', err);
6+
async function setupDatabase() {
7+
// Check if file exists
8+
try {
9+
await fs.access(filePath);
10+
} catch {
11+
// File doesn't exist, create it with the DATABASE_URL
12+
await fs.writeFile(filePath, lineToAdd, 'utf8');
13+
console.log('Created .env file with DATABASE_URL');
1414
return;
1515
}
1616

17-
// Split file contents into lines
17+
// File exists, read its contents
18+
const data = await fs.readFile(filePath, 'utf8');
1819
const lines = data.trim().split('\n');
1920

21+
// Check if DATABASE_URL already exists
2022
for (const line of lines) {
21-
//db url already exists, no need to add it
22-
if (line.startsWith('DATABASE_URL=')) return;
23-
}
24-
25-
fs.appendFile(filePath, `\n${lineToAdd}`, 'utf8', (err) => {
26-
if (err) {
27-
console.error('Error appending line to file:', err);
23+
if (line.startsWith('DATABASE_URL=')) {
24+
console.log('DATABASE_URL already exists in .env file');
2825
return;
2926
}
30-
});
31-
});
27+
}
28+
29+
// DATABASE_URL doesn't exist, append it
30+
await fs.appendFile(filePath, `\n${lineToAdd}`, 'utf8');
31+
console.log('Added DATABASE_URL to existing .env file');
32+
}
33+
34+
await setupDatabase();

scripts/test-setup.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
import fs from 'fs';
1+
import fs from 'fs/promises';
22

33
const filePath = './src/backend/.env';
44
const lineToAdd = 'DATABASE_URL="postgresql://postgres:docker@localhost:5433/nerpm?schema=public"';
55

6-
fs.readFile(filePath, 'utf8', (err, data) => {
7-
if (err) {
8-
console.error('Error reading file:', err);
9-
return;
10-
}
11-
12-
// Split file contents into lines
6+
async function setupTest() {
7+
const data = await fs.readFile(filePath, 'utf8');
138
const lines = data.trim().split('\n');
149

1510
// Check if the last line matches the lineToAdd
1611
if (lines[lines.length - 1] !== lineToAdd) {
1712
// Append the line if it's not already the last line
18-
fs.appendFile(filePath, `\n${lineToAdd}`, 'utf8', (err) => {
19-
if (err) {
20-
console.error('Error appending line to file:', err);
21-
return;
22-
}
23-
});
13+
await fs.appendFile(filePath, `\n${lineToAdd}`, 'utf8');
14+
console.log('Added test DATABASE_URL to .env file');
15+
} else {
16+
console.log('Test DATABASE_URL already present in .env file');
2417
}
25-
});
18+
}
19+
20+
await setupTest();

scripts/test-teardown.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import fs from 'fs';
1+
import fs from 'fs/promises';
22

33
const filePath = './src/backend/.env';
44
const lineToMatch = 'DATABASE_URL="postgresql://postgres:docker@localhost:5433/nerpm?schema=public"';
55

6-
fs.readFile(filePath, 'utf8', (err, data) => {
7-
if (err) {
8-
console.error('Error reading file:', err);
9-
return;
10-
}
11-
12-
// Split file contents into lines
6+
async function teardownTest() {
7+
const data = await fs.readFile(filePath, 'utf8');
138
const lines = data.trim().split('\n');
149

1510
// Check if the last line matches the lineToMatch
@@ -18,11 +13,11 @@ fs.readFile(filePath, 'utf8', (err, data) => {
1813
lines.pop();
1914

2015
// Write modified contents back to the file
21-
fs.writeFile(filePath, lines.join('\n'), 'utf8', (err) => {
22-
if (err) {
23-
console.error('Error writing file:', err);
24-
return;
25-
}
26-
});
16+
await fs.writeFile(filePath, lines.join('\n'), 'utf8');
17+
console.log('Removed test DATABASE_URL from .env file');
18+
} else {
19+
console.log('Test DATABASE_URL not found as last line in .env file');
2720
}
28-
});
21+
}
22+
23+
await teardownTest();

0 commit comments

Comments
 (0)