Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proper yarn workspace support #379

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions src/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,40 @@ export interface YarnDependency {
children: YarnDependency[];
}

function asYarnDependency(prefix: string, tree: YarnTreeNode, prune: boolean): YarnDependency | null {
if (prune && /@[\^~]/.test(tree.name)) {
function asYarnDependency(prefix: string, name: string, prune: boolean, parentStack:string[]): YarnDependency | null {
if (prune && /@[\^~]/.test(name)) {
return null;
}

let name: string;

try {
const parseResult = parseSemver(tree.name);
name = parseResult.name;
} catch (err) {
name = tree.name.replace(/^([^@+])@.*$/, '$1');
}

const dependencyPath = path.join(prefix, name);
const children: YarnDependency[] = [];

for (const child of (tree.children || [])) {
const dep = asYarnDependency(path.join(prefix, name, 'node_modules'), child, prune);

if (dep) {
children.push(dep);
let dependencyPath;
let newPrefix = prefix;
// Follow the same resolve logic that is used within npm / yarn
while(newPrefix !== "/" && !dependencyPath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works on Windows?

if (fs.existsSync(path.join(newPrefix, "node_modules", name)))
{
dependencyPath = path.join(newPrefix, "node_modules", name)
}
else {
newPrefix = path.join(newPrefix, '..');
}
}
if(!dependencyPath) {
dependencyPath = path.join(prefix, "node_modules", name)
}
const depPackage = require(path.join(dependencyPath, "package.json"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use require to load a JSON file from disk.

const children = [];
parentStack.push(name);
if(depPackage.dependencies) {
const depChildren = Object.keys(depPackage.dependencies);
depChildren.forEach((childName) => {
if(parentStack.indexOf(childName) === -1) {
const dep = asYarnDependency(dependencyPath, childName, prune, parentStack.concat());
if (dep) {
children.push(dep);
}
}
});
}

return { name, path: dependencyPath, children };
}
Expand Down Expand Up @@ -146,18 +156,13 @@ function selectYarnDependencies(deps: YarnDependency[], packagedDependencies: st
}

async function getYarnProductionDependencies(cwd: string, packagedDependencies?: string[]): Promise<YarnDependency[]> {
const raw = await new Promise<string>((c, e) => cp.exec('yarn list --prod --json', { cwd, encoding: 'utf8', env: { ...process.env }, maxBuffer: 5000 * 1024 }, (err, stdout) => err ? e(err) : c(stdout)));
const match = /^{"type":"tree".*$/m.exec(raw);

if (!match || match.length !== 1) {
throw new Error('Could not parse result of `yarn list --json`');
}

// `yarn list` command does not behave like `npm ls` and as a result is not reliable to get project dependencies, instead let's just mimic what npm does internally (technically this could be shared with npm implementation to be only one like of code)
const usingPackagedDependencies = Array.isArray(packagedDependencies);
const trees = JSON.parse(match[0]).data.trees as YarnTreeNode[];
const rootPackage = require(path.join(cwd, 'package.json'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use require to load a JSON file from disk.

const trees = Object.keys(rootPackage.dependencies);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of trees, shouldn't it be packages? Or dependencies?


let result = trees
.map(tree => asYarnDependency(path.join(cwd, 'node_modules'), tree, !usingPackagedDependencies))
.map(tree => asYarnDependency(path.join(cwd, 'node_modules'), tree, !usingPackagedDependencies, []))
.filter(dep => !!dep);

if (usingPackagedDependencies) {
Expand Down
9 changes: 8 additions & 1 deletion src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,14 @@ export function collect(manifest: Manifest, options: IPackageOptions = {}): Prom
const processors = createDefaultProcessors(manifest, options);

return collectFiles(cwd, useYarn, packagedDependencies).then(fileNames => {
const files = fileNames.map(f => ({ path: `extension/${f}`, localPath: path.join(cwd, f) }));
const files = fileNames.map(f => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we change the return values of collectFiles to return absolute paths instead of hardcoding yarn workspaces knowledge down here.

// In case of a yarn workspace the filepath can point to node_modules at a higher level, we can just strip that within the extension
let basePathF = f;
while(basePathF.indexOf('../') === 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test this on Windows? Also, please do not use indexOf to check for a prefix...

basePathF = basePathF.substr(3);
}
return ({ path: `extension/${basePathF}`, localPath: path.join(cwd, f) });
});

return processFiles(processors, files, options);
});
Expand Down