mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 04:48:53 +00:00
chore: standardize tsconfig (#9568)
This commit is contained in:
@@ -4,9 +4,9 @@ import { Cli } from 'clipanion';
|
||||
import { BuildCommand } from './build';
|
||||
import { BundleCommand } from './bundle';
|
||||
import { CleanCommand } from './clean';
|
||||
import { CodegenCommand } from './codegen';
|
||||
import type { CliContext } from './context';
|
||||
import { DevCommand } from './dev';
|
||||
import { InitCommand } from './init';
|
||||
import { RunCommand } from './run';
|
||||
|
||||
const cli = new Cli<CliContext>({
|
||||
@@ -18,7 +18,7 @@ const cli = new Cli<CliContext>({
|
||||
});
|
||||
|
||||
cli.register(RunCommand);
|
||||
cli.register(CodegenCommand);
|
||||
cli.register(InitCommand);
|
||||
cli.register(CleanCommand);
|
||||
cli.register(BuildCommand);
|
||||
cli.register(DevCommand);
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
|
||||
import type { Path } from '@affine-tools/utils/path';
|
||||
import { type BuiltInParserName, format } from 'prettier';
|
||||
|
||||
import { Command } from './command';
|
||||
|
||||
export class CodegenCommand extends Command {
|
||||
static override paths = [['init'], ['i'], ['codegen']];
|
||||
|
||||
async execute() {
|
||||
this.logger.info('Generating Workspace configs');
|
||||
await this.generateWorkspaceFiles();
|
||||
this.logger.info('Workspace configs generated');
|
||||
}
|
||||
|
||||
async generateWorkspaceFiles() {
|
||||
const filesToGenerate: [Path, () => string, BuiltInParserName?][] = [
|
||||
[
|
||||
this.workspace.join('tsconfig.project.json'),
|
||||
this.workspace.genProjectTsConfig.bind(this.workspace),
|
||||
'json',
|
||||
],
|
||||
[
|
||||
this.workspace
|
||||
.getPackage('@affine-tools/utils')
|
||||
.join('src/workspace.gen.ts'),
|
||||
this.workspace.genWorkspaceInfo.bind(this.workspace),
|
||||
'typescript',
|
||||
],
|
||||
[this.workspace.join('oxlint.json'), this.genOxlintConfig, 'json'],
|
||||
];
|
||||
|
||||
for (const [path, content, formatter] of filesToGenerate) {
|
||||
this.logger.info(`Output: ${path}`);
|
||||
let file = content();
|
||||
if (formatter) {
|
||||
file = await this.format(file, formatter);
|
||||
}
|
||||
writeFileSync(path.value, file);
|
||||
}
|
||||
}
|
||||
|
||||
format(content: string, parser: BuiltInParserName) {
|
||||
const config = JSON.parse(
|
||||
readFileSync(this.workspace.join('.prettierrc').value, 'utf-8')
|
||||
);
|
||||
return format(content, { parser, ...config });
|
||||
}
|
||||
|
||||
genOxlintConfig = () => {
|
||||
const json = JSON.parse(
|
||||
readFileSync(this.workspace.join('oxlint.json').value, 'utf-8')
|
||||
);
|
||||
|
||||
const ignoreList = readFileSync(
|
||||
this.workspace.join('.prettierignore').value,
|
||||
'utf-8'
|
||||
)
|
||||
.split('\n')
|
||||
.filter(line => line.trim() && !line.startsWith('#'));
|
||||
|
||||
json['ignorePatterns'] = ignoreList;
|
||||
|
||||
return JSON.stringify(json, null, 2);
|
||||
};
|
||||
}
|
||||
@@ -16,6 +16,10 @@ export abstract class Command extends BaseCommand<CliContext> {
|
||||
get workspace() {
|
||||
return this.context.workspace;
|
||||
}
|
||||
|
||||
set workspace(workspace: Workspace) {
|
||||
this.context.workspace = workspace;
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class PackageCommand extends Command {
|
||||
|
||||
140
tools/cli/src/init.ts
Executable file
140
tools/cli/src/init.ts
Executable file
@@ -0,0 +1,140 @@
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
|
||||
import type { Path } from '@affine-tools/utils/path';
|
||||
import { type Package, Workspace } from '@affine-tools/utils/workspace';
|
||||
import { applyEdits, modify } from 'jsonc-parser';
|
||||
import { type BuiltInParserName, format } from 'prettier';
|
||||
|
||||
import { Command } from './command';
|
||||
|
||||
export class InitCommand extends Command {
|
||||
static override paths = [['init'], ['i'], ['codegen']];
|
||||
|
||||
async execute() {
|
||||
this.logger.info('Generating Workspace configs');
|
||||
await this.generateWorkspaceFiles();
|
||||
this.logger.info('Workspace configs generated');
|
||||
}
|
||||
|
||||
async generateWorkspaceFiles() {
|
||||
this.workspace = new Workspace(this.workspace.yarnList());
|
||||
const filesToGenerate: [
|
||||
Path,
|
||||
(prev: string) => string,
|
||||
BuiltInParserName?,
|
||||
][] = [
|
||||
[this.workspace.join('tsconfig.json'), this.genProjectTsConfig, 'json'],
|
||||
[
|
||||
this.workspace
|
||||
.getPackage('@affine-tools/utils')
|
||||
.join('src/workspace.gen.ts'),
|
||||
this.genWorkspaceInfo,
|
||||
'typescript',
|
||||
],
|
||||
[this.workspace.join('oxlint.json'), this.genOxlintConfig, 'json'],
|
||||
...this.workspace.packages
|
||||
.filter(p => p.isTsProject)
|
||||
.map(
|
||||
p =>
|
||||
[
|
||||
p.join('tsconfig.json'),
|
||||
this.genPackageTsConfig.bind(this, p),
|
||||
'json',
|
||||
] as any
|
||||
),
|
||||
];
|
||||
|
||||
for (const [path, content, formatter] of filesToGenerate) {
|
||||
this.logger.info(`Generating: ${path}`);
|
||||
const previous = readFileSync(path.value, 'utf-8');
|
||||
let file = content(previous);
|
||||
if (formatter) {
|
||||
file = await this.format(file, formatter);
|
||||
}
|
||||
writeFileSync(path.value, file);
|
||||
}
|
||||
}
|
||||
|
||||
format(content: string, parser: BuiltInParserName) {
|
||||
const config = JSON.parse(
|
||||
readFileSync(this.workspace.join('.prettierrc').value, 'utf-8')
|
||||
);
|
||||
return format(content, { parser, ...config });
|
||||
}
|
||||
|
||||
genOxlintConfig = () => {
|
||||
const json = JSON.parse(
|
||||
readFileSync(this.workspace.join('oxlint.json').value, 'utf-8')
|
||||
);
|
||||
|
||||
const ignoreList = readFileSync(
|
||||
this.workspace.join('.prettierignore').value,
|
||||
'utf-8'
|
||||
)
|
||||
.split('\n')
|
||||
.filter(line => line.trim() && !line.startsWith('#'));
|
||||
|
||||
json['ignorePatterns'] = ignoreList;
|
||||
|
||||
return JSON.stringify(json, null, 2);
|
||||
};
|
||||
|
||||
genWorkspaceInfo = () => {
|
||||
const list = this.workspace.yarnList();
|
||||
|
||||
const names = list.map(p => p.name);
|
||||
|
||||
const content = [
|
||||
'// Auto generated content',
|
||||
'// DO NOT MODIFY THIS FILE MANUALLY',
|
||||
`export const PackageList = ${JSON.stringify(list, null, 2)}`,
|
||||
'',
|
||||
`export type PackageName = ${names.map(n => `'${n}'`).join(' | ')}`,
|
||||
];
|
||||
|
||||
return content.join('\n');
|
||||
};
|
||||
|
||||
genProjectTsConfig = (prev: string) => {
|
||||
return applyEdits(
|
||||
prev,
|
||||
modify(
|
||||
prev,
|
||||
['references'],
|
||||
this.workspace.packages
|
||||
.filter(p => p.isTsProject)
|
||||
.filter(
|
||||
p =>
|
||||
// NOTE(@forehalo): there two packages including outdated types, will be fixed when merged with affine-e2e
|
||||
!['@blocksuite/playground', '@blocksuite/legacy-e2e'].includes(
|
||||
p.name
|
||||
)
|
||||
)
|
||||
.map(p => ({ path: p.path.relativePath })),
|
||||
{}
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
genPackageTsConfig = (pkg: Package, prev: string) => {
|
||||
// TODO(@forehalo):
|
||||
// currently electron-api => electron => nbstore => electron-api
|
||||
// this is a circular dependency, we need to fix it
|
||||
// basically, the electron app don't need to use nbstore for exposing js bridge apis
|
||||
if (pkg.name === '@affine/electron-api') {
|
||||
return prev;
|
||||
}
|
||||
|
||||
return applyEdits(
|
||||
prev,
|
||||
modify(
|
||||
prev,
|
||||
['references'],
|
||||
pkg.deps
|
||||
.filter(p => p.isTsProject)
|
||||
.map(d => ({ path: pkg.path.relative(d.path.value) })),
|
||||
{}
|
||||
)
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -47,8 +47,8 @@ export class RunCommand extends PackageCommand {
|
||||
`Run custom 'xxx' script defined in @affine/web's package.json`,
|
||||
'$0 web xxx',
|
||||
],
|
||||
[`Run 'codegen' for workspace`, '$0 codegen'],
|
||||
[`Clean tsbuild and dist under each package`, '$0 clean --ts --dist'],
|
||||
[`Run 'init' for workspace`, '$0 init'],
|
||||
[`Clean dist of each package`, '$0 clean --dist'],
|
||||
[`Clean node_modules under each package`, '$0 clean --node-modules'],
|
||||
[`Clean everything`, '$0 clean --all'],
|
||||
[`Run 'build' script for @affine/web`, '$0 build -p web'],
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { execSync } from 'node:child_process';
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
import type { BUILD_CONFIG_TYPE } from '@affine/env/global';
|
||||
import { Path, ProjectRoot } from '@affine-tools/utils/path';
|
||||
import { Repository } from '@napi-rs/simple-git';
|
||||
import HTMLPlugin from 'html-webpack-plugin';
|
||||
|
||||
Reference in New Issue
Block a user