1
0
mirror of https://github.com/0O0o0oOoO00/Alas.git synced 2026-05-21 02:59:30 +08:00
Files
Alas/webapp/packages/common/utils/getAlasABSPath.ts

73 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-04-18 23:54:06 +08:00
import {app} from 'electron';
2023-04-19 19:02:01 +08:00
import {isMacintosh} from './env';
import fs from 'fs';
2023-04-18 23:54:06 +08:00
/**
* Get the absolute path of the project root directory
2023-04-19 19:02:01 +08:00
* @param files
2023-05-08 18:54:16 +08:00
* @param rootName
2023-04-18 23:54:06 +08:00
*/
2023-04-19 19:02:01 +08:00
const getAlasABSPath = (
files: string[] = ['**/config/deploy.yaml', '**/config/deploy.template.yaml'],
2023-05-08 18:54:16 +08:00
rootName: string | string[] = ['AzurLaneAutoScript', 'Alas'],
2023-04-19 19:02:01 +08:00
) => {
2023-04-18 23:54:06 +08:00
const path = require('path');
const sep = path.sep;
const fg = require('fast-glob');
2023-04-19 19:02:01 +08:00
let appAbsPath = process.cwd();
if (isMacintosh && import.meta.env.PROD) {
2023-04-18 23:54:06 +08:00
appAbsPath = app?.getAppPath() || process.execPath;
}
2023-04-27 21:42:54 +08:00
while (fs.lstatSync(appAbsPath).isFile()) {
appAbsPath = appAbsPath.split(sep).slice(0, -1).join(sep);
}
2023-04-18 23:54:06 +08:00
let alasABSPath = '';
2023-04-27 21:42:54 +08:00
2023-05-08 18:54:16 +08:00
let hasRootName = false;
if (typeof rootName === 'string') {
hasRootName = appAbsPath.includes(rootName);
} else if (Array.isArray(rootName)) {
hasRootName = rootName.some(item =>
appAbsPath.toLocaleLowerCase().includes(item.toLocaleLowerCase()),
);
}
if (hasRootName) {
2023-04-27 21:42:54 +08:00
const appAbsPathArr = appAbsPath.split(sep);
let flag = false;
2023-05-08 18:54:16 +08:00
while (hasRootName && !flag) {
2023-04-27 21:42:54 +08:00
const entries = fg.sync(files, {
dot: true,
cwd: appAbsPathArr.join(sep) as string,
});
if (entries.length > 0) {
flag = true;
alasABSPath = appAbsPathArr.join(sep);
}
appAbsPathArr.pop();
}
} else {
let step = 4;
const appAbsPathArr = appAbsPath.split(sep);
let flag = false;
while (step > 0 && !flag) {
appAbsPathArr.pop();
const entries = fg.sync(files, {
dot: true,
cwd: appAbsPathArr.join(sep) as string,
});
if (entries.length > 0) {
flag = true;
alasABSPath = appAbsPathArr.join(sep);
}
step--;
2023-04-18 23:54:06 +08:00
}
}
2023-04-27 21:42:54 +08:00
2023-04-18 23:54:06 +08:00
return alasABSPath.endsWith(sep) ? alasABSPath : alasABSPath + sep;
};
export default getAlasABSPath;