UNPKG

4.76 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const path = require('path')
3const util = require('util')
4const exec = require('child_process').exec
5const puppeteer = require('puppeteer')
6const npmRunScript = require('npm-run-script')
7const terminate = require('terminate')
8
9//
10
11const dirProjects = path.resolve(__dirname, '.projects')
12const project = {
13 name: 'koot-boilerplate',
14 github: 'cmux/koot-boilerplate',
15 type: [
16 'react-isomorphic',
17 'react-spa'
18 ]
19}
20
21//
22
23// const getPort = require('./utils/get-port')
24const getPathnameDevServerStart = require('./utils/get-pathname-dev-server-start')
25const checkFileChange = require('./libs/check-file-change')
26
27//
28
29global.kootTest = true
30process.env.KOOT_TEST_MODE = JSON.stringify(true)
31
32//
33
34const addCommand = async (name, command, dir) => {
35 const pathPackage = path.resolve(dir, 'package.json')
36 const p = await fs.readJson(pathPackage)
37 if (!p.scripts[name])
38 p.scripts[name] = command
39 await fs.writeJson(pathPackage, p, {
40 spaces: 4
41 })
42}
43
44const { name } = project
45const dir = path.resolve(dirProjects, name)
46
47const run = async () => {
48 // const commandName = `koot-buildtest-isomorphic-start-server`
49 // const command = `koot-start --no-build`
50 // await addCommand(commandName, command, dir)
51
52 const child = exec(
53 `npm run dev:no-open`,
54 {
55 cwd: dir,
56 // detached: true,
57 }
58 )
59 const errors = []
60
61 // child.stdin.pipe(process.stdin)
62 // child.stdout.pipe(process.stdout)
63 // child.stderr.pipe(process.stderr)
64 const port = await new Promise(resolve => {
65 let port
66 child.stdout.on('data', msg => {
67 console.log(msg)
68 try {
69 const obj = JSON.parse(msg)
70 if (obj['koot-test']) {
71 port = obj.port
72 }
73 } catch (e) { }
74
75 if (!port) {
76 // const matches = /port.*\[32m([0-9]+)/.exec(msg)
77 const matches = / on.*http:.*:([0-9]+)/.exec(msg)
78 if (Array.isArray(matches) && matches.length > 1) {
79 port = parseInt(matches[1])
80 }
81 }
82
83 if (port)
84 return resolve(port)
85 })
86 })
87 child.stderr.on('data', err => {
88 errors.push(err)
89 })
90
91 // console.log({
92 // port,
93 // errors,
94 // })
95
96 // child.on('message', (message, sendHandle) => {
97 // console.log(message, sendHandle)
98 // })
99 // child.on('error', (error) => {
100 // console.log(error)
101 // })
102 // child.on('close', (code, signal) => {
103 // console.log(code, signal)
104 // })
105
106 // console.log(process)
107 // setTimeout(() => {
108 // // console.log(process)
109 // process.kill('SIGINT')
110 // }, 5 * 1000)
111
112 // console.log(stdout)
113
114 const browser = await puppeteer.launch({
115 headless: false
116 })
117
118 // 访问首页
119 const page = await browser.newPage()
120 const res = await page.goto(`http://127.0.0.1:${port}`, {
121 waitUntil: 'networkidle0'
122 })
123 // const isOK = res.ok()
124 // const body = await res.text()
125 // console.log({
126 // url: res.url(),
127 // status: res.status(),
128 // ok: res.ok(),
129 // headers: res.headers(),
130 // body: await res.text()
131 // })
132 // const elTitle = await page.$('title')
133 const pageTitle = await page.evaluate(() => document.querySelector('title').innerText)
134 // const elApp = await page.evaluate(() => document.querySelector('#app'))
135 console.log('pageTitle', pageTitle)
136 // console.log(elApp)
137 // console.log(await page.$('#aapp'))
138 const localeId = await page.evaluate(() => document.querySelector('meta[name="koot-locale-id"]').getAttribute('content'))
139 console.log('localeId', localeId)
140
141 const linksToOtherLang = await page.$$eval(`link[rel="alternate"][hreflang][href]:not([hreflang="${localeId}"])`, els => (
142 Array.from(els).map(el => ({
143 lang: el.getAttribute('hreflang'),
144 href: el.getAttribute('href')
145 }))
146 ))
147 console.log(linksToOtherLang)
148 for (let { lang, href } of linksToOtherLang) {
149 await page.goto(href, {
150 waitUntil: 'networkidle0'
151 })
152 const localeId = await page.evaluate(() => document.querySelector('meta[name="koot-locale-id"]').getAttribute('content'))
153 console.log(lang, localeId)
154 }
155
156 await browser.close()
157
158 // expect(typeof stderr).toBe('string')
159 // expect(stderr).toBe('')
160
161 terminate(child.pid)
162}
163
164run()