-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
135 lines (116 loc) · 3.54 KB
/
index.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
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
134
135
"use strict";
const { Reporter } = require("@parcel/plugin");
const fs = require("fs");
const path = require("path");
const PACKAGE_JSON_SECTION = "staticFiles";
const staticCopyPlugin = new Reporter({
async report({ event, options }) {
if (event.type === "buildSuccess") {
const projectRoot = findProjectRoot(event, options);
const configs = getSettings(projectRoot);
// Get all dist dir from targets, we'll copy static files into them
const targets = Array.from(
new Set(
event.bundleGraph
.getBundles()
.filter((b) => b.target && b.target.distDir)
.map((b) => b.target.distDir)
)
);
for (var config of configs) {
let distPaths = config.distDir ? [config.distDir] : targets;
if (config.env) {
if (!doesEnvironmentVarsMatches(config.env)) {
continue;
}
}
if (config.staticOutPath) {
distPaths = distPaths.map((p) => path.join(p, config.staticOutPath));
}
let staticPath = config.staticPath || path.join(projectRoot, "static");
let fn = fs.statSync(staticPath).isDirectory() ? copyDir : copyFile;
for (let distPath of distPaths) {
fn(staticPath, distPath);
}
}
}
},
});
const copyFile = (copyFrom, copyTo) => {
if (!fs.existsSync(copyTo)) {
fs.mkdirSync(copyTo, { recursive: true });
}
fs.copyFileSync(copyFrom, path.join(copyTo, path.basename(copyFrom)));
};
const copyDir = (copyFrom, copyTo) => {
if (!fs.existsSync(copyTo)) {
fs.mkdirSync(copyTo, { recursive: true });
}
const copy = (filepath, relative, filename) => {
const dest = path.join(copyTo, relative);
if (!filename) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
} else {
fs.copyFileSync(filepath, dest);
}
};
recurseSync(copyFrom, copy);
};
/**
* Recurse into directory and execute callback function for each file and folder.
*
* Based on https://github.com/douzi8/file-system/blob/master/file-system.js#L254
*
* @param dirpath directory to start from
* @param callback function to be run on every file/directory
*/
const recurseSync = (dirpath, callback) => {
const rootpath = dirpath;
function recurse(dirpath) {
fs.readdirSync(dirpath).forEach(function (filename) {
const filepath = path.join(dirpath, filename);
const stats = fs.statSync(filepath);
const relative = path.relative(rootpath, filepath);
if (stats.isDirectory()) {
callback(filepath, relative);
recurse(filepath);
} else {
callback(filepath, relative, filename);
}
});
}
recurse(dirpath);
};
const findProjectRoot = (event, options) => {
if (options.env["npm_package_json"]) {
return path.dirname(options.env["npm_package_json"]);
}
if (options.env["PNPM_SCRIPT_SRC_DIR"]) {
return options.env["PNPM_SCRIPT_SRC_DIR"];
}
return options.projectRoot;
}
const getSettings = (projectRoot) => {
let packageJson = JSON.parse(
fs.readFileSync(path.join(projectRoot, "package.json"))
);
var section = packageJson[PACKAGE_JSON_SECTION];
if (Array.isArray(section)) {
return section;
} else {
return [Object.assign({}, section)];
}
};
const doesEnvironmentVarsMatches = (envVars) => {
var allMatches = true;
for (var envVarName in envVars) {
if (process.env[envVarName] !== envVars[envVarName]) {
allMatches = false;
break;
}
}
return allMatches;
}
exports.default = staticCopyPlugin;