Skip to content

Commit

Permalink
feat: support pnpm workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Aug 23, 2023
1 parent 33cf053 commit d22b946
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"d3-node": "3.0.0",
"fast-glob": "^3.3.1",
"ini": "^4.0.0",
"js-yaml": "^4.1.0",
"node-fetch": "^3.3.1",
"ora": "6.1.2",
"prompts": "^2.4.2",
Expand Down
60 changes: 38 additions & 22 deletions src/files/workspace.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
const fs = require('fs');
const path = require('path');
const fg = require('fast-glob');
const yaml = require('js-yaml');

const {loadManifest, getPackageSize} = require('.');

const loadWorkspace = async (startPath) => {
const resolvedAppPath = path.resolve(startPath);
const manifestPath = path.join(resolvedAppPath, 'package.json');
if (fs.existsSync(manifestPath)) {
const pnpmConfigPath = path.join(resolvedAppPath, 'pnpm-workspace.yaml');
let packagePaths;

if (fs.existsSync(pnpmConfigPath)) {
const pnpmConfig = yaml.load(fs.readFileSync(pnpmConfigPath, 'utf8'));

if (Array.isArray(pnpmConfig.packages)) {
packagePaths = pnpmConfig.packages;
}
}

if (!packagePaths && fs.existsSync(manifestPath)) {
const manifest = await loadManifest(resolvedAppPath);

if (Array.isArray(manifest.workspaces)) {
const entries = await fg(manifest.workspaces, {
onlyDirectories: true,
unique: true,
cwd: resolvedAppPath,
packagePaths = manifest.workspaces;
}
}

if (packagePaths) {
const entries = await fg(packagePaths, {
onlyDirectories: true,
unique: true,
cwd: resolvedAppPath,
});

const workspaceProjects = await entries.reduce(async (aggPromise, relativePath) => {
const agg = await aggPromise;
const projectPath = path.join(resolvedAppPath, relativePath);
const projectManifest = await loadManifest(projectPath);
agg.push({
...projectManifest,
relativePath,
size: await getPackageSize(projectPath),
});
return agg;
}, Promise.resolve([]));

const workspaceProjects = await entries.reduce(async (aggPromise, relativePath) => {
const agg = await aggPromise;
const projectPath = path.join(resolvedAppPath, relativePath);
const projectManifest = await loadManifest(projectPath);
agg.push({
...projectManifest,
relativePath,
size: await getPackageSize(projectPath),
});
return agg;
}, Promise.resolve([]));

return {
path: resolvedAppPath,
workspaceProjects,
};
}
return {
path: resolvedAppPath,
workspaceProjects,
};
}

if (resolvedAppPath !== '/') {
Expand Down

0 comments on commit d22b946

Please sign in to comment.