forked from pattern-lab/patternlab-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopier.js
More file actions
133 lines (115 loc) · 3.52 KB
/
Copy pathcopier.js
File metadata and controls
133 lines (115 loc) · 3.52 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
const _ = require('lodash');
const path = require('path');
const process = require('process');
const copyFile = require('./copyFile');
const watchAssets = require('./watchAssets');
const watchPatternLabFiles = require('./watchPatternLabFiles');
const copier = () => {
const transform_paths = (directories) => {
//create array with all source keys minus our blacklist
const dirs = {};
const blackList = [
'root',
'patterns',
'data',
'meta',
'annotations',
'patternlabFiles',
'styleguide',
];
_.each(directories.source, (dir, key) => {
if (blackList.includes(key)) {
return;
}
if (!dirs.key) {
dirs[key] = {};
}
});
// loop through all source keys
_.each(dirs, (dir, key) => {
// add source key path
dirs[key].source = directories.source[key];
// add public key path
dirs[key].public = directories.public[key];
});
return dirs;
};
const copyAndWatch = (assetDirectories, patternlab, options) => {
//take our configured paths and sanitize best we can to only the assets
const dirs = transform_paths(assetDirectories);
//find out where we are
const basePath = path.resolve(process.cwd());
const copyOptions = {
overwrite: true,
emitter: patternlab.events,
debug: patternlab.config.logLevel === 'debug',
};
// Adding assets to filter for in case of transformedAssetTypes defined; adapted regex from https://stackoverflow.com/a/6745455
if (patternlab.config.transformedAssetTypes) {
copyOptions.filter = new RegExp(
`.*(?<}))$`,
'i'
);
}
//loop through each directory asset object (source / public pairing)
const copyPromises = [];
_.each(dirs, (dir, key) => {
//if we want to watch files, do so, otherwise just copy each file
if (options.watch) {
watchAssets(patternlab, basePath, dir, key, copyOptions);
} else {
//just copy
copyPromises.push(
_.map(patternlab.uikits, (uikit) => {
copyFile(
dir.source,
path.join(basePath, uikit.outputDir, dir.public),
copyOptions
);
})
);
}
});
// copy the styleguide
copyPromises.push(
_.map(patternlab.uikits, (uikit) => {
copyFile(
path.join(uikit.modulePath, assetDirectories.source.styleguide),
path.join(basePath, uikit.outputDir, assetDirectories.public.root),
copyOptions
);
})
);
// copy the favicon
copyPromises.push(
_.map(patternlab.uikits, (uikit) => {
copyFile(
`${assetDirectories.source.root}favicon.ico`,
path.join(
basePath,
uikit.outputDir,
`${assetDirectories.public.root}/favicon.ico`
),
copyOptions
);
})
);
return Promise.all(copyPromises).then(() => {
//we need to special case patterns/**/*.md|.json|.pattern-extensions as well as the global structures
if (options.watch) {
return watchPatternLabFiles(patternlab, assetDirectories, basePath);
}
return Promise.resolve();
});
};
return {
copyAndWatch: (assetDirectories, patternlab, options) => {
return copyAndWatch(assetDirectories, patternlab, options);
},
transformConfigPaths: (paths) => {
return transform_paths(paths);
},
};
};
module.exports = copier;