Skip to content

Commit

Permalink
chore(BuildTool): adjust tools structure
Browse files Browse the repository at this point in the history
  • Loading branch information
YSMJ1994 committed Dec 20, 2023
1 parent 6557275 commit ce8e3be
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .stylelintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": ["@alifd/stylelint-config-next"],
"ignoreFiles": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"],
"ignoreFiles": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx", "**/__docs__/**"],
"rules": {
"max-nesting-depth": 4,
"max-empty-lines": 3,
Expand Down
2 changes: 2 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ declare module 'enzyme-adapter-react-16';
declare module 'es6-promise-polyfill' {
export { Promise };
}
declare module 'conventional-changelog';
declare module 'conventional-changelog-alifd';

declare const mountNode: HTMLDivElement;
42 changes: 13 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,23 @@
"*.css"
],
"scripts": {
"report-coverage": "codecov",
"preview": "node ./scripts/preview/index.js",
"dev": "node ./scripts/server/index.js",
"build": "node ./scripts/build/index.js",
"db-build": "node --inspect=9229 ./scripts/build/index.js",
"check": "node ./scripts/check/index.js",
"create": "node ./scripts/create-new.js",
"check-sass": "node ./scripts/check/sass.js",
"docs": "node ./scripts/docs/index.js",
"clear-dist": "node ./scripts/clear-dist.js",
"api": "node ./scripts/api.js",
"pack": "node ./scripts/pack.js",
"pack-adaptor": "node ./scripts/adaptor/build.js",
"release": "node ./scripts/release/index.js",
"test:js": "node --max_old_space_size=8192 ./scripts/test/index.js",
"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",
"eslint": "eslint '@(src|scripts)/**/*.@(js|jsx)' && eslint --fix 'docs/**/@(demo|theme)/*.@(md)'",
"tslint": "tsc",
"prettierjs": "prettier --write '@(src|test|scripts)/**/*.@(js|jsx)'",
"stylelint": "stylelint --fix 'src/**/*.@(css|scss)'",
"commitmsg": "commitlint -E GIT_PARAMS",
"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",
"release": "tsnode ./tools/release.ts",
"release:check": "npm run check:eslint ./components",
"release:check:eslint": "npm run check:eslint ./components",
"release:check:stylelint": "npm run check:stylelint ./components",
"release:changelog": "ts-node ./tools/changelog.ts",
"tool:rename2ts": "ts-node ./tools/rename2ts.ts",
"check:types": "ts-node ./tools/checkers/types/index.ts",
"check:eslint": "ts-node ./tools/checkers/eslint.ts",
"check:all": "npm run check:types && npm run check:eslint",
"check:stylelint": "ts-node ./tools/checkers/stylelint.ts",
"check:all": "npm run check:types ./components && npm run check:eslint ./components && npm run check:stylelint ./components",
"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",
"test:js": "node --max_old_space_size=8192 ./scripts/test/index.js",
"test:head": "ts-node ./tools/test.ts --head",
"test": "ts-node ./tools/test.ts"
"test": "ts-node ./tools/test.ts",
"commitmsg": "commitlint -E GIT_PARAMS",
"precommit": "lint-staged"
},
"lint-staged": {
"test/**/*": [
Expand Down
4 changes: 1 addition & 3 deletions tools/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
"env": {
"node": true
},
"rules": {
"no-console": "off"
}
"rules": {}
}
105 changes: 105 additions & 0 deletions tools/build/tools/changelog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import * as path from 'path';
import * as fs from 'fs-extra';
import * as semver from 'semver';
import * as inquirer from 'inquirer';
import conventionalChangelog from 'conventional-changelog';
import config from 'conventional-changelog-alifd';
import { CWD, log, querySync, warn } from '../../utils';

const changelogPath = 'CHANGELOG.md';
const latestedLogPath = 'LATESTLOG.md';

function updateVersion(version: string, type = 'z', addend = 1) {
if (!semver.valid(version)) {
return version;
}

const versionArr: Array<string | number> = version.split('.');

switch (type) {
case 'x':
versionArr[2] = 0;
versionArr[1] = 0;
versionArr[0] = parseInt(versionArr[0] as string) + 1;
break;
case 'y':
versionArr[2] = 0;
versionArr[1] = parseInt(versionArr[1] as string) + 1;
break;
default:
versionArr[2] = parseInt(versionArr[2] as string) + addend;
}

return versionArr.join('.');
}

export async function changelog() {
const packagePath = path.resolve('package.json');

const packageInfo = fs.readJSONSync(packagePath);

// const npmInfo = yield getRemotePkgInfo();

const npmVersion = querySync('npm', ['show', packageInfo.name, 'version']);
log(`[提示] [local:${packageInfo.version}] [npm:${npmVersion}] 请为本次提交指定新的版本号:`);

const current = await inquirer.prompt([
{
name: 'version',
type: 'input',
default: updateVersion(packageInfo.version, 'z'),
message: '请输入待发布的版本号:',
validate: function (value) {
if (!semver.valid(value) || semver.lte(value, npmVersion)) {
warn('请输入正确的版本号,并且大于基线版本号!');
return false;
}
return true;
},
},
]);

packageInfo.version = current.version;

await fs.writeJson(packagePath, packageInfo, { spaces: 2 });

log(`[提示] 回写版本号 ${packageInfo.version} 到 package.json success`);

log(`正在生成 ${changelogPath} 文件,请稍等几秒钟...`);

conventionalChangelog({
config,
}).on('data', (chunk: Buffer) => {
const log = chunk.toString().replace(/(\n## [.\d\w]+ )\(([\d-]+)\)\n/g, (all, s1, s2) => {
return `${s1}/ ${s2}\n`;
});

let changelogContent = fs.readFileSync(changelogPath, 'utf8');
changelogContent = changelogContent.split('\n').slice(1).join('\n');
fs.writeFileSync(changelogPath, `# Change Log \n\n${log}${changelogContent}`);

const lines = log.split(/\n/g);
let firstIndex = -1,
secondIndex = -1;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (/^##? \[/.test(line)) {
if (firstIndex === -1) {
firstIndex = i;
} else if (secondIndex === -1) {
secondIndex = i;
} else {
break;
}
}
}

if (firstIndex > -1) {
secondIndex = secondIndex === -1 ? lines.length : secondIndex;
const latestedLog = lines.slice(firstIndex, secondIndex - 1).join('\n');
fs.writeFileSync(latestedLogPath, `# Latest Log \n\n${latestedLog}`);
}
});

log(`成功将 ${changelogPath} 文件生成到 ${CWD} 目录下`);
}
10 changes: 10 additions & 0 deletions tools/changelog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
------------------------------------------------------------
author: 珵之
create: 2023-12-19 16:33:40
description: 生成 changelog 信息
------------------------------------------------------------
*/
import { changelog } from './build/tools/changelog';

