forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(BuildTool): tester & rename2ts & tsdoc
- Loading branch information
Showing
12 changed files
with
269 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": ["../.eslintrc"], | ||
"env": { | ||
"node": true | ||
}, | ||
"rules": { | ||
"no-console": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('校验失败')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Oops, something went wrong.