From 3e640aea8eeb5cf4b11401ab98886279219a16b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 23 Sep 2019 13:16:21 +0200 Subject: [PATCH] refactor(cli): move helpers from ArtifactGenerator to BaseGenerator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduce two new helpers to be used in generators not inheriting form `ArtifactGenerator`: - `_isGenerationSuccessful` to detect whether all target files were successfully created or modified. - `_updateIndexFile` to add a single entry to a given `index.ts` file Signed-off-by: Miroslav Bajtoš --- packages/cli/lib/artifact-generator.js | 27 ++-------------------- packages/cli/lib/base-generator.js | 31 ++++++++++++++++++++++++++ packages/cli/lib/update-index.js | 2 +- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/packages/cli/lib/artifact-generator.js b/packages/cli/lib/artifact-generator.js index bd5ad38a5a84..f4945ace67e2 100644 --- a/packages/cli/lib/artifact-generator.js +++ b/packages/cli/lib/artifact-generator.js @@ -7,7 +7,6 @@ const BaseGenerator = require('./base-generator'); const debug = require('./debug')('artifact-generator'); const utils = require('./utils'); -const updateIndex = require('./update-index'); const path = require('path'); const chalk = require('chalk'); @@ -103,16 +102,7 @@ module.exports = class ArtifactGenerator extends BaseGenerator { return; } - // Check all files being generated to ensure they succeeded - const generationStatus = !!Object.entries( - this.conflicter.generationStatus, - ).find(([key, val]) => { - // If a file was modified, update the indexes and say stuff about it - return val !== 'skip' && val !== 'identical'; - }); - debug(`Generation status: ${generationStatus}`); - - if (generationStatus) { + if (this._isGenerationSuccessful()) { await this._updateIndexFiles(); const classes = this.artifactInfo.name @@ -171,20 +161,7 @@ module.exports = class ArtifactGenerator extends BaseGenerator { } for (const idx of this.artifactInfo.indexesToBeUpdated) { - await updateIndex(idx.dir, idx.file); - // Output for users - const updateDirRelPath = path.relative( - this.artifactInfo.relPath, - idx.dir, - ); - - const outPath = path.join( - this.artifactInfo.relPath, - updateDirRelPath, - 'index.ts', - ); - - this.log(chalk.green(' update'), `${outPath}`); + await this._updateIndexFile(idx.dir, idx.file); } } }; diff --git a/packages/cli/lib/base-generator.js b/packages/cli/lib/base-generator.js index 679581358e01..44b69caee182 100644 --- a/packages/cli/lib/base-generator.js +++ b/packages/cli/lib/base-generator.js @@ -12,6 +12,7 @@ const path = require('path'); const fs = require('fs'); const debug = require('./debug')('base-generator'); const semver = require('semver'); +const updateIndex = require('./update-index'); /** * Base Generator for LoopBack 4 @@ -444,4 +445,34 @@ module.exports = class BaseGenerator extends Generator { } await this._runLintFix(); } + + // Check all files being generated to ensure they succeeded + _isGenerationSuccessful() { + const generationStatus = !!Object.entries( + this.conflicter.generationStatus, + ).find(([key, val]) => { + // If a file was modified, update the indexes and say stuff about it + return val !== 'skip' && val !== 'identical'; + }); + debug(`Generation status: ${generationStatus}`); + return generationStatus; + } + + async _updateIndexFile(dir, file) { + await updateIndex(dir, file); + + // Output for users + const updateDirRelPath = path.relative( + this.artifactInfo.relPath, + this.artifactInfo.outDir, + ); + + const outPath = path.join( + this.artifactInfo.relPath, + updateDirRelPath, + 'index.ts', + ); + + this.log(chalk.green(' update'), `${outPath}`); + } }; diff --git a/packages/cli/lib/update-index.js b/packages/cli/lib/update-index.js index 71f563497aa4..865587372d0b 100644 --- a/packages/cli/lib/update-index.js +++ b/packages/cli/lib/update-index.js @@ -17,7 +17,7 @@ const exists = util.promisify(fs.exists); * @param {*} file The new file to be exported from index.ts */ module.exports = async function(dir, file) { - debug(`Updating index ${path.join(dir, file)}`); + debug(`Updating index with ${path.join(dir, file)}`); const indexFile = path.join(dir, 'index.ts'); if (!file.endsWith('.ts')) { throw new Error(`${file} must be a TypeScript (.ts) file`);