Browse Source

chore: Migrate CJS to ESM (#42067)

* chore: Migrate CJS to ESM

* fix lint
pull/42070/head
lijianan 2 years ago
committed by GitHub
parent
commit
6dde4c4ebe
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      package.json
  2. 48
      scripts/check-site.ts

1
package.json

@ -177,6 +177,7 @@
"@testing-library/user-event": "^14.4.2", "@testing-library/user-event": "^14.4.2",
"@types/fs-extra": "^11.0.1", "@types/fs-extra": "^11.0.1",
"@types/gtag.js": "^0.0.12", "@types/gtag.js": "^0.0.12",
"@types/http-server": "^0.12.1",
"@types/inquirer": "^9.0.3", "@types/inquirer": "^9.0.3",
"@types/isomorphic-fetch": "^0.0.36", "@types/isomorphic-fetch": "^0.0.36",
"@types/jest": "^29.0.0", "@types/jest": "^29.0.0",

48
scripts/check-site.js → scripts/check-site.ts

@ -1,61 +1,52 @@
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */ /* eslint-disable no-restricted-syntax */
const fetch = require('isomorphic-fetch'); import cheerio from 'cheerio';
const { join } = require('path'); import { globSync } from 'glob';
const cheerio = require('cheerio'); import type http from 'http';
const glob = require('glob'); import { createServer } from 'http-server';
const uniq = require('lodash/uniq'); import type https from 'https';
const { createServer } = require('http-server'); import fetch from 'isomorphic-fetch';
import uniq from 'lodash/uniq';
import { join } from 'path';
const components = uniq( const components = uniq(
glob globSync('components/!(overview)/*.md', { cwd: join(process.cwd()), dot: false }).map((path) =>
.globSync('components/!(overview)/*.md', { path.replace(/(\/index)?((\.zh-cn)|(\.en-us))?\.md$/i, ''),
cwd: join(process.cwd()), ),
dot: false,
})
.map((path) => path.replace(/(\/index)?((\.zh-cn)|(\.en-us))?\.md$/i, '')),
); );
describe('site test', () => { describe('site test', () => {
let server; let server: http.Server | https.Server;
const port = 3000; const port = 3000;
const render = async (path) => { const render = async (path: string) => {
const resp = await fetch(`http://127.0.0.1:${port}${path}`).then(async (res) => { const resp = await fetch(`http://127.0.0.1:${port}${path}`).then(async (res) => {
const html = await res.text(); const html = await res.text();
const $ = cheerio.load(html, { decodeEntities: false, recognizeSelfClosing: true }); const $ = cheerio.load(html, { decodeEntities: false, recognizeSelfClosing: true });
return { return { html, status: res.status, $ };
html,
status: res.status,
$,
};
}); });
return resp; return resp;
}; };
const handleComponentName = (name) => { const handleComponentName = (name: string) => {
const componentName = name.split('/')[1]; const [, componentName] = name.split('/');
return componentName.toLowerCase().replace('-cn', '').replace('-', ''); return componentName.toLowerCase().replace('-cn', '').replace('-', '');
}; };
const expectComponent = async (component) => { const expectComponent = async (component: string) => {
const { status, $ } = await render(`/${component}/`); const { status, $ } = await render(`/${component}/`);
expect(status).toBe(200); expect(status).toBe(200);
expect($('h1').text().toLowerCase()).toMatch(handleComponentName(component)); expect($('h1').text().toLowerCase()).toMatch(handleComponentName(component));
}; };
beforeAll(() => { beforeAll(() => {
server = createServer({ server = createServer({ root: join(process.cwd(), '_site') });
root: join(process.cwd(), '_site'),
});
server.listen(port); server.listen(port);
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('site static server run: http://localhost:3000'); console.log('site static server run: http://localhost:3000');
}); });
afterAll(() => { afterAll(() => {
if (server) { server?.close();
server.close();
}
}); });
it('Basic Pages en', async () => { it('Basic Pages en', async () => {
@ -89,7 +80,6 @@ describe('site test', () => {
it(`Component ${component} zh Page`, async () => { it(`Component ${component} zh Page`, async () => {
await expectComponent(`${component}-cn`); await expectComponent(`${component}-cn`);
}); });
it(`Component ${component} en Page`, async () => { it(`Component ${component} en Page`, async () => {
await expectComponent(component); await expectComponent(component);
}); });
Loading…
Cancel
Save