-
Notifications
You must be signed in to change notification settings - Fork 213
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use |
||
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 }; | ||
} | ||
|
@@ -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')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use |
||
const trees = Object.keys(rootPackage.dependencies); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of |
||
|
||
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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather we change the return values of |
||
// 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you test this on Windows? Also, please do not use |
||
basePathF = basePathF.substr(3); | ||
} | ||
return ({ path: `extension/${basePathF}`, localPath: path.join(cwd, f) }); | ||
}); | ||
|
||
return processFiles(processors, files, options); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works on Windows?