From c940873ec8cbfdd3542ae7c31e81180b4f53bafa Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Wed, 20 Apr 2022 23:36:05 +0800 Subject: [PATCH] Fix create nocobase app (#305) * chore: create-nocobase-app * chore: change create-nocobase-app lib to src --- package.json | 2 +- .../bin/create-nocobase-app.js | 2 +- .../core/create-nocobase-app/package.json | 2 +- .../{lib => src}/create-app.js | 101 ++++++++++++++---- .../{lib => src}/create-nocobase-app.js | 0 .../create-nocobase-app/{lib => src}/index.js | 0 .../resources/files/.eslintignore | 0 .../{lib => src}/resources/files/.eslintrc | 0 .../{lib => src}/resources/files/.gitignore | 0 .../resources/files/.prettierignore | 0 .../{lib => src}/resources/files/.prettierrc | 0 .../{lib => src}/resources/files/jest.cli.js | 0 .../resources/files/jest.config.js | 0 .../resources/files/jest.setup.ts | 0 .../{lib => src}/resources/files/lerna.json | 0 .../files/packages/app/client/.gitkeep | 0 .../files/packages/app/client/.umirc.ts | 0 .../files/packages/app/server/.gitkeep | 0 .../packages/app/server/tsconfig.build.json | 0 .../files/packages/app/server/tsconfig.json | 0 .../resources/files/packages/plugins/.gitkeep | 0 .../resources/files/pnpm-workspace.yaml | 0 .../resources/files/tsconfig.build.json | 0 .../resources/files/tsconfig.jest.json | 0 .../resources/files/tsconfig.json | 0 .../{lib => src}/resources/templates/env.js | 14 ++- .../resources/templates/env.template | 0 .../resources/templates/load-src-from-npm.js | 0 .../resources/templates/package.json.js | 2 +- .../templates/server.package.json.js | 1 + .../create-nocobase-app/{lib => src}/utils.js | 0 31 files changed, 96 insertions(+), 28 deletions(-) rename packages/core/create-nocobase-app/{lib => src}/create-app.js (51%) rename packages/core/create-nocobase-app/{lib => src}/create-nocobase-app.js (100%) rename packages/core/create-nocobase-app/{lib => src}/index.js (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/.eslintignore (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/.eslintrc (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/.gitignore (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/.prettierignore (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/.prettierrc (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/jest.cli.js (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/jest.config.js (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/jest.setup.ts (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/lerna.json (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/packages/app/client/.gitkeep (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/packages/app/client/.umirc.ts (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/packages/app/server/.gitkeep (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/packages/app/server/tsconfig.build.json (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/packages/app/server/tsconfig.json (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/packages/plugins/.gitkeep (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/pnpm-workspace.yaml (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/tsconfig.build.json (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/tsconfig.jest.json (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/files/tsconfig.json (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/templates/env.js (62%) rename packages/core/create-nocobase-app/{lib => src}/resources/templates/env.template (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/templates/load-src-from-npm.js (100%) rename packages/core/create-nocobase-app/{lib => src}/resources/templates/package.json.js (96%) rename packages/core/create-nocobase-app/{lib => src}/resources/templates/server.package.json.js (96%) rename packages/core/create-nocobase-app/{lib => src}/utils.js (100%) diff --git a/package.json b/package.json index 35cf5656a..c694938e3 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "packages/plugins/*" ], "scripts": { - "start": "concurrently \"npm run start-server\" \"npm run start-client\"", + "start": "concurrently --kill-others \"npm run start-server\" \"npm run start-client\"", "start-pm2": "yarn pm2-runtime start packages/app/server/lib/index.js", "start-docs": "dumi dev", "bootstrap": "lerna bootstrap", diff --git a/packages/core/create-nocobase-app/bin/create-nocobase-app.js b/packages/core/create-nocobase-app/bin/create-nocobase-app.js index 509a3a7ab..8cca84635 100755 --- a/packages/core/create-nocobase-app/bin/create-nocobase-app.js +++ b/packages/core/create-nocobase-app/bin/create-nocobase-app.js @@ -1,3 +1,3 @@ #!/usr/bin/env node -require('../lib/create-nocobase-app'); +require('../src/create-nocobase-app'); diff --git a/packages/core/create-nocobase-app/package.json b/packages/core/create-nocobase-app/package.json index a2a18a009..d07e0f03b 100755 --- a/packages/core/create-nocobase-app/package.json +++ b/packages/core/create-nocobase-app/package.json @@ -1,7 +1,7 @@ { "name": "create-nocobase-app", "version": "0.6.2-alpha.8", - "main": "lib/index.js", + "main": "src/index.js", "files": [ "lib", "bin", diff --git a/packages/core/create-nocobase-app/lib/create-app.js b/packages/core/create-nocobase-app/src/create-app.js similarity index 51% rename from packages/core/create-nocobase-app/lib/create-app.js rename to packages/core/create-nocobase-app/src/create-app.js index ce69758ac..c6e10c780 100644 --- a/packages/core/create-nocobase-app/lib/create-app.js +++ b/packages/core/create-nocobase-app/src/create-app.js @@ -11,30 +11,87 @@ const createPackageJson = require('./resources/templates/package.json.js'); const createServerPackageJson = require('./resources/templates/server.package.json.js'); const loadSrcFromNpm = require('./resources/templates/load-src-from-npm'); +let envs = undefined; + +const parseEnvs = (options) => { + if (envs) { + return envs; + } + + for (const env of options.env) { + if (!env.match(/\w+=\w+/)) { + console.log(`${chalk.red(env)} is not a valid environment value`); + process.exit(1); + } + } + + envs = options.env + .map((env) => env.split('=')) + .reduce((carry, item) => { + carry[item[0]] = item[1]; + return carry; + }, {}); + + return envs; +}; + +function checkDialect(dialect) { + const supportDialects = ['mysql', 'sqlite', 'postgres']; + if (!supportDialects.includes(dialect)) { + console.log( + `dialect ${chalk.red(dialect)} is not supported, currently supported dialects are ${chalk.green( + supportDialects.join(','), + )}.`, + ); + process.exit(1); + } +} + const getDatabaseOptionsFromCommandOptions = (commandOptions) => { - if ( - commandOptions.quickstart || - !commandOptions.dbdialect || - commandOptions.dbdialect === 'sqlite' || - commandOptions.dbstorage - ) { + const envs = parseEnvs(commandOptions); + + if (!commandOptions.dbDialect || commandOptions.dbDialect === 'sqlite' || envs['DB_STORAGE']) { return { dialect: 'sqlite', - storage: commandOptions.dbstorage || 'db.sqlite', + storage: envs['DB_STORAGE'] || 'db.sqlite', }; } - return { - dialect: commandOptions.dbdialect, - host: commandOptions.dbhost, - port: commandOptions.dbport, - database: commandOptions.dbdatabase, - username: commandOptions.dbusername, - password: commandOptions.dbpassword, + const databaseOptions = { + dialect: commandOptions.dbDialect, + host: envs['DB_HOST'], + port: envs['DB_PORT'], + database: envs['DB_DATABASE'], + username: envs['DB_USERNAME'], + password: envs['DB_PASSWORD'], }; + + const emptyValues = Object.entries(databaseOptions).filter((items) => !items[1]); + + if (emptyValues.length > 0) { + console.log( + chalk.red( + `Please set ${emptyValues + .map((i) => i[0]) + .map((i) => `DB_${i.toUpperCase()}`) + .join(' ')} in .env file to complete database settings`, + ), + ); + } + + return databaseOptions; }; async function createApp(directory, options) { + const dbDialect = options.dbDialect || 'sqlite'; + checkDialect(dbDialect); + + if (options.quickstart) { + console.log(`⚠️ ${chalk.yellow('quickstart option is deprecated')}`); + } + + parseEnvs(options); + console.log(`Creating a new NocoBase application at ${chalk.green(directory)}.`); console.log('Creating files.'); @@ -53,7 +110,7 @@ async function createApp(directory, options) { await loadSrcFromNpm('@nocobase/app-client', path.join(projectPath, 'packages/app/client')); // write .env file - await fse.writeFile(join(projectPath, '.env'), createEnvFile({ dbOptions })); + await fse.writeFile(join(projectPath, '.env'), createEnvFile({ dbOptions, envs: parseEnvs(options) })); // write root packages.json await fse.writeJson( @@ -79,19 +136,19 @@ async function createApp(directory, options) { ); // run install command + console.log('finished'); +} + +function collect(value, previous) { + return previous.concat([value]); } function setCommandOptions(command) { return command .arguments('', 'directory of new NocoBase app') .option('--quickstart', 'Quickstart app creation') - .option('--dbdialect ', 'Database dialect, current support sqlite/mysql/postgres') - .option('--dbhost ', 'Database host') - .option('--dbport ', 'Database port') - .option('--dbdatabase ', 'Database name') - .option('--dbusername ', 'Database username') - .option('--dbpassword ', 'Database password') - .option('--dbstorage ', 'Database file storage path for sqlite') + .option('-d, --db-dialect ', 'Database dialect, current support sqlite/mysql/postgres') + .option('-e, --env ', 'environment variables write into .env file', collect, []) .description('create a new application'); } diff --git a/packages/core/create-nocobase-app/lib/create-nocobase-app.js b/packages/core/create-nocobase-app/src/create-nocobase-app.js similarity index 100% rename from packages/core/create-nocobase-app/lib/create-nocobase-app.js rename to packages/core/create-nocobase-app/src/create-nocobase-app.js diff --git a/packages/core/create-nocobase-app/lib/index.js b/packages/core/create-nocobase-app/src/index.js similarity index 100% rename from packages/core/create-nocobase-app/lib/index.js rename to packages/core/create-nocobase-app/src/index.js diff --git a/packages/core/create-nocobase-app/lib/resources/files/.eslintignore b/packages/core/create-nocobase-app/src/resources/files/.eslintignore similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/.eslintignore rename to packages/core/create-nocobase-app/src/resources/files/.eslintignore diff --git a/packages/core/create-nocobase-app/lib/resources/files/.eslintrc b/packages/core/create-nocobase-app/src/resources/files/.eslintrc similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/.eslintrc rename to packages/core/create-nocobase-app/src/resources/files/.eslintrc diff --git a/packages/core/create-nocobase-app/lib/resources/files/.gitignore b/packages/core/create-nocobase-app/src/resources/files/.gitignore similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/.gitignore rename to packages/core/create-nocobase-app/src/resources/files/.gitignore diff --git a/packages/core/create-nocobase-app/lib/resources/files/.prettierignore b/packages/core/create-nocobase-app/src/resources/files/.prettierignore similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/.prettierignore rename to packages/core/create-nocobase-app/src/resources/files/.prettierignore diff --git a/packages/core/create-nocobase-app/lib/resources/files/.prettierrc b/packages/core/create-nocobase-app/src/resources/files/.prettierrc similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/.prettierrc rename to packages/core/create-nocobase-app/src/resources/files/.prettierrc diff --git a/packages/core/create-nocobase-app/lib/resources/files/jest.cli.js b/packages/core/create-nocobase-app/src/resources/files/jest.cli.js similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/jest.cli.js rename to packages/core/create-nocobase-app/src/resources/files/jest.cli.js diff --git a/packages/core/create-nocobase-app/lib/resources/files/jest.config.js b/packages/core/create-nocobase-app/src/resources/files/jest.config.js similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/jest.config.js rename to packages/core/create-nocobase-app/src/resources/files/jest.config.js diff --git a/packages/core/create-nocobase-app/lib/resources/files/jest.setup.ts b/packages/core/create-nocobase-app/src/resources/files/jest.setup.ts similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/jest.setup.ts rename to packages/core/create-nocobase-app/src/resources/files/jest.setup.ts diff --git a/packages/core/create-nocobase-app/lib/resources/files/lerna.json b/packages/core/create-nocobase-app/src/resources/files/lerna.json similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/lerna.json rename to packages/core/create-nocobase-app/src/resources/files/lerna.json diff --git a/packages/core/create-nocobase-app/lib/resources/files/packages/app/client/.gitkeep b/packages/core/create-nocobase-app/src/resources/files/packages/app/client/.gitkeep similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/packages/app/client/.gitkeep rename to packages/core/create-nocobase-app/src/resources/files/packages/app/client/.gitkeep diff --git a/packages/core/create-nocobase-app/lib/resources/files/packages/app/client/.umirc.ts b/packages/core/create-nocobase-app/src/resources/files/packages/app/client/.umirc.ts similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/packages/app/client/.umirc.ts rename to packages/core/create-nocobase-app/src/resources/files/packages/app/client/.umirc.ts diff --git a/packages/core/create-nocobase-app/lib/resources/files/packages/app/server/.gitkeep b/packages/core/create-nocobase-app/src/resources/files/packages/app/server/.gitkeep similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/packages/app/server/.gitkeep rename to packages/core/create-nocobase-app/src/resources/files/packages/app/server/.gitkeep diff --git a/packages/core/create-nocobase-app/lib/resources/files/packages/app/server/tsconfig.build.json b/packages/core/create-nocobase-app/src/resources/files/packages/app/server/tsconfig.build.json similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/packages/app/server/tsconfig.build.json rename to packages/core/create-nocobase-app/src/resources/files/packages/app/server/tsconfig.build.json diff --git a/packages/core/create-nocobase-app/lib/resources/files/packages/app/server/tsconfig.json b/packages/core/create-nocobase-app/src/resources/files/packages/app/server/tsconfig.json similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/packages/app/server/tsconfig.json rename to packages/core/create-nocobase-app/src/resources/files/packages/app/server/tsconfig.json diff --git a/packages/core/create-nocobase-app/lib/resources/files/packages/plugins/.gitkeep b/packages/core/create-nocobase-app/src/resources/files/packages/plugins/.gitkeep similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/packages/plugins/.gitkeep rename to packages/core/create-nocobase-app/src/resources/files/packages/plugins/.gitkeep diff --git a/packages/core/create-nocobase-app/lib/resources/files/pnpm-workspace.yaml b/packages/core/create-nocobase-app/src/resources/files/pnpm-workspace.yaml similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/pnpm-workspace.yaml rename to packages/core/create-nocobase-app/src/resources/files/pnpm-workspace.yaml diff --git a/packages/core/create-nocobase-app/lib/resources/files/tsconfig.build.json b/packages/core/create-nocobase-app/src/resources/files/tsconfig.build.json similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/tsconfig.build.json rename to packages/core/create-nocobase-app/src/resources/files/tsconfig.build.json diff --git a/packages/core/create-nocobase-app/lib/resources/files/tsconfig.jest.json b/packages/core/create-nocobase-app/src/resources/files/tsconfig.jest.json similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/tsconfig.jest.json rename to packages/core/create-nocobase-app/src/resources/files/tsconfig.jest.json diff --git a/packages/core/create-nocobase-app/lib/resources/files/tsconfig.json b/packages/core/create-nocobase-app/src/resources/files/tsconfig.json similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/files/tsconfig.json rename to packages/core/create-nocobase-app/src/resources/files/tsconfig.json diff --git a/packages/core/create-nocobase-app/lib/resources/templates/env.js b/packages/core/create-nocobase-app/src/resources/templates/env.js similarity index 62% rename from packages/core/create-nocobase-app/lib/resources/templates/env.js rename to packages/core/create-nocobase-app/src/resources/templates/env.js index e0d997cc1..70bb4fa91 100644 --- a/packages/core/create-nocobase-app/lib/resources/templates/env.js +++ b/packages/core/create-nocobase-app/src/resources/templates/env.js @@ -6,7 +6,7 @@ const _ = require('lodash'); const generateASecret = () => crypto.randomBytes(256).toString('base64'); module.exports = (options) => { - const { dbOptions } = options; + const { dbOptions, envs } = options; const tmpl = fs.readFileSync(path.join(__dirname, 'env.template')); const compile = _.template(tmpl); @@ -15,10 +15,20 @@ module.exports = (options) => { .map((item) => `${item[0]}=${item[1] || ''}`) .join('\n'); - const envContent = compile({ + let envContent = compile({ jwtSecret: generateASecret(), dbEnvs, }); + for (const env of Object.entries(envs)) { + const [key, value] = env; + const re = new RegExp(`${key}=\\w+`); + if (envContent.match(re)) { + envContent = envContent.replace(re, `${key}=${value}`); + } else { + envContent = `${envContent}${key}=${value}\n`; + } + } + return envContent; }; diff --git a/packages/core/create-nocobase-app/lib/resources/templates/env.template b/packages/core/create-nocobase-app/src/resources/templates/env.template similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/templates/env.template rename to packages/core/create-nocobase-app/src/resources/templates/env.template diff --git a/packages/core/create-nocobase-app/lib/resources/templates/load-src-from-npm.js b/packages/core/create-nocobase-app/src/resources/templates/load-src-from-npm.js similarity index 100% rename from packages/core/create-nocobase-app/lib/resources/templates/load-src-from-npm.js rename to packages/core/create-nocobase-app/src/resources/templates/load-src-from-npm.js diff --git a/packages/core/create-nocobase-app/lib/resources/templates/package.json.js b/packages/core/create-nocobase-app/src/resources/templates/package.json.js similarity index 96% rename from packages/core/create-nocobase-app/lib/resources/templates/package.json.js rename to packages/core/create-nocobase-app/src/resources/templates/package.json.js index 0a12ab36d..dd49288d6 100644 --- a/packages/core/create-nocobase-app/lib/resources/templates/package.json.js +++ b/packages/core/create-nocobase-app/src/resources/templates/package.json.js @@ -7,7 +7,7 @@ module.exports = (opts) => { workspaces: ['packages/app/*', 'packages/plugins/*'], license: 'MIT', scripts: { - start: 'concurrently "npm run start-server" "npm run start-client"', + start: 'concurrently --kill-others "npm run start-server" "npm run start-client"', bootstrap: 'lerna bootstrap', clean: 'rimraf -rf packages/{app,plugins}/*/{lib,esm,dist} && lerna clean', nocobase: diff --git a/packages/core/create-nocobase-app/lib/resources/templates/server.package.json.js b/packages/core/create-nocobase-app/src/resources/templates/server.package.json.js similarity index 96% rename from packages/core/create-nocobase-app/lib/resources/templates/server.package.json.js rename to packages/core/create-nocobase-app/src/resources/templates/server.package.json.js index c1394f7ec..bec66f0e9 100644 --- a/packages/core/create-nocobase-app/lib/resources/templates/server.package.json.js +++ b/packages/core/create-nocobase-app/src/resources/templates/server.package.json.js @@ -15,6 +15,7 @@ const dialectLib = (dialect) => { if (dialect === 'postgres') { return { pg: '^8.7.3', + 'pg-hstore': '^2.3.4', }; } }; diff --git a/packages/core/create-nocobase-app/lib/utils.js b/packages/core/create-nocobase-app/src/utils.js similarity index 100% rename from packages/core/create-nocobase-app/lib/utils.js rename to packages/core/create-nocobase-app/src/utils.js