This is an automated email from the ASF dual-hosted git repository. robin0716 pushed a commit to branch dev in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit 59080727cf4ff074953f0cafc96ed73f4de2c789 Author: robin <[email protected]> AuthorDate: Thu Oct 10 17:47:36 2024 +0800 refactor(ui): update pre-install script in package.json --- ui/package.json | 2 +- ui/scripts/importPlugins.js | 68 ++++++++++++++++++++++++++++++++ ui/scripts/{plugin.js => loadPlugins.js} | 30 +------------- ui/scripts/preinstall.js | 2 +- 4 files changed, 71 insertions(+), 31 deletions(-) diff --git a/ui/package.json b/ui/package.json index 51cd29d1..3d9c2614 100644 --- a/ui/package.json +++ b/ui/package.json @@ -8,7 +8,7 @@ "build": "node ./scripts/env.js && react-app-rewired build", "lint": "eslint . --cache --fix --ext .ts,.tsx", "prettier": "prettier --write 'src/**/*.{js,jsx,ts,tsx,json,css,scss,md}' --config ./.prettierrc.json", - "pre-install": "pnpm install && node ./scripts/preinstall.js", + "pre-install": "node ./scripts/importPlugins.js && pnpm install && node ./scripts/preinstall.js ", "prepare": "pnpm build:packages", "pre-commit": "lint-staged", "build:packages": "pnpm -r --filter=./src/plugins/* run build", diff --git a/ui/scripts/importPlugins.js b/ui/scripts/importPlugins.js new file mode 100644 index 00000000..0862a015 --- /dev/null +++ b/ui/scripts/importPlugins.js @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const path = require('path'); +const fs = require('fs'); + +const pluginPath = path.join(__dirname, '../src/plugins'); +const pluginFolders = fs.readdirSync(pluginPath); + +function resetPackageJson() { + const packageJsonPath = path.join(__dirname, '..', 'package.json'); + const packageJsonContent = require(packageJsonPath); + const dependencies = packageJsonContent.dependencies; + for (const key in dependencies) { + if (dependencies[key].startsWith('workspace')) { + delete dependencies[key]; + } + } + fs.writeFileSync( + packageJsonPath, + JSON.stringify(packageJsonContent, null, 2), + ); +} + +function addPluginToPackageJson(packageName) { + const packageJsonPath = path.join(__dirname, '..', 'package.json'); + const packageJsonContent = require(packageJsonPath); + packageJsonContent.dependencies[packageName] = 'workspace:*'; + + fs.writeFileSync( + packageJsonPath, + JSON.stringify(packageJsonContent, null, 2), + ); +} + + +resetPackageJson(); + +pluginFolders.forEach((folder) => { + const pluginFolder = path.join(pluginPath, folder); + const stat = fs.statSync(pluginFolder); + + if (stat.isDirectory() && folder !== 'builtin') { + if (!fs.existsSync(path.join(pluginFolder, 'index.ts'))) { + return; + } + const packageJson = require(path.join(pluginFolder, 'package.json')); + const packageName = packageJson.name; + + addPluginToPackageJson(packageName); + } +}); \ No newline at end of file diff --git a/ui/scripts/plugin.js b/ui/scripts/loadPlugins.js similarity index 76% rename from ui/scripts/plugin.js rename to ui/scripts/loadPlugins.js index 7ad3d7c5..77b5fe5d 100644 --- a/ui/scripts/plugin.js +++ b/ui/scripts/loadPlugins.js @@ -28,37 +28,11 @@ function pascalize(str) { return str.split(/[_-]/).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join(''); } -function resetPackageJson() { - const packageJsonPath = path.join(__dirname, '..', 'package.json'); - const packageJsonContent = require(packageJsonPath); - const dependencies = packageJsonContent.dependencies; - for (const key in dependencies) { - if (dependencies[key].startsWith('workspace')) { - delete dependencies[key]; - } - } - fs.writeFileSync( - packageJsonPath, - JSON.stringify(packageJsonContent, null, 2), - ); -} - function resetIndexTs() { const indexTsPath = path.join(pluginPath, 'index.ts'); fs.writeFileSync(indexTsPath, ''); } -function addPluginToPackageJson(packageName) { - const packageJsonPath = path.join(__dirname, '..', 'package.json'); - const packageJsonContent = require(packageJsonPath); - packageJsonContent.dependencies[packageName] = 'workspace:*'; - - fs.writeFileSync( - packageJsonPath, - JSON.stringify(packageJsonContent, null, 2), - ); -} - function addPluginToIndexTs(packageName, pluginFolder) { const indexTsPath = path.join(pluginPath, 'index.ts'); const indexTsContent = fs.readFileSync(indexTsPath, 'utf-8'); @@ -87,8 +61,6 @@ if (pluginLength > 0) { resetIndexTs(); } -resetPackageJson(); - pluginFolders.forEach((folder) => { const pluginFolder = path.join(pluginPath, folder); const stat = fs.statSync(pluginFolder); @@ -100,7 +72,7 @@ pluginFolders.forEach((folder) => { const packageJson = require(path.join(pluginFolder, 'package.json')); const packageName = packageJson.name; - addPluginToPackageJson(packageName); addPluginToIndexTs(packageName, pluginFolder); } }); + diff --git a/ui/scripts/preinstall.js b/ui/scripts/preinstall.js index e3608091..94de3f32 100644 --- a/ui/scripts/preinstall.js +++ b/ui/scripts/preinstall.js @@ -19,7 +19,7 @@ // There is a bug when using npm to install: the execution of preinstall is after install, so when this prompt is displayed, the dependent packages have already been installed. -require('./plugin'); +require('./loadPlugins'); if (!/pnpm/.test(process.env.npm_execpath)) { console.warn(
