├── .gitignore ├── History.md ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.sw* 3 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 0.0.1 / 2014-02-02 3 | ================== 4 | 5 | * fix dir name computation 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # npm-clone 3 | 4 | Clone a node module(, install its dependencies (and run its tests)). 5 | 6 | ## Usage 7 | 8 | ```bash 9 | # clone a node module 10 | npm-clone periodic 11 | 12 | # clone using its private url 13 | npm-clone --ssh periodic 14 | npm-clone --https periodic 15 | 16 | # clone and install dependencies 17 | npm-clone install periodic 18 | 19 | # clone, install dependencies and run its tests 20 | npm-clone install test periodic 21 | 22 | # with a short form 23 | npm-clone all periodic 24 | 25 | # flags still apply 26 | npm-clone --ssh all periodic 27 | 28 | # link globally 29 | npm-clone --global install periodic 30 | ``` 31 | 32 | ## Installation 33 | 34 | With [npm](https://npmjs.org/) do: 35 | 36 | ```bash 37 | npm install -g npm-clone 38 | ``` 39 | 40 | ## License 41 | 42 | The MIT license. 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var repoUrl = require('repo-url'); 4 | var spawn = require('child_process').spawn; 5 | var parse = require('url').parse; 6 | var base = require('path').basename; 7 | var commands = process.argv.slice(2); 8 | var pkg = require('./package.json'); 9 | 10 | var name = commands.pop(); 11 | if (!name) error('module name required'); 12 | 13 | function cmd (str) { 14 | return commands.indexOf(str) > -1 15 | || commands.indexOf('--' + str) > -1; 16 | } 17 | 18 | 19 | function help() { 20 | console.log(pkg.description); 21 | console.log(''); 22 | console.log('Usage'); 23 | console.log(' $ npm-clone [FLAG] [TYPE] '); 24 | console.log(' Types: all, install, test, link'); 25 | console.log(' Flags: --ssh, --https, --global'); 26 | console.log(''); 27 | console.log('Example'); 28 | console.log(' $ npm-clone --ssh all periodic'); 29 | } 30 | 31 | if (process.argv.indexOf('-h') !== -1 || process.argv.indexOf('--help') !== -1) { 32 | help(); 33 | return; 34 | } 35 | 36 | if (process.argv.indexOf('-v') !== -1 || process.argv.indexOf('--version') !== -1) { 37 | console.log(pkg.version); 38 | return; 39 | } 40 | 41 | var get = 42 | cmd('ssh') ? repoUrl.ssh : 43 | cmd('https') ? repoUrl.https : 44 | repoUrl; 45 | 46 | get(name, function (err, url) { 47 | if (err) error(err); 48 | 49 | run('git', ['clone', url, name], function (code) { 50 | if (code != 0) process.exit(code); 51 | process.chdir(name); 52 | 53 | if (cmd('install') || cmd('test') || cmd('all')) { 54 | run('npm', [cmd('global') ? 'link' : 'install'], function (code) { 55 | if (code != 0) process.exit(code); 56 | 57 | if (cmd('test') || cmd('all')) { 58 | run('npm', ['test'], function (code) { 59 | process.exit(code); 60 | }); 61 | } 62 | }); 63 | } 64 | }); 65 | }); 66 | 67 | function run (cmd, args, opts, fn) { 68 | if (typeof opts == 'function') { 69 | fn = opts; 70 | opts = {}; 71 | } 72 | var ps = spawn(cmd, args, opts); 73 | ps.stdout.pipe(process.stdout); 74 | ps.stderr.pipe(process.stderr); 75 | ps.on('exit', fn); 76 | } 77 | 78 | function error (err) { 79 | console.error(err); 80 | process.exit(1); 81 | } 82 | 83 | function dirname (url) { 84 | return base(parse(url).pathname).replace(/\.git$/, ''); 85 | } 86 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "npm-clone", 3 | "description": "Clone a node module, install its dependencies and run its tests", 4 | "version": "1.0.0", 5 | "repository": { 6 | "type": "git", 7 | "url": "git://github.com/juliangruber/npm-clone.git" 8 | }, 9 | "homepage": "https://github.com/juliangruber/npm-clone", 10 | "main": "index.js", 11 | "bin": { 12 | "npm-clone": "./index.js" 13 | }, 14 | "dependencies": { 15 | "repo-url": "^1.0.0" 16 | }, 17 | "keywords": [ 18 | "git", 19 | "clone", 20 | "scm", 21 | "npm", 22 | "module", 23 | "shell" 24 | ], 25 | "author": { 26 | "name": "Julian Gruber", 27 | "email": "mail@juliangruber.com", 28 | "url": "http://juliangruber.com" 29 | }, 30 | "license": "MIT" 31 | } 32 | --------------------------------------------------------------------------------