-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (81 loc) · 2.17 KB
/
index.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
const tmp = require("tmp-promise")
const path = require("path")
const fs = require("fs")
const childProcess = require("child_process")
const rmfr = require("rmfr")
module.exports = async ({ packageNameOrPath, props }) => {
const { path: tmpDirPath, cleanup } = await tmp.dir()
childProcess.execSync("npm init -y", {
shell: true,
stdio: "inherit",
cwd: tmpDirPath,
})
// TODO windows support
let isPathToPackage = packageNameOrPath.includes("/")
if (isPathToPackage) {
packageNameOrPath = path.resolve(packageNameOrPath)
}
// TODO run the parcel from the react-install-render node_modules folder instead
// of reinstalling parcel on every run
childProcess.execSync(
`npm install --production react parcel react-test-renderer jsdom global-jsdom jsdom-worker node-fetch ${packageNameOrPath}`,
{
shell: true,
stdio: "inherit",
cwd: tmpDirPath,
}
)
let packageJSON
if (isPathToPackage) {
packageJSON = JSON.parse(
fs
.readFileSync(path.resolve(packageNameOrPath, "package.json"))
.toString()
)
} else {
packageJSON = JSON.parse(
fs
.readFileSync(
path.resolve(
tmpDirPath,
"node_modules",
packageNameOrPath,
"package.json"
)
)
.toString()
)
}
fs.writeFileSync(
path.resolve(tmpDirPath, "test.js"),
`
import TestRenderer from "react-test-renderer"
import TestComponent from "${packageJSON.name}"
window.runTest = () => {
const testRenderer = TestRenderer.create(TestComponent(${props}))
console.log(testRenderer.toJSON())
}
`.trim()
)
childProcess.execSync(`npx parcel build --no-minify ./test.js`, {
shell: true,
stdio: "inherit",
cwd: tmpDirPath,
})
fs.writeFileSync(
path.resolve(tmpDirPath, "run-packed-with-jsdom.js"),
`
require("global-jsdom")();
require("jsdom-worker");
require("./dist/test.js");
window.runTest();
`.trim()
)
console.log("running ", "node run-packed-with-jsdom.js")
childProcess.execSync(`node run-packed-with-jsdom.js`, {
shell: true,
stdio: "inherit",
cwd: tmpDirPath,
})
await rmfr(tmpDirPath)
}