From 9f72ffa08f243438888a3559ee09805109095097 Mon Sep 17 00:00:00 2001 From: imcuttle Date: Tue, 14 Aug 2018 20:04:00 +0800 Subject: [PATCH] feat(edam): supports placeholder of dest in `move` and `copy` 11 --- .../edam/src/__tests__/FileProcessor.test.js | 45 +++++++++++++++++++ .../src/core/TreeProcessor/FileProcessor.ts | 29 ++++++++++-- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/packages/edam/src/__tests__/FileProcessor.test.js b/packages/edam/src/__tests__/FileProcessor.test.js index 3348fb9..7ff8560 100644 --- a/packages/edam/src/__tests__/FileProcessor.test.js +++ b/packages/edam/src/__tests__/FileProcessor.test.js @@ -148,6 +148,51 @@ describe('FileProcessor', function() { ) }) + it('should FileProcessor supports the special placeholder', function () { + fp.tree['root.js'] = { + output: 'im root.js output' + } + fp.move('root.*', '[path][new-root][ext]') + expect(Object.keys(fp.tree)).toEqual( + expect.arrayContaining([ + normalize('[new-root].txt'), + normalize('[new-root].js') + ]) + ) + + fp.tree['new.ts'] = { + output: 'im root.js output' + } + fp.move('*.ts', '[path][name].jsx') + expect(Object.keys(fp.tree)).toEqual( + expect.arrayContaining([ + normalize('new.jsx'), + normalize('pull/npm.ts') + ]) + ) + + fp.tree['newNew.ts'] = { + output: 'im root.js output' + } + fp.tree['a/b/c.ts'] = { + output: 'im root.js output' + } + fp.tree['a/b/c-1.ts'] = { + output: 'im root.js output' + } + fp.move('**/*.ts', '[path][name].jsx') + expect(Object.keys(fp.tree)).toEqual( + expect.arrayContaining([ + normalize('newNew.jsx'), + normalize('pull/npm.jsx'), + normalize('pull/file.jsx'), + normalize('pull/git.jsx'), + normalize('a/b/c.jsx'), + normalize('a/b/c-1.jsx') + ]) + ) + }) + // NOT Support now // it('should FileProcessor move works on directory', async function () { // fp.move('pull/', 'x') diff --git a/packages/edam/src/core/TreeProcessor/FileProcessor.ts b/packages/edam/src/core/TreeProcessor/FileProcessor.ts index bda9e71..65907c2 100644 --- a/packages/edam/src/core/TreeProcessor/FileProcessor.ts +++ b/packages/edam/src/core/TreeProcessor/FileProcessor.ts @@ -21,6 +21,7 @@ import * as nps from 'path' import fileSystem from '../../lib/fileSystem' import * as mm from 'micromatch' import DefaultLogger from '../DefaultLogger' +import { ParsedPath } from 'path' const debug = require('debug')('edam:FileProcessor') const tildify = require('tildify') @@ -127,17 +128,37 @@ export default class FileProcessor extends TreeProcessor { action: Function ): FileProcessor { debug('move input: %o, %s', m, dest) - toArray(m).forEach(eachm => { - const paths = this.match(eachm) + toArray(m).forEach(eachMatcher => { + const paths = this.match(eachMatcher) + // mv *.js [dir][name][ext] + if (/\[(root|path|name|ext)]/.test(dest)) { + paths.forEach(from => { + let pathObj = nps.posix.parse(from) + pathObj.path = pathObj.dir + if (pathObj.path !== '' && !pathObj.path.endsWith('/')) { + pathObj.path = pathObj.path + '/' + } + let realDest = dest.replace(/\[(root|path|name|ext)]/g, (_, $1) => { + if (typeof pathObj[$1] === 'string') { + return pathObj[$1] + } + return _ + }) + + action(from, realDest) + }) + return + } + // mv a.js b.js - if (paths.length === 1 && paths[0] === eachm) { + if (paths.length === 1 && paths[0] === eachMatcher) { action(paths[0], dest) } else { // implement simply, not robustly // a: mv a/* b // b: mv a/ b // https://www.npmjs.com/package/micromatch#parse - const { nodes } = mm.parse(eachm) + const { nodes } = mm.parse(eachMatcher) let basename = '' let hasGlobFlag = false if (nodes && nodes.length) {