Repository navigation
Expand file tree
/
Copy pathassembler.js
More file actions
89 lines (80 loc) · 2.47 KB
/
Copy pathassembler.js
File metadata and controls
89 lines (80 loc) · 2.47 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
/**
* @license
* Copyright 2018 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs');
const path = require('path');
/**
* Create a new directory and any necessary subdirectories
* if they do not exist.
*/
function mkdirp(p) {
if (!fs.existsSync(p)) {
mkdirp(path.dirname(p));
fs.mkdirSync(p);
}
}
function main(params) {
const outdir = params.shift();
const rootDirs = [];
while (params.length && params[0] !== '--assets') {
let r = params.shift();
if (!r.endsWith('/')) {
r += '/';
}
rootDirs.push(r);
}
// Always trim the longest prefix
rootDirs.sort((a, b) => b.length - a.length);
params.shift(); // --assets
function relative(execPath) {
if (execPath.startsWith('external/')) {
execPath = execPath.substring('external/'.length);
}
for (const r of rootDirs) {
if (execPath.startsWith(r)) {
return execPath.substring(r.length);
}
}
return execPath;
}
function copy(f) {
if (fs.statSync(f).isDirectory()) {
for (const file of fs.readdirSync(f)) {
copy(path.join(f, file));
}
} else {
const dest = path.join(outdir, relative(f));
mkdirp(path.dirname(dest));
fs.copyFileSync(f, dest);
}
}
// Remove duplicate files (which may come from this rule) from the
// list since fs.copyFileSync may fail with `EACCES: permission denied`
// as it will not have permission to overwrite duplicate files that were
// copied from within bazel-bin.
// See https://github.com/bazelbuild/rules_nodejs/pull/546.
for (const f of new Set(params)) {
copy(f);
}
return 0;
}
module.exports = {main};
if (require.main === module) {
// We always require the arguments are encoded into a flagfile
// so that we don't exhaust the command-line limit.
const params = fs.readFileSync(process.argv[2], {encoding: 'utf-8'}).split('\n').filter(l => !!l);
process.exitCode = main(params);
}