2023-09-12 22:39:23 +08:00
|
|
|
const { dirname, resolve } = require('path');
|
2024-05-16 18:46:56 +08:00
|
|
|
const { realpath, readFile, writeFile, readdir, symlink, unlink, mkdir, stat } = require('fs').promises;
|
2023-09-12 22:39:23 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-09-13 15:48:11 +08:00
|
|
|
await symlink(resolve(storagePluginsPath, pluginName), link, 'dir');
|
2023-09-12 22:39:23 +08:00
|
|
|
} 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)) {
|
2024-05-16 18:46:56 +08:00
|
|
|
const real = await realpath(link);
|
|
|
|
if (real === resolve(packagePluginsPath, pluginName)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-12 22:39:23 +08:00
|
|
|
await unlink(link);
|
|
|
|
}
|
2024-01-24 13:06:06 +08:00
|
|
|
await symlink(resolve(packagePluginsPath, pluginName), link, 'dir');
|
2023-09-12 22:39:23 +08:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.createDevPluginSymLink = createDevPluginSymLink;
|
2024-01-18 00:33:15 +08:00
|
|
|
|
|
|
|
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;
|