Skip to content

Commit 5cb8b56

Browse files
fix(amplify-codegen): Add Amplify CLI version as a comment for the Modelgen output files (#133)
* feat(amplify-codegen): Add Amplify CLI version as a comment for the Modelgen output files * fix(amplify-codegen): rephrase the amplify version comment * fix(amplify-codegen): add amplify-codegen version to version metadata comment * fix(amplify-codegen): minor changes to use lambda functions * fix(amplify-codegen): remove commented test * fix(amplify-codegen): remove optional chaining for backwards compatibility * fix(amplify-codegen): refactor the if conditional * test: add warning in context Co-authored-by: Zeyu Li <[email protected]>
1 parent eeb6df5 commit 5cb8b56

File tree

2 files changed

+256
-91
lines changed

2 files changed

+256
-91
lines changed

packages/amplify-codegen/src/commands/models.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ async function generateModels(context) {
141141
appsyncLocalConfig.forEach((cfg, idx) => {
142142
const outPutPath = cfg.filename;
143143
fs.ensureFileSync(outPutPath);
144-
fs.writeFileSync(outPutPath, generatedCode[idx]);
144+
const contentsToWrite = [getVersionsMetadataComment(context), generatedCode[idx]].filter(content => content);
145+
const contentToWrite = contentsToWrite.join('\n\n');
146+
fs.writeFileSync(outPutPath, contentToWrite);
145147
});
146148

147149
generateEslintIgnore(context);
@@ -197,6 +199,29 @@ function getModelOutputPath(context) {
197199
}
198200
}
199201

202+
// Generate a Comment string with Amplify CLI and Amplify Codegen version metadata
203+
function getVersionsMetadataComment(context) {
204+
const versionMetadata = [];
205+
if (context.usageData && context.usageData.version) {
206+
versionMetadata.push('amplify-cli-version: ' + context.usageData.version);
207+
}
208+
209+
if (context.pluginPlatform && context.pluginPlatform.plugins) {
210+
const codegenPluginsInfo = context.pluginPlatform.plugins.codegen;
211+
if (codegenPluginsInfo && codegenPluginsInfo.length > 0) {
212+
const amplifyCodegenPluginInfo = codegenPluginsInfo.filter(plugin => plugin.packageName == 'amplify-codegen');
213+
if (amplifyCodegenPluginInfo && amplifyCodegenPluginInfo.length > 0 && amplifyCodegenPluginInfo[0].packageVersion) {
214+
versionMetadata.push('amplify-codegen-version: ' + amplifyCodegenPluginInfo[0].packageVersion);
215+
}
216+
}
217+
}
218+
219+
if (versionMetadata.length > 0) {
220+
return '// Generated using ' + versionMetadata.join(', ');
221+
}
222+
return null;
223+
}
224+
200225
function generateEslintIgnore(context) {
201226
const projectConfig = context.amplify.getProjectConfig();
202227

0 commit comments

Comments
 (0)