2021-10-01 09:19:36 +02:00
|
|
|
import { promises as fs, constants } from 'fs';
|
|
|
|
|
import path from 'path';
|
2024-02-01 22:38:21 +09:00
|
|
|
import yaml from 'js-yaml';
|
2023-06-04 16:59:38 +02:00
|
|
|
import { PackageItem } from '../theme/types';
|
2021-10-01 09:19:36 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
const fileExist = (filePath) =>
|
|
|
|
|
fs
|
|
|
|
|
.access(filePath, constants.F_OK)
|
|
|
|
|
.then(() => true)
|
|
|
|
|
.catch(() => false);
|
2021-10-01 09:19:36 +02:00
|
|
|
|
2022-09-13 09:03:13 +02:00
|
|
|
const fetchPackages = async (): Promise<PackageItem[]> => {
|
2021-10-01 09:19:36 +02:00
|
|
|
const docsDir = path.resolve(process.cwd(), '../packages');
|
2024-02-01 22:38:21 +09:00
|
|
|
const fileNames = await (
|
|
|
|
|
await fs.readdir(docsDir)
|
|
|
|
|
).map((filename) => ({ filename, directory: docsDir }));
|
2021-10-01 09:19:36 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
const packageJsons = await Promise.all(
|
|
|
|
|
fileNames.map(async ({ filename, directory }) => {
|
|
|
|
|
const filePath = path.resolve(directory, filename);
|
|
|
|
|
const fileStat = await fs.lstat(filePath);
|
2021-10-01 09:19:36 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
if (!fileStat.isDirectory()) return null;
|
2021-10-01 09:19:36 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
const jsonFilePath = path.resolve(filePath, 'package.json');
|
|
|
|
|
if (await fileExist(jsonFilePath)) {
|
|
|
|
|
return JSON.parse(await fs.readFile(jsonFilePath, 'utf-8'));
|
|
|
|
|
}
|
2021-10-01 09:19:36 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
const ymlFilePath = path.resolve(filePath, 'pubspec.yaml');
|
|
|
|
|
if (await fileExist(ymlFilePath)) {
|
|
|
|
|
return yaml.load(await fs.readFile(ymlFilePath, 'utf-8'));
|
|
|
|
|
}
|
2021-10-01 09:19:36 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
return null;
|
|
|
|
|
}),
|
|
|
|
|
);
|
2021-10-01 09:19:36 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
return packageJsons;
|
|
|
|
|
};
|
2021-10-01 09:19:36 +02:00
|
|
|
|
|
|
|
|
export default fetchPackages;
|