ice/scripts/getPackages.ts

26 lines
893 B
TypeScript
Raw Permalink Normal View History

2022-01-27 14:32:38 +08:00
/* eslint no-restricted-syntax:0, no-await-in-loop:0, no-restricted-syntax:0 */
import path from 'path';
2022-01-27 15:51:02 +08:00
import fse from 'fs-extra';
import glob from 'glob';
2022-01-27 14:32:38 +08:00
export default async function getPackages() {
const packageNames: string[] = [];
const packageDirs: string[] = [];
2022-01-27 14:32:38 +08:00
const rootPkgPath = path.join(process.cwd(), 'package.json');
const rootPkgContent = fse.readJSONSync(rootPkgPath);
for (const workspace of rootPkgContent.workspaces || []) {
2022-01-27 15:51:02 +08:00
const dirs = glob.sync(workspace);
2022-01-27 14:32:38 +08:00
for (const dir of dirs) {
if (fse.existsSync(path.resolve(dir, 'package.json'))) {
const pkgContent = fse.readJSONSync(path.resolve(dir, 'package.json'));
packageNames.push(pkgContent.name);
packageDirs.push(path.resolve(dir));
} else {
console.warn('Invalid workspace package:', dir);
}
}
}
return { packageNames, packageDirs };
}