cjs-rename
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "cjs-rename", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Rename a CJS file thing", | ||
"main": "index.js", | ||
"main": "lib/rename.js", | ||
"scripts": { | ||
@@ -31,8 +31,8 @@ "test": "mocha -R spec --bail test/" | ||
"bluebird": "1.0.x", | ||
"recursive-readdir-filter": "0.0.3" | ||
"recursive-readdir-filter": "0.0.x" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^1.17.1", | ||
"ncp": "^0.5.0" | ||
"mocha": "1.17.x", | ||
"ncp": "0.5.x" | ||
} | ||
} |
115
README.md
@@ -5,13 +5,63 @@ # cjs-rename | ||
Given a module a bad name? Used all over your codebase and tests? Sounds like you want to rename a CJS module, and this here module can do that for you: | ||
```shell | ||
> cjs-rename src/some-bad-name.js src/some-good-name.js | ||
Renaming: | ||
- src/foo/bad.js fixed 2 require()s | ||
- src/foo/qux.js fixed 3 require()s | ||
- test/foo/bar.js fixed 1 require()s | ||
``` | ||
And magically, where you saw: | ||
```javascript | ||
require('../../src/some-bad-name'); | ||
``` | ||
You'll now find: | ||
```javascript | ||
require('../../src/some-good-name'); | ||
``` | ||
## Install | ||
```shell | ||
npm install -g cjs-rename | ||
``` | ||
## CLI Usage | ||
```shell | ||
> cjs-rename | ||
Usage: cjs-rename [options] [command] | ||
Commands: | ||
* cjs-rename [from] [to] [source...] | ||
Options: | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-d, --dry Do not write changes to disk. | ||
``` | ||
// current usage | ||
cjs-rename -t ../source/old.js -f ../source/new.js -s ../source | ||
// timruffles idea - only specify file names, not paths | ||
cjs-rename old.js new.js ../source | ||
**Parameters:** | ||
// my idea - if no source is specified, use current dir | ||
cjs-rename old.js new.js | ||
- `from`: path to the current file | ||
- `to`: path to the file has been moved to | ||
- `folder`: (optional). Which files to search through. Uses cwd by default. | ||
**Example:** | ||
```shell | ||
> cjs-rename source/template.js source/view.js | ||
Renaming: | ||
- source/core.js fixed 1 require()s | ||
- source/utils.js fixed 1 require()s | ||
- test/template.js fixed 1 requires()s | ||
``` | ||
@@ -25,6 +75,7 @@ | ||
var rename = new Rename({ | ||
cwd: '...', // optional | ||
to: '...', | ||
from: '...', | ||
folder: '...' | ||
folder: '...', | ||
cwd: '...', // optional | ||
dryrun: false // optional | ||
}); | ||
@@ -39,14 +90,34 @@ | ||
}); | ||
// Also supports promises | ||
rename.run().then(function (changes) { ... }); | ||
// If you set 'options.dryrun', then you need to save your changes | ||
rename.save(); | ||
``` | ||
## Important Notes | ||
- This will only search through files with the `.js` extension. | ||
- It will ignore any `node_modules` folders. | ||
- It will currently not move any files. | ||
## Todo | ||
- Only add the '.js' extension if it was there before | ||
- Write tests | ||
- Improve command line interface | ||
- Add MIT license | ||
- Move files | ||
- Add support for coffeescript | ||
## Changelog | ||
## 0.0.3 | ||
- Improve command line interface. | ||
- If the original 'require()' call didn't have the extension, than the replaced | ||
path will not have the extension. | ||
- Make 'options.folder' optional. It now uses the current working directory. | ||
- Add 'options.dryrun'. If set to a truthy value, it will not save changes to | ||
disk. | ||
- Add 'Rename.prototype.save'. This will write pending changes to disk. | ||
### 0.0.2 | ||
@@ -62,2 +133,22 @@ | ||
MIT | ||
The MIT License (MIT) | ||
Copyright (c) 2014 George Czabania | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
var assert = require('assert'); | ||
var Rename = require('../index'); | ||
var Rename = require('../lib/rename'); | ||
var ncp = require('ncp'); | ||
var fs = require('fs'); | ||
var TESTDIR = __dirname + '/testdir'; | ||
var TESTDIR_BACKUP = __dirname + '/testdir-backup'; | ||
var BACKUP = __dirname + '/testdir-backup'; | ||
var EXPECTED = __dirname + '/testdir-expected'; | ||
var SAMPLE = { | ||
cwd: '/Home', | ||
to: '/new.js', | ||
from: '/old.js', | ||
folder: '.' | ||
}; | ||
describe('cjs-rename', function () { | ||
before(function (done) { | ||
ncp(TESTDIR_BACKUP, TESTDIR, done); | ||
ncp(BACKUP, TESTDIR, done); | ||
}); | ||
@@ -29,9 +38,26 @@ | ||
it('should work without "new" keyword', function () { | ||
var rename = Rename(SAMPLE); | ||
assert(rename instanceof Rename); | ||
assert.equal(rename.cwd, '/Home'); | ||
}); | ||
it('should rename files', function (done) { | ||
var expectedChanges = [ | ||
{ path: TESTDIR + '/extension.js', count: 2, | ||
contents: fs.readFileSync(EXPECTED + '/extension.js').toString() }, | ||
{ path: TESTDIR + '/quotes.js', count: 2, | ||
contents: fs.readFileSync(EXPECTED + '/quotes.js').toString() }, | ||
{ path: TESTDIR + '/folder/parent.js', count: 1, | ||
contents: fs.readFileSync(EXPECTED + '/folder/parent.js').toString() } | ||
]; | ||
var rename = new Rename({ | ||
cwd: __dirname, | ||
to: './testdir/new-name.js', | ||
to: './testdir/done.js', | ||
from: './testdir/replace.js', | ||
folder: './testdir/' | ||
folder: './testdir/', | ||
save: true | ||
}); | ||
@@ -41,8 +67,21 @@ | ||
assert.ifError(err); | ||
console.log(changes); | ||
assert.deepEqual(changes, expectedChanges); | ||
done(); | ||
}); | ||
}); | ||
it('should add extension', function () { | ||
var rename = new Rename(SAMPLE); | ||
assert.equal(rename._addExtension('test'), 'test.js'); | ||
assert.equal(rename._addExtension('test.js'), 'test.js'); | ||
}); | ||
it('should create relative paths', function () { | ||
var rename = new Rename(SAMPLE); | ||
assert.equal(rename._relativeTo('/Home/folder'), '../../new.js'); | ||
assert.equal(rename._relativeTo('/Home'), '../new.js'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14333
16
286
151
4
1