changelog();
47 changes: 26 additions & 21 deletions tools/checkers/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,41 @@
author: 珵之
create: 2023-12-18 14:19:12
description: 执行 eslint 检查
example:
npm run check:eslint button
npm run check:eslint ./components/button
npm run check:eslint button --fix
------------------------------------------------------------
*/
import { resolve, join } from 'path';
import { existsSync } from 'fs-extra';
import { join } from 'path';
import chalk from 'chalk';
import { spawnSync } from 'child_process';
import { cwd, targets } from '../utils';
import { TARGETS, ARGV, execSync, getBin, log, error } from '../utils';

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

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

const args: string[] = ['--no-error-on-unmatched-pattern'];
const fix = ARGV.fix;
if (fix) {
args.push('--fix');
}
const includes = TARGETS.map(dir => [join(dir, '**/*.ts'), join(dir, '**/*.tsx')]).flat();
args.push(...includes);

const includes = targets.map(dir => [join(dir, '**/*.ts'), join(dir, '**/*.tsx')]).flat();

const child = spawnSync(binPath, includes, { cwd, stdio: 'inherit' });
const result = execSync(binPath, args);

if (!child.status) {
if (child.status === 0) {
console.log(chalk.green('校验通过'));
}
} else {
console.log(chalk.red('校验失败'));
if (result === true) {
log('eslint 校验通过');
} else if (result === false) {
error('eslint 校验失败');
}
44 changes: 44 additions & 0 deletions tools/checkers/stylelint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
------------------------------------------------------------
author: 珵之
create: 2023-12-19 15:19:10
description: css 样式检查
example:
npm run check:stylelint button
npm run check:stylelint button --fix
npm run check:stylelint ./components/button table
------------------------------------------------------------
*/

import chalk from 'chalk';
import { execSync, TARGETS, ARGV, getBin, log, error } from '../utils';

const binPath = getBin('stylelint');

if (!binPath) {
throw new Error('Not found stylelint');
}

if (!TARGETS.length) {
log(chalk.yellow('[Example]:'));
log('npm run check:stylelint button');
log('npm run check:stylelint button --fix');
log(`npm run check:stylelint ./components/button table`);

throw new Error('请指定一个合法目录');
}

const args: string[] = [];

if (ARGV.fix) {
args.push('--fix');
}
args.push(...TARGETS);

const result = execSync(binPath, args);

if (result === true) {
log('stylelint 校验通过');
} else if (result === false) {
error('stylelint 校验失败');
}
35 changes: 17 additions & 18 deletions tools/checkers/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,49 @@
author: 珵之
create: 2023-12-17 16:43:00
description: 检测目标目录所有 ts 文件类型定义
example: ts-node ./tools/checkers/types/index.ts ./components/table button
example:
npm run check:types button
npm run check:types ./components/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';
import { beforeExit, CWD, TARGETS, execSync, log, error } from '../../utils';

const tscPath = resolve(cwd, 'node_modules/.bin/tsc');
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();
if (!TARGETS.length) {
log(chalk.yellow('[Example]:'));
log(chalk.green('npm run check:types button table'));
log(chalk.green(`npm run check:types ./components/button ./components/table`));
log();
throw new Error('请指定一个合法目录');
}

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

const include = targets.map(dir => [join(dir, '**/*.ts'), join(dir, '**/*.tsx')]).flat();
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);
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' });
const result = execSync(tscPath, ['-p', checkTsConfigPath]);
rollback();

if (!child.status) {
if (child.status === 0) {
console.log(chalk.green('校验通过'));
}
} else {
console.log(chalk.red('校验失败'));
if (result === true) {
log('类型校验通过');
} else if (result === false) {
error('类型校验失败');
}
11 changes: 11 additions & 0 deletions tools/release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
------------------------------------------------------------
author: 珵之
create: 2023-12-19 17:13:32
description: 发布组件库
------------------------------------------------------------
*/

import { execSync } from './utils';


Loading

0 comments on commit ce8e3be

Please sign in to comment.