Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
E.Azer Koçulu
committed
Sep 2, 2013
0 parents
commit 1b84b50
Showing
9 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test | ||
test.js | ||
example | ||
examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
USAGE | ||
|
||
rename [name] | ||
|
||
OPTIONS | ||
|
||
-v --version Show version and exit | ||
-h --help Show help and exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var manifest = require("./manifest"); | ||
var git = require("./git"); | ||
|
||
module.exports = { | ||
manifest: manifest, | ||
git: git | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |