Skip to content

Commit

Permalink
chore(BuildTool): tester & rename2ts & tsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
YSMJ1994 committed Dec 17, 2023
1 parent df966d2 commit a09b1d8
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 130 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"prettier",
"./a11y.eslintrc"
],
"plugins": ["@typescript-eslint"],
"plugins": ["@typescript-eslint", "eslint-plugin-tsdoc"],
"settings": {
"react": {
"pragma": "React",
Expand All @@ -25,6 +25,7 @@
}
],
"rules": {
"tsdoc/syntax": "error",
"valid-jsdoc": "off",
"max-statements": "off",
"max-len": "off",
Expand Down
21 changes: 17 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@
"pack": "node ./scripts/pack.js",
"pack-adaptor": "node ./scripts/adaptor/build.js",
"release": "node ./scripts/release/index.js",
"test:headed": "npx cypress open --component --browser chrome",
"test": "npx cypress run --component --browser chrome",
"test:v2": "node --max_old_space_size=8192 ./scripts/test/v2-index.js",
"test:a11y": "node --max_old_space_size=8192 ./scripts/test/a11y-index.js",
"order-var": "node ./scripts/order-var.js",
Expand All @@ -79,7 +77,11 @@
"precommit": "lint-staged",
"changelog": "node ./scripts/changelog.js",
"mix": "node ./scripts/mix.js",
"prepub": "npm run eslint && npm run stylelint && npm run changelog && npm run build && npm run check && npm run docs && npm run clear-dist && npm run pack && npm run pack -- minimize && npm run pack-adaptor"
"prepub": "npm run eslint && npm run stylelint && npm run changelog && npm run build && npm run check && npm run docs && npm run clear-dist && npm run pack && npm run pack -- minimize && npm run pack-adaptor",
"tool:rename2ts": "ts-node ./tools/rename2ts.ts",
"check:types": "ts-node ./tools/checkers/types/index.ts",
"test:head": "ts-node ./tools/test.ts --head",
"test": "ts-node ./tools/test.ts"
},
"lint-staged": {
"test/**/*": [
Expand All @@ -90,6 +92,10 @@
"prettier --write",
"git add"
],
"components/**/*.@(ts|tsx|css|scss|md)": [
"prettier --write",
"git add"
],
"@(src|scripts|docs/*/demo)/**/*.@(js|jsx)": [
"prettier --write"
],
Expand Down Expand Up @@ -136,12 +142,16 @@
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/plugin-syntax-jsx": "^7.12.1",
"@commitlint/cli": "^7.0.0",
"@microsoft/tsdoc": "^0.14.2",
"@octokit/rest": "^18.0.9",
"@types/enzyme": "^3.10.14",
"@types/fs-extra": "^5.1.0",
"@types/md5": "^2.3.5",
"@types/node": "^17.0.41",
"@types/react": "^16.14.37",
"@types/react-dom": "^16.9.24",
"@types/react-test-renderer": "^16.9.12",
"@types/yargs": "^17.0.32",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"@vitejs/plugin-react": "^4.2.1",
Expand Down Expand Up @@ -187,6 +197,7 @@
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-markdown": "^3.0.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-tsdoc": "^0.2.17",
"expect.js": "^0.3.1",
"extract-text-webpack-plugin": "^3.0.0",
"fast-sass-loader": "^2.0.0",
Expand Down Expand Up @@ -256,13 +267,15 @@
"stylelint-config-recommended": "^3.0.0",
"stylelint-csstree-validator": "^1.8.0",
"stylelint-scss": "^3.1.1",
"ts-node": "^10.9.2",
"tslint": "^5.18.0",
"typescript": "^5.3.2",
"urllib": "^2.36.1",
"vite": "^4.5.1",
"webpack": "^3.0.0",
"webpack-dev-server": "^2.x",
"whatwg-fetch": "^2.0.3"
"whatwg-fetch": "^2.0.3",
"yargs": "^17.7.2"
},
"peerDependencies": {
"@alifd/meet-react": "^2.0.0",
Expand Down
124 changes: 0 additions & 124 deletions test.mjs

This file was deleted.

9 changes: 9 additions & 0 deletions tools/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": ["../.eslintrc"],
"env": {
"node": true
},
"rules": {
"no-console": "off"
}
}
7 changes: 7 additions & 0 deletions tools/checkers/types/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["../../../tsconfig.json"],
"compilerOptions": {
"noEmit": true
},
"include": ["global.d.ts", "cypress/**/*.ts"]
}
52 changes: 52 additions & 0 deletions tools/checkers/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
------------------------------------------------------------
author: 珵之
create: 2023-12-17 16:43:00
description: 检测目标目录所有 ts 文件类型定义
example: ts-node ./tools/checkers/types/index.ts ./components/table button
------------------------------------------------------------
*/
import { writeFileSync, readFileSync, existsSync, writeJSONSync } from 'fs-extra';
import { resolve, join } from 'path';
import { spawnSync } from 'child_process';
import chalk from 'chalk';
import { beforeExit, cwd, targets } from '../../utils';

