Skip to content

Commit

Permalink
fix: update CLI to handle UTF8 BOM (#1357)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yuth committed Apr 29, 2019
1 parent 13d9fc3 commit b0afa07
Show file tree
Hide file tree
Showing 55 changed files with 190 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require('fs');

function addResource(context, category, service) {
const serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
const serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const { defaultValuesFilename, serviceWalkthroughFilename } = serviceMetadata;

const serviceWalkthroughSrc = `${__dirname}/service-walkthroughs/${serviceWalkthroughFilename}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function configure(context, defaultValuesFilename, serviceMetadata, resourceName
inputs = inputs.filter(input => input.key !== 'resourceName');
const resourceDirPath = path.join(projectBackendDirPath, category, resourceName);
const parametersFilePath = path.join(resourceDirPath, parametersFileName);
const parameters = JSON.parse(fs.readFileSync(parametersFilePath));
const parameters = context.amplify.readJsonFile(parametersFilePath);
parameters.resourceName = resourceName;
Object.assign(defaultValues, parameters);
}
Expand Down Expand Up @@ -178,7 +178,7 @@ function writeCfnFile(context, resourceDirPath, force = false) {
const templateFilePath = path.join(resourceDirPath, templateFileName);
if (!fs.existsSync(templateFilePath) || force) {
const templateSourceFilePath = `${__dirname}/../cloudformation-templates/${templateFileName}`;
const templateSource = JSON.parse(fs.readFileSync(templateSourceFilePath));
const templateSource = context.amplify.readJsonFile(templateSourceFilePath);
templateSource.Mappings = getTemplateMappings(context);
const jsonString = JSON.stringify(templateSource, null, 4);
fs.writeFileSync(templateFilePath, jsonString, 'utf8');
Expand Down Expand Up @@ -214,14 +214,14 @@ function migrate(context) {
const { analytics = {} } = amplifyMeta;
Object.keys(analytics).forEach((resourceName) => {
const resourcePath = path.join(projectBackendDirPath, category, resourceName);
const cfn = JSON.parse(fs.readFileSync(path.join(resourcePath, 'pinpoint-cloudformation-template.json')));
const cfn = context.amplify.readJsonFile(path.join(resourcePath, 'pinpoint-cloudformation-template.json'));
const updatedCfn = migrateCFN(cfn);

fs.ensureDirSync(resourcePath);
const templateFilePath = path.join(resourcePath, templateFileName);
fs.writeFileSync(templateFilePath, JSON.stringify(updatedCfn, null, 4), 'utf8');

const parameters = JSON.parse(fs.readFileSync(path.join(resourcePath, 'parameters.json')));
const parameters = context.amplify.readJsonFile(path.join(resourcePath, 'parameters.json'));
const updatedParams = migrateParams(context, parameters);
const parametersFilePath = path.join(resourcePath, parametersFileName);
fs.writeFileSync(parametersFilePath, JSON.stringify(updatedParams, null, 4), 'utf8');
Expand Down
4 changes: 2 additions & 2 deletions packages/amplify-category-api/commands/api/add-datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

if (!teamProviderInfo[currEnv][categories]) {
teamProviderInfo[currEnv][categories] = {};
Expand All @@ -75,7 +75,7 @@ module.exports = {
fs.writeFileSync(teamProviderInfoFilePath, JSON.stringify(teamProviderInfo, null, 4));

const backendConfigFilePath = amplify.pathManager.getBackendConfigFilePath();
const backendConfig = JSON.parse(fs.readFileSync(backendConfigFilePath));
const backendConfig = context.amplify.readJsonFile(backendConfigFilePath);

backendConfig[category][resourceName][rdsInit] = true;

Expand Down
4 changes: 2 additions & 2 deletions packages/amplify-category-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function initEnv(context) {
* configured an RDS datasource
*/
const backendConfigFilePath = amplify.pathManager.getBackendConfigFilePath();
const backendConfig = JSON.parse(fs.readFileSync(backendConfigFilePath));
const backendConfig = amplify.readJsonFile(backendConfigFilePath);

let resourceName;
const apis = Object.keys(backendConfig[category]);
Expand Down Expand Up @@ -90,7 +90,7 @@ async function initEnv(context) {
*/
const currEnv = amplify.getEnvInfo().envName;
const teamProviderInfoFilePath = amplify.pathManager.getProviderInfoFilePath();
const teamProviderInfo = JSON.parse(fs.readFileSync(teamProviderInfoFilePath));
const teamProviderInfo = amplify.readJsonFile(teamProviderInfoFilePath);
if (teamProviderInfo[currEnv][categories]
&& teamProviderInfo[currEnv][categories][category]
&& teamProviderInfo[currEnv][categories][category][resourceName]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function copyCfnTemplate(context, category, options, cfnFilename) {


function console(context, service) {
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const { serviceWalkthroughFilename } = serviceMetadata;
const serviceWalkthroughSrc = `${__dirname}/service-walkthroughs/${serviceWalkthroughFilename}`;
const { openConsole } = require(serviceWalkthroughSrc);
Expand All @@ -48,7 +48,7 @@ function console(context, service) {

function addResource(context, category, service, options) {
let answers;
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
let { cfnFilename } = serviceMetadata;
const { defaultValuesFilename, serviceWalkthroughFilename } = serviceMetadata;
const projectBackendDirPath = context.amplify.pathManager.getBackendDirPath();
Expand Down Expand Up @@ -101,7 +101,7 @@ function addResource(context, category, service, options) {

async function updateResource(context, category, service) {
let answers;
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
let { cfnFilename } = serviceMetadata;
const { defaultValuesFilename, serviceWalkthroughFilename } = serviceMetadata;
const serviceWalkthroughSrc = `${__dirname}/service-walkthroughs/${serviceWalkthroughFilename}`;
Expand Down Expand Up @@ -151,7 +151,7 @@ async function updateResource(context, category, service) {
}

async function migrateResource(context, projectPath, service, resourceName) {
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const { serviceWalkthroughFilename } = serviceMetadata;
const serviceWalkthroughSrc = `${__dirname}/service-walkthroughs/${serviceWalkthroughFilename}`;
const { migrate } = require(serviceWalkthroughSrc);
Expand All @@ -165,7 +165,7 @@ async function migrateResource(context, projectPath, service, resourceName) {
}

function addDatasource(context, category, datasource) {
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-datasources.json`))[datasource];
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-datasources.json`)[datasource];
const { defaultValuesFilename, serviceWalkthroughFilename } = serviceMetadata;
return serviceQuestions(context, defaultValuesFilename, serviceWalkthroughFilename);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function updateWalkthrough(context, defaultValuesFilename) {
const parametersFilePath = pathLib.join(resourceDirPath, parametersFileName);
let parameters;
try {
parameters = JSON.parse(fs.readFileSync(parametersFilePath));
parameters = context.amplify.readJsonFile(parametersFilePath);
} catch (e) {
parameters = {};
}
Expand Down Expand Up @@ -578,7 +578,7 @@ async function migrate(context, projectPath, resourceName) {
const parametersFilePath = pathLib.join(resourceDirPath, parametersFileName);
let parameters;
try {
parameters = JSON.parse(fs.readFileSync(parametersFilePath));
parameters = amplify.readJsonFile(parametersFilePath);
} catch (e) {
context.print.error(`Error reading api-params.json file for ${resourceName} resource`);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ async function updateWalkthrough(context) {
let parameters = {};

try {
parameters = JSON.parse(fs.readFileSync(parametersFilePath));
parameters = context.amplify.readJsonFile(parametersFilePath);
} catch (e) {
context.print.error('Parameters file not found');
context.print.info(e.stack);
Expand All @@ -270,14 +270,14 @@ async function updateWalkthrough(context) {
const authType = await askSecurityQuestions(context, parameters);

const amplifyMetaFilePath = context.amplify.pathManager.getAmplifyMetaFilePath();
const amplifyMeta = JSON.parse(fs.readFileSync(amplifyMetaFilePath));
const amplifyMeta = context.amplify.readJsonFile(amplifyMetaFilePath);

amplifyMeta[category][resourceName].output.securityType = authType;
let jsonString = JSON.stringify(amplifyMeta, null, '\t');
fs.writeFileSync(amplifyMetaFilePath, jsonString, 'utf8');

const backendConfigFilePath = context.amplify.pathManager.getBackendConfigFilePath();
const backendConfig = JSON.parse(fs.readFileSync(backendConfigFilePath));
const backendConfig = context.amplify.readJsonFile(backendConfigFilePath);

backendConfig[category][resourceName].output.securityType = authType;
jsonString = JSON.stringify(backendConfig, null, '\t');
Expand Down
3 changes: 1 addition & 2 deletions packages/amplify-category-auth/commands/auth/enable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const fs = require('fs');
const { messages } = require('../../provider-utils/awscloudformation/assets/string-maps');

const subcommand = 'enable';
Expand All @@ -10,7 +9,7 @@ module.exports = {
alias: ['add'],
run: async (context) => {
const { amplify } = context;
const servicesMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../../provider-utils/supported-services.json`));
const servicesMetadata = amplify.readJsonFile(`${__dirname}/../../provider-utils/supported-services.json`);

