From 72c1685fa6d469869314fb730a886a5542ffb18d Mon Sep 17 00:00:00 2001 From: "a.bouhuolia" Date: Mon, 13 Feb 2023 20:47:09 +0200 Subject: [PATCH] feat(server): move all cli commands codebase to be TS based. --- packages/server/bin/utils.js | 51 ------------ packages/server/package.json | 4 +- packages/server/scripts/webpack.cli.js | 11 +++ packages/server/scripts/webpack.common.js | 76 ++++++++++++++++++ packages/server/scripts/webpack.config.js | 77 ++----------------- .../commands/bigcapital.ts} | 14 ++-- packages/server/src/commands/index.ts | 4 + .../server/src/config/{index.js => index.ts} | 2 +- 8 files changed, 109 insertions(+), 130 deletions(-) delete mode 100644 packages/server/bin/utils.js create mode 100644 packages/server/scripts/webpack.cli.js create mode 100644 packages/server/scripts/webpack.common.js rename packages/server/{bin/bigcapital.js => src/commands/bigcapital.ts} (96%) create mode 100644 packages/server/src/commands/index.ts rename packages/server/src/config/{index.js => index.ts} (99%) diff --git a/packages/server/bin/utils.js b/packages/server/bin/utils.js deleted file mode 100644 index 82c4d8cde..000000000 --- a/packages/server/bin/utils.js +++ /dev/null @@ -1,51 +0,0 @@ -const Knex = require('knex'); -const { knexSnakeCaseMappers } = require('objection'); -const color = require('colorette'); -const config = require('./config'); - -function initSystemKnex() { - return Knex({ - client: config.system.db_client, - connection: { - host: config.system.db_host, - user: config.system.db_user, - password: config.system.db_password, - database: config.system.db_name, - charset: 'utf8', - }, - migrations: { - directory: config.system.migrations_dir, - }, - seeds: { - directory: config.system.seeds_dir, - }, - pool: { min: 0, max: 7 }, - ...knexSnakeCaseMappers({ upperCase: true }), - }); -} - -function exit(text) { - if (text instanceof Error) { - console.error( - color.red(`${text.detail ? `${text.detail}\n` : ''}${text.stack}`) - ); - } else { - console.error(color.red(text)); - } - process.exit(1); -} - -function success(text) { - console.log(text); - process.exit(0); -} -function log(text) { - console.log(text); -} - -module.exports = { - log, - success, - exit, - initSystemKnex, -}; diff --git a/packages/server/package.json b/packages/server/package.json index bd7899d2e..7b23f9734 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -8,7 +8,9 @@ "clear": "rimraf build", "dev": "cross-env NODE_ENV=development webpack --config scripts/webpack.config.js", "build:resources": "gulp --gulpfile=scripts/gulpfile.js styles styles-rtl", - "build": "cross-env NODE_ENV=production webpack --config scripts/webpack.config.js", + "build:app": "cross-env NODE_ENV=production webpack --config scripts/webpack.config.js", + "build:commands": "cross-env NODE_ENV=production webpack --config scripts/webpack.cli.js", + "build": "npm-run-all build:*", "lint:fix": "eslint --fix ./**/*.ts" }, "author": "Ahmed Bouhuolia, ", diff --git a/packages/server/scripts/webpack.cli.js b/packages/server/scripts/webpack.cli.js new file mode 100644 index 000000000..b4ca9edc5 --- /dev/null +++ b/packages/server/scripts/webpack.cli.js @@ -0,0 +1,11 @@ +const { getCommonWebpackOptions } = require('./webpack.common'); + +const inputEntry = './src/commands/index.ts'; +const outputDir = '../build'; +const outputFilename = 'commands.js'; + +module.exports = getCommonWebpackOptions({ + inputEntry, + outputDir, + outputFilename, +}); diff --git a/packages/server/scripts/webpack.common.js b/packages/server/scripts/webpack.common.js new file mode 100644 index 000000000..b04d6dc4f --- /dev/null +++ b/packages/server/scripts/webpack.common.js @@ -0,0 +1,76 @@ +const path = require('path'); +const { NormalModuleReplacementPlugin } = require('webpack'); +const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); +const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin'); +const nodeExternals = require('webpack-node-externals'); +const ProgressBarPlugin = require('progress-bar-webpack-plugin'); + +const isDev = process.env.NODE_ENV === 'development'; + +exports.getCommonWebpackOptions = ({ + inputEntry, + outputDir, + outputFilename, +}) => { + const webpackOptions = { + entry: ['regenerator-runtime/runtime', inputEntry], + target: 'node', + mode: isDev ? 'development' : 'production', + watch: isDev, + watchOptions: { + aggregateTimeout: 200, + poll: 1000, + }, + output: { + path: path.resolve(__dirname, outputDir), + filename: outputFilename, + }, + resolve: { + extensions: ['.ts', '.tsx', '.js'], + extensionAlias: { + '.ts': ['.js', '.ts'], + '.cts': ['.cjs', '.cts'], + '.mts': ['.mjs', '.mts'], + }, + plugins: [ + new TsconfigPathsPlugin({ + configFile: './tsconfig.json', + extensions: ['.ts', '.tsx', '.js'], + }), + ], + }, + plugins: [ + // Ignore knex dynamic required dialects that we don't use + new NormalModuleReplacementPlugin( + /m[sy]sql2?|oracle(db)?|sqlite3|pg-(native|query)/, + 'noop2' + ), + new ProgressBarPlugin(), + ], + externals: [nodeExternals(), 'aws-sdk', 'prettier'], + module: { + rules: [ + { + test: /\.([cm]?ts|tsx|js)$/, + use: [ + { + loader: 'ts-loader', + options: { + transpileOnly: true, + configFile: 'tsconfig.json', + }, + }, + ], + exclude: /(node_modules)/, + }, + ], + }, + }; + + if (isDev) { + webpackOptions.plugins.push( + new RunScriptWebpackPlugin({ name: outputFilename }) + ); + } + return webpackOptions; +}; diff --git a/packages/server/scripts/webpack.config.js b/packages/server/scripts/webpack.config.js index cdf738bef..5a4cda373 100644 --- a/packages/server/scripts/webpack.config.js +++ b/packages/server/scripts/webpack.config.js @@ -1,74 +1,11 @@ -const path = require('path'); -const { NormalModuleReplacementPlugin } = require('webpack'); -const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); -const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin'); -const nodeExternals = require('webpack-node-externals'); -const ProgressBarPlugin = require('progress-bar-webpack-plugin'); +const { getCommonWebpackOptions } = require('./webpack.common'); -const isDev = process.env.NODE_ENV === 'development'; +const inputEntry = './src/server.ts'; const outputDir = '../build'; const outputFilename = 'index.js'; -const inputEntry = './src/server.ts'; -const webpackOptions = { - entry: ['regenerator-runtime/runtime', inputEntry], - target: 'node', - mode: isDev ? 'development' : 'production', - watch: isDev, - watchOptions: { - aggregateTimeout: 200, - poll: 1000, - }, - output: { - path: path.resolve(__dirname, outputDir), - filename: outputFilename, - }, - resolve: { - extensions: ['.ts', '.tsx', '.js'], - extensionAlias: { - '.ts': ['.js', '.ts'], - '.cts': ['.cjs', '.cts'], - '.mts': ['.mjs', '.mts'], - }, - plugins: [ - new TsconfigPathsPlugin({ - configFile: './tsconfig.json', - extensions: ['.ts', '.tsx', '.js'], - }), - ], - }, - plugins: [ - // Ignore knex dynamic required dialects that we don't use - new NormalModuleReplacementPlugin( - /m[sy]sql2?|oracle(db)?|sqlite3|pg-(native|query)/, - 'noop2' - ), - new ProgressBarPlugin(), - ], - externals: [nodeExternals(), 'aws-sdk', 'prettier'], - module: { - rules: [ - { - test: /\.([cm]?ts|tsx|js)$/, - use: [ - { - loader: 'ts-loader', - options: { - transpileOnly: true, - configFile: 'tsconfig.json', - }, - }, - ], - exclude: /(node_modules)/, - }, - ], - }, -}; - -if (isDev) { - webpackOptions.plugins.push( - new RunScriptWebpackPlugin({ name: outputFilename }) - ); -} - -module.exports = webpackOptions; +module.exports = getCommonWebpackOptions({ + inputEntry, + outputDir, + outputFilename, +}); diff --git a/packages/server/bin/bigcapital.js b/packages/server/src/commands/bigcapital.ts similarity index 96% rename from packages/server/bin/bigcapital.js rename to packages/server/src/commands/bigcapital.ts index 5224d0dc5..fa52b5e38 100644 --- a/packages/server/bin/bigcapital.js +++ b/packages/server/src/commands/bigcapital.ts @@ -1,10 +1,10 @@ #!/usr/bin/env node -const commander = require('commander'); -const color = require('colorette'); -const argv = require('getopts'); -const Knex = require('knex'); -const { knexSnakeCaseMappers } = require('objection'); -const config = require('../src/config'); +import commander from 'commander'; +import color from 'colorette'; +import argv from 'getopts'; +import Knex from 'knex'; +import { knexSnakeCaseMappers } from 'objection'; +import config from '../config'; function initSystemKnex() { return Knex({ @@ -279,4 +279,4 @@ commander exit(error); } }); -commander.parse(); + diff --git a/packages/server/src/commands/index.ts b/packages/server/src/commands/index.ts new file mode 100644 index 000000000..627eeb658 --- /dev/null +++ b/packages/server/src/commands/index.ts @@ -0,0 +1,4 @@ +import commander from 'commander'; +import './bigcapital'; + +commander.parse(); diff --git a/packages/server/src/config/index.js b/packages/server/src/config/index.ts similarity index 99% rename from packages/server/src/config/index.js rename to packages/server/src/config/index.ts index 6bc4e0116..9563d914c 100644 --- a/packages/server/src/config/index.js +++ b/packages/server/src/config/index.ts @@ -1,4 +1,4 @@ -const dotenv = require('dotenv'); +import dotenv from 'dotenv'; // Set the NODE_ENV to 'development' by default // process.env.NODE_ENV = process.env.NODE_ENV || 'development';