Skip to content

Commit

Permalink
[chore] Add script to symlink dependencies from monorepo to project (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and bjoerge committed Oct 12, 2017
1 parent ea55a97 commit 1fcc70b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-flow": "^6.23.0",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.26.0",
"boxen": "^1.1.0",
"chalk": "^1.1.3",
Expand All @@ -52,7 +52,7 @@
"husky": "^0.13.4",
"lerna": "^2.1.0",
"pre-commit": "^1.2.2",
"rimraf": "^2.6.1",
"rimraf": "^2.6.2",
"semver": "^5.3.0",
"stylelint": "^8.0.0",
"stylelint-config-css-modules": "^1.1.0",
Expand Down
55 changes: 55 additions & 0 deletions scripts/symlinkDependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env node
/* eslint-disable no-sync, no-console, strict */
'use strict'

const fs = require('fs')
const path = require('path')
const rimraf = require('rimraf')

const pkgPath = path.join(__dirname, '..', 'packages')
const targetDir = process.argv[2]
if (!targetDir) {
throw new Error('Target directory must be specified (`.` for current dir)')
}

const notSanity = dir => dir !== '@sanity'
const prefix = name => `@sanity/${name}`
const normalize = dir => dir.replace(/@sanity\//g, `@sanity${path.sep}`)

const rootPackages = fs.readdirSync(pkgPath).filter(notSanity)
const sanityPackages = fs.readdirSync(path.join(pkgPath, '@sanity')).map(prefix)
const packages = [].concat(rootPackages, sanityPackages)

const targetPath = path.resolve(path.relative(process.cwd(), targetDir))
const targetDepsPath = path.join(targetPath, 'node_modules')

const targetPkg = require(path.join(targetPath, 'package.json'))
const targetDeclared = Object.assign({}, targetPkg.dependencies, targetPkg.devDependencies)
const targetDeps = Object.keys(targetDeclared)

const targetRootPackages = fs.readdirSync(targetDepsPath).filter(notSanity)
const targetSanityPackages = fs.readdirSync(path.join(targetDepsPath, '@sanity')).map(prefix)
const targetPackages = [].concat(targetRootPackages, targetSanityPackages, targetDeps)

const sharedPackages = packages.filter(pkg => targetPackages.includes(pkg))
const sharedDeclared = packages.filter(pkg => targetDeps.includes(pkg))

const removeFolders = sharedPackages.map(normalize).map(dir => path.join(targetDepsPath, dir))

// First, remove all locally installed dependencies that exists as a package in our monorepo
console.log('Removing dependencies from node_modules:')
console.log(`\n ${sharedPackages.join('\n ')}\n`)

removeFolders.forEach(dir => rimraf.sync(dir))

// Secondly, symlink into node_modules, but only the dependencies declared in package.json
console.log('Symlinking dependencies to node_modules:\n')
sharedDeclared.forEach(dep => {
const sourceDepDir = path.join(pkgPath, normalize(dep))
const targetDepDir = path.join(targetDepsPath, normalize(dep))

console.log(` ${sourceDepDir} -> ${targetDepDir}`)
fs.symlinkSync(sourceDepDir, targetDepDir, 'dir')
})

console.log('\nDone!')
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3885,7 +3885,7 @@ right-align@^0.1.1:
dependencies:
align-text "^0.1.1"

rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.6.1:
rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.6.1, rimraf@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
dependencies:
Expand Down

0 comments on commit 1fcc70b

Please sign in to comment.