const existingAuth = amplify.getProjectDetails().amplifyMeta.auth || {};

Expand Down
3 changes: 1 addition & 2 deletions packages/amplify-category-auth/commands/auth/update.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const fs = require('fs');
const { messages } = require('../../provider-utils/awscloudformation/assets/string-maps');


Expand All @@ -11,7 +10,7 @@ module.exports = {
alias: ['update'],
run: async (context) => {
const { amplify } = context;
const servicesMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../../provider-utils/supported-services.json`));
const servicesMetadata = amplify.readJsonFile(`${__dirname}/../../provider-utils/supported-services.json`);
const existingAuth = amplify.getProjectDetails().amplifyMeta.auth || {};

if (!Object.keys(existingAuth).length > 0) {
Expand Down
11 changes: 5 additions & 6 deletions packages/amplify-category-auth/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const category = 'auth';
const fs = require('fs');
const _ = require('lodash');
const uuid = require('uuid');
const sequential = require('promise-sequential');
Expand All @@ -15,7 +14,7 @@ const {
// this function is being kept for temporary compatability.
async function add(context) {
const { amplify } = context;
const servicesMetadata = JSON.parse(fs.readFileSync(`${__dirname}/provider-utils/supported-services.json`));
const servicesMetadata = amplify.readJsonFile(`${__dirname}/provider-utils/supported-services.json`);

const existingAuth = amplify.getProjectDetails().amplifyMeta.auth || {};

Expand Down Expand Up @@ -55,7 +54,7 @@ async function add(context) {

async function externalAuthEnable(context, externalCategory, resourceName, requirements) {
const { amplify } = context;
const serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/provider-utils/supported-services.json`));
const serviceMetadata = amplify.readJsonFile(`${__dirname}/provider-utils/supported-services.json`);
const { cfnFilename, provider } = serviceMetadata.Cognito;
const authExists =
amplify.getProjectDetails().amplifyMeta.auth &&
Expand Down Expand Up @@ -165,9 +164,9 @@ async function checkRequirements(requirements, context) {
let authParameters;

if (existingAuth && Object.keys(existingAuth).length > 0) {
authParameters = JSON.parse(fs.readFileSync(`${amplify.pathManager.getBackendDirPath()}/auth/${
authParameters = amplify.readJsonFile(`${amplify.pathManager.getBackendDirPath()}/auth/${
Object.keys(existingAuth)[0]
}/parameters.json`));
}/parameters.json`);
} else {
return { authEnabled: false };
}
Expand Down Expand Up @@ -210,7 +209,7 @@ async function initEnv(context) {

async function console(context) {
const { amplify } = context;
const supportedServices = JSON.parse(fs.readFileSync(`${__dirname}/provider-utils/supported-services.json`));
const supportedServices = amplify.readJsonFile(`${__dirname}/provider-utils/supported-services.json`);
const amplifyMeta = amplify.getProjectMeta();

if (!amplifyMeta.auth || Object.keys(amplifyMeta.auth).length === 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const fs = require('fs');
const inquirer = require('inquirer');
const opn = require('opn');
const _ = require('lodash');
Expand Down Expand Up @@ -124,7 +123,7 @@ function saveResourceParameters(

async function addResource(context, category, service) {
let props = {};
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const {
cfnFilename,
defaultValuesFilename,
Expand Down Expand Up @@ -170,7 +169,7 @@ async function addResource(context, category, service) {
async function updateResource(context, category, serviceResult) {
const { service, resourceName } = serviceResult;
let props = {};
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const {
cfnFilename,
defaultValuesFilename,
Expand Down Expand Up @@ -250,7 +249,7 @@ async function updateResource(context, category, serviceResult) {
}

async function updateConfigOnEnvInit(context, category, service) {
const srvcMetaData = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))
const srvcMetaData = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)
.Cognito;
const { defaultValuesFilename, stringMapFilename, serviceWalkthroughFilename } = srvcMetaData;

Expand Down Expand Up @@ -333,7 +332,7 @@ async function migrate(context) {
if (!Object.keys(existingAuth).length > 0) {
return;
}
const servicesMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../../provider-utils/supported-services.json`));
const servicesMetadata = amplify.readJsonFile(`${__dirname}/../../provider-utils/supported-services.json`);
const { provider, cfnFilename, defaultValuesFilename } = servicesMetadata.Cognito;
const defaultValuesSrc = `${__dirname}/assets/${defaultValuesFilename}`;

Expand Down
2 changes: 2 additions & 0 deletions packages/amplify-category-auth/tests/commands/enable.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs');
const add = require('../../commands/auth/enable');
const { messages } = require('../../provider-utils/awscloudformation/assets/string-maps');

Expand All @@ -12,6 +13,7 @@ describe('auth enable: ', () => {
executeProviderUtils: mockExecuteProviderUtils,
getProjectDetails: mockGetProjectDetails,
serviceSelectionPrompt: mockSelectionPrompt,
readJsonFile: jest.fn(path => JSON.parse(fs.readFileSync(path))),
},
print: {
warning: jest.fn(),
Expand Down
3 changes: 3 additions & 0 deletions packages/amplify-category-auth/tests/commands/update.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs');
const update = require('../../commands/auth/update');
const { messages } = require('../../provider-utils/awscloudformation/assets/string-maps');

Expand All @@ -10,13 +11,15 @@ describe('auth update: ', () => {
const mockGetProjectDetails = jest.fn();
const mockSelectionPrompt = jest.fn(() => Promise.reject(new Error()));
const mockProjectPath = '/User/someone/Documents/Project/amplify-test';

const mockPluginInstance = { loadResourceParameters: jest.fn() };
const mockContext = {
amplify: {
executeProviderUtils: mockExecuteProviderUtils,
getProjectDetails: mockGetProjectDetails,
serviceSelectionPrompt: mockSelectionPrompt,
getPluginInstance: jest.fn().mockReturnValue(mockPluginInstance),
readJsonFile: jest.fn(path => JSON.parse(fs.readFileSync(path))),
pathManager: {
getBackendDirPath: jest.fn(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function copyCfnTemplate(context, category, options, cfnFilename) {

async function addResource(context, category, service, options) {
let answers;
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const { cfnFilename, defaultValuesFilename, serviceWalkthroughFilename } = serviceMetadata;

const result = await serviceQuestions(context, defaultValuesFilename, serviceWalkthroughFilename);
Expand Down Expand Up @@ -142,7 +142,7 @@ async function openEditor(context, category, options) {

async function invoke(context, category, service, resourceName) {
const { amplify } = context;
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
serviceMetadata = amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const { inputs } = serviceMetadata;
const resourceQuestions = [
{
Expand Down Expand Up @@ -200,7 +200,7 @@ async function invoke(context, category, service, resourceName) {
}

function migrateResource(context, projectPath, service, resourceName) {
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const { serviceWalkthroughFilename } = serviceMetadata;
const serviceWalkthroughSrc = `${__dirname}/service-walkthroughs/${serviceWalkthroughFilename}`;
const { migrate } = require(serviceWalkthroughSrc);
Expand All @@ -210,7 +210,7 @@ function migrateResource(context, projectPath, service, resourceName) {
return;
}

return migrate(projectPath, resourceName);
return migrate(context, projectPath, resourceName);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async function getTableParameters(context, dynamoAnswers) {
const parametersFilePath = path.join(resourceDirPath, parametersFileName);
let parameters;
try {
parameters = JSON.parse(fs.readFileSync(parametersFilePath));
parameters = context.amplify.readJsonFile(parametersFilePath);
} catch (e) {
parameters = {};
}
Expand Down Expand Up @@ -207,10 +207,10 @@ async function askDynamoDBQuestions(context, inputs) {
}
}

function migrate(projectPath, resourceName) {
function migrate(context, projectPath, resourceName) {
const resourceDirPath = path.join(projectPath, 'amplify', 'backend', category, resourceName);
const cfnFilePath = path.join(resourceDirPath, `${resourceName}-cloudformation-template.json`);
const oldCfn = JSON.parse(fs.readFileSync(cfnFilePath, 'utf8'));
const oldCfn = context.amplify.readJsonFile(cfnFilePath);
const newCfn = {};
Object.assign(newCfn, oldCfn);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const fs = require('fs-extra');
const path = require('path');
const inquirer = require('inquirer');

Expand All @@ -18,7 +17,7 @@ const originErrorCodes = {

async function configure(context) {
const templateFilePath = path.join(__dirname, '../template.json');
const originalTemplate = JSON.parse(fs.readFileSync(templateFilePath));
const originalTemplate = context.amplify.readJsonFile(templateFilePath);
if (!context.exeInfo.template.Resources.CloudFrontDistribution) {
context.print.info('CloudFront is NOT in the current hosting');
const answer = await inquirer.prompt({
Expand Down

0 comments on commit b0afa07

Please sign in to comment.