From c64e32945e440ba856c4dbef396d805a5321ea0c Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 2 Dec 2022 14:23:07 +0800 Subject: [PATCH] fix: yarn start error in windows system (#1177) * feat: add .env.test.example * fix: yarn start error in windows system --- .env.test.example | 69 +++++++++++++++++++ docs/zh-CN/development/server/test.md | 4 ++ packages/core/cli/bin/index.js | 1 + packages/core/cli/src/commands/postinstall.js | 4 ++ packages/core/cli/src/commands/start.js | 6 -- packages/plugins/client/src/server.ts | 23 ++++--- storage/.gitignore | 1 + 7 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 .env.test.example diff --git a/.env.test.example b/.env.test.example new file mode 100644 index 000000000..d6c983ea6 --- /dev/null +++ b/.env.test.example @@ -0,0 +1,69 @@ +################# NOCOBASE APPLICATION ################# + +APP_ENV=development +APP_PORT=13000 +APP_KEY=test-key + +API_BASE_PATH=/api/ +API_BASE_URL= + +PROXY_TARGET_URL= + +################# DATABASE ################# + +DB_DIALECT=sqlite +DB_STORAGE=storage/db/nocobase-test.sqlite +# DB_HOST=localhost +# DB_PORT=5432 +# DB_DATABASE=nocobase-test +# DB_USER=nocobase +# DB_PASSWORD=nocobase +# DB_LOGGING=on + +################# CACHE ################# +# default is memory cache, when develop mode,code's change will be clear memory cache, so can use 'cache-manager-fs-hash' +# CACHE_CONFIG={"storePackage":"cache-manager-fs-hash","ttl":86400,"max":1000} + +################# STORAGE (Initialization only) ################# + +INIT_ROOT_EMAIL=admin@nocobase.com +INIT_ROOT_PASSWORD=admin123 +INIT_ROOT_NICKNAME=Super Admin + +# local or ali-oss +DEFAULT_STORAGE_TYPE=local + +# LOCAL STORAGE +LOCAL_STORAGE_BASE_URL= +LOCAL_STORAGE_DEST=storage/uploads + +# ALI OSS STORAGE +ALI_OSS_STORAGE_BASE_URL= +ALI_OSS_REGION=oss-cn-beijing +ALI_OSS_ACCESS_KEY_ID= +ALI_OSS_ACCESS_KEY_SECRET= +ALI_OSS_BUCKET= + +# Tencent COS STORAGE +TX_COS_STORAGE_BASE_URL= +TX_COS_REGION=ap-guangzhou +TX_COS_SECRET_ID= +TX_COS_SECRET_KEY= +TX_COS_BUCKET= + +# AWS +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= +AWS_S3_REGION= +AWS_S3_BUCKET= +AWS_S3_STORAGE_BASE_URL= + +# ALI SMS VERIFY CODE CONFIG +INIT_ALI_SMS_ACCESS_KEY= +INIT_ALI_SMS_ACCESS_KEY_SECRET= +INIT_ALI_SMS_ENDPOINT= +INIT_ALI_SMS_VERIFY_CODE_TEMPLATE= +INIT_ALI_SMS_VERIFY_CODE_SIGN= + +# use any string name (no space) +DEFAULT_SMS_VERIFY_CODE_PROVIDER= diff --git a/docs/zh-CN/development/server/test.md b/docs/zh-CN/development/server/test.md index 2964f5087..03bdff8b6 100644 --- a/docs/zh-CN/development/server/test.md +++ b/docs/zh-CN/development/server/test.md @@ -2,6 +2,10 @@ 测试基于 [Jest](https://jestjs.io/) 测试框架。为了方便的编写测试,提供了 `mockDatabase()` 和 `mockServer()` 用于数据库和服务端应用的测试。 + +避免破坏生产环境数据,建议使用 `.env.test` 文件来配置测试环境的环境变量,并使用独立的测试数据库进行测试。 + + ## `mockDatabase()` 默认提供一种完全隔离的 db 测试环境 diff --git a/packages/core/cli/bin/index.js b/packages/core/cli/bin/index.js index f4fc029e2..06bba306e 100755 --- a/packages/core/cli/bin/index.js +++ b/packages/core/cli/bin/index.js @@ -16,6 +16,7 @@ const env = { DEFAULT_STORAGE_TYPE: 'local', LOCAL_STORAGE_DEST: 'storage/uploads', MFSU_AD: 'none', + PM2_HOME: resolve(process.cwd(), './storage/.pm2'), }; if (!process.env.APP_ENV_PATH && process.argv[2] && process.argv[2] === 'test') { diff --git a/packages/core/cli/src/commands/postinstall.js b/packages/core/cli/src/commands/postinstall.js index b80265950..94e6439bb 100644 --- a/packages/core/cli/src/commands/postinstall.js +++ b/packages/core/cli/src/commands/postinstall.js @@ -21,6 +21,10 @@ module.exports = (cli) => { const content = await readFile(resolve(cwd, '.env.example'), 'utf-8'); await writeFile(resolve(cwd, '.env'), content, 'utf-8'); } + if (!existsSync(resolve(cwd, '.env.test')) && existsSync(resolve(cwd, '.env.test.example'))) { + const content = await readFile(resolve(cwd, '.env.test.example'), 'utf-8'); + await writeFile(resolve(cwd, '.env.test'), content, 'utf-8'); + } if (!isPackageValid('umi')) { return; } diff --git a/packages/core/cli/src/commands/start.js b/packages/core/cli/src/commands/start.js index 63653a5e3..b31ff1a3b 100644 --- a/packages/core/cli/src/commands/start.js +++ b/packages/core/cli/src/commands/start.js @@ -20,12 +20,6 @@ module.exports = (cli) => { if (opts.port) { process.env.APP_PORT = opts.port; } - if (process.platform === 'win32') { - console.log( - chalk.yellow(`It is not supported on win platform, please use \`${chalk.green('yarn dev')}\` instead`), - ); - return; - } if (process.argv.includes('-h') || process.argv.includes('--help')) { promptForTs(); run('ts-node', [ diff --git a/packages/plugins/client/src/server.ts b/packages/plugins/client/src/server.ts index 79949efbf..dfd819d4d 100644 --- a/packages/plugins/client/src/server.ts +++ b/packages/plugins/client/src/server.ts @@ -101,16 +101,19 @@ export class ClientPlugin extends Plugin { root = resolve(process.cwd(), root); } if (process.env.APP_ENV !== 'production' && root) { - this.app.middleware.nodes.unshift(async (ctx, next) => { - if (ctx.path.startsWith(this.app.resourcer.options.prefix)) { - return next(); - } - await serve(root)(ctx, next); - // console.log('koa-send', root, ctx.status); - if (ctx.status == 404) { - return send(ctx, 'index.html', { root }); - } - }); + this.app.use( + async (ctx, next) => { + if (ctx.path.startsWith(this.app.resourcer.options.prefix)) { + return next(); + } + await serve(root)(ctx, next); + // console.log('koa-send', root, ctx.status); + if (ctx.status == 404) { + return send(ctx, 'index.html', { root }); + } + }, + { tag: 'clientStatic', before: 'cors' }, + ); } } } diff --git a/storage/.gitignore b/storage/.gitignore index e69de29bb..bde6210e9 100644 --- a/storage/.gitignore +++ b/storage/.gitignore @@ -0,0 +1 @@ +.pm2 \ No newline at end of file