forked from electrode-io/electrode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xclap.js
198 lines (177 loc) · 6.05 KB
/
xclap.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"use strict";
const assert = require("assert");
const xclap = require("xclap");
const xsh = require("xsh");
const shell = xsh.$;
const exec = xsh.exec;
const fs = require("fs");
const Path = require("path");
const yoTest = require("yeoman-test");
const _ = require("lodash");
const isWin32 = process.platform.startsWith("win32");
const packagesDir = Path.join(__dirname, "packages");
const pullLocalPackages = dir => {
dir = Path.isAbsolute(dir) ? dir : Path.join(__dirname, 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, packagesDir);
const appPkgFile = Path.join(dir, "package.json");
const appPkgData = fs.readFileSync(appPkgFile).toString();
const appPkg = JSON.parse(appPkgData);
const updateToLocalPkgs = (section, pkgs) => {
if (appPkg[section]) {
pkgs.forEach(pkg => {
if (appPkg[section][pkg]) {
_.set(appPkg, ["fyn", section, pkg], Path.join(localPackagesDir, pkg));
}
});
}
};
updateToLocalPkgs("dependencies", localPkgs);
updateToLocalPkgs("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);
const restore = () => {
if (appPkgData) {
const appPkgFile = Path.join(dir, "package.json");
fs.writeFileSync(appPkgFile, appPkgData);
}
};
const localClap = Path.join("node_modules", ".bin", "clap");
return exec({ cwd: dir }, `fyn --pg simple -q v i && ${localClap} ?fix-generator-eslint`)
.then(() => exec({ cwd: dir }, `npm test`))
.then(() => exec({ cwd: dir }, `${localClap} build`));
};
const testGenerator = (testDir, name, clean, runTest, prompts) => {
name = name || "test-app";
const yoApp = Path.join(packagesDir, "generator-electrode/generators/app/index.js");
const defaultPrompts = {
name,
description: "test test",
homepage: "http://test",
serverType: "HapiJS",
authorName: "John Smith",
authorEmail: "john@smith.com",
authorUrl: "http://www.test.com",
keywords: ["test", "electrode"],
pwa: true,
autoSsr: true,
createDirectory: false,
githubAccount: "test",
license: "Apache-2.0"
};
prompts = _.extend({}, defaultPrompts, prompts || {});
const testAppDir = Path.join(testDir, name);
const yoRun = yoTest.run(yoApp);
return (clean ? yoRun.inDir(testAppDir) : yoRun.cd(testAppDir))
.withOptions({
"skip-install": true
})
.withPrompts(prompts)
.then(() => {
return runTest ? runAppTest(testAppDir, true) : pullLocalPackages(testAppDir);
});
};
xclap.load({
".lerna.test": "~$lerna run test --ignore=electrode-webpack-reporter",
"test-reporter": {
task: () => {
return exec(true, "lerna updated")
.then(r => {
if (r.stdout.indexOf("electrode-webpack-reporter") >= 0) {
return `~$cd packages/electrode-webpack-reporter && fyn --pg none install && npm test`;
}
})
.catch(err => {
assert(
err.output.stderr.indexOf("lerna info No packages need updating") > 0,
".test-reporter: lerna updated failed without 'No packages need updating' message"
);
});
}
},
bootstrap: "~$fynpo",
test: ["bootstrap", ".lerna.test", "test-reporter", "build-test"],
"test-generator": [".test-generator --hapi"],
"gen-hapi-app": [".test-generator --hapi --no-test"],
"test-demo-component": [`~$cd samples/demo-component && fyn --pg none install && npm test`],
"test-boilerplate": [".test-boilerplate"],
"test-stylus-sample": [".test-stylus-sample"],
"update-changelog": ["~$node tools/update-changelog.js"],
"gitbook-serve": ["~$gitbook serve --no-watch --no-live"],
"build-test": {
desc: "Run CI test",
task: () => {
process.env.BUILD_TEST = "true";
process.env.NODE_PRESERVE_SYMLINKS = "1";
const tasks = ["test-boilerplate", "test-stylus-sample"];
let updated;
return exec("lerna updated")
.then(output => {
updated = output.stdout
.split("\n")
.filter(x => x.startsWith("- "))
.map(x => x.substr(2));
if (updated.indexOf("generator-electrode") >= 0) {
tasks.push("test-generator");
}
if (updated.indexOf("electrode-archetype-react-component") >= 0) {
tasks.push("test-demo-component");
}
return tasks;
})
.catch(err => {
if (err.output.stderr.indexOf("No packages need updating") < 0) {
throw err;
}
});
}
},
".test-boilerplate": {
desc: "Run tests for the boilerplage app universal-react-node",
task: () => {
return runAppTest(Path.join(__dirname, "samples/universal-react-node"));
}
},
".test-stylus-sample": {
desc: "Run tests for the boilerplage app stylus-sample",
task: () => {
return runAppTest(Path.join(__dirname, "samples/stylus-sample"));
}
},
"samples-local": {
desc: "modify all samples to pull electrode packages from local",
task: () => {
[
"electrode-demo-index",
"stylus-sample",
"universal-material-ui",
"universal-react-node"
].forEach(a => {
pullLocalPackages(Path.join(__dirname, "samples", a));
});
}
},
".test-generator": {
desc: "Run tests for the yeoman generators",
task: function() {
const hapiOnly = this.argv.indexOf("--hapi") >= 0;
const runTest = this.argv.indexOf("--no-test") < 0;
const testDir = Path.join(__dirname, "tmp");
return testGenerator(testDir, "hapi-app", true, runTest, { serverType: "HapiJS" }).then(
() =>
hapiOnly ||
testGenerator(testDir, "express-app", false, runTest, { serverType: "ExpressJS" })
);
}
}
});