You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.3 KiB
87 lines
2.3 KiB
4 years ago
|
/* eslint-disable no-await-in-loop, no-console */
|
||
2 years ago
|
import chalk from 'chalk';
|
||
|
import { spawn } from 'child_process';
|
||
|
import fs from 'fs-extra';
|
||
|
import { globSync } from 'glob';
|
||
|
import path from 'path';
|
||
4 years ago
|
|
||
|
(async () => {
|
||
|
console.time('Execution...');
|
||
|
|
||
2 years ago
|
const demoFiles = globSync(path.join(process.cwd(), 'components/**/demo/*.md'));
|
||
4 years ago
|
|
||
|
const tmpFolder = path.resolve('components', '~tmp');
|
||
|
await fs.remove(tmpFolder);
|
||
|
await fs.ensureDir(tmpFolder);
|
||
|
|
||
2 years ago
|
function getTypescriptDemo(content: string, demoPath: string) {
|
||
4 years ago
|
const lines = content.split(/[\n\r]/);
|
||
|
|
||
2 years ago
|
const tsxStartLine = lines.findIndex((line) =>
|
||
2 years ago
|
line.replace(/\s/g, '').toLowerCase().includes('```tsx'),
|
||
4 years ago
|
);
|
||
|
|
||
|
if (tsxStartLine < 0) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
const tsxEndLine = lines.findIndex(
|
||
|
(line, index) => index > tsxStartLine && line.trim() === '```',
|
||
|
);
|
||
|
|
||
|
let script = lines.slice(tsxStartLine + 1, tsxEndLine).join('\n');
|
||
|
|
||
3 years ago
|
// insert React
|
||
4 years ago
|
if (!script.includes('import React') && !script.includes('import * as React')) {
|
||
|
script = `import React from 'react';\n${script}`;
|
||
|
}
|
||
|
|
||
|
// Replace mountNode
|
||
|
script = script.replace('mountNode', `document.getElementById('#root')`);
|
||
|
|
||
|
// Replace antd
|
||
|
script = script.replace(`from 'antd'`, `from '..'`);
|
||
|
|
||
|
// Add path
|
||
|
script = `/* eslint-disabled */\n// ${demoPath}\n${script}`;
|
||
|
|
||
|
return script;
|
||
|
}
|
||
|
|
||
|
for (let i = 0; i < demoFiles.length; i += 1) {
|
||
|
const demoPath = demoFiles[i];
|
||
|
|
||
|
const content = await fs.readFile(demoPath, 'utf8');
|
||
|
const script = getTypescriptDemo(content, demoPath);
|
||
|
|
||
|
const dirs = path.dirname(demoPath).split(path.sep);
|
||
|
|
||
|
// Parse TSX
|
||
|
if (script) {
|
||
|
const tmpFile = path.join(
|
||
|
tmpFolder,
|
||
|
`${dirs[dirs.length - 2]}-${path.basename(demoPath).replace(/\..*/, '')}.tsx`,
|
||
|
);
|
||
|
await fs.writeFile(tmpFile, script, 'utf8');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const child = spawn('npm', ['run', 'tsc']);
|
||
|
|
||
|
child.stdout.pipe(process.stdout);
|
||
|
child.stderr.pipe(process.stderr);
|
||
|
|
||
2 years ago
|
child.on('exit', async (code: number) => {
|
||
4 years ago
|
console.timeEnd('Execution...');
|
||
|
|
||
|
if (code) {
|
||
|
console.log(chalk.red('💥 OPS! Seems some tsx demo not pass tsc...'));
|
||
|
} else {
|
||
|
await fs.remove(tmpFolder);
|
||
|
console.log(chalk.green('🤪 All tsx demo passed. Congratulations!'));
|
||
|
}
|
||
|
|
||
|
process.exit(code);
|
||
|
});
|
||
|
})();
|