Skip to content

Commit

Permalink
fix(cli): fix logs in cli (sourcefuse#515)
Browse files Browse the repository at this point in the history
* fix(cli): fix logs in cli

* fix logs in cli
  • Loading branch information
Jyoti-13 authored Feb 12, 2022
1 parent 8b868a1 commit 0aac3bb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
14 changes: 14 additions & 0 deletions packages/cli/generators/logbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports.logBarStart = function(bar1, start){
// start the progress bar with a total value of 200 and start value of 0
bar1.start(start, 0);
}

exports.logBar = function(bar1){
// update the current value in your application..
bar1.increment(1);
}

exports.logBarStop = function(bar1){
// stop the progress bar
bar1.stop();
}
42 changes: 33 additions & 9 deletions packages/cli/generators/microservice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const path = require('path');
const spawnProcess = require('../spawn');
const fs = require('fs');
const g = require('@loopback/cli/lib/globalize');
const {logBar, logBarStart, logBarStop} = require('../logbar');
const cliProgress = require('cli-progress');

const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);

module.exports = class MGenerator extends AppGenerator {
constructor(args, opts) {
Expand Down Expand Up @@ -118,6 +122,7 @@ module.exports = class MGenerator extends AppGenerator {
install() {
const packageJsonFile = path.join(process.cwd(), 'package.json');
const packageJson = require(packageJsonFile);
packageJson.name = `${this.answers.uniquePrefix}-${packageJson.name}`;
const scripts = packageJson.scripts;
const symlinkresolver = 'symlink-resolver';
this._setupMicroservice(packageJson.name);
Expand All @@ -126,12 +131,12 @@ module.exports = class MGenerator extends AppGenerator {
scripts['prestart'] = "npm run rebuild && npm run openapi-spec";
scripts['rebuild'] = "npm run clean && npm run build";
scripts['start'] = "node -r ./dist/opentelemetry-registry.js -r source-map-support/register .";
scripts['docker:build'] = `DOCKER_BUILDKIT=1 sudo docker build --build-arg NR_ENABLED=$NR_ENABLED_VALUE -t $IMAGE_REPO_NAME/
${this.answers.uniquePrefix} -$npm_package_name:$npm_package_version .`;
scripts['docker:push'] = `sudo docker push $IMAGE_REPO_NAME/ ${this.answers.uniquePrefix} -$npm_package_name:$npm_package_version`;
scripts['docker:build:dev'] = `DOCKER_BUILDKIT=1 sudo docker build --build-arg NR_ENABLED=$NR_ENABLED_VALUE -t $IMAGE_REPO_NAME/
${this.answers.uniquePrefix} -$npm_package_name:$IMAGE_TAG_VERSION .`;
scripts['docker:push:dev'] = `sudo docker push $IMAGE_REPO_NAME/ ${this.answers.uniquePrefix} -$npm_package_name:$IMAGE_TAG_VERSION`;
scripts['docker:build'] = `DOCKER_BUILDKIT=1 sudo docker build --build-arg NR_ENABLED=$NR_ENABLED_VALUE -t $IMAGE_REPO_NAME/
${this.answers.uniquePrefix}-$npm_package_name:$npm_package_version .`;
scripts['docker:push'] = `sudo docker push $IMAGE_REPO_NAME/${this.answers.uniquePrefix}-$npm_package_name:$npm_package_version`;
scripts['docker:build:dev'] = `DOCKER_BUILDKIT=1 sudo docker build --build-arg NR_ENABLED=$NR_ENABLED_VALUE -t $IMAGE_REPO_NAME/
${this.answers.uniquePrefix}-$npm_package_name:$IMAGE_TAG_VERSION .`;
scripts['docker:push:dev'] = `sudo docker push $IMAGE_REPO_NAME/${this.answers.uniquePrefix}-$npm_package_name:$IMAGE_TAG_VERSION`;
scripts['coverage'] = "nyc npm run test";
packageJson.scripts = scripts;
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson), null, 2);
Expand All @@ -146,9 +151,11 @@ module.exports = class MGenerator extends AppGenerator {
this._swaggerStat(packageName).then(() =>
this._opentelemetry(packageName).then(() =>
this._nyc(packageName).then(() =>
this._promclient(packageName).then(() =>
this._addDependency(packageName).then(() =>
this._openapi(packageName),
this._addDependency(packageName).then(() =>
this._promclient(packageName).then(() =>
this._openapi(packageName).then(() =>
this._prettierfix(packageName),
),
),
),
),
Expand All @@ -164,32 +171,39 @@ module.exports = class MGenerator extends AppGenerator {
}

async _symlink(packageName){
logBarStart(bar1, 11);
await spawnProcess('npx', ['lerna', 'add', '-D', 'symlink-resolver', '--scope='+`${packageName}`], {packageName});
logBar(bar1);
}

async _dotenv(packageName){
await spawnProcess('npx', ['lerna', 'add', 'dotenv', '--scope='+`${packageName}`], {packageName});
await spawnProcess('npx', ['lerna', 'add', 'dotenv-extended', '--scope='+`${packageName}`], {packageName});
await spawnProcess('npx', ['lerna', 'add', '-D', '@types/dotenv', '--scope='+`${packageName}`], {packageName});
logBar(bar1);
}

async _sourceloopCore(packageName){
await spawnProcess('npx', ['lerna', 'add', '@sourceloop/core', '--scope='+`${packageName}`], {packageName});
logBar(bar1);
}

async _addDependency(packageName){
if(this.answers.serviceSelect === 'y' || this.answers.serviceSelect === 'Y'){
await spawnProcess('npx', ['lerna', 'add', '@sourceloop/'+`${this.service.selector}`, '--scope='+`${packageName}`], {packageName});
}
logBar(bar1);
}

async _bearerVerifier(packageName){
await spawnProcess('npx', ['lerna', 'add', 'loopback4-authentication', '--scope='+`${packageName}`], {packageName});
await spawnProcess('npx', ['lerna', 'add', 'loopback4-authorization', '--scope='+`${packageName}`], {packageName});
logBar(bar1);
}

async _swaggerStat(packageName){
await spawnProcess('npx', ['lerna', 'add', 'swagger-stats', '--scope='+`${packageName}`], {packageName});
logBar(bar1);
}

async _opentelemetry(packageName){
Expand All @@ -201,18 +215,28 @@ module.exports = class MGenerator extends AppGenerator {
await spawnProcess('npx', ['lerna', 'add', '@opentelemetry/plugin-pg', '--scope='+`${packageName}`], {packageName});
await spawnProcess('npx', ['lerna', 'add', '@opentelemetry/plugin-pg-pool', '--scope='+`${packageName}`], {packageName});
await spawnProcess('npx', ['lerna', 'add', '@opentelemetry/tracing', '--scope='+`${packageName}`], {packageName});
logBar(bar1);
}

async _nyc(packageName){
await spawnProcess('npx', ['lerna', 'add', '-D', '@istanbuljs/nyc-config-typescript', '--scope='+`${packageName}`], {packageName});
await spawnProcess('npx', ['lerna', 'add', '-D', 'nyc', '--scope='+`${packageName}`], {packageName});
logBar(bar1);
}
async _promclient(packageName){
await spawnProcess('npm', ['i', 'prom-client'], {packageName});
logBar(bar1);
}

async _openapi(packageName){
await spawnProcess('npm', ['run', 'openapi-spec'], {packageName});
logBar(bar1);
}

async _prettierfix(packageName){
await spawnProcess('npm',['run', 'prettier:fix'], {packageName});
logBar(bar1);
logBarStop(bar1);
}

end() {
Expand Down
6 changes: 0 additions & 6 deletions packages/cli/generators/scaffold/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ module.exports = class extends Generator {

// refactor since this is calling every function and order matters. Order should not matter
initProject() {
const templatePath = this.templatePath();
console.log(`Template path: ${templatePath}`);
console.log(`Creating directory ${this.answers.projectDir}`);
fs.mkdir(this.answers.projectDir, {recursive: true}, (err) => {
if (err) {
throw err;
Expand Down Expand Up @@ -113,16 +110,13 @@ module.exports = class extends Generator {

_copyTemplates() {
fs.readdirSync(this.templatePath()).forEach(file => {
console.log(this.templatePath());
const targetFileName = file.replace('.tpl', '');
const sourcePath = this.templatePath(file);
console.log(`Source path: ${sourcePath}`);
const destinationPath = path.join(this.answers.projectDir, targetFileName);
this.fs.copyTpl(sourcePath, destinationPath, {
upperDbKey: this.answers.dbKey.toUpperCase(),
...this.answers
});
console.log(file);
});

fs.mkdirSync(path.join(this.answers.projectDir, 'services'));
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"camelcase-keys": "^7.0.0",
"chalk": "^4.1.2",
"change-case": "^4.1.2",
"cli-progress": "^3.10.0",
"db-migrate": "^0.11.12",
"debug": "^4.3.2",
"fs-extra": "^10.0.0",
Expand Down

0 comments on commit 0aac3bb

Please sign in to comment.