-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathmove.ts
39 lines (33 loc) · 907 Bytes
/
move.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { join, normalize } from '@angular-devkit/core';
import { Rule } from '../engine/interface';
import { noop } from './base';
export function move(from: string, to?: string): Rule {
if (to === undefined) {
to = from;
from = '/';
}
const fromPath = normalize('/' + from);
const toPath = normalize('/' + to);
if (fromPath === toPath) {
return noop;
}
return (tree) => {
if (tree.exists(fromPath)) {
// fromPath is a file
tree.rename(fromPath, toPath);
} else {
// fromPath is a directory
tree.getDir(fromPath).visit((path) => {
tree.rename(path, join(toPath, path.slice(fromPath.length)));
});
}
return tree;
};
}