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