From e491d2311e19553add148eea30f5415ad888fa99 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Mon, 10 Jun 2019 18:07:42 +0800 Subject: [PATCH] feat: add .rename method (#11) * add .rename * add semantic-release --- circle.yml | 5 ++++- docs/api.md | 4 ++++ src/index.js | 11 +++++++++++ test/fixture/rename/a.txt | 1 + test/index.test.js | 14 ++++++++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/fixture/rename/a.txt diff --git a/circle.yml b/circle.yml index 7aa2b49..ebb3ecc 100644 --- a/circle.yml +++ b/circle.yml @@ -20,4 +20,7 @@ jobs: - ./node_modules - run: name: test - command: yarn test \ No newline at end of file + command: yarn test + - run: + name: release + command: npx semantic-release diff --git a/docs/api.md b/docs/api.md index 11874b4..84b08d9 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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. diff --git a/src/index.js b/src/index.js index 4ce618c..86e88a7 100644 --- a/src/index.js +++ b/src/index.js @@ -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() diff --git a/test/fixture/rename/a.txt b/test/fixture/rename/a.txt new file mode 100644 index 0000000..7898192 --- /dev/null +++ b/test/fixture/rename/a.txt @@ -0,0 +1 @@ +a diff --git a/test/index.test.js b/test/index.test.js index 45fcbb9..5099c3a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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']) +})