2022-05-19 00:40:55 +08:00
|
|
|
const net = require('net');
|
|
|
|
const chalk = require('chalk');
|
|
|
|
const execa = require('execa');
|
2023-09-15 08:51:20 +08:00
|
|
|
const fg = require('fast-glob');
|
2023-11-27 09:07:16 +08:00
|
|
|
const { dirname, join, resolve, sep } = require('path');
|
2022-05-19 00:40:55 +08:00
|
|
|
const { readFile, writeFile } = require('fs').promises;
|
2023-09-15 08:51:20 +08:00
|
|
|
const { existsSync, mkdirSync, cpSync, writeFileSync } = require('fs');
|
2023-12-21 20:39:11 +08:00
|
|
|
const dotenv = require('dotenv');
|
|
|
|
const fs = require('fs');
|
2022-05-19 00:40:55 +08:00
|
|
|
|
2023-12-21 20:39:11 +08:00
|
|
|
exports.isPackageValid = (pkg) => {
|
2022-05-19 00:40:55 +08:00
|
|
|
try {
|
2023-12-21 20:39:11 +08:00
|
|
|
require.resolve(pkg);
|
2022-05-19 00:40:55 +08:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.hasCorePackages = () => {
|
|
|
|
const coreDir = resolve(process.cwd(), 'packages/core/build');
|
|
|
|
return existsSync(coreDir);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.hasTsNode = () => {
|
|
|
|
return exports.isPackageValid('ts-node/dist/bin');
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.isDev = function isDev() {
|
|
|
|
if (process.env.APP_ENV === 'production') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return exports.hasTsNode();
|
|
|
|
};
|
|
|
|
|
2022-09-18 14:10:01 +08:00
|
|
|
const isProd = () => {
|
|
|
|
const { APP_PACKAGE_ROOT } = process.env;
|
2023-08-02 00:07:52 +08:00
|
|
|
const file = `${APP_PACKAGE_ROOT}/lib/index.js`;
|
2022-09-18 14:10:01 +08:00
|
|
|
if (!existsSync(resolve(process.cwd(), file))) {
|
|
|
|
console.log('For production environment, please build the code first.');
|
|
|
|
console.log();
|
|
|
|
console.log(chalk.yellow('$ yarn build'));
|
|
|
|
console.log();
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.isProd = isProd;
|
|
|
|
|
2022-05-19 00:40:55 +08:00
|
|
|
exports.nodeCheck = () => {
|
|
|
|
if (!exports.hasTsNode()) {
|
|
|
|
console.log('Please install all dependencies');
|
|
|
|
console.log(chalk.yellow('$ yarn install'));
|
2022-09-18 14:10:01 +08:00
|
|
|
process.exit(1);
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-27 09:07:16 +08:00
|
|
|
exports.run = (command, args, options = {}) => {
|
2023-12-23 15:27:11 +08:00
|
|
|
if (command === 'tsx') {
|
2023-12-25 15:03:48 +08:00
|
|
|
command = 'node';
|
|
|
|
args = ['./node_modules/tsx/dist/cli.mjs'].concat(args || []);
|
2023-12-23 15:27:11 +08:00
|
|
|
}
|
2023-11-27 09:07:16 +08:00
|
|
|
return execa(command, args, {
|
2022-05-19 00:40:55 +08:00
|
|
|
shell: true,
|
|
|
|
stdio: 'inherit',
|
|
|
|
...options,
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
...options.env,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.isPortReachable = async (port, { timeout = 1000, host } = {}) => {
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
const socket = new net.Socket();
|
|
|
|
|
|
|
|
const onError = () => {
|
|
|
|
socket.destroy();
|
|
|
|
reject();
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.setTimeout(timeout);
|
|
|
|
socket.once('error', onError);
|
|
|
|
socket.once('timeout', onError);
|
|
|
|
|
|
|
|
socket.connect(port, host, () => {
|
|
|
|
socket.end();
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await promise;
|
|
|
|
return true;
|
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.postCheck = async (opts) => {
|
|
|
|
const port = opts.port || process.env.APP_PORT;
|
|
|
|
const result = await exports.isPortReachable(port);
|
|
|
|
if (result) {
|
|
|
|
console.error(chalk.red(`post already in use ${port}`));
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.runInstall = async () => {
|
2023-09-13 12:21:29 +08:00
|
|
|
const { APP_PACKAGE_ROOT, SERVER_TSCONFIG_PATH } = process.env;
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
if (exports.isDev()) {
|
|
|
|
const argv = [
|
2023-07-27 10:29:07 +08:00
|
|
|
'--tsconfig',
|
2023-09-13 12:21:29 +08:00
|
|
|
SERVER_TSCONFIG_PATH,
|
2022-05-19 00:40:55 +08:00
|
|
|
'-r',
|
|
|
|
'tsconfig-paths/register',
|
2023-08-02 00:07:52 +08:00
|
|
|
`${APP_PACKAGE_ROOT}/src/index.ts`,
|
2022-05-19 00:40:55 +08:00
|
|
|
'install',
|
|
|
|
'-s',
|
|
|
|
];
|
2023-07-27 10:29:07 +08:00
|
|
|
await exports.run('tsx', argv);
|
2022-09-18 14:10:01 +08:00
|
|
|
} else if (isProd()) {
|
2023-08-02 00:07:52 +08:00
|
|
|
const file = `${APP_PACKAGE_ROOT}/lib/index.js`;
|
2022-09-18 14:10:01 +08:00
|
|
|
const argv = [file, 'install', '-s'];
|
2022-05-19 00:40:55 +08:00
|
|
|
await exports.run('node', argv);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.runAppCommand = async (command, args = []) => {
|
2023-09-13 12:21:29 +08:00
|
|
|
const { APP_PACKAGE_ROOT, SERVER_TSCONFIG_PATH } = process.env;
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
if (exports.isDev()) {
|
|
|
|
const argv = [
|
2023-07-27 10:29:07 +08:00
|
|
|
'--tsconfig',
|
2023-09-13 12:21:29 +08:00
|
|
|
SERVER_TSCONFIG_PATH,
|
2022-05-19 00:40:55 +08:00
|
|
|
'-r',
|
|
|
|
'tsconfig-paths/register',
|
2023-08-02 00:07:52 +08:00
|
|
|
`${APP_PACKAGE_ROOT}/src/index.ts`,
|
2022-05-19 00:40:55 +08:00
|
|
|
command,
|
|
|
|
...args,
|
|
|
|
];
|
2023-07-27 10:29:07 +08:00
|
|
|
await exports.run('tsx', argv);
|
2022-09-18 14:10:01 +08:00
|
|
|
} else if (isProd()) {
|
2023-08-02 00:07:52 +08:00
|
|
|
const argv = [`${APP_PACKAGE_ROOT}/lib/index.js`, command, ...args];
|
2022-05-19 00:40:55 +08:00
|
|
|
await exports.run('node', argv);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.promptForTs = () => {
|
|
|
|
console.log(chalk.green('WAIT: ') + 'TypeScript compiling...');
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.updateJsonFile = async (target, fn) => {
|
|
|
|
const content = await readFile(target, 'utf-8');
|
|
|
|
const json = JSON.parse(content);
|
|
|
|
await writeFile(target, JSON.stringify(fn(json), null, 2), 'utf-8');
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getVersion = async () => {
|
|
|
|
const { stdout } = await execa('npm', ['v', '@nocobase/app-server', 'versions']);
|
2022-06-17 14:21:56 +08:00
|
|
|
const versions = new Function(`return (${stdout})`)();
|
2022-05-19 00:40:55 +08:00
|
|
|
return versions[versions.length - 1];
|
|
|
|
};
|
2023-08-02 00:07:52 +08:00
|
|
|
|
|
|
|
exports.generateAppDir = function generateAppDir() {
|
|
|
|
const appPkgPath = dirname(dirname(require.resolve('@nocobase/app/src/index.ts')));
|
|
|
|
const appDevDir = resolve(process.cwd(), './storage/.app-dev');
|
|
|
|
if (exports.isDev() && !exports.hasCorePackages() && appPkgPath.includes('node_modules')) {
|
|
|
|
if (!existsSync(appDevDir)) {
|
|
|
|
mkdirSync(appDevDir, { force: true, recursive: true });
|
|
|
|
cpSync(appPkgPath, appDevDir, {
|
|
|
|
recursive: true,
|
|
|
|
force: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
process.env.APP_PACKAGE_ROOT = appDevDir;
|
|
|
|
} else {
|
|
|
|
process.env.APP_PACKAGE_ROOT = appPkgPath;
|
|
|
|
}
|
2024-03-16 20:01:34 +08:00
|
|
|
buildIndexHtml();
|
2023-08-02 00:07:52 +08:00
|
|
|
};
|
2023-09-15 08:51:20 +08:00
|
|
|
|
|
|
|
exports.genTsConfigPaths = function genTsConfigPaths() {
|
2023-12-21 20:39:11 +08:00
|
|
|
try {
|
|
|
|
fs.unlinkSync(resolve(process.cwd(), 'node_modules/.bin/tsx'));
|
|
|
|
fs.symlinkSync(
|
|
|
|
resolve(process.cwd(), 'node_modules/tsx/dist/cli.mjs'),
|
|
|
|
resolve(process.cwd(), 'node_modules/.bin/tsx'),
|
|
|
|
'file',
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2023-09-15 08:51:20 +08:00
|
|
|
const cwd = process.cwd();
|
|
|
|
const cwdLength = cwd.length;
|
|
|
|
const paths = {
|
|
|
|
'@@/*': ['.dumi/tmp/*'],
|
|
|
|
};
|
|
|
|
const packages = fg.sync(['packages/*/*/package.json', 'packages/*/*/*/package.json'], {
|
|
|
|
absolute: true,
|
|
|
|
onlyFiles: true,
|
|
|
|
});
|
|
|
|
packages.forEach((packageFile) => {
|
|
|
|
const packageJsonName = require(packageFile).name;
|
|
|
|
const packageDir = dirname(packageFile);
|
2023-11-27 09:07:16 +08:00
|
|
|
const relativePath = packageDir
|
|
|
|
.slice(cwdLength + 1)
|
|
|
|
.split(sep)
|
|
|
|
.join('/');
|
2023-09-23 10:52:14 +08:00
|
|
|
paths[`${packageJsonName}/client`] = [`${relativePath}/src/client`];
|
2023-12-21 20:39:11 +08:00
|
|
|
paths[`${packageJsonName}/package.json`] = [`${relativePath}/package.json`];
|
|
|
|
paths[packageJsonName] = [`${relativePath}/src`];
|
|
|
|
if (packageJsonName === '@nocobase/test') {
|
|
|
|
paths[`${packageJsonName}/server`] = [`${relativePath}/src/server`];
|
|
|
|
paths[`${packageJsonName}/e2e`] = [`${relativePath}/src/e2e`];
|
|
|
|
}
|
2023-12-29 09:13:00 +08:00
|
|
|
if (packageJsonName === '@nocobase/plugin-workflow-test') {
|
|
|
|
paths[`${packageJsonName}/e2e`] = [`${relativePath}/src/e2e`];
|
|
|
|
}
|
2023-09-15 08:51:20 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const tsConfigJsonPath = join(cwd, './tsconfig.paths.json');
|
|
|
|
const content = { compilerOptions: { paths } };
|
|
|
|
writeFileSync(tsConfigJsonPath, JSON.stringify(content, null, 2), 'utf-8');
|
|
|
|
return content;
|
|
|
|
};
|
2023-12-21 20:39:11 +08:00
|
|
|
|
|
|
|
function generatePlaywrightPath(clean = false) {
|
|
|
|
try {
|
|
|
|
const playwright = resolve(process.cwd(), 'storage/playwright/tests');
|
|
|
|
if (clean && fs.existsSync(playwright)) {
|
|
|
|
fs.rmSync(dirname(playwright), { force: true, recursive: true });
|
|
|
|
}
|
|
|
|
if (!fs.existsSync(playwright)) {
|
|
|
|
const testPkg = require.resolve('@nocobase/test/package.json');
|
|
|
|
fs.cpSync(resolve(dirname(testPkg), 'playwright/tests'), playwright, { recursive: true });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.generatePlaywrightPath = generatePlaywrightPath;
|
|
|
|
|
2024-01-08 19:05:14 +08:00
|
|
|
function parseEnv(name) {
|
|
|
|
if (name === 'DB_UNDERSCORED') {
|
|
|
|
if (process.env.DB_UNDERSCORED === 'true') {
|
|
|
|
return 'true';
|
|
|
|
}
|
|
|
|
if (process.env.DB_UNDERSCORED) {
|
|
|
|
return 'true';
|
|
|
|
}
|
|
|
|
return 'false';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 20:01:34 +08:00
|
|
|
function buildIndexHtml(force = false) {
|
|
|
|
const file = `${process.env.APP_PACKAGE_ROOT}/dist/client/index.html`;
|
|
|
|
if (!fs.existsSync(file)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const tpl = `${process.env.APP_PACKAGE_ROOT}/dist/client/index.html.tpl`;
|
|
|
|
if (force && fs.existsSync(tpl)) {
|
|
|
|
fs.rmSync(tpl);
|
|
|
|
}
|
|
|
|
if (!fs.existsSync(tpl)) {
|
|
|
|
fs.copyFileSync(file, tpl);
|
|
|
|
}
|
|
|
|
const data = fs.readFileSync(tpl, 'utf-8');
|
|
|
|
const replacedData = data
|
|
|
|
.replace(/\{\{env.APP_PUBLIC_PATH\}\}/g, process.env.APP_PUBLIC_PATH)
|
|
|
|
.replace(/\{\{env.API_BASE_URL\}\}/g, process.env.API_BASE_URL || process.env.API_BASE_PATH)
|
|
|
|
.replace(/\{\{env.WS_URL\}\}/g, process.env.WEBSOCKET_URL || '')
|
|
|
|
.replace(/\{\{env.WS_PATH\}\}/g, process.env.WS_PATH)
|
|
|
|
.replace('src="/umi.', `src="${process.env.APP_PUBLIC_PATH}umi.`);
|
|
|
|
fs.writeFileSync(file, replacedData, 'utf-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.buildIndexHtml = buildIndexHtml;
|
|
|
|
|
2023-12-21 20:39:11 +08:00
|
|
|
exports.initEnv = function initEnv() {
|
|
|
|
const env = {
|
|
|
|
APP_ENV: 'development',
|
|
|
|
APP_KEY: 'test-jwt-secret',
|
|
|
|
APP_PORT: 13000,
|
|
|
|
API_BASE_PATH: '/api/',
|
|
|
|
DB_DIALECT: 'sqlite',
|
|
|
|
DB_STORAGE: 'storage/db/nocobase.sqlite',
|
|
|
|
DB_TIMEZONE: '+00:00',
|
2024-01-08 19:05:14 +08:00
|
|
|
DB_UNDERSCORED: parseEnv('DB_UNDERSCORED'),
|
2023-12-21 20:39:11 +08:00
|
|
|
DEFAULT_STORAGE_TYPE: 'local',
|
|
|
|
LOCAL_STORAGE_DEST: 'storage/uploads',
|
|
|
|
PLUGIN_STORAGE_PATH: resolve(process.cwd(), 'storage/plugins'),
|
|
|
|
MFSU_AD: 'none',
|
2024-01-13 18:05:22 +08:00
|
|
|
WS_PATH: '/ws',
|
2024-02-20 10:35:24 +08:00
|
|
|
SOCKET_PATH: 'storage/gateway.sock',
|
2023-12-21 20:39:11 +08:00
|
|
|
NODE_MODULES_PATH: resolve(process.cwd(), 'node_modules'),
|
|
|
|
PM2_HOME: resolve(process.cwd(), './storage/.pm2'),
|
|
|
|
PLUGIN_PACKAGE_PREFIX: '@nocobase/plugin-,@nocobase/plugin-sample-,@nocobase/preset-',
|
|
|
|
SERVER_TSCONFIG_PATH: './tsconfig.server.json',
|
|
|
|
PLAYWRIGHT_AUTH_FILE: resolve(process.cwd(), 'storage/playwright/.auth/admin.json'),
|
2024-02-20 10:35:24 +08:00
|
|
|
CACHE_DEFAULT_STORE: 'memory',
|
|
|
|
CACHE_MEMORY_MAX: 2000,
|
2024-03-16 20:01:34 +08:00
|
|
|
PLUGIN_STATICS_PATH: '/static/plugins/',
|
2024-02-20 10:35:24 +08:00
|
|
|
LOGGER_BASE_PATH: 'storage/logs',
|
2024-03-16 20:01:34 +08:00
|
|
|
APP_SERVER_BASE_URL: '',
|
|
|
|
APP_PUBLIC_PATH: '/',
|
2023-12-21 20:39:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (
|
|
|
|
!process.env.APP_ENV_PATH &&
|
|
|
|
process.argv[2] &&
|
|
|
|
['test', 'test:client', 'test:server'].includes(process.argv[2])
|
|
|
|
) {
|
|
|
|
if (fs.existsSync(resolve(process.cwd(), '.env.test'))) {
|
|
|
|
process.env.APP_ENV_PATH = '.env.test';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 09:13:00 +08:00
|
|
|
if (!process.env.APP_ENV_PATH && process.argv[2] === 'e2e') {
|
2023-12-21 20:39:11 +08:00
|
|
|
// 用于存放 playwright 自动生成的相关的文件
|
|
|
|
generatePlaywrightPath();
|
|
|
|
if (!fs.existsSync('.env.e2e') && fs.existsSync('.env.e2e.example')) {
|
|
|
|
const env = fs.readFileSync('.env.e2e.example');
|
|
|
|
fs.writeFileSync('.env.e2e', env);
|
|
|
|
}
|
|
|
|
if (!fs.existsSync('.env.e2e')) {
|
|
|
|
throw new Error('Please create .env.e2e file first!');
|
|
|
|
}
|
|
|
|
process.env.APP_ENV_PATH = '.env.e2e';
|
|
|
|
}
|
|
|
|
|
|
|
|
dotenv.config({
|
|
|
|
path: resolve(process.cwd(), process.env.APP_ENV_PATH || '.env'),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (process.argv[2] === 'e2e' && !process.env.APP_BASE_URL) {
|
|
|
|
process.env.APP_BASE_URL = `http://127.0.0.1:${process.env.APP_PORT}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key in env) {
|
|
|
|
if (!process.env[key]) {
|
|
|
|
process.env[key] = env[key];
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 20:01:34 +08:00
|
|
|
|
|
|
|
if (process.env.APP_PUBLIC_PATH) {
|
|
|
|
const publicPath = process.env.APP_PUBLIC_PATH.replace(/\/$/g, '');
|
|
|
|
const keys = ['API_BASE_PATH', 'WS_PATH', 'PLUGIN_STATICS_PATH'];
|
|
|
|
for (const key of keys) {
|
|
|
|
process.env[key] = publicPath + process.env[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.APP_SERVER_BASE_URL && !process.env.API_BASE_URL) {
|
|
|
|
process.env.API_BASE_URL = process.env.APP_SERVER_BASE_URL + process.env.API_BASE_PATH;
|
|
|
|
}
|
2023-12-21 20:39:11 +08:00
|
|
|
};
|