@@ -5,44 +5,50 @@ namespace ecoAPM.StatiqPipelines;
5
5
6
6
public class CopyFromNPM : Pipeline
7
7
{
8
+ private const string NodeModulesDirectory = "node_modules/" ;
9
+ private Dictionary < string , ReadFiles > _files ;
10
+
8
11
/// <summary>
9
12
/// Copies specific files from a `node_modules` directory to the output
10
13
/// </summary>
11
14
/// <param name="paths">The file paths (relative to `node_modules`) to copy</param>
12
15
/// <param name="output">The path (relative to the output root) where the files will be copied</param>
16
+ /// <param name="flatten">Flatten all files into the <see cref="output">output</see> directory</param>
13
17
public CopyFromNPM ( IEnumerable < string > paths , string output = "lib" )
14
18
{
19
+ _files = paths . ToDictionary ( p => p , p => new ReadFiles ( npmPath ( p ) ) ) ;
20
+
15
21
Isolated = true ;
16
- InputModules = new ModuleList
17
- {
18
- new NodeRestore ( ) ,
19
- new ReadFiles ( npmPath ( paths ) )
20
- } ;
21
- ProcessModules = new ModuleList
22
- {
23
- CopyTo ( output )
24
- } ;
25
- OutputModules = new ModuleList
26
- {
27
- new WriteFiles ( )
28
- } ;
22
+ InputModules = new ModuleList { new NodeRestore ( ) } ;
23
+ InputModules . AddRange ( _files . Values ) ;
24
+
25
+ ProcessModules = new ModuleList { CopyTo ( output ) } ;
26
+
27
+ OutputModules = new ModuleList { new WriteFiles ( ) } ;
29
28
}
30
29
31
- private static IReadOnlyList < string > npmPath ( IEnumerable < string > paths )
32
- => paths . Select ( p => IExecutionContext . Current . FileSystem
33
- . GetRootDirectory ( "node_modules" ) . GetFile ( p )
34
- . Path . FullPath . Replace ( "\\ " , "/" ) )
35
- . ToArray ( ) ;
30
+ private static string npmPath ( string path )
31
+ => IExecutionContext . Current . FileSystem
32
+ . GetRootDirectory ( NodeModulesDirectory ) . GetFile ( path )
33
+ . Path . FullPath . Replace ( "\\ " , "/" ) ;
36
34
37
- private static SetDestination CopyTo ( string output )
35
+ private SetDestination CopyTo ( string output )
38
36
=> new ( SetPath ( output ) ) ;
39
37
40
- private static Config < NormalizedPath > SetPath ( string output )
38
+ private Config < NormalizedPath > SetPath ( string output )
41
39
=> Config . FromDocument ( d => NewPath ( output , d ) ) ;
42
40
43
- private static NormalizedPath NewPath ( string output , IDocument d )
41
+ private NormalizedPath NewPath ( string output , IDocument d )
44
42
=> new ( OutputPath ( output , d ) ) ;
45
43
46
- private static string OutputPath ( string output , IDocument d )
47
- => Path . Combine ( output , d . Source . FileName . ToString ( ) ) ;
44
+ private string OutputPath ( string output , IDocument d )
45
+ => Path . Combine ( output , RelativeOutputPath ( d ) ) ;
46
+
47
+ private string RelativeOutputPath ( IDocument d )
48
+ => _files . ContainsKey ( RelativePath ( d ) )
49
+ ? d . Source . FileName . ToString ( )
50
+ : RelativePath ( d ) ;
51
+
52
+ private static string RelativePath ( IDocument d )
53
+ => d . Source . RootRelative . ToString ( ) . RemoveStart ( NodeModulesDirectory ) ;
48
54
}
0 commit comments