|
1 | | -import path from 'node:path'; |
2 | | -import fse from 'fs-extra'; |
| 1 | +import { canWriteToPath, getPathFiles } from '@lib/fs'; |
3 | 2 |
|
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'; |
18 | 5 |
|
19 | 6 |
|
20 | 7 | /** |
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. |
23 | 9 | */ |
24 | 10 | 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!'); |
42 | 19 | } |
| 20 | + } catch (error) { |
| 21 | + if ((error as any).code !== 'ENOENT') throw error; |
43 | 22 | } |
| 23 | + return true as const; |
44 | 24 | }; |
45 | 25 |
|
46 | 26 |
|
47 | 27 | /** |
48 | 28 | * Create a template recipe file |
49 | 29 | */ |
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'); |
0 commit comments