Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update CLI to handle UTF8 BOM #1357

Merged
merged 4 commits into from
Apr 29, 2019
Merged

fix: update CLI to handle UTF8 BOM #1357

merged 4 commits into from
Apr 29, 2019

Conversation

yuth
Copy link
Contributor

@yuth yuth commented Apr 26, 2019

Some editors and platforms can add UTF BOM at the begining of the file. Node JSON.parse doesn't
handle the BOM enchoding very well and chokes the CLI. Updating the CLI to use a util method which
can handle the BOM chars

fix #1355 and fix #1122

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Some editors and platforms can add UTF BOM at the begining of the file. Node JSON.parse doesn't
handle the BOM enchoding very well and chokes the CLI. Updating the CLI to use a util method which
can handle the BOM chars

fix #1355 #1122
@ghost ghost assigned yuth Apr 26, 2019
@ghost ghost added the review label Apr 26, 2019
Copy link
Contributor

@UnleashedMind UnleashedMind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still 9 places where JSON.parse(fs.readFileSync(.)) is still directly used, for example, amplify-category-analytics/commands/analytics/add.js Line 5.
All of them are for reading "servicesMetadata".
For consistency, that line can be moved inside the run function, and use the new approach to read.

@@ -49,7 +49,7 @@ module.exports = {
*/
const currEnv = amplify.getEnvInfo().envName;
const teamProviderInfoFilePath = amplify.pathManager.getProviderInfoFilePath();
const teamProviderInfo = JSON.parse(fs.readFileSync(teamProviderInfoFilePath));
const teamProviderInfo = context.amplify.readJsonFile(teamProviderInfoFilePath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, use amplify directly without the context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets do that later. If there are any problem in the ServiceMetaData the CLI will fail to start as they are at the top level

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point, but don't you think it's better that this is consistent across the whole project? currently, some are on top of the file direct using JSON.parse(fs.readFileSync(.)) directly, such as amplify-category-analytics/commands/analytics/add.js; and some are inside the run method using context.amplify.readJsonFile(.), such as amplify-category-auth/commands/auth/enable.js

Copy link
Contributor Author

@yuth yuth Apr 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code to not to use context.amplify where amplify is available.

@aws-amplify aws-amplify deleted a comment from UnleashedMind Apr 26, 2019
@yuth yuth requested a review from UnleashedMind April 29, 2019 17:53
const resourceDirPath = path.join(projectPath, 'amplify', 'backend', category, resourceName);

// Change CFN file

const cfnFilePath = path.join(resourceDirPath, 's3-cloudformation-template.json');
const oldCfn = JSON.parse(fs.readFileSync(cfnFilePath, 'utf8'));
const oldCfn = context.amplify.readJsonFile(cfnFilePath, 'utf8');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deleted comment is here, you don't need to use encoding if the encoding is 'utf8'.

@@ -349,7 +349,7 @@ function migrate(projectPath, resourceName) {

// Change Parameters file
const parametersFilePath = path.join(resourceDirPath, parametersFileName);
const oldParameters = JSON.parse(fs.readFileSync(parametersFilePath, 'utf8'));
const oldParameters = context.amplify.readJsonFile(parametersFilePath, 'utf8');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to use encoding if the encoding is 'utf8'.

@@ -49,7 +49,7 @@ module.exports = {
*/
const currEnv = amplify.getEnvInfo().envName;
const teamProviderInfoFilePath = amplify.pathManager.getProviderInfoFilePath();
const teamProviderInfo = JSON.parse(fs.readFileSync(teamProviderInfoFilePath));
const teamProviderInfo = context.amplify.readJsonFile(teamProviderInfoFilePath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point, but don't you think it's better that this is consistent across the whole project? currently, some are on top of the file direct using JSON.parse(fs.readFileSync(.)) directly, such as amplify-category-analytics/commands/analytics/add.js; and some are inside the run method using context.amplify.readJsonFile(.), such as amplify-category-auth/commands/auth/enable.js

@yuth yuth requested a review from UnleashedMind April 29, 2019 20:46
@yuth yuth merged commit b0afa07 into master Apr 29, 2019
@ghost ghost removed the review label Apr 29, 2019
@yuth yuth deleted the issue-1335 branch April 29, 2019 22:02
@github-actions
Copy link

This pull request has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Unexpected token in JSON at position 0" during amplify init Amplify init fails on another machine
2 participants