Skip to content

Commit

Permalink
fix(cli): proper error message when microservice command not run from…
Browse files Browse the repository at this point in the history
… root folder (#1961)

gh-1956
  • Loading branch information
yeshamavani authored Feb 2, 2024
1 parent eeac7f6 commit 3ea4325
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 20 deletions.
6 changes: 3 additions & 3 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $ npm install -g @sourceloop/cli
$ sl COMMAND
running command...
$ sl (-v|--version|version)
@sourceloop/cli/7.1.4 linux-x64 node-v18.19.0
@sourceloop/cli/7.1.4 darwin-arm64 node-v18.16.0
$ sl --help [COMMAND]
USAGE
$ sl COMMAND
Expand Down Expand Up @@ -139,10 +139,10 @@ OPTIONS
-s, --baseService=(authentication-service|audit-service|chat-service|notification-service|bpmn-service|feature-toggle-
service|in-mail-service|payment-service|scheduler-service|search-service|survey-service|task-service|video-conferencing-service|use
r-tenant-service)
Base sourceloop microservice
Base ARC microservice
--[no-]baseOnService
Base on sourceloop microservice or not
Base on ARC microservice or not
--customMigrations
Setup custom migration for this microservice
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sourceloop/cli",
"version": "7.1.4",
"description": "Custom Sourceloop CLI.",
"description": "Custom ARC CLI.",
"bin": {
"sl": "./bin/run"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/__tests__/suite/microservice-prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const microservicePromptsSuite = [
{
input: {
name: 'baseOnService',
message: 'Base on sourceloop microservice or not',
message: 'Base on ARC microservice or not',
type: 'confirm',
default: false,
},
Expand All @@ -81,7 +81,7 @@ export const microservicePromptsSuite = [
{
input: {
name: 'baseService',
message: 'Base sourceloop microservice',
message: 'Base ARC microservice',
type: 'list',
choices: Object.values(SERVICES),
},
Expand Down Expand Up @@ -168,7 +168,7 @@ export const microservicePromptsSuite = [
{
input: {
name: 'baseOnService',
message: 'Base on sourceloop microservice or not',
message: 'Base on ARC microservice or not',
type: 'confirm',
default: false,
},
Expand All @@ -177,7 +177,7 @@ export const microservicePromptsSuite = [
{
input: {
name: 'baseService',
message: 'Base sourceloop microservice',
message: 'Base ARC microservice',
type: 'list',
choices: Object.values(SERVICES),
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/command-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default abstract class CommandBase<T extends object> extends Command {
if (currentVersion !== latestVersion) {
this.log(
chalk.yellow(
'Current Sourceloop CLI version is v%s. To use latest features, please update to v%s',
'Current ARC CLI version is v%s. To use latest features, please update to v%s',
),
currentVersion,
latestVersion,
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/microservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export class Microservice extends Base<MicroserviceOptions> {
baseOnService: flags.boolean({
name: 'baseOnService',
exclusive: ['facade'],
description: 'Base on sourceloop microservice or not',
description: 'Base on ARC microservice or not',
allowNo: true,
}),
baseService: flags.enum({
name: 'service',
char: 's',
description: 'Base sourceloop microservice',
description: 'Base ARC microservice',
options: Object.values(SERVICES),
required: false,
exclusive: ['facade'],
Expand Down
88 changes: 83 additions & 5 deletions packages/cli/src/generators/microservice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
import fs from 'fs';
import { join } from 'path';
import {join} from 'path';
// eslint-disable-next-line @typescript-eslint/naming-convention
import AppGenerator from '../../app-generator';
import {
Expand All @@ -17,14 +17,14 @@ import {
SEQUELIZESERVICES,
SERVICES,
} from '../../enum';
import { AnyObject, MicroserviceOptions } from '../../types';
import {AnyObject, MicroserviceOptions} from '../../types';
import {
JSON_SPACING,
appendDependencies,
getDependencyVersion,
} from '../../utils';
const chalk = require('chalk'); //NOSONAR
const { promisify } = require('util');
const {promisify} = require('util');

const DATASOURCE_TEMPLATE = join(
'..',
Expand Down Expand Up @@ -69,6 +69,9 @@ const MIGRATION_FOLDER = join('packages', 'migrations');
const sourceloopMigrationPath = (packageName: SERVICES) =>
join('node_modules', `@sourceloop/${packageName}`, 'migrations');

const baseServicePath = (servicename: SERVICES) =>
join('node_modules', `@sourceloop/${servicename}`);

const BACK_TO_ROOT = join('..', '..');

const DEFAULT_NAME = 'microservice';
Expand All @@ -87,50 +90,59 @@ export default class MicroserviceGenerator extends AppGenerator<MicroserviceOpti
}

async setOptions() {
if (this.shouldExit()) return;
return super.setOptions();
}

async setFacade() {
if (this.shouldExit()) return false;
this.projectInfo.facade = this.options.facade;
}

async setBaseService() {
if (this.shouldExit()) return false;
if (this.options.baseService) {
this.projectInfo.serviceDependency = this.options.baseService;
}
}

//Loopback4 prompts
async promptProjectName() {
if (this.shouldExit()) return;
return super.promptProjectName();
}

async promptApplication() {
if (this.shouldExit()) return;
return super.promptApplication();
}

async promptOptions() {
if (this.shouldExit()) return;
return super.promptOptions();
}

async buildAppClassMixins() {
if (this.shouldExit()) return;
return super.buildAppClassMixins();
}

async setDatasource() {
if (this.shouldExit()) return false;
this.projectInfo.datasourceName = this.options.datasourceName;
this.projectInfo.datasourceClassName = this._capitalizeFirstLetter(
this.options.datasourceName,
);
this.projectInfo.datasourceConnector =
DATASOURCE_CONNECTORS[
this.options.datasourceType ?? DATASOURCES.POSTGRES
this.options.datasourceType ?? DATASOURCES.POSTGRES
];
this.projectInfo.datasourceConnectorName =
this.projectInfo.datasourceConnector;
this.projectInfo.datasourceType = this.options.datasourceType;
}
async setSequelize() {
if (this.shouldExit()) return false;
this.projectInfo.sequelize = this.options.sequelize;
if (this.projectInfo.sequelize) {
this.projectInfo.baseServiceBindingName =
Expand Down Expand Up @@ -162,6 +174,7 @@ export default class MicroserviceGenerator extends AppGenerator<MicroserviceOpti
}

async setMigrationType() {
if (this.shouldExit()) return false;
if (this.options.customMigrations) {
this.options.migrations = true;
this.projectInfo.migrationType = MIGRATIONS.CUSTOM;
Expand All @@ -174,6 +187,7 @@ export default class MicroserviceGenerator extends AppGenerator<MicroserviceOpti
}

scaffold() {
if (this.shouldExit()) return false;
const type = this.options.facade ? 'facades' : 'services';
if (!this.shouldExit()) {
if (type === 'services') {
Expand Down Expand Up @@ -204,6 +218,7 @@ export default class MicroserviceGenerator extends AppGenerator<MicroserviceOpti
}

async writing() {
if (this.shouldExit()) return false;
if (!this.shouldExit()) {
if (this.options.baseService ?? this.options.datasourceName) {
await this._createDataSourceAsync();
Expand All @@ -214,6 +229,7 @@ export default class MicroserviceGenerator extends AppGenerator<MicroserviceOpti
}

async install() {
if (this.shouldExit()) return false;
if (!this.shouldExit()) {
const packageJsonFile = join(this.destinationPath(), 'package.json');
const packageJson = this.fs.readJSON(packageJsonFile) as AnyObject;
Expand Down Expand Up @@ -258,6 +274,7 @@ export default class MicroserviceGenerator extends AppGenerator<MicroserviceOpti

await this._addMigrations();
await this._appendDockerScript();
await this._updateEnvFiles();
return true;
}
return false;
Expand All @@ -280,6 +297,7 @@ export default class MicroserviceGenerator extends AppGenerator<MicroserviceOpti
}

addScope() {
if (this.shouldExit()) return false;
let czConfig = this.fs.read(
join(this.destinationPath(), '../../', '.cz-config.js'),
);
Expand Down Expand Up @@ -311,7 +329,7 @@ export default class MicroserviceGenerator extends AppGenerator<MicroserviceOpti
async _appendDockerScript() {
const packageJsonFile = join(this.destinationRoot(), './package.json');
const packageJson = this.fs.readJSON(packageJsonFile) as AnyObject;
const scripts = { ...packageJson.scripts };
const scripts = {...packageJson.scripts};
scripts[`docker:build:${this.options.baseService}`] =
`docker build --build-arg SERVICE_NAME=${this.options.baseService} --build-arg FROM_FOLDER=services -t $REPOSITORY_URI:${this.options.baseService} -f ./services/${this.options.name}/Dockerfile .`;
packageJson.scripts = scripts;
Expand Down Expand Up @@ -606,6 +624,66 @@ export default class MicroserviceGenerator extends AppGenerator<MicroserviceOpti
}
}

async _updateEnvFiles() {
//if env.example present in base service then copy that
if (this.options.baseService) {
const envExists = this.fs.exists(
join(
this.destinationPath(),
baseServicePath(this.options.baseService),
'.env.example',
),
);
if (envExists) {
const example = this.fs.read(
join(
this.destinationPath(),
baseServicePath(this.options.baseService),
'.env.example',
),
);
const defaults = this.fs.read(
join(
this.destinationPath(),
baseServicePath(this.options.baseService),
'.env.defaults',
),
);

fs.writeFile(
join(
this.destinationPath(),
'services',
this.options.name ?? DEFAULT_NAME,
'.env.example',
),
example,
{
flag: 'w',
},
function () {
//This is intentional.
},
);
fs.writeFile(
join(
this.destinationPath(),
'services',
this.options.name ?? DEFAULT_NAME,
'.env.defaults',
),
defaults,
{
flag: 'w',
},
function () {
//This is intentional.
},
);
}
}
}

async end() {
if (this.projectInfo) {
this.projectInfo.outdir = this.options.name ?? DEFAULT_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"cz-conventional-changelog": "^3.3.0",
"cz-customizable": "^6.3.0",
"husky": "^7.0.4",
"lerna": "^7.3.0"
"lerna": "^7.3.0",
"run-s": "^0.0.0"
},
"workspaces":["packages/*", "services/*", "facades/*"],
"overrides":{
Expand Down
4 changes: 3 additions & 1 deletion services/authentication-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
"migration.js",
"migrations",
"database.json",
"!*/__tests__"
"!*/__tests__",
".env.defaults",
".env.example"
],
"peerDependencies": {
"db-migrate": "^1.0.0-beta.21",
Expand Down
37 changes: 37 additions & 0 deletions services/oidc-service/.env.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
NODE_ENV=dev
LOG_LEVEL=
DB_HOST=localhost
DB_PORT=1234
DB_USER=potgres
DB_PASSWORD=abc
DB_DATABASE=oidc
DB_SCHEMA=main
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_URL=redis://localhost:6379/
REDIS_PASSWORD=
REDIS_DATABASE=0
JWT_PRIVATE_KEY=
JWT_PUBLIC_KEY=
JWT_SECRET=secret
JWT_ISSUER=issuer
USER_TEMP_PASSWORD=
OIDC_CLAIMS_PROFILE=
OIDC_ISSUER_URL=
OIDC_INTERACTION_TIME=
OIDC_SESSION_TIME=
OIDC_GRANT_TIME=
OIDC_ID_TOKEN_TIME=
OIDC_JWKS_KTY=
OIDC_JWKS_ALG=
OIDC_JWKS_USE=
OIDC_JWKS_D=
OIDC_JWKS_DP=
OIDC_JWKS_DQ=
OIDC_JWKS_E=
OIDC_JWKS_N=
OIDC_JWKS_P=
OIDC_JWKS_Q=
OIDC_JWKS_QI=
OIDC_JWKS_KID=
OIDC_COOKIES=
4 changes: 3 additions & 1 deletion services/oidc-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
"migration.js",
"migrations",
"database.json",
"!*/__tests__"
"!*/__tests__",
".env.defaults",
".env.example"
],
"peerDependencies": {
"db-migrate": "^1.0.0-beta.21",
Expand Down
Loading

0 comments on commit 3ea4325

Please sign in to comment.