1 | const yargs = require("yargs");
|
2 | const childProcess = require("child_process");
|
3 | const fs = require("fs-extra");
|
4 | const path = require("path");
|
5 |
|
6 | const otherOptionKeys = Object.keys(yargs.argv).filter(item => !["_", "config", "package"].includes(item) && !item.startsWith("$"));
|
7 | const otherOptions = {};
|
8 | for (const key of otherOptionKeys) {
|
9 | otherOptions[key] = yargs.argv[key];
|
10 | }
|
11 |
|
12 | const cmdOptions = yargs.argv["_"];
|
13 |
|
14 | const projectConfigPath = yargs.argv["config"] || "simplism.json";
|
15 | const projectConfig = fs.readJsonSync(projectConfigPath);
|
16 |
|
17 | const runAsync = async packageName => {
|
18 | const testConfig = projectConfig.tests.find(item => item.name === packageName);
|
19 | if (!testConfig) {
|
20 | throw new Error(`테스트 패키지 ${packageName}에 대한 테스트 설정이 없습니다.`);
|
21 | }
|
22 |
|
23 | const shimRequireArg = testConfig.angular ? [`--require`, `"${path.resolve(__dirname, "../loaders/test-angular.shim.js")}"`] :
|
24 | testConfig.jsdom ? [`--require`, `"${path.resolve(__dirname, "../loaders/test-jsdom.shim.js")}"`]
|
25 | : [];
|
26 |
|
27 | const requireOrWebpackConfig = testConfig.angular ? [
|
28 | "--webpack-config", `"${path.resolve(__dirname, "../loaders/test-angular-webpack.config.js")}"`,
|
29 | "--webpack-env.packageName", `"${packageName}"`
|
30 | ]
|
31 | : [
|
32 | `--require`, `ts-node/register`,
|
33 | `--require`, `tsconfig-paths/register`
|
34 | ];
|
35 |
|
36 | childProcess.spawnSync(
|
37 | testConfig.angular ? path.normalize(`node_modules/.bin/mocha-webpack`) : path.normalize(`node_modules/.bin/mocha`),
|
38 | [
|
39 | ...requireOrWebpackConfig,
|
40 | ...shimRequireArg,
|
41 | `--timeout`, `10000`,
|
42 | ...Object.keys(otherOptions).map(key => [`--${key}`, otherOptions[key]]).reduce((a, b) => a.concat(b)),
|
43 | ...cmdOptions
|
44 | ],
|
45 | {
|
46 | shell: true,
|
47 | stdio: "inherit",
|
48 | env: {
|
49 | ...process.env,
|
50 | TS_NODE_PROJECT: `tests/${packageName}/tsconfig.json`,
|
51 | NODE_ENV: "test",
|
52 | JSDOM_CONFIG: JSON.stringify(testConfig.jsdom || testConfig.angular)
|
53 | }
|
54 | }
|
55 | )
|
56 | };
|
57 |
|
58 | if (!yargs.argv.package) {
|
59 | for (const testConfig of projectConfig.tests) {
|
60 | runAsync(testConfig.name);
|
61 | }
|
62 | }
|
63 | else {
|
64 | for (const pack of yargs.argv.package.split(",")) {
|
65 | runAsync(pack.trim());
|
66 | }
|
67 | } |
\ | No newline at end of file |