feat: add detect app micro

This commit is contained in:
2024-11-24 20:50:22 +08:00
parent 4bbc531ac7
commit ce6037ead9
5 changed files with 91 additions and 13 deletions

View File

@@ -47,7 +47,14 @@ export const installApp = async (opts: InstallAppOpts) => {
file: filePath,
cwd: extractPath,
});
const pkgs = path.join(extractPath, 'package.json');
return installAppFromKey(key);
};
export const installAppFromKey = async (key: string) => {
const directory = path.join(appsPath, key);
if (!fileIsExist(directory)) {
throw new Error('App not found');
}
const pkgs = path.join(directory, 'package.json');
if (!fileIsExist(pkgs)) {
throw new Error('Invalid package.json');
}
@@ -57,8 +64,7 @@ export const installApp = async (opts: InstallAppOpts) => {
if (!name || !version || !app) {
throw new Error('Invalid package.json');
}
const readmeFile = path.join(extractPath, 'README.md');
const readmeFile = path.join(directory, 'README.md');
let readmeDesc = '';
if (fileIsExist(readmeFile)) {
readmeDesc = fs.readFileSync(readmeFile, 'utf-8');
@@ -71,11 +77,19 @@ export const installApp = async (opts: InstallAppOpts) => {
version,
//
entry: app?.entry || '',
path: extractPath,
path: directory,
origin: app,
};
app.key = key;
fs.writeFileSync(pkgs, JSON.stringify(pkg, null, 2));
// fs.unlinkSync(filePath);
return { path: filePath, pkg, showAppInfo };
return { pkg, showAppInfo };
};
export const getAppPathKeys = async () => {
let files = fs.readdirSync(appsPath);
files = files.filter((file) => {
const stat = fs.statSync(path.join(appsPath, file));
if (file === 'node_modules') return false;
return stat.isDirectory();
});
return files;
};