const tscPath = resolve(cwd, 'node_modules/.bin/tsc');
if (!existsSync(tscPath)) {
throw new Error('Not found tsc');
}

if (!targets.length) {
console.log(chalk.yellow('[Example]:'));
console.log(chalk.green('npm run check:types button table'));
console.log(chalk.green(`npm run check:types ./components/button ./components/table`));
console.log();
throw new Error('请指定一个合法目录');
}

const checkTsConfigPath = resolve(__dirname, 'config.json');

const include = targets.map(dir => [join(dir, '**/*.ts'), join(dir, '**/*.tsx')]).flat();
const checkTsConfigText = readFileSync(checkTsConfigPath, 'utf-8');

const checkTsConfig: { include: string[] } = JSON.parse(checkTsConfigText);

checkTsConfig.include = checkTsConfig.include.map(str => resolve(cwd, str)).concat(include);
writeJSONSync(checkTsConfigPath, checkTsConfig, { spaces: 4 });
const rollback = () => {
writeFileSync(checkTsConfigPath, checkTsConfigText, 'utf-8');
};

beforeExit(rollback);

const child = spawnSync(tscPath, ['-p', checkTsConfigPath], { cwd, stdio: 'inherit' });
rollback();

if (!child.status) {
if (child.status === 0) {
console.log(chalk.green('校验通过'));
}
} else {
console.log(chalk.red('校验失败'));
}
54 changes: 54 additions & 0 deletions tools/rename2ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
------------------------------------------------------------
author: 珵之
create: 2023-12-14 16:13:36
description: 将目标目录内的所有 js、jsx 文件转换为 ts、tsx 文件
example: ts-node ./tools/rename2ts.ts ./components/table
------------------------------------------------------------
*/
import { resolve, relative, extname } from 'path';
import { readdirSync, statSync, readFileSync, renameSync } from 'fs';
import chalk from 'chalk';
import { cwd, targets } from './utils';

if (!targets.length) {
throw new Error('请指定一个合法目录');
}

function renameJs2Ts(js: string) {
const text = readFileSync(js, 'utf-8');
if (/<[a-zA-Z][\s\S]*(>|\/>)/m.test(text)) {
// 文件内有 jsx 语法,转为 tsx
return js.replace(/\.(js|jsx|ts)$/, '.tsx');
} else {
return js.replace(/\.(?:j|t)s(x?)$/, (_, x) => {
return `.ts${x || ''}`;
});
}
}

async function renameDirs(files: string[]) {
for (const file of files) {
const stat = statSync(file);
if (stat.isDirectory()) {
const children = readdirSync(file).map(f => resolve(file, f));
await renameDirs(children);
} else if (stat.isFile()) {
const ext = extname(file);
if (['.js', '.jsx'].includes(ext)) {
const tsFile = renameJs2Ts(file);
renameSync(file, tsFile);
console.log(
chalk.green('[rename2ts]'),
relative(cwd, file),
' -> ',
relative(cwd, tsFile)
);
}
} else {
console.warn('不能处理的文件类型', file);
}
}
}

renameDirs(targets);
35 changes: 35 additions & 0 deletions tools/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
------------------------------------------------------------
author: 珵之
create: 2023-12-17 18:05:55
description: 执行测试
example:
test in head: ts-node ./tools/test.ts button --head
test in headless: ts-node ./tools/test.ts button ./components/affix
test all in headless: ts-node ./tools/test.ts
------------------------------------------------------------
*/

import { join, relative, resolve } from 'path';
import { spawnSync } from 'child_process';
import { existsSync } from 'fs-extra';
import { cwd, targets, argv } from './utils';

const cypressBin = resolve(cwd, 'node_modules/.bin/cypress');

if (!existsSync(cypressBin)) {
throw new Error('Not found cypress');
}

if (argv.head) {
spawnSync(cypressBin, ['open', '--component', '-b', 'chrome'], { cwd, stdio: 'inherit' });
} else {
const specArgs = targets.map(dir => [relative(cwd, join(dir, '__tests__/**/*'))]).flat();
if (specArgs.length) {
specArgs.unshift('-s');
}
spawnSync(cypressBin, ['run', '--component', '-b', 'chrome'].concat(specArgs), {
cwd,
stdio: 'inherit',
});
}
10 changes: 10 additions & 0 deletions tools/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"types": ["node"],
"target": "ESNext"
},
"include": ["./**/*.ts"]
}
Loading

0 comments on commit a09b1d8

Please sign in to comment.