tachybase_todo/packages/core/utils/plugin-symlink.js
chenos 8217ebfb1b
feat: improve plugin manager process (#3386)
* feat: improve plugin manager process

* fix: skip help error

* fix: ipc check

* fix: improve remove

* fix: refresh

* fix: remove dir

* fix: improve code

* fix: update yarn.lock

* fix: e2e error

* fix: migration

* fix: pm create

* Revert "fix: migration"

This reverts commit 8f8fe04436ac96798259fb6debd88fffcb613560.

* fix: remove sample-hello
2024-01-18 00:33:15 +08:00

99 lines
3.2 KiB
JavaScript

const { dirname, resolve } = require('path');
const { readFile, writeFile, readdir, symlink, unlink, mkdir, stat } = require('fs').promises;
async function getStoragePluginNames(target) {
const plugins = [];
const items = await readdir(target);
for (const item of items) {
if (item.startsWith('@')) {
const children = await getStoragePluginNames(resolve(target, item));
plugins.push(
...children.map((child) => {
return `${item}/${child}`;
}),
);
} else if (await fsExists(resolve(target, item, 'package.json'))) {
plugins.push(item);
}
}
return plugins;
}
async function fsExists(path) {
try {
await stat(path);
return true;
} catch (error) {
return false;
}
}
exports.fsExists = fsExists;
async function createStoragePluginSymLink(pluginName) {
const storagePluginsPath = resolve(process.cwd(), 'storage/plugins');
const nodeModulesPath = process.env.NODE_MODULES_PATH; // resolve(dirname(require.resolve('@nocobase/server/package.json')), 'node_modules');
// const nodeModulesPath = resolve(process.cwd(), 'node_modules');
try {
if (pluginName.startsWith('@')) {
const [orgName] = pluginName.split('/');
if (!(await fsExists(resolve(nodeModulesPath, orgName)))) {
await mkdir(resolve(nodeModulesPath, orgName), { recursive: true });
}
}
const link = resolve(nodeModulesPath, pluginName);
if (await fsExists(link)) {
await unlink(link);
}
await symlink(resolve(storagePluginsPath, pluginName), link, 'dir');
} catch (error) {
console.error(error);
}
}
exports.createStoragePluginSymLink = createStoragePluginSymLink;
async function createStoragePluginsSymlink() {
const storagePluginsPath = resolve(process.cwd(), 'storage/plugins');
if (!(await fsExists(storagePluginsPath))) {
return;
}
const pluginNames = await getStoragePluginNames(storagePluginsPath);
await Promise.all(pluginNames.map((pluginName) => createStoragePluginSymLink(pluginName)));
}
exports.createStoragePluginsSymlink = createStoragePluginsSymlink;
async function createDevPluginSymLink(pluginName) {
const packagePluginsPath = resolve(process.cwd(), 'packages/plugins');
const nodeModulesPath = process.env.NODE_MODULES_PATH; // resolve(dirname(require.resolve('@nocobase/server/package.json')), 'node_modules');
try {
if (pluginName.startsWith('@')) {
const [orgName] = pluginName.split('/');
if (!(await fsExists(resolve(nodeModulesPath, orgName)))) {
await mkdir(resolve(nodeModulesPath, orgName), { recursive: true });
}
}
const link = resolve(nodeModulesPath, pluginName);
if (await fsExists(link)) {
await unlink(link);
}
await symlink(resolve(packagePluginsPath, pluginName), link);
} catch (error) {
console.error(error);
}
}
exports.createDevPluginSymLink = createDevPluginSymLink;
async function createDevPluginsSymlink() {
const storagePluginsPath = resolve(process.cwd(), 'packages/plugins');
if (!(await fsExists(storagePluginsPath))) {
return;
}
const pluginNames = await getStoragePluginNames(storagePluginsPath);
await Promise.all(pluginNames.map((pluginName) => createDevPluginSymLink(pluginName)));
}
exports.createDevPluginsSymlink = createDevPluginsSymlink;