1 | const fs = require('fs-extra')
|
2 | const path = require('path')
|
3 |
|
4 | const exec = require('child_process').exec
|
5 | const puppeteer = require('puppeteer')
|
6 |
|
7 | const terminate = require('terminate')
|
8 |
|
9 |
|
10 |
|
11 | const dirProjects = path.resolve(__dirname, '.projects')
|
12 | const project = {
|
13 | name: 'koot-boilerplate',
|
14 | github: 'cmux/koot-boilerplate',
|
15 | type: [
|
16 | 'react-isomorphic',
|
17 | 'react-spa'
|
18 | ]
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const checkFileChange = require('./libs/check-file-change')
|
26 |
|
27 |
|
28 |
|
29 | global.kootTest = true
|
30 | process.env.KOOT_TEST_MODE = JSON.stringify(true)
|
31 |
|
32 |
|
33 |
|
34 | const 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 |
|
44 | const { name } = project
|
45 | const dir = path.resolve(dirProjects, name)
|
46 |
|
47 | const run = async () => {
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | const child = exec(
|
53 | `npm run dev:no-open`,
|
54 | {
|
55 | cwd: dir,
|
56 |
|
57 | }
|
58 | )
|
59 | const errors = []
|
60 |
|
61 |
|
62 |
|
63 |
|
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 |
|
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 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
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 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | const pageTitle = await page.evaluate(() => document.querySelector('title').innerText)
|
134 |
|
135 | console.log('pageTitle', pageTitle)
|
136 |
|
137 |
|
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 |
|
159 |
|
160 |
|
161 | terminate(child.pid)
|
162 | }
|
163 |
|
164 | run()
|