Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add cmd spm tree

  • Loading branch information...
commit 670cd0c61c1352faba4881c2a18ada2deb5f33d8 1 parent 3f78b12
Hsiaoming Yang authored
Showing with 102 additions and 2 deletions.
  1. +1 −0  bin/spm
  2. +97 −0 bin/spm-tree
  3. +1 −0  index.js
  4. +3 −2 package.json
View
1  bin/spm
@@ -55,6 +55,7 @@ commander
var moduleCommands = {
build: 'build a module',
+ tree: 'show dependencies tree',
info: 'information of a module',
login: 'login your account',
search: 'search modules',
View
97 bin/spm-tree
@@ -0,0 +1,97 @@
+#!/usr/bin/env node
+
+var path = require('path');
+var resolve = path.resolve;
+var commander = require('commander');
+var archy = require('archy');
+
+require('colorful').colorful();
+var spm = require('..');
+
+commander
+ .option('-d, --depth <n>', 'set the depth to <n>', Infinity)
+ .option('--no-color', 'disable colorful print');
+
+
+commander.on('--help', function() {
+ console.log();
+ console.log(' ' + 'Examples:'.to.bold.blue);
+ console.log();
+ console.log(' $', 'spm info'.to.magenta.color, 'jquery');
+ console.log(' $', 'spm info'.to.magenta.color, 'arale/class');
+ console.log(' $', 'spm info'.to.magenta.color, 'arale/class@1.0.0');
+ console.log();
+});
+
+commander.parse(process.argv);
+
+// color
+spm.log.config(commander);
+
+var file = spm.sdk.grunt.file;
+
+if (!file.exists('package.json')) {
+ console.log(' package.json missing.');
+ process.exit(1);
+}
+
+console.log();
+console.log(indent(archy(tree('package.json'))));
+
+function tree(f, paths, depth) {
+ depth = depth || 0;
+ f = resolve(f);
+ if (!file.exists(f)) {
+ console.log(' can not find ' + path.relative(process.cwd(), f));
+ console.log();
+ return {};
+ }
+ var pkg = require(f);
+
+ paths = paths || ['sea-modules'];
+
+ var node = {};
+ var title = '';
+ if (pkg.family) {
+ title = pkg.family + '/' + pkg.name;
+ } else {
+ title = pkg.name;
+ }
+ node.label = title.to.cyan + ('@' + pkg.version).to.grey;
+ if (++depth > commander.depth) return node;
+
+ if (pkg.spm && pkg.spm.alias) {
+ var alias = pkg.spm.alias;
+
+ var deps = Object.keys(alias).map(function(key) {
+ var parsed = spm.sdk.module.parseIdentify(alias[key]);
+ if (parsed && parsed.family && parsed.name && parsed.version) {
+ return parsed;
+ }
+ }).filter(function(key) {
+ return key;
+ });
+ node.nodes = deps.map(function(parsed) {
+ return tree(lookup(paths, parsed));
+ });
+ }
+ return node;
+}
+
+function lookup(paths, pkg) {
+ var pkgfile;
+ paths.some(function(f) {
+ var fpath = path.join(f, pkg.family, pkg.name, pkg.version, 'package.json');
+ if (file.exists(fpath));
+ pkgfile = fpath;
+ return true;
+ });
+ if (!pkgfile) {
+ throw new Error('failed to resolve location of ' + pkg.family + '/' + pkg.name + '@' + pkg.version);
+ }
+ return pkgfile;
+}
+
+function indent(str) {
+ return str.replace(/^/gm, ' ');
+}
View
1  index.js
@@ -28,3 +28,4 @@ exports.sdk.spmrc = require('spmrc');
exports.sdk.iduri = require('./lib/sdk/iduri');
exports.sdk.yuan = require('./lib/sdk/yuan');
exports.sdk.grunt = require('./lib/sdk/grunt');
+exports.sdk.module = require('./lib/sdk/module');
View
5 package.json
@@ -13,9 +13,10 @@
"async": "~0.1.22",
"tar": "~0.1.16",
"request": "~2.16.2",
- "colorful": "~2.0.1",
+ "colorful": "~2.0.2",
"commander": "~1.1.1",
"cmd-util": "~0.3.3",
+ "archy": "~0.0.2",
"uglify-js": "~2.2.5",
"clean-css": "~0.10.1",
@@ -23,7 +24,7 @@
"lodash": "~0.9.2",
"grunt": "~0.4.1",
- "grunt-spm-build": "~0.2.0a4"
+ "grunt-spm-build": "~0.2.0b1"
},
"devDependencies": {
"sinon": "*",
Please sign in to comment.
Something went wrong with that request. Please try again.