UNPKG

1.8 kBPlain TextView Raw
1import { resolve as resolvePath, dirname, basename } from 'path';
2import { IPostInstallCopy } from './types';
3import getFolderContent from 'vamtiger-get-directory-content';
4import copy from 'vamtiger-copy-file';
5import getPathData from 'vamtiger-get-path-data';
6
7export default async function ({workingDirectory, sourceFolder: currentSourceFolder, pattern}: IPostInstallCopy) {
8 const sourceFileName = new RegExp(pattern, 'i');
9 const mainPath = require.resolve(workingDirectory);
10 const sourcePath = require.resolve(currentSourceFolder);
11 const [mainPathData, sourcePathData] = await Promise.all([
12 getPathData(mainPath),
13 getPathData(sourcePath),
14 ]);
15 const mainFolder = mainPathData.isDirectory() && mainPath || dirname(mainPath);
16 const sourceFolder = sourcePathData.isDirectory() && sourcePath || dirname(sourcePath);
17 const sourceFolderEntryPaths = await getFolderContent(sourceFolder)
18 .then(entries => entries.map(entry => resolvePath(sourceFolder, entry)));
19 const sourceFolderEntryPathData = await Promise.all(sourceFolderEntryPaths.map(async sourceFolderEntryPath => ({
20 sourceFolderEntryPath,
21 pathData: await getPathData(sourceFolderEntryPath)
22 })));
23 const sourceFolderEntryFiles = sourceFolderEntryPathData
24 .map(({sourceFolderEntryPath, pathData}) => pathData.isFile() && basename(sourceFolderEntryPath) || '')
25 .filter(sourceFolderEntryFile => sourceFolderEntryFile && sourceFolderEntryFile.match(sourceFileName));
26 const copyParams = sourceFolderEntryFiles.map(sourceFolderEntryFile => ({
27 source: resolvePath(sourceFolder, sourceFolderEntryFile),
28 destination: resolvePath(mainFolder, sourceFolderEntryFile)
29 }));
30
31 await Promise.all(copyParams.map(copy));
32
33 return true;
34}
\No newline at end of file