Skip to content

Commit 6a1af89

Browse files
authored
Use a custom script to copy resources (element-hq#2893)
All of those cpx invocations were getting unwieldy, and I suspect the exotic quoting needed to run them under parallelshell was breaking things on windows. Replace the unwieldy cpx invocations with an unwieldy custom script.
1 parent b0319ec commit 6a1af89

2 files changed

Lines changed: 90 additions & 11 deletions

File tree

‎package.json‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,22 @@
2727
"matrix-react-parent": "matrix-react-sdk",
2828
"scripts": {
2929
"reskindex": "reskindex -h src/header",
30-
"build:res": "cpx \"{src/skins/vector/fonts,src/skins/vector/img}/**\" webapp/ && cpx \"{res/media,res/vector-icons}/**\" webapp/",
31-
"build:config": "cpx config.json webapp/",
32-
"build:emojione": "cpx \"node_modules/emojione/assets/svg/*\" webapp/emojione/svg/",
30+
"build:res": "node scripts/copy-res.js",
3331
"build:modernizr": "modernizr -c .modernizr.json -d src/vector/modernizr.js",
3432
"build:css": "mkdirp build && catw \"src/skins/vector/css/**/*.css\" -o build/components.css --no-watch",
3533
"build:compile": "babel --source-maps -d lib src",
3634
"build:bundle": "NODE_ENV=production webpack -p --progress",
3735
"build:bundle:dev": "webpack --optimize-occurence-order --progress",
3836
"build:electron": "npm run clean && npm run build && build -wml --ia32 --x64",
39-
"build": "node scripts/babelcheck.js && npm run build:res && npm run build:config && npm run build:emojione && npm run build:css && npm run build:bundle",
40-
"build:dev": "node scripts/babelcheck.js && npm run build:res && npm run build:config && npm run build:emojione && npm run build:css && npm run build:bundle:dev",
37+
"build": "node scripts/babelcheck.js && npm run build:res && npm run build:css && npm run build:bundle",
38+
"build:dev": "node scripts/babelcheck.js && npm run build:res && npm run build:css && npm run build:bundle:dev",
4139
"dist": "scripts/package.sh",
42-
"start:res": "parallelshell \"cpx -w \\\"{src/skins/vector/fonts,src/skins/vector/img}/**\\\" webapp/\" \"cpx -w \\\"{res/media,res/vector-icons}/**\\\" webapp/\"",
43-
"start:config": "cpx -w config.json webapp/",
44-
"start:emojione": "cpx \"node_modules/emojione/assets/svg/*\" webapp/emojione/svg/ -w",
40+
"start:res": "node scripts/copy-res.js -w",
4541
"start:js": "webpack-dev-server -w --progress",
4642
"start:js:prod": "NODE_ENV=production webpack-dev-server -w --progress",
47-
"start:skins:css": "mkdirp build && catw \"src/skins/vector/css/**/*.css\" -o build/components.css",
48-
"start": "node scripts/babelcheck.js && parallelshell \"npm run start:emojione\" \"npm run start:res\" \"npm run start:config\" \"npm run start:js\" \"npm run start:skins:css\"",
49-
"start:prod": "parallelshell \"npm run start:emojione\" \"npm run start:js:prod\" \"npm run start:skins:css\"",
43+
"start:css": "mkdirp build && catw \"src/skins/vector/css/**/*.css\" -o build/components.css",
44+
"start": "node scripts/babelcheck.js && parallelshell \"npm run start:res\" \"npm run start:js\" \"npm run start:css\"",
45+
"start:prod": "parallelshell \"npm run start:res\" \"npm run start:js:prod\" \"npm run start:css\"",
5046
"clean": "rimraf build lib webapp electron/dist",
5147
"prepublish": "npm run build:compile",
5248
"test": "karma start --single-run=true --autoWatch=false --browsers PhantomJS --colors=false",
@@ -95,6 +91,7 @@
9591
"babel-preset-react": "^6.16.0",
9692
"babel-preset-stage-2": "^6.17.0",
9793
"catw": "^1.0.1",
94+
"chokidar": "^1.6.1",
9895
"cpx": "^1.3.2",
9996
"css-raw-loader": "^0.1.1",
10097
"electron-builder": "^10.4.1",
@@ -111,6 +108,7 @@
111108
"karma-phantomjs-launcher": "^1.0.0",
112109
"karma-sourcemap-loader": "^0.3.7",
113110
"karma-webpack": "^1.7.0",
111+
"minimist": "^1.2.0",
114112
"mkdirp": "^0.5.1",
115113
"mocha": "^2.4.5",
116114
"parallelshell": "^1.2.0",

‎scripts/copy-res.js‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
3+
// copies the resources into the webapp directory.
4+
//
5+
6+
// cpx includes globbed parts of the filename in the destination, but excludes
7+
// common parents. Hence, "res/{a,b}/**": the output will be "dest/a/..." and
8+
// "dest/b/...".
9+
const COPY_LIST = [
10+
["res/{media,vector-icons}/**", "webapp"],
11+
["src/skins/vector/{fonts,img}/**", "webapp"],
12+
["node_modules/emojione/assets/svg/*", "webapp/emojione/svg/"],
13+
["./config.json", "webapp", {directwatch: 1}],
14+
];
15+
16+
const parseArgs = require('minimist');
17+
const Cpx = require('cpx');
18+
const chokidar = require('chokidar');
19+
20+
const argv = parseArgs(
21+
process.argv.slice(2), {}
22+
);
23+
24+
var watch = argv.w;
25+
var verbose = argv.v;
26+
27+
function errCheck(err) {
28+
if (err) {
29+
console.error(err.message);
30+
process.exit(1);
31+
}
32+
}
33+
34+
function next(i, err) {
35+
errCheck(err);
36+
37+
if (i >= COPY_LIST.length) {
38+
return;
39+
}
40+
41+
const ent = COPY_LIST[i];
42+
const source = ent[0];
43+
const dest = ent[1];
44+
const opts = ent[2] || {};
45+
46+
const cpx = new Cpx.Cpx(source, dest);
47+
48+
if (verbose) {
49+
cpx.on("copy", (event) => {
50+
console.log(`Copied: ${event.srcPath} --> ${event.dstPath}`);
51+
});
52+
cpx.on("remove", (event) => {
53+
console.log(`Removed: ${event.path}`);
54+
});
55+
}
56+
57+
const cb = (err) => {next(i+1, err)};
58+
59+
if (watch) {
60+
if (opts.directwatch) {
61+
// cpx -w creates a watcher for the parent of any files specified,
62+
// which in the case of config.json is '.', which inevitably takes
63+
// ages to crawl. So we create our own watcher on the files
64+
// instead.
65+
const copy = () => {cpx.copy(errCheck)};
66+
chokidar.watch(source)
67+
.on('add', copy)
68+
.on('change', copy)
69+
.on('ready', cb)
70+
.on('error', errCheck);
71+
} else {
72+
cpx.on('watch-ready', cb);
73+
cpx.on("watch-error", cb);
74+
cpx.watch();
75+
}
76+
} else {
77+
cpx.copy(cb);
78+
}
79+
}
80+
81+
next(0);

0 commit comments

Comments
 (0)