Skip to content

fix: always use relative symlinks #72

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

Open
wants to merge 8 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: 4 additions & 8 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
{
"taskName": "tsc",
"command": "node",
"args": [
"./node_modules/typescript/lib/tsc.js",
"-w",
"-p",
"."
],
"args": ["./node_modules/typescript/lib/tsc.js", "-w", "-p", "."],
"isBackground": true,
"problemMatcher": "$tsc-watch"
}]
}
}
]
}
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,36 @@
"scripts": {
"build": "tsc",
"clean": "trash **/*.js **/*.js.map **/*.d.ts **/*.log !**/node_modules/**",
"precommit": "yalc check",
"prepublishOnly": "yarn clean && tsc && yarn test",
"test": "tsc && mocha test && yarn lint",
"watch": "mocha test --watch",
"ci": "tsc && yarn test",
"lint": "tslint -p ."
},
"husky": {
"hooks": {
"pre-commit": "yalc check && pretty-quick --staged"
}
},
"dependencies": {
"del": "^2.2.2",
"fs-extra": "^4.0.2",
"graceful-fs": "^4.1.15",
"ignore": "^5.0.4",
"npm-packlist-fixed": "^1.1.12",
"user-home": "^2.0.0",
"yargs": "^7.1.0"
},
"devDependencies": {
"@types/del": "^2.2.32",
"@types/fs-extra": "^4.0.2",
"@types/glob": "^5.0.30",
"@types/klaw": "^1.3.2",
"@types/mocha": "^2.2.40",
"@types/node": "^6.0.48",
"@types/npm-packlist": "^1.1.0",
"@types/yargs": "^6.6.0",
"clean-ts-built": "^1.0.0",
"husky": "^0.13.3",
"husky": "^1.3.1",
"mocha": "^5.2.0",
"prettier": "^1.14.2",
"pretty-quick": "^1.8.0",
"trash-cli": "^1.4.0",
"ts-clean-built": "^1.0.0",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0",
"tslint-plugin-prettier": "^1.3.0",
Expand Down
153 changes: 78 additions & 75 deletions src/add.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { execSync } from 'child_process'
import * as fs from 'fs-extra'
import { join } from 'path'
import * as del from 'del'
import { PackageInstallation, addInstallations } from './installations'
import { execSync } from 'child_process'
import { dirname, join, relative } from 'path'
import { addInstallations } from './installations'

import { addPackageToLockfile } from './lockfile'

Expand All @@ -20,10 +19,9 @@ const ensureSymlinkSync = fs.ensureSymlinkSync as typeof fs.symlinkSync
export interface AddPackagesOptions {
dev?: boolean
link?: boolean
linkDep?: boolean
yarn?: boolean
safe?: boolean
pure?: boolean
noSave?: boolean
workingDir: string
}

Expand All @@ -40,26 +38,6 @@ const getLatestPackageVersion = (packageName: string) => {
return latest || ''
}

const emptyDirExcludeNodeModules = (path: string) => {
// TODO: maybe use fs.remove + readdir for speed.
del.sync('**', {
dot: true,
cwd: path,
ignore: '**/node_modules/**'
})
}

const isSymlink = (path: string) => {
try {
return !!fs.readlinkSync(path)
} catch (e) {
return false
}
}

const gracefulFs = require('graceful-fs')
gracefulFs.gracefulify(fs)

export const addPackages = async (
packages: string[],
options: AddPackagesOptions
Expand All @@ -71,7 +49,8 @@ export const addPackages = async (
return
}
const doPure =
options.pure === false ? false : options.pure || !!localPkg.workspaces
options.pure !== undefined ? options.pure : !!localPkg.workspaces

const addedInstalls = packages
.map(packageName => {
const { name, version = '' } = parsePackageName(packageName)
Expand All @@ -87,10 +66,9 @@ export const addPackages = async (
)
return null
}
const versionToInstall = version || getLatestPackageVersion(name)

const versionToInstall = version || getLatestPackageVersion(name)
const storedPackageDir = getPackageStoreDir(name, versionToInstall)

if (!fs.existsSync(storedPackageDir)) {
console.log(
`Could not find package \`${packageName}\` ` + storedPackageDir,
Expand All @@ -101,13 +79,10 @@ export const addPackages = async (

const pkg = readPackageManifest(storedPackageDir)
if (!pkg) {
return
return null
}
const destYalcCopyDir = join(workingDir, values.yalcPackagesFolder, name)

emptyDirExcludeNodeModules(destYalcCopyDir)
fs.copySync(storedPackageDir, destYalcCopyDir)

const signature = readSignatureFile(storedPackageDir)
let replacedVersion = ''
if (doPure) {
if (localPkg.workspaces) {
Expand All @@ -126,61 +101,89 @@ export const addPackages = async (
)
}
if (!doPure) {
const destModulesDir = join(workingDir, 'node_modules', name)
if (options.link || options.linkDep || isSymlink(destModulesDir)) {
fs.removeSync(destModulesDir)
}

if (options.link || options.linkDep) {
ensureSymlinkSync(destYalcCopyDir, destModulesDir, 'junction')
} else {
emptyDirExcludeNodeModules(destModulesDir)
fs.copySync(destYalcCopyDir, destModulesDir)
}

if (!options.link) {
const protocol = options.linkDep ? 'link:' : 'file:'
const protocol = options.link ? 'link:' : 'file:'
const localAddress =
protocol + values.yalcPackagesFolder + '/' + pkg.name

const dependencies = localPkg.dependencies || {}
const devDependencies = localPkg.devDependencies || {}
let whereToAdd = options.dev ? devDependencies : dependencies

if (options.dev) {
if (dependencies[pkg.name]) {
replacedVersion = dependencies[pkg.name]
delete dependencies[pkg.name]
const whereToRemove = devDependencies[pkg.name]
? devDependencies
: dependencies

replacedVersion = whereToRemove[pkg.name] || ''
if (replacedVersion !== localAddress) {
const whereToAdd =
options.dev || whereToRemove === devDependencies
? devDependencies
: dependencies

localPkgUpdated = true
whereToAdd[pkg.name] = localAddress
if (whereToAdd !== whereToRemove) {
delete whereToRemove[pkg.name]
}
} else {
if (!dependencies[pkg.name]) {
if (devDependencies[pkg.name]) {
whereToAdd = devDependencies
}
}
replacedVersion = ''
}
}

if (whereToAdd[pkg.name] !== localAddress) {
replacedVersion = replacedVersion || whereToAdd[pkg.name]
whereToAdd[pkg.name] = localAddress
localPkg.dependencies =
whereToAdd === dependencies ? dependencies : localPkg.dependencies
localPkg.devDependencies =
whereToAdd === devDependencies
? devDependencies
: localPkg.devDependencies
localPkgUpdated = true
const localPackageDir = join(
workingDir,
values.yalcPackagesFolder,
name
)

if (signature === readSignatureFile(localPackageDir)) {
console.log(
`"${packageName}" already exists in the local ".yalc" directory`
)
return null
}

// Replace the local ".yalc/{name}" directory.
fs.removeSync(localPackageDir)
fs.copySync(storedPackageDir, localPackageDir)

// Replace the local "node_modules/{name}" symlink.
const nodeModulesDest = join(workingDir, 'node_modules', name)
fs.removeSync(nodeModulesDest)
if (options.link) {
const target = relative(dirname(nodeModulesDest), nodeModulesDest)
ensureSymlinkSync(target, nodeModulesDest)
} else {
fs.copySync(localPackageDir, nodeModulesDest)
}

if (pkg.bin) {
const binDir = join(workingDir, 'node_modules', '.bin')
const addBinScript = (src: string, dest: string) => {
const srcPath = relative(binDir, join(localPackageDir, src))
const destPath = join(binDir, dest)
ensureSymlinkSync(srcPath, destPath)
fs.chmodSync(destPath, 700)
}
if (typeof pkg.bin === 'string') {
fs.ensureDirSync(binDir)
addBinScript(pkg.bin, pkg.name)
} else if (typeof pkg.bin === 'object') {
fs.ensureDirSync(binDir)
for (const name in pkg.bin) {
addBinScript(name, pkg.bin[name])
}
}
replacedVersion =
replacedVersion == localAddress ? '' : replacedVersion
}
const addedAction = options.link ? 'linked' : 'added'

const addedAction = options.noSave ? 'linked' : 'added'
console.log(
`Package ${pkg.name}@${pkg.version} ${addedAction} ==> ${destModulesDir}.`
`Package ${pkg.name}@${
pkg.version
} ${addedAction} ==> ${nodeModulesDest}.`
)
}

const signature = readSignatureFile(storedPackageDir)
return {
signature,
name,
Expand All @@ -202,8 +205,8 @@ export const addPackages = async (
version: i!.version,
replaced: i!.replaced,
pure: doPure,
file: !options.link && !options.linkDep && !doPure,
link: options.linkDep && !doPure,
file: !options.link && !doPure,
link: options.link && !doPure,
signature: i.signature
})),
{ workingDir: options.workingDir }
Expand All @@ -213,6 +216,6 @@ export const addPackages = async (

if (options.yarn) {
console.log('Running yarn:')
execSync('yarn', {cwd: options.workingDir})
execSync('yarn', { cwd: options.workingDir })
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface PackageManifest {
name: string
version: string
private?: boolean
files: string[]
bin?: string | { [name: string]: string }
dependencies?: { [name: string]: string }
devDependencies?: { [name: string]: string }
workspaces?: string[]
Expand Down
14 changes: 6 additions & 8 deletions src/installations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs-extra'
import * as path from 'path'
import { getStoreMainDir, values, readPackageManifest } from '.'
import { getStoreMainDir, values } from '.'
import { readLockfile } from './lockfile'

export type PackageName = string & { __packageName: true }
Expand Down Expand Up @@ -38,9 +38,8 @@ export const readInstallationsFile = (): InstallationsFile => {
export const showInstallations = ({ packages }: { packages: string[] }) => {
const config = readInstallationsFile()
Object.keys(config)
.filter(
packageName =>
packages.length ? packages.indexOf(packageName) >= 0 : true
.filter(packageName =>
packages.length ? packages.indexOf(packageName) >= 0 : true
)
.map((name: PackageName) => ({ name, locations: config[name] }))
.forEach(({ name, locations }) => {
Expand All @@ -55,14 +54,13 @@ export const cleanInstallations = async ({
packages,
dry
}: {
packages: string[],
packages: string[]
dry: boolean
}) => {
const config = readInstallationsFile()
const installsToRemove = Object.keys(config)
.filter(
packageName =>
packages.length ? packages.indexOf(packageName) >= 0 : true
.filter(packageName =>
packages.length ? packages.indexOf(packageName) >= 0 : true
)
.map((name: PackageName) => ({ name, locations: config[name] }))
.reduce(
Expand Down
5 changes: 3 additions & 2 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ export const updatePackages = async (
await addPackages(packagesLinks, {
workingDir: options.workingDir,
link: true,
noSave: true,
pure: false
})

const packagesLinkDep = lockPackages.filter(p => p.link).map(p => p.name)
await addPackages(packagesLinkDep, {
workingDir: options.workingDir,
linkDep: true,
workingDir,
link: true,
pure: false
})

Expand Down
12 changes: 7 additions & 5 deletions src/yalc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ yargs
describe: 'Add package from yalc repo to the project',
builder: () => {
return yargs
.default('yarn', false)
.boolean(['file', 'dev', 'save-dev', 'link', 'yarn', 'pure'])
.boolean(['link', 'dev', 'save', 'yarn', 'pure'])
.default('link', true)
.default('save', true)
.help(true)
},
handler: argv => {
Expand All @@ -132,10 +133,10 @@ yargs
false
)
return addPackages(argv._.slice(1), {
dev: argv.dev || argv.saveDev,
yarn: argv.yarn,
linkDep: argv.link,
dev: argv.dev,
link: argv.link,
pure: hasPureArg ? argv.pure : undefined,
noSave: !argv.save,
workingDir: process.cwd()
})
}
Expand All @@ -149,6 +150,7 @@ yargs
handler: argv => {
return addPackages(argv._.slice(1), {
link: true,
noSave: true,
workingDir: process.cwd()
})
}
Expand Down
Loading