forked from electrode-io/electrode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
134 lines (118 loc) · 3.9 KB
/
gulpfile.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
"use strict";
const gulp = require("gulp");
const helper = require("electrode-gulp-helper");
const shell = helper.shell;
const exec = helper.exec;
const fs = require("fs");
const path = require("path");
const yoTest = require("yeoman-test");
const _ = require("lodash");
if (!process.env.PACKAGES_DIR) {
process.env.PACKAGES_DIR = path.resolve("packages");
}
const pullLocalPackages = (dir) => {
dir = path.resolve(dir);
const localPkgs = ["electrode-archetype-react-app", "electrode-react-webapp", "electrode-redux-router-engine", "electrode-auto-ssr"];
const localDevPkgs = ["electrode-archetype-react-app-dev"];
const localPackagesDir = path.relative(dir, process.env.PACKAGES_DIR);
const updateToLocalPkgs = (pkgSection, pkgs) => {
if (pkgSection) {
pkgs.forEach((pkg) => {
if (pkgSection[pkg]) {
pkgSection[pkg] = path.join(localPackagesDir, pkg);
}
});
}
};
const appPkgFile = path.join(dir, "package.json");
const appPkgData = fs.readFileSync(appPkgFile).toString();
const appPkg = JSON.parse(appPkgData);
updateToLocalPkgs(appPkg["dependencies"], localPkgs);
updateToLocalPkgs(appPkg["devDependencies"], localDevPkgs);
fs.writeFileSync(appPkgFile, `${JSON.stringify(appPkg, null, 2)}\n`);
return appPkgData;
};
const runAppTest = (dir, forceLocal) => {
const appPkgData = (forceLocal || process.env.BUILD_TEST || process.env.CI) && pullLocalPackages(dir);
shell.pushd(dir);
const restore = () => {
shell.popd();
if (appPkgData) {
const appPkgFile = path.resolve(dir, "package.json");
fs.writeFileSync(appPkgFile, appPkgData);
}
};
return exec(`npm i`)
.then(() => exec(`npm test`))
.then(restore)
.catch((err) => {
restore();
throw err;
});
};
const testGenerator = (testDir, clean, prompts) => {
const yoApp = path.join(process.env.PACKAGES_DIR, ("generator-electrode/generators/app/index.js"));
const defaultPrompts = {
name: "test-app",
description: "test test",
homepage: "http://test",
serverType: "ExpressJS",
authorName: "John Smith",
authorEmail: "[email protected]",
authorUrl: "http://www.test.com",
keywords: ["test", "electrode"],
pwa: true,
autoSsr: true,
createDirectory: true,
githubAccount: "test",
license: "Apache-2.0"
};
prompts = _.extend({}, defaultPrompts, prompts || {});
const yoRun = yoTest.run(yoApp);
return (clean ? yoRun.inDir(testDir) : yoRun.cd(testDir))
.withOptions({
"skip-install": true
})
.withPrompts(prompts)
.then(() => runAppTest(path.join(testDir, "test-app"), true));
};
helper.loadTasks({
"build-test": {
task: () => {
process.env.BUILD_TEST = "true";
let updated;
return exec("lerna updated")
.then((output) => {
updated = output.stdout.split("\n").filter((x) => x.startsWith("- ")).map((x) => x.substr(2));
})
.then(() => {
if (updated.indexOf("generator-electrode") >= 0) {
return exec("gulp test-generator");
}
})
.then(() => exec("gulp test-boilerplate"));
}
},
"test-boilerplate": {
task: () => runAppTest(path.resolve("samples/universal-react-node"))
},
"samples-local": {
desc: "modify all samples to pull electrode packages from local",
task: () => {
["electrode-demo-index", "universal-material-ui", "universal-react-node"].forEach((a) => {
pullLocalPackages(path.resolve("samples", a));
});
}
},
"test-generator": {
task: () => {
const testDir = path.resolve("tmp");
return testGenerator(testDir, true, {serverType: "ExpressJS"})
.then(() => {
const appFiles = ["package.json", "client", "config", "server", "test"];
shell.rm("-rf", appFiles.map((x) => path.join(testDir, "test-app", x)));
})
.then(() => testGenerator(testDir, false, {serverType: "HapiJS"}));
}
}
}, gulp);