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

resolve devDependencies for root package #4

Merged
merged 4 commits into from
Jun 20, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 21 additions & 9 deletions lib/spm-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var debug = require('debug')('father:spm');
var SpmPackage = Package.extend({

readPackage: function() {
var pkgFile = normalize(join(this.dest, 'package.json'));
var pkgFile = normalize(join(this.dest, 'package.json'), this.father);
debug('readPackage(%s) info %s', pkgFile.id, JSON.stringify(pkgFile));

Object.keys(pkgFile.dependencies)
Expand All @@ -23,7 +23,7 @@ var SpmPackage = Package.extend({

module.exports = SpmPackage;

function normalize(pkg) {
function normalize(pkg, father) {
var dest = path.dirname(pkg);
delete require.cache[pkg];
pkg = require(pkg);
Expand All @@ -36,15 +36,10 @@ function normalize(pkg) {
if (typeof pkg.spm.main !== 'string') {
throw new Error('pkg.spm.main should be string.');
}

// handle ./index.js
pkg.spm.main = pkg.spm.main.replace(/^\.\//, '');

// handle index
if (path.extname(pkg.spm.main) === '') {
pkg.spm.main += '.js';
}

// detect output type
if (!pkg.spm.output) {
pkg.spm.output = [];
Expand All @@ -53,11 +48,18 @@ function normalize(pkg) {
throw new Error('pkg.spm.output should be array.');
}

var deps = pkg.spm.dependencies || {};

// resolve devDependencies for root package
if (!father) {
extend(deps, pkg.spm.devDependencies);
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽然这样实现比较简单,但我还是希望将 devDependencies 独立处理出来,合在依赖中很奇怪

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

放在 _parsePkgDeps 里合?

var ret = {
id: pkg.name + '@' + pkg.version,
name: pkg.name,
version: pkg.version,
dependencies: pkg.spm.dependencies || {},
dependencies: deps,
main: pkg.spm.main,
dest: dest,
output: pkg.spm.output,
Expand All @@ -80,3 +82,13 @@ function resolveDeps(name, pkgFile, pkg) {
dest: join(dest, version)
};
}

function extend(target) {
var args = [].slice.call(arguments, 1);
args.forEach(function(obj) {
for (var i in obj) {
target[i] = obj[i];
}
});
return target;
}
3 changes: 3 additions & 0 deletions test/fixtures/spm/normal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"b": "1.1.0",
"c": "1.1.1",
"d": "~0.1.0"
},
"devDependencies": {
"e": "1.1.0"
}
}
}
10 changes: 10 additions & 0 deletions test/fixtures/spm/normal/sea-modules/e/1.1.0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "e",
"version": "1.1.0",
"spm": {
"main": "src/e.js",
"devDependencies": {
"f": "0.1.0"
}
}
}
1 change: 1 addition & 0 deletions test/fixtures/spm/normal/sea-modules/e/1.1.0/src/e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert
10 changes: 9 additions & 1 deletion test/spm.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('Father.SpmPackage', function() {
pkgDeps['b'].should.eql(pkg.get('b@1.1.0'));
pkgDeps['c'].should.eql(pkg.get('c@1.1.1'));
pkgDeps['d'].should.eql(pkg.get('d@0.1.1'));
pkgDeps['e'].should.eql(pkg.get('e@1.1.0'));

var b = pkg.get('b@1.1.0');
var bDeps = b.dependencies;
Expand Down Expand Up @@ -48,6 +49,12 @@ describe('Father.SpmPackage', function() {
d2.name.should.eql('d');
d2.version.should.eql('0.1.1');
d2.files['index.js'].dependencies.should.eql([]);

var e = pkg.get('e@1.1.0');
e.main.should.eql('src/e.js');
e.name.should.eql('e');
e.version.should.eql('1.1.0');
e.files['src/e.js'].dependencies.should.eql([]);
});

it('version cache', function() {
Expand Down Expand Up @@ -152,7 +159,8 @@ describe('Father.SpmPackage', function() {
'd@0.1.0',
'c@1.1.1',
'b@1.1.0',
'd@0.1.1'
'd@0.1.1',
'e@1.1.0'
]);
});

Expand Down