Skip to content

Commit

Permalink
chore: Refine Dart config parsing
Browse files Browse the repository at this point in the history
We are experimenting with some new config layouts and the current parsing behavior prevents changing the config structure. This expands on the current behavior to allow more flexibility in the
config file contents.
  • Loading branch information
Dillon Nys committed Oct 13, 2022
1 parent 3cbfc1a commit 0af7ec2
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/amplify-frontend-flutter/lib/dart-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ function readJsonFromDart(jsonFilePath, encoding = 'utf8', throwOnError = true)
return undefined;
}
const fileContents = fs.readFileSync(jsonFilePath, encoding);
let jsonValue = fileContents.substring(fileContents.indexOf('{'), fileContents.lastIndexOf('}') + 1);
const configStart = fileContents.indexOf('const amplifyconfig');
if (configStart === -1) {
return undefined;
}
const QUOTE = "'''";
const jsonStart = fileContents.indexOf(QUOTE, configStart) + QUOTE.length;
const jsonEnd = fileContents.indexOf(QUOTE, jsonStart);
const jsonValue = fileContents.substring(jsonStart, jsonEnd).trim();
return JSON.parse(jsonValue);
}

function writeJsonToDart(dest, obj) {
function writeJsonToDart(dest, json) {
const destPath = path.parse(dest).dir;
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath, { recursive: true });
}
const dartContent = `const amplifyconfig = ''' ${obj}''';`;
const dartContent = `const amplifyconfig = '''${json}''';\n`;
fs.writeFileSync(dest, dartContent);
}

Expand Down

0 comments on commit 0af7ec2

Please sign in to comment.