Skip to content

Commit 1f0dacd

Browse files
committed
tweak(deployer): don't create folder until deployer starts
1 parent 6d9e444 commit 1f0dacd

3 files changed

Lines changed: 43 additions & 46 deletions

File tree

core/deployer/utils.ts

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,40 @@
1-
import path from 'node:path';
2-
import fse from 'fs-extra';
1+
import { canWriteToPath, getPathFiles } from '@lib/fs';
32

4-
5-
/**
6-
* Check if its possible to create a file in a folder
7-
*/
8-
const canCreateFile = async (targetPath: string) => {
9-
try {
10-
const filePath = path.join(targetPath, '.empty');
11-
await fse.outputFile(filePath, '#save_attempt_please_ignore');
12-
await fse.remove(filePath);
13-
return true;
14-
} catch (error) {
15-
return false;
16-
}
17-
};
3+
//File created up to v7.3.2
4+
const EMPTY_FILE_NAME = '.empty';
185

196

207
/**
21-
* Perform deployer local target path permission/emptiness checking
22-
* FIXME: timeout to remove folders, or just autoremove them idk
8+
* Perform deployer local target path permission/emptiness checking.
239
*/
2410
export const validateTargetPath = async (deployPath: string) => {
25-
if (await fse.pathExists(deployPath)) {
26-
const pathFiles = await fse.readdir(deployPath);
27-
if (pathFiles.some((x) => x !== '.empty')) {
28-
throw new Error('This folder is not empty!');
29-
} else {
30-
if (await canCreateFile(deployPath)) {
31-
return 'Exists, empty, and writtable!';
32-
} else {
33-
throw new Error('Path exists, but its not a folder, or its not writtable.');
34-
}
35-
}
36-
} else {
37-
if (await canCreateFile(deployPath)) {
38-
await fse.remove(deployPath);
39-
return 'Path didn\'t existed, we created one (then deleted it).';
40-
} else {
41-
throw new Error('Path doesn\'t exist, and we could not create it. Please check parent folder permissions.');
11+
const canCreateFolder = await canWriteToPath(deployPath);
12+
if(!canCreateFolder) {
13+
throw new Error('Path is not writable due to missing permissions or invalid path.');
14+
}
15+
try {
16+
const pathFiles = await getPathFiles(deployPath);
17+
if (pathFiles.some((x) => x.name !== EMPTY_FILE_NAME)) {
18+
throw new Error('This folder already exists and is not empty!');
4219
}
20+
} catch (error) {
21+
if ((error as any).code !== 'ENOENT') throw error;
4322
}
23+
return true as const;
4424
};
4525

4626

4727
/**
4828
* Create a template recipe file
4929
*/
50-
export const makeTemplateRecipe = (serverName: string, author: string) => `name: ${serverName}
51-
author: ${author}
52-
53-
# This is just a placeholder, please don't run it!
54-
tasks:
55-
- action: waste_time
56-
seconds: 5
57-
- action: waste_time
58-
seconds: 5
59-
`;
30+
export const makeTemplateRecipe = (serverName: string, author: string) => [
31+
`name: ${serverName}`,
32+
`author: ${author}`,
33+
'',
34+
'# This is just a placeholder, please don\'t use it!',
35+
'tasks: ',
36+
' - action: waste_time',
37+
' seconds: 5',
38+
' - action: waste_time',
39+
' seconds: 5',
40+
].join('\n');

core/lib/fs.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
import fs from 'node:fs';
12
import fsp from 'node:fs/promises';
23
import type { Dirent } from 'node:fs';
34
import { txEnv } from '@core/globalData';
5+
import path from 'node:path';
6+
7+
8+
/**
9+
* Check if its possible to create a file in a folder
10+
*/
11+
export const canWriteToPath = async (targetPath: string) => {
12+
try {
13+
await fsp.access(path.dirname(targetPath), fs.constants.W_OK);
14+
return true;
15+
} catch (error) {
16+
return false;
17+
}
18+
}
419

520

621
/**

core/routes/setup/post.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ async function handleValidateLocalDeployPath(ctx) {
138138

139139
//Perform path checking
140140
try {
141-
return ctx.send({success: true, message: await validateTargetPath(deployPath)});
141+
await validateTargetPath(deployPath); // will throw if invalid
142+
return ctx.send({success: true, message: 'Path is valid.'});
142143
} catch (error) {
143144
return ctx.send({success: false, message: error.message});
144145
}

0 commit comments

Comments
 (0)