mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 01:09:54 +08:00
fix(tools): patch monorepo tools (#9285)
This commit is contained in:
@@ -4,12 +4,14 @@ export class BuildCommand extends PackageCommand {
|
|||||||
static override paths = [['build'], ['b']];
|
static override paths = [['build'], ['b']];
|
||||||
|
|
||||||
async execute() {
|
async execute() {
|
||||||
const args = ['affine build', this.package];
|
const args: string[] = [];
|
||||||
|
|
||||||
if (this.deps) {
|
if (this.deps) {
|
||||||
args.push('--deps');
|
args.push('--deps');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
args.push(this.package, 'build');
|
||||||
|
|
||||||
await this.cli.run(args);
|
await this.cli.run(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,8 @@ export class BundleCommand extends PackageCommand {
|
|||||||
static override paths = [['bundle'], ['webpack'], ['pack'], ['bun']];
|
static override paths = [['bundle'], ['webpack'], ['pack'], ['bun']];
|
||||||
|
|
||||||
// bundle is not able to run with deps
|
// bundle is not able to run with deps
|
||||||
override deps = false;
|
override _deps = false;
|
||||||
|
override waitDeps = false;
|
||||||
|
|
||||||
dev = Option.Boolean('--dev,-d', false, {
|
dev = Option.Boolean('--dev,-d', false, {
|
||||||
description: 'Run in Development mode',
|
description: 'Run in Development mode',
|
||||||
|
|||||||
@@ -38,10 +38,18 @@ export abstract class PackageCommand extends Command {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
deps = Option.Boolean('--deps', false, {
|
protected _deps = Option.Boolean('--deps', false, {
|
||||||
description:
|
description:
|
||||||
'Execute the same command in workspace dependencies, if defined.',
|
'Execute the same command in workspace dependencies, if defined.',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
get deps() {
|
||||||
|
return this._deps;
|
||||||
|
}
|
||||||
|
|
||||||
|
waitDeps = Option.Boolean('--wait-deps', false, {
|
||||||
|
description: 'Wait for dependencies to be ready before running the command',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class PackagesCommand extends Command {
|
export abstract class PackagesCommand extends Command {
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ export class DevCommand extends PackageCommand {
|
|||||||
static override paths = [['dev'], ['d']];
|
static override paths = [['dev'], ['d']];
|
||||||
|
|
||||||
async execute() {
|
async execute() {
|
||||||
const args = [this.package, 'dev'];
|
const args = [];
|
||||||
|
|
||||||
if (this.deps) {
|
if (this.deps) {
|
||||||
args.push('--deps');
|
args.push('--deps', '--wait-deps');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
args.push(this.package, 'dev');
|
||||||
|
|
||||||
await this.cli.run(args);
|
await this.cli.run(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+84
-54
@@ -1,12 +1,13 @@
|
|||||||
import { Path } from '@affine-tools/utils/path';
|
import { Path } from '@affine-tools/utils/path';
|
||||||
import { execAsync } from '@affine-tools/utils/process';
|
import { execAsync } from '@affine-tools/utils/process';
|
||||||
import type { PackageName } from '@affine-tools/utils/workspace';
|
import type { Package, PackageName } from '@affine-tools/utils/workspace';
|
||||||
|
|
||||||
import { Option, PackageCommand } from './command';
|
import { Option, PackageCommand } from './command';
|
||||||
|
|
||||||
interface RunScriptOptions {
|
interface RunScriptOptions {
|
||||||
includeDependencies?: boolean;
|
includeDependencies?: boolean;
|
||||||
waitDependencies?: boolean;
|
waitDependencies?: boolean;
|
||||||
|
ignoreIfNotFound?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentDir = Path.dir(import.meta.url);
|
const currentDir = Path.dir(import.meta.url);
|
||||||
@@ -68,68 +69,97 @@ export class RunCommand extends PackageCommand {
|
|||||||
async execute() {
|
async execute() {
|
||||||
await this.run(this.package, this.args, {
|
await this.run(this.package, this.args, {
|
||||||
includeDependencies: this.deps,
|
includeDependencies: this.deps,
|
||||||
waitDependencies: true,
|
waitDependencies: this.waitDeps,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(name: PackageName, args: string[], opts: RunScriptOptions = {}) {
|
async run(name: PackageName, args: string[], opts: RunScriptOptions = {}) {
|
||||||
opts = { includeDependencies: false, ...opts };
|
opts = {
|
||||||
|
includeDependencies: false,
|
||||||
|
waitDependencies: true,
|
||||||
|
ignoreIfNotFound: false,
|
||||||
|
...opts,
|
||||||
|
};
|
||||||
|
|
||||||
const pkg = this.workspace.getPackage(name);
|
const pkg = this.workspace.getPackage(name);
|
||||||
const script = args[0];
|
const scriptName = args[0];
|
||||||
const pkgScript = pkg.scripts[script];
|
const pkgScript = pkg.scripts[scriptName];
|
||||||
|
|
||||||
let isPackageJsonScript = false;
|
|
||||||
let isAFFiNEScript = false;
|
|
||||||
|
|
||||||
if (pkgScript) {
|
if (pkgScript) {
|
||||||
isPackageJsonScript = true;
|
await this.runScript(pkg, scriptName, args.slice(1), opts);
|
||||||
isAFFiNEScript = pkgScript.startsWith('affine ');
|
|
||||||
} else {
|
} else {
|
||||||
isAFFiNEScript = script.startsWith('affine ');
|
await this.runCommand(pkg, scriptName, args.slice(1));
|
||||||
}
|
|
||||||
|
|
||||||
if (isPackageJsonScript && opts.includeDependencies) {
|
|
||||||
this.logger.info(
|
|
||||||
`Running [${script}] script in dependencies of ${pkg.name}...`
|
|
||||||
);
|
|
||||||
|
|
||||||
await Promise.all(
|
|
||||||
pkg.deps.map(dep => {
|
|
||||||
this.logger.info(`Running [${script}] script in ${dep.name}...`);
|
|
||||||
return this.run(dep.name, args, opts);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isPackageJsonScript) {
|
|
||||||
this.logger.info(`Running [${script}] script in ${pkg.name}...`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isAFFiNEScript) {
|
|
||||||
await this.cli.run([
|
|
||||||
...pkgScript.split(' ').slice(1),
|
|
||||||
...args.slice(1),
|
|
||||||
'-p',
|
|
||||||
pkg.name,
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
const script = pkgScript ?? args[0];
|
|
||||||
// very simple test for auto ts/mjs scripts
|
|
||||||
const isLoaderRequired = !ignoreLoaderScripts.some(ignore =>
|
|
||||||
new RegExp(ignore).test(script)
|
|
||||||
);
|
|
||||||
|
|
||||||
await execAsync(name, ['yarn', ...args], {
|
|
||||||
cwd: pkg.path.value,
|
|
||||||
...(isLoaderRequired
|
|
||||||
? {
|
|
||||||
env: {
|
|
||||||
NODE_OPTIONS: `--import=${currentDir.join('../register.js').toFileUrl()}`,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async runScript(
|
||||||
|
pkg: Package,
|
||||||
|
scriptName: string,
|
||||||
|
args: string[],
|
||||||
|
opts: RunScriptOptions = {}
|
||||||
|
) {
|
||||||
|
const script = pkg.scripts[scriptName];
|
||||||
|
|
||||||
|
if (!script) {
|
||||||
|
if (opts.ignoreIfNotFound) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Script ${scriptName} not found in ${pkg.name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const isAFFiNECommand = script.startsWith('affine ');
|
||||||
|
if (opts.includeDependencies) {
|
||||||
|
const depsRun = Promise.all(
|
||||||
|
pkg.deps.map(dep => {
|
||||||
|
return this.runScript(
|
||||||
|
pkg.workspace.getPackage(dep.name),
|
||||||
|
scriptName,
|
||||||
|
args,
|
||||||
|
{
|
||||||
|
...opts,
|
||||||
|
ignoreIfNotFound: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
if (opts.waitDependencies) {
|
||||||
|
await depsRun;
|
||||||
|
} else {
|
||||||
|
depsRun.catch(e => {
|
||||||
|
this.logger.error(e);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
args = [...script.split(' '), ...args];
|
||||||
|
|
||||||
|
if (isAFFiNECommand) {
|
||||||
|
args.shift();
|
||||||
|
args.push('-p', pkg.name);
|
||||||
|
} else {
|
||||||
|
args.unshift(pkg.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.cli.run(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
async runCommand(pkg: Package, scriptName: string, args: string[]) {
|
||||||
|
// very simple test for auto ts/mjs scripts
|
||||||
|
// TODO(@forehalo): bypass cross-env and fetch the next script after envs
|
||||||
|
const isLoaderRequired = !ignoreLoaderScripts.some(ignore =>
|
||||||
|
new RegExp(ignore).test(scriptName)
|
||||||
|
);
|
||||||
|
|
||||||
|
await execAsync(pkg.name, ['yarn', scriptName, ...args], {
|
||||||
|
cwd: pkg.path.value,
|
||||||
|
...(isLoaderRequired
|
||||||
|
? {
|
||||||
|
env: {
|
||||||
|
NODE_OPTIONS: `--import=${currentDir.join('../register.js').toFileUrl()}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user