Skip to content

Commit

Permalink
feat: add .rename method (#11)
Browse files Browse the repository at this point in the history
* add .rename

* add semantic-release
  • Loading branch information
egoist authored Jun 10, 2019
1 parent 02a8a50 commit e491d23
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ jobs:
- ./node_modules
- run:
name: test
command: yarn test
command: yarn test
- run:
name: release
command: npx semantic-release
4 changes: 4 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,7 @@ Write decoded contents to a file by given relative path.
## stream.fileStats(relative)

Get stats for a file by given relative path.

## stream.rename(fromPath, toPath)

Rename a file.
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ class Majo extends EventEmitter {
get fileList() {
return Object.keys(this.files).sort()
}

rename(fromPath, toPath) {
const file = this.files[fromPath]
this.createFile(toPath, {
path: path.resolve(this.baseDir, toPath),
stats: file.stats,
contents: file.contents
})
this.deleteFile(fromPath)
return this
}
}

const majo = () => new Majo()
Expand Down
1 change: 1 addition & 0 deletions test/fixture/rename/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
14 changes: 14 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,17 @@ test('stats', async () => {

expect(typeof stream.files['foo.md'].stats).toBe('object')
})

test('rename', async () => {
const stream = majo()

stream.source('**/*', { baseDir: path.join(__dirname, 'fixture/rename') })

stream.use(ctx => {
ctx.rename('a.txt', 'b/c.txt')
})

await stream.process()

expect(stream.fileList).toEqual(['b/c.txt'])
})

0 comments on commit e491d23

Please sign in to comment.