Skip to content

Commit

Permalink
refactor: move shared logic to artifact-generator
Browse files Browse the repository at this point in the history
Saw same logic being used in datasource-generator
  • Loading branch information
virkt25 committed May 26, 2018
1 parent 81ed77a commit 19ee9fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
14 changes: 0 additions & 14 deletions packages/cli/generators/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,5 @@ module.exports = class ControllerGenerator extends ArtifactGenerator {

async end() {
await super.end();
if (this.shouldExit()) return false;
// logs a message if there is no file conflict
if (
this.conflicter.generationStatus[this.artifactInfo.filename] !== 'skip' &&
this.conflicter.generationStatus[this.artifactInfo.filename] !==
'identical'
) {
this.log();
this.log(
'Controller %s is now created in src/controllers/',
this.artifactInfo.name,
);
this.log();
}
}
};
38 changes: 29 additions & 9 deletions packages/cli/lib/artifact-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,36 @@ module.exports = class ArtifactGenerator extends BaseGenerator {
const success = super.end();
if (!success) return false;

// Update `index.ts`
await makeIndex(this.artifactInfo.outDir, {
prefix: `${this.artifactInfo.type}`,
let generationStatus = true;
// Check all files being generated to ensure they succeeded
Object.entries(this.conflicter.generationStatus).forEach(([key, val]) => {
if (val === 'skip' || val === 'identical') generationStatus = false;
});

const relPath = path.relative(
this.destinationPath(),
this.artifactInfo.outDir,
);
this.log(chalk.green(' update'), `${relPath}/index.ts`);
return true;
if (generationStatus) {
// Update `index.ts`
await makeIndex(this.artifactInfo.outDir, {
prefix: `${this.artifactInfo.type}`,
});

// Relative path of output directory
const relPath = path.relative(
this.destinationPath(),
this.artifactInfo.outDir,
);

// Output for users
this.log(chalk.green(' update'), `${relPath}/index.ts`);
this.log();
this.log(
utils.toClassName(this.artifactInfo.type),
chalk.yellow(this.artifactInfo.name),
'is now created in',
`${relPath}/`,
);
this.log();
}

return false;
}
};

0 comments on commit 19ee9fb

Please sign in to comment.