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

Support Yarn v3 (berry) #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ yargs
describe: "Install relative deps",
handler: relativeDeps.installRelativeDeps
})
.option("P", {
alias: ["specifiedPackage"],
description: "Specify package",
default: undefined,
type: "string"
})
.command({
command: "watch",
describe: "Watch relative deps and install on change",
handler: relativeDeps.watchRelativeDeps
})
.option("P", {
alias: ["specifiedPackage"],
description: "Specify package",
default: undefined,
type: "string"
})
.command({
command: "init",
describe: "Initialize relative-deps",
Expand Down
57 changes: 47 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const globby = require("globby")
const checksum = require("checksum")
const merge = require("lodash/merge")
const debounce = require("lodash/debounce")
const { spawn } = require("yarn-or-npm")
const { spawn, hasYarn } = require("yarn-or-npm")
const tar = require("tar")

async function installRelativeDeps() {
async function installRelativeDeps({specifiedPackage}) {
const projectPkgJson = readPkgUp.sync()

const relativeDependencies = projectPkgJson.package.relativeDependencies
Expand All @@ -20,10 +20,26 @@ async function installRelativeDeps() {
process.exit(0)
}

const targetDir = path.dirname(projectPkgJson.path)

const depNames = Object.keys(relativeDependencies)
for (const name of depNames) {
if (!specifiedPackage) {
for (const name of depNames) {
installPackage(projectPkgJson, name)
}
} else {
const name = depNames.find((d) => d === specifiedPackage)
if (name) {
installPackage(projectPkgJson, name)
} else {
console.warn(`No dependency found with name '${specifiedPackage}'`);
}
}
}

async function installPackage(projectPkgJson, name) {
const targetDir = path.dirname(projectPkgJson.path)
const relativeDependencies = projectPkgJson.package.relativeDependencies

const libDir = path.resolve(targetDir, relativeDependencies[name])
console.log(`[relative-deps] Checking '${name}' in '${libDir}'`)

Expand Down Expand Up @@ -60,10 +76,9 @@ async function installRelativeDeps() {
fs.writeFileSync(hashStore.file, hashStore.hash)
console.log(`[relative-deps] Re-installing ${name}... DONE`)
}
}
}

async function watchRelativeDeps() {
async function watchRelativeDeps({specifiedPackage}) {
const projectPkgJson = readPkgUp.sync()

const relativeDependencies = projectPkgJson.package.relativeDependencies
Expand All @@ -73,9 +88,19 @@ async function watchRelativeDeps() {
process.exit(0)
}

Object.values(relativeDependencies).forEach(path => {
fs.watch(path, { recursive: true }, debounce(installRelativeDeps, 500))
});
if (specifiedPackage) {
Object.values(relativeDependencies).forEach(path => {
fs.watch(path, { recursive: true }, debounce(installRelativeDeps, 500))
});
} else {
const name = Object.keys(relativeDependencies).find((k) => k === specifiedPackage);
const path = relativeDependencies[name];
if (path) {
fs.watch(path, { recursive: true }, debounce(installRelativeDeps, 500));
} else {
console.warn(`No dependency found with name '${specifiedPackage}'`);
}
}
}

async function libraryHasChanged(name, libDir, targetDir, hashStore) {
Expand Down Expand Up @@ -154,10 +179,22 @@ function packAndInstallLibrary(name, dir, targetDir) {
fs.mkdirSync(libDestDir, { recursive: true })

const tmpName = name.replace(/[\s\/]/g, "-").replace(/@/g, "")



// npm replaces @... with at- where yarn just removes it, so we test for both files here
const regex = new RegExp(`^(at-)?${tmpName}(.*).tgz$`)

const packagedName = fs.readdirSync(dir).find(file => regex.test(file))
let packagedName = fs.readdirSync(dir).find(file => regex.test(file))

if(hasYarn) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

According to the docs, hasYarn is a function.

Have you tested your code on yarn 1 or 2 with these changes?

let yarnVersion = spawn.sync(['-v'], { cwd: dir, encoding: "utf8" });
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't there a longhand version of the same flag, which will clearly signal the intent of the command? I know that yarn@1 has --version in addition to -v.


if(yarnVersion.stdout.replace('\n','').split('.')[0] >= 3) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Non strict comparison with type coercion is a strongly discouraged because it is very error prone. Can we do it in a more safe manner?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I can use a semver parsing library if that works. I'll work on this over the next couple of days.

packagedName = "package.tgz"
}
}

fullPackageName = path.join(dir, packagedName)

console.log(`[relative-deps] Extracting "${fullPackageName}" to ${libDestDir}`)
Expand Down
Loading