Skip to content

Commit

Permalink
refactor(cli): move helpers from ArtifactGenerator to BaseGenerator
Browse files Browse the repository at this point in the history
Introduce two new helpers to be used in generators not inheriting form
`ArtifactGenerator`:

- `_wasGenerationSuccessful` 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š <[email protected]>
  • Loading branch information
bajtos committed Sep 23, 2019
1 parent 778230b commit fe7028e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
27 changes: 2 additions & 25 deletions packages/cli/lib/artifact-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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._wasGenerationSuccessful()) {
await this._updateIndexFiles();

const classes = this.artifactInfo.name
Expand Down Expand Up @@ -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);
}
}
};
31 changes: 31 additions & 0 deletions packages/cli/lib/base-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -444,4 +445,34 @@ module.exports = class BaseGenerator extends Generator {
}
await this._runLintFix();
}

// Check all files being generated to ensure they succeeded
_wasGenerationSuccessful() {
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}`);
}
};
2 changes: 1 addition & 1 deletion packages/cli/lib/update-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down

0 comments on commit fe7028e

Please sign in to comment.