Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
E.Azer Koçulu committed Sep 2, 2013
0 parents commit 1b84b50
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
4 changes: 4 additions & 0 deletions .npmignore
@@ -0,0 +1,4 @@
test
test.js
example
examples
16 changes: 16 additions & 0 deletions README.md
@@ -0,0 +1,16 @@
## rename

Rename a NodeJS project

```bash
$ cd foo
$ rename bar
Renamed foo to bar in manifest.json
Renamed Git remote from git@github.com:azer/foo.git to git@github.com:azer/bar.git
```

## Install

```bash
$ npm install rename
```
24 changes: 24 additions & 0 deletions bin/rname
@@ -0,0 +1,24 @@
#!/usr/bin/env node

require('default-debug')('*');

var opt = require('optimist');
var rename = require("../");

var argv = opt
.options('version', { alias: 'v' })
.options('help', { alias: 'h' })
.argv;

var name = argv._[0];

if(argv.version) require('show-version');
if(argv.help || argv._.length == 0 || !name) require('show-help');

rename.manifest(name, function (old, rpl) {
if (!old) return console.error(style.red('Failed to read package.json'));

rename.git(old, name, function (old, rpl) {
if (!old) return console.error(style.red('Failed to change Git remote'));
});
});
8 changes: 8 additions & 0 deletions docs/man
@@ -0,0 +1,8 @@
USAGE

rename [name]

OPTIONS

-v --version Show version and exit
-h --help Show help and exit
26 changes: 26 additions & 0 deletions git.js
@@ -0,0 +1,26 @@
var exec = require('child_process').exec;
var debug = require('local-debug')('git');

module.exports = git;

function git (old, name, callback) {
exec('git remote --verbose', function (error, output) {
if (error) return callback();

var match = output.match('(git\@[^ ]+)');

if (!match) return callback();

debug('Read existing remote as %s', match[1]);

var url = match[1];
var rpl = url.replace(old, name);

debug('Replacing %s with %s', url, rpl);

exec('git remote set-url origin ' + rpl, function (error, output) {
if (error) return callback();
callback(url, rpl);
});
});
}
7 changes: 7 additions & 0 deletions index.js
@@ -0,0 +1,7 @@
var manifest = require("./manifest");
var git = require("./git");

module.exports = {
manifest: manifest,
git: git
};
57 changes: 57 additions & 0 deletions manifest.js
@@ -0,0 +1,57 @@
var fs = require("fs");
var debug = require("local-debug")('manifest');

module.exports = manifest;

function bin (doc, old, name) {
if (!doc.bin) return;

var newBin = {};
var key;
var value;
var newValue;

for (key in doc.bin) {
value = doc.bin[key];
newValue = value.replace(old, name);
newBin[key.replace(old, name)] = newValue;

if (value != newValue) {
debug('Moving %s to %s', value, newValue);
fs.renameSync(value, newValue);
} else {
debug("Not changed: %s", value);
}
}

doc.bin = newBin;
}

function manifest (name, callback) {
if (!name) callback();

debug('Reading the manifest file in working directory');

fs.readFile('./package.json', function (error, buffer) {
var doc = JSON.parse(buffer.toString());
var old = doc.name;
doc.name = name;

debug('Renamed package name from %s to %s', old, name);

bin(doc, old, name);
repo(doc, old, name);

fs.writeFile('./package.json', JSON.stringify(doc, null, ' '), function (error) {
if (error) return console.error('Failed to write package.json');
callback(old, name);
});

});
}

function repo (doc, old, name) {
if (!doc.manifest || !doc.manifest.url) return;
doc.repository.url = doc.repository.url.replace(old, name);
}

23 changes: 23 additions & 0 deletions package.json
@@ -0,0 +1,23 @@
{
"name": "rname",
"version": "0.0.0",
"description": "Rename a NodeJS project",
"main": "index.js",
"bin": {
"rname": "./bin/rname"
},
"dependencies": {
"styled": "0.0.1",
"optimist": "0.x",
"show-help": "0.x",
"show-version": "0.x",
"local-debug": "0.0.0",
"default-debug": "0.0.0"
},
"repository": {
"url": "git@github.com:azer/rname.git",
"type": "git"
},
"author": "Azer Koculu <azer@kodfabrik.com>",
"license": "BSD"
}

0 comments on commit 1b84b50

Please sign in to comment.