/
copy.js
56 lines (43 loc) · 1.44 KB
/
copy.js
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
// @ts-check
const fs = require('fs');
const path = require('path');
const { series, resolveCwd, copyTask } = require('just-scripts');
function expandSourcePath(pattern) {
if (!pattern) {
return null;
}
// just returns the relative paths
if (pattern.startsWith('.')) {
return pattern;
}
// tries to resolve the packages, handling scoped packages
const splitPattern = pattern.split('/');
const packageName = pattern[0] == '@' ? `${splitPattern[0]}/${splitPattern[1]}` : splitPattern[0];
try {
const resolvedPackageJson = resolveCwd(`${packageName}/package.json`);
if (!resolvedPackageJson) {
// returns pattern if the packageName didn't contain a package.json (not really a package)
return pattern;
}
return pattern.replace(packageName, path.dirname(resolvedPackageJson));
} catch (e) {
console.error(e);
}
}
function copy() {
let tasks = [];
let configPath = path.resolve(process.cwd(), 'config/pre-copy.json');
if (!fs.existsSync(configPath)) {
return;
}
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
if (config && config.copyTo) {
for (let destination in config.copyTo) {
const sources = config.copyTo[destination];
destination = path.resolve(process.cwd(), destination);
tasks.push(copyTask(sources.map(src => expandSourcePath(src)), destination));
}
}
return series.apply(null, tasks);
}
module.exports = { expandSourcePath, copy };