feat(plugin-infra): add plugin cli (#3344)

This commit is contained in:
Alex Yang
2023-07-22 10:17:40 -07:00
committed by GitHub
parent a494bad543
commit dd31d1e8c6
12 changed files with 244 additions and 135 deletions

View File

@@ -43,12 +43,6 @@ const flags = {
coverage: process.env.COVERAGE === 'true',
} satisfies BuildFlags;
spawn('vite', ['build'], {
cwd: projectRoot,
stdio: 'inherit',
shell: true,
});
spawn(
'node',
[

View File

@@ -91,12 +91,6 @@ flags.mode = buildFlags.mode as any;
flags.channel = buildFlags.channel as any;
flags.coverage = buildFlags.coverage;
spawn('vite', ['build', '--watch'], {
cwd: projectRoot,
stdio: 'inherit',
shell: true,
});
spawn(
'node',
[

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env node
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
const child = spawnSync(
process.execPath,
[
'--loader',
'ts-node/esm/transpile-only',
fileURLToPath(new URL('./dev-plugin.ts', import.meta.url)),
...process.argv.slice(2),
],
{ stdio: 'inherit' }
);
if (child.status) process.exit(child.status);

View File

@@ -0,0 +1,172 @@
import { ok } from 'node:assert';
import { open, readFile } from 'node:fs/promises';
import path from 'node:path';
import { parseArgs } from 'node:util';
import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
import react from '@vitejs/plugin-react-swc';
import { build } from 'vite';
import { z } from 'zod';
import { projectRoot } from '../config/index.js';
const args = process.argv.splice(2);
const result = parseArgs({
args,
options: {
watch: {
type: 'boolean',
default: false,
},
plugin: {
type: 'string',
},
},
});
const plugin = result.values.plugin;
if (typeof plugin !== 'string') {
throw new Error('plugin is required');
}
const isWatch = result.values.watch;
ok(typeof isWatch === 'boolean');
const packageJsonSchema = z.object({
name: z.string(),
affinePlugin: z.object({
release: z.boolean(),
entry: z.object({
core: z.string(),
}),
}),
});
const external = [
// built-in packages
/^@affine/,
/^@blocksuite/,
/^@toeverything/,
// react
/^react/,
/^react-dom/,
// store
/^jotai/,
// css
/^@vanilla-extract/,
];
const allPluginDir = path.resolve(projectRoot, 'plugins');
const getPluginDir = (plugin: string) => path.resolve(allPluginDir, plugin);
const pluginDir = getPluginDir(plugin);
const packageJsonFile = path.resolve(pluginDir, 'package.json');
const json: z.infer<typeof packageJsonSchema> = await readFile(
packageJsonFile,
{
encoding: 'utf-8',
}
)
.then(text => JSON.parse(text))
.then(async json => {
const { success } = await packageJsonSchema.safeParseAsync(json);
if (success) {
return json;
} else {
throw new Error('invalid package.json');
}
});
type Metadata = {
assets: Set<string>;
};
const metadata: Metadata = {
assets: new Set(),
};
const outDir = path.resolve(
projectRoot,
'apps',
'core',
'public',
'plugins',
plugin
);
const pluginListJsonPath = path.resolve(outDir, '..', 'plugin-list.json');
const coreEntry = path.resolve(pluginDir, json.affinePlugin.entry.core);
await build({
build: {
watch: isWatch ? {} : undefined,
minify: false,
outDir,
emptyOutDir: true,
lib: {
entry: coreEntry,
fileName: 'index',
formats: ['cjs'],
},
rollupOptions: {
output: {
assetFileNames: chunkInfo => {
if (chunkInfo.name) {
metadata.assets.add(chunkInfo.name);
return chunkInfo.name;
} else {
throw new Error('no name');
}
},
manualChunks: () => 'plugin',
},
external,
},
},
plugins: [
vanillaExtractPlugin(),
react(),
{
name: 'generate-list-json',
async generateBundle() {
const file = await open(pluginListJsonPath, 'as+');
const txt = await file.readFile({
encoding: 'utf-8',
});
if (!txt) {
console.log('generate plugin-list.json');
await file.write(
JSON.stringify([
{
release: json.affinePlugin.release,
name: plugin,
assets: [...metadata.assets],
},
])
);
} else {
console.log('modify plugin-list.json');
const list = JSON.parse(txt);
const index = list.findIndex((item: any) => item.name === plugin);
if (index === -1) {
list.push({
release: json.affinePlugin.release,
name: plugin,
assets: [...metadata.assets],
});
} else {
list[index].assets = [...metadata.assets];
}
await file.write(JSON.stringify(list), 0);
}
},
},
],
});