You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.8 KiB
JavaScript
66 lines
1.8 KiB
JavaScript
const os = require('os');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { base } = require('../config/index');
|
|
|
|
// 获取最终工作目录
|
|
const getInputDir = () => {
|
|
if (base.inputDir) {
|
|
return base.inputDir;
|
|
}
|
|
|
|
base.inputDir = getLatestFolder(base.directoryPath);
|
|
return base.inputDir;
|
|
};
|
|
|
|
// 获取该文件夹下最新创建的文件夹作为工作目录
|
|
const getLatestFolder = (directory) => {
|
|
let currentInputDir = null;
|
|
let latestTime = 0;
|
|
|
|
// 读取指定目录
|
|
fs.readdirSync(directory, { withFileTypes: true }).forEach((file) => {
|
|
if (file.isDirectory()) {
|
|
const folderPath = path.join(directory, file.name);
|
|
const stats = fs.statSync(folderPath);
|
|
const creationTime = stats.birthtimeMs; // 获取创建时间
|
|
// 找到最新创建的文件夹
|
|
if (creationTime > latestTime) {
|
|
latestTime = creationTime;
|
|
currentInputDir = folderPath;
|
|
}
|
|
}
|
|
});
|
|
|
|
if (currentInputDir) {
|
|
console.log('工作目录:', currentInputDir);
|
|
} else {
|
|
console.log('未找到工作目录.');
|
|
}
|
|
|
|
return currentInputDir;
|
|
};
|
|
|
|
// 获取操作系统下面的桌面路径
|
|
const getDesktopPath = () => {
|
|
const platform = os.platform();
|
|
|
|
let desktopPath;
|
|
if (platform === 'win32') {
|
|
// Windows
|
|
desktopPath = path.join(process.env.USERPROFILE, 'Desktop');
|
|
} else if (platform === 'darwin') {
|
|
// macOS
|
|
desktopPath = path.join(process.env.HOME, 'Desktop');
|
|
} else if (platform === 'linux') {
|
|
// Linux
|
|
desktopPath = path.join(process.env.HOME, 'Desktop');
|
|
} else {
|
|
throw new Error('Unsupported platform: ' + platform);
|
|
}
|
|
|
|
return desktopPath;
|
|
};
|
|
|
|
module.exports = { getInputDir, getLatestFolder, getDesktopPath };
|