Skip to content

Commit

Permalink
feat(edam): supports placeholder of dest in move and copy
Browse files Browse the repository at this point in the history
11
  • Loading branch information
imcuttle committed Aug 14, 2018
1 parent bf1cfdb commit 9f72ffa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
45 changes: 45 additions & 0 deletions packages/edam/src/__tests__/FileProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
29 changes: 25 additions & 4 deletions packages/edam/src/core/TreeProcessor/FileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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(<string>eachm)
toArray(m).forEach(eachMatcher => {
const paths = this.match(<string>eachMatcher)
// mv *.js [dir][name][ext]
if (/\[(root|path|name|ext)]/.test(dest)) {
paths.forEach(from => {
let pathObj = <ParsedPath & { path: string }>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) {
Expand Down

0 comments on commit 9f72ffa

Please sign in to comment.