Skip to content

Commit

Permalink
Primary implementation of scoped modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Jan 28, 2016
1 parent 0ce1500 commit cad8489
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 60 deletions.
103 changes: 62 additions & 41 deletions bin/pnpm-install
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ var debug = require('debug')('pnpm:install')
var dirname = require('path').dirname
var join = require('path').join
var chalk = require('chalk')
var readPkgUp = require('read-pkg-up')
var assign = require('object-assign')

var mkdirp = require('../lib/mkdirp')
var getPath = require('../lib/get_path')
var installMultiple = require('../lib/install_multiple')
var Promise = require('../lib/promise')

Expand Down Expand Up @@ -35,66 +36,86 @@ const cli = require('meow')([
}
})

var observatory = require('observatory')
observatory.settings({ prefix: ' ', width: 74 })

/*
* Perform
*/

function run (cli) {
var observatory = require('observatory')
observatory.settings({ prefix: ' ', width: 74 })

var ctx = {}
var pkg

return readPkgUp()
.then(pkg_ => { pkg = pkg_ })
.then(_ => updateContext(pkg.path))
.then(_ => install())

function install () {
var packages

if (cli.input && cli.input.length) {
packages = cli.input
} else {
packages = assign({},
pkg.pkg.dependencies || {},
pkg.pkg.devDependencies || {})
}

return getPath()
.then(path => updateContext(path))
.then(_ => installMultiple(ctx, cli.input, join(ctx.root, 'node_modules'), cli.flags))
return installMultiple(ctx,
packages,
join(ctx.root, 'node_modules'),
cli.flags)
}

function updateContext (root) {
function updateContext (packageJson) {
var root = dirname(packageJson)
ctx.root = root
ctx.tmp = join(root, 'node_modules', '.tmp')
if (!cli.flags.quiet) { ctx.log = logger() }
else { ctx.log = function () { return function () {} } }
}
}

function logger () {
return function (pkg) {
var pkgData
function logger () {
return function (pkg) {
var pkgData

var t = observatory.add(pkg.name + ' ' +
chalk.gray(pkg.rawSpec || ''))
.status(chalk.yellow('·'))
return status
var t = observatory.add(pkg.name + ' ' +
chalk.gray(pkg.rawSpec || ''))
.status(chalk.yellow('·'))
return status

// log('resolved', pkgData)
// log('downloading')
// log('downloading', { done: 1, total: 200 })
// log('depnedencies')
// log('error', err)
function status (status, args) {
if (status === 'resolved') {
pkgData = args
} else if (status === 'downloading') {
t.status(chalk.yellow('downloading ' + pkgData.version + ' ·'))
if (args && args.total && args.done < args.total) {
t.details('' + Math.round(args.done / args.total * 100) + '%')
} else {
t.details('')
}
} else if (status === 'done') {
if (pkgData) {
t.status(chalk.green('' + pkgData.version + ' ✓'))
.details('')
} else {
t.status(chalk.green('OK ✓'))
.details('')
}
} else if (status === 'dependencies') {
t.status(chalk.gray('' + pkgData.version + ' ·'))
// log('resolved', pkgData)
// log('downloading')
// log('downloading', { done: 1, total: 200 })
// log('depnedencies')
// log('error', err)
function status (status, args) {
if (status === 'resolved') {
pkgData = args
} else if (status === 'downloading') {
t.status(chalk.yellow('downloading ' + pkgData.version + ' ·'))
if (args && args.total && args.done < args.total) {
t.details('' + Math.round(args.done / args.total * 100) + '%')
} else {
t.details('')
}
} else if (status === 'done') {
if (pkgData) {
t.status(chalk.green('' + pkgData.version + ' ✓'))
.details('')
} else {
t.status(status)
t.status(chalk.green('OK ✓'))
.details('')
}
} else if (status === 'dependencies') {
t.status(chalk.gray('' + pkgData.version + ' ·'))
.details('')
} else {
t.status(status)
.details('')
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion lib/err.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module.exports = function err (error) {
console.error('')
console.error('Error: ' + (error.stack || error.message || error))
if (error.host && error.path) {
console.error('' + error.message)
console.error('' + error.method + ' ' + error.host + error.path)
} else {
console.error('Error: ' + (error.stack || error.message || error))
}
process.exit(1)
}
16 changes: 0 additions & 16 deletions lib/get_path.js

This file was deleted.

2 changes: 2 additions & 0 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var fs = require('mz/fs')
*
* - `ctx` (Object) - the context.
* - `root` (String) - root path of the package.
* - `tmp` (String) - temp dir
* - `log` (Function) - logger
*
* What it does:
*
Expand Down
12 changes: 10 additions & 2 deletions lib/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ module.exports = function resolve (pkg) {

function toUri (pkg) {
// TODO: handle scoped packages
var name, uri

if (pkg.name.substr(0, 1) === '@') {
name = '@' + enc(pkg.name.substr(1))
} else {
name = enc(pkg.name)
}

if (pkg.rawSpec.length) {
uri = join(enc(pkg.name), enc(pkg.rawSpec))
uri = join(name, enc(pkg.rawSpec))
} else {
uri = join(enc(pkg.name), 'latest')
uri = join(name, 'latest')
}

return url.resolve(config.registry, uri)
Expand Down

0 comments on commit cad8489

Please sign in to comment.