Skip to content

Commit

Permalink
move to separate files add/update
Browse files Browse the repository at this point in the history
  • Loading branch information
wclr committed Apr 25, 2017
1 parent 3d101fd commit b5ac181
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 184 deletions.
13 changes: 7 additions & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

{
"typescript.tsdk": "./node_modules/typescript/lib",
"typescript.tsserver.trace": "verbose",
"search.exclude": {
"**/out": true
},
"typescript.tsserver.trace": "verbose",
"search.exclude": {},
"files.exclude": {
"**/*.js": true,
"**/*.js.map": true,
"**/*.d.ts": true
},
"tslint.run": "onSave",

"editor.formatOnType": true,
"editor.tabSize": 2,
"editor.insertSpaces": true
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"version": "1.0.0-pre.0",
"description": "Work yarn packages locally like a boss.",
"bin": {
"yalc": "out/yalc.js"
"yalc": "src/yalc.js"
},
"main": "src/index.js",
"scripts": {
"build": "tsc",
"precommit": "yalc check",
"test": "mocha out/test",
"watch": "mocha out/test --watch",
"test": "mocha test",
"watch": "mocha test --watch",
"ci": "tsc && yarn test"
},
"license": "MIT",
Expand Down
145 changes: 145 additions & 0 deletions src/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { execSync } from 'child_process'
import * as fs from 'fs-extra'
import { join } from 'path'
import {
PackageInstallation, InstallationsFile,
readInstallationsFile,
addInstallations,
removeInstallations,
PackageName
} from './installations'

import {
readLockfile,
addPackageToLockfile,
LockFilePackageEntry
} from './lockfile'

import {
getStorePackagesDir,
PackageManifest,
getPackageStoreDir,
values,
parsePackageName
} from '.'

const ensureSymlinkSync = fs.ensureSymlinkSync as typeof fs.symlinkSync

export interface AddPackagesOptions {
dev?: boolean,
link?: boolean,
yarn?: boolean,
safe?: boolean
workingDir: string,
}

const getLatestPackageVersion = (packageName: string) => {
const dir = getPackageStoreDir(packageName)
const versions = fs.readdirSync(dir)
const latest = versions.map(version => ({
version, created: fs.statSync(join(dir, version)).ctime.getTime()
}))
.sort((a, b) => b.created - a.created).map(x => x.version)[0]
return latest || ''
}

export const addPackages = (packages: string[], options: AddPackagesOptions) => {
const packagesStoreDir = getStorePackagesDir()
const addedInstalls = packages.map((packageName) => {
const { name, version = '' } = parsePackageName(packageName)

if (!name) {
console.log('Could not parse package name', packageName)
}

if (!fs.existsSync(getPackageStoreDir(name))) {
console.log(`Could not find package \`${name}\` in store.`)
return null
}
const versionToInstall = version || getLatestPackageVersion(name)

const storedPackageDir = getPackageStoreDir(name, versionToInstall)

if (!fs.existsSync(storedPackageDir)) {
console.log(`Could not find package \`${packageName}\` ` + storedPackageDir)
return null
}

const pkgFile = join(storedPackageDir, 'package.json')
let pkg
try {
pkg = fs.readJsonSync(join(storedPackageDir, 'package.json'))
} catch (e) {
console.log('Could not read and parse ', pkgFile)
return null
}
const destLoctedCopyDir = join(options.workingDir,
values.locedPackagesFolder, name)
const destloctedLinkDir = join(options.workingDir, 'node_modules', name)

fs.emptyDirSync(destLoctedCopyDir)
fs.copySync(storedPackageDir, destLoctedCopyDir)
fs.removeSync(destloctedLinkDir)

let replacedVersion = ''
if (options.link) {
ensureSymlinkSync(destLoctedCopyDir, destloctedLinkDir, 'dir')
} else {
const localAddress = 'file:' + values.locedPackagesFolder + '/' + pkg.name
fs.copySync(destLoctedCopyDir, destloctedLinkDir)
const localManifestFile = join(options.workingDir, 'package.json')
const localPkg = fs.readJsonSync(localManifestFile) as PackageManifest

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]
}
} else {
if (!dependencies[pkg.name]) {
if (devDependencies[pkg.name]) {
whereToAdd = devDependencies
}
}
}

if (whereToAdd[pkg.name] !== localAddress) {
replacedVersion = replacedVersion || whereToAdd[pkg.name]
whereToAdd[pkg.name] = localAddress
localPkg.dependencies = dependencies
localPkg.devDependencies = devDependencies
fs.writeJsonSync(localManifestFile, localPkg)
}
replacedVersion = replacedVersion == localAddress ? '' : replacedVersion
}
console.log(`${pkg.name}@${pkg.version} locted ==> ${destloctedLinkDir}`)
return {
name,
version,
replaced: replacedVersion,
path: options.workingDir
}
}).filter(_ => _) as PackageInstallation[]

addPackageToLockfile(
addedInstalls
.map((i) => ({
name: i!.name,
version: i!.version,
replaced: i!.replaced,
file: !options.link
})), { workingDir: options.workingDir }
)

addInstallations(addedInstalls)

if (options.yarn) {
const changeDirCmd = 'cd ' + options.workingDir + ' && '
execSync(changeDirCmd + 'yarn')
}
}
1 change: 1 addition & 0 deletions src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type CheckOptions = {
const stagedChangesCmd = 'git diff --cached --name-only'
const allChangesCmd = 'git diff HEAD --name-only'
const notStagedChangesCmd = 'git diff --name-only'
//git ls-files --others --exclude-standard
//const filesInCommitCmd = 'git diff-tree --no-commit-id --name-only -r bd61ad98'

const isPackageManifest = (fileName: string) =>
Expand Down
Loading

0 comments on commit b5ac181

Please sign in to comment.