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

feat: install dependencies in global store #66

Open
wants to merge 6 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"
}]
}
}
]
}
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
"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",
Expand All @@ -44,11 +48,11 @@
"@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
8 changes: 5 additions & 3 deletions src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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 { addInstallations } from './installations'

import { addPackageToLockfile } from './lockfile'

Expand Down Expand Up @@ -176,7 +176,9 @@ export const addPackages = async (
}
const addedAction = options.link ? 'linked' : 'added'
console.log(
`Package ${pkg.name}@${pkg.version} ${addedAction} ==> ${destModulesDir}.`
`Package ${pkg.name}@${
pkg.version
} ${addedAction} ==> ${destModulesDir}.`
)
}

Expand Down Expand Up @@ -213,6 +215,6 @@ export const addPackages = async (

if (options.yarn) {
console.log('Running yarn:')
execSync('yarn', {cwd: options.workingDir})
execSync('yarn', { cwd: options.workingDir })
}
}
49 changes: 32 additions & 17 deletions src/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import * as crypto from 'crypto'
const npmPacklist = require('npm-packlist-fixed')
import ignore from 'ignore'

import { execSync } from 'child_process'
import { join, dirname } from 'path'
import { readIgnoreFile, readSignatureFile } from '.'
import { readIgnoreFile, readSignatureFile, getPackageManager } from '.'
import {
PackageManifest,
getStorePackagesDir,
Expand Down Expand Up @@ -60,47 +61,56 @@ export const copyPackageToStore = async (
}
) => {
const { workingDir } = options

const copyFromDir = options.workingDir
const storePackageStoreDir = join(
getStorePackagesDir(),
pkg.name,
pkg.version
)

const ignoreFileContent = readIgnoreFile(workingDir)

const ignoreRule = ignore().add(ignoreFileContent)

const npmList: string[] = await npmPacklist({ path: workingDir })
const filesToCopy = npmList.filter(f => !ignoreRule.ignores(f))

// Ensure the lockfile is always copied.
const npmBin = getPackageManager(workingDir)
const lockfileName = npmBin === 'yarn' ? 'yarn.lock' : 'package-lock.json'
if (fs.existsSync(join(workingDir, lockfileName))) {
filesToCopy.push(lockfileName)
}

if (options.files) {
console.log('Files included in published content:')
filesToCopy.forEach(f => {
console.log(`- ${f}`)
})
console.log(`Total ${filesToCopy.length} files.`)
}

const storePackageStoreDir = join(
getStorePackagesDir(),
pkg.name,
pkg.version
)

const copyFilesToStore = async () => {
await fs.remove(storePackageStoreDir)
return Promise.all(
filesToCopy
.sort()
.map(relPath =>
copyFile(
join(copyFromDir, relPath),
join(workingDir, relPath),
join(storePackageStoreDir, relPath),
relPath
)
)
)
}

const hashes = options.changed
? await Promise.all(
filesToCopy
.sort()
.map(relPath => getFileHash(join(copyFromDir, relPath), relPath))
.map(relPath => getFileHash(join(workingDir, relPath), relPath))
)
: await copyFilesToStore()

const signature = crypto
.createHash('md5')
.update(hashes.join(''))
Expand All @@ -115,28 +125,33 @@ export const copyPackageToStore = async (
}
}

// Install dependencies for the copied package.
console.log('Installing dependencies...')
execSync(`${npmBin} install --production`, { cwd: storePackageStoreDir })

if (options.knit) {
fs.removeSync(storePackageStoreDir)
const ensureSymlinkSync = fs.ensureSymlinkSync as any
filesToCopy.forEach(f => {
const source = join(copyFromDir, f)
const source = join(workingDir, f)
if (fs.statSync(source).isDirectory()) {
return
}
ensureSymlinkSync(source, join(storePackageStoreDir, f))
})
}

writeSignatureFile(storePackageStoreDir, signature)
const versionPre =
options.signature && !options.knit
? '-' + signature.substr(0, shortSignatureLength)
: ''
const pkgToWrite: PackageManifest = {

writePackageManifest(storePackageStoreDir, {
...pkg,
version: pkg.version + versionPre,
devDependencies: undefined
}
writePackageManifest(storePackageStoreDir, pkgToWrite)
})

writeSignatureFile(storePackageStoreDir, signature)
return signature
}
12 changes: 5 additions & 7 deletions src/installations.ts
Original file line number Diff line number Diff line change
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/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from './installations'

import {
values,
PackageManifest,
execLoudOptions,
getPackageManager,
Expand Down Expand Up @@ -118,7 +117,9 @@ export const publishPackage = async (options: PublishPackageOptions) => {
})
installationsToRemove.concat(installationsToRemoveForPkg)
}
await removeInstallations(installationsToRemove)
if (installationsToRemove.length) {
await removeInstallations(installationsToRemove)
}
}
//await workaroundYarnCacheBug(pkg)
const publishedPackageDir = join(getStorePackagesDir(), pkg.name, pkg.version)
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/project/nested/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"dependencies": {
"dep-package": "1.0.0"
}
}
}
14 changes: 7 additions & 7 deletions test/fixture/project/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "yalc-test-project",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"dep-package": "1.0.0"
}
"name": "yalc-test-project",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"dep-package": "1.0.0"
}
}
Loading