From 5642c376cd4a27218662b06e07603b22c128ad30 Mon Sep 17 00:00:00 2001 From: David Edmondson Date: Thu, 26 Apr 2018 12:04:03 -0700 Subject: [PATCH 1/2] Ignore file dependencies when checking mismatches. This prevents the "would you like to update your dependencies" dialog when developing Gluestick locally. --- .../__tests__/checkForMismatch.test.js | 45 +++++++++++++------ .../commands/autoUpgrade/checkForMismatch.js | 18 +++++--- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/packages/gluestick/src/commands/autoUpgrade/__tests__/checkForMismatch.test.js b/packages/gluestick/src/commands/autoUpgrade/__tests__/checkForMismatch.test.js index 1915339c..f1909cd9 100644 --- a/packages/gluestick/src/commands/autoUpgrade/__tests__/checkForMismatch.test.js +++ b/packages/gluestick/src/commands/autoUpgrade/__tests__/checkForMismatch.test.js @@ -1,7 +1,7 @@ /* @flow */ jest.mock('../getSingleEntryFromGenerator.js', () => jest.fn()); jest.mock('gluestick-generators', () => ({ - parseConfig: jest.fn(() => ({ + parseConfig: () => ({ entry: { template: JSON.stringify({ dependencies: { @@ -13,7 +13,7 @@ jest.mock('gluestick-generators', () => ({ }, }), }, - })), + }), })); const utils = require('../utils'); @@ -24,7 +24,7 @@ const orignialPromptModulesUpdate = utils.promptModulesUpdate; describe('autoUpgrade/checkForMismatch', () => { beforeEach(() => { utils.promptModulesUpdate = jest.fn(() => - Promise.resolve({ shouldFix: true, mismatchedModules: {} }), + Promise.resolve({ shouldFix: false, mismatchedModules: {} }), ); }); @@ -33,17 +33,19 @@ describe('autoUpgrade/checkForMismatch', () => { utils.promptModulesUpdate = orignialPromptModulesUpdate; }); - it('should detect mismatched modules', done => { - // $FlowIgnore - checkForMismatch({ - dependencies: { - depA: '1.0.0', - depB: '1.0.0', + it('detects mismatched modules', () => { + return checkForMismatch( + { + dependencies: { + depA: '1.0.0', + depB: '1.0.0', + }, + devDependencies: {}, }, - }).then(result => { + false, + ).then(result => { expect(result).toBeTruthy(); - // $FlowIgnore - expect(utils.promptModulesUpdate.mock.calls[0][0]).toEqual({ + expect(utils.promptModulesUpdate).toHaveBeenCalledWith({ depA: { required: '2.0.0', project: '1.0.0', @@ -55,7 +57,24 @@ describe('autoUpgrade/checkForMismatch', () => { type: 'devDependencies', }, }); - done(); + }); + }); + + it('ignores local file dependencies', () => { + return checkForMismatch( + { + dependencies: { + depA: 'file:../gluestick/packages/gluestick', + depB: '1.0.0', + }, + devDependencies: { + depC: '1.0.0', + }, + }, + false, + ).then(result => { + expect(result).toBeTruthy(); + expect(utils.promptModulesUpdate).not.toHaveBeenCalled(); }); }); }); diff --git a/packages/gluestick/src/commands/autoUpgrade/checkForMismatch.js b/packages/gluestick/src/commands/autoUpgrade/checkForMismatch.js index 828644fd..5d7eec9b 100644 --- a/packages/gluestick/src/commands/autoUpgrade/checkForMismatch.js +++ b/packages/gluestick/src/commands/autoUpgrade/checkForMismatch.js @@ -12,6 +12,10 @@ type ProjectPackage = { devDependencies: Object, }; +const isFileDependency = (name: string) => { + return name.startsWith('file'); +}; + /** * Open the package.json file in both the project as well as the one used by * this command line interface, then compare the versions for shared modules. @@ -74,19 +78,21 @@ const checkForMismatch = ( }; }; Object.keys(templatePackage.dependencies).forEach((dep: string): void => { + const projectDependency = projectPackage.dependencies[dep]; if ( dev && dep === 'gluestick' && - !/\d+\.\d+\.\d+.*/.test(projectPackage.dependencies[dep]) + !/\d+\.\d+\.\d+.*/.test(projectDependency) ) { return; } if ( - !projectPackage.dependencies[dep] || - !isValidVersion( - projectPackage.dependencies[dep], - templatePackage.dependencies[dep], - ) + (!projectDependency || + !isValidVersion( + projectDependency, + templatePackage.dependencies[dep], + )) && + !isFileDependency(projectDependency) ) { markMissing(dep, 'dependencies'); } From c8d9f0c99e4ee9b4108c207fd21f32ef9fd6a859 Mon Sep 17 00:00:00 2001 From: David Edmondson Date: Thu, 26 Apr 2018 12:20:47 -0700 Subject: [PATCH 2/2] Refactor out duplication and long property access Remove unused "replaceFile" function --- .../__tests__/checkForMismatch.test.js | 8 ++-- .../autoUpgrade/__tests__/utils.test.js | 25 +----------- .../commands/autoUpgrade/checkForMismatch.js | 39 ++++++++----------- .../src/commands/autoUpgrade/utils.js | 18 --------- 4 files changed, 22 insertions(+), 68 deletions(-) diff --git a/packages/gluestick/src/commands/autoUpgrade/__tests__/checkForMismatch.test.js b/packages/gluestick/src/commands/autoUpgrade/__tests__/checkForMismatch.test.js index f1909cd9..67a92009 100644 --- a/packages/gluestick/src/commands/autoUpgrade/__tests__/checkForMismatch.test.js +++ b/packages/gluestick/src/commands/autoUpgrade/__tests__/checkForMismatch.test.js @@ -17,20 +17,22 @@ jest.mock('gluestick-generators', () => ({ })); const utils = require('../utils'); -const checkForMismatch = require('../checkForMismatch'); -const orignialPromptModulesUpdate = utils.promptModulesUpdate; +const originalPromptModulesUpdate = utils.promptModulesUpdate; describe('autoUpgrade/checkForMismatch', () => { + let checkForMismatch; beforeEach(() => { utils.promptModulesUpdate = jest.fn(() => Promise.resolve({ shouldFix: false, mismatchedModules: {} }), ); + checkForMismatch = require('../checkForMismatch'); }); afterEach(() => { jest.resetAllMocks(); - utils.promptModulesUpdate = orignialPromptModulesUpdate; + jest.resetModules(); + utils.promptModulesUpdate = originalPromptModulesUpdate; }); it('detects mismatched modules', () => { diff --git a/packages/gluestick/src/commands/autoUpgrade/__tests__/utils.test.js b/packages/gluestick/src/commands/autoUpgrade/__tests__/utils.test.js index 63f47b00..c60292ca 100644 --- a/packages/gluestick/src/commands/autoUpgrade/__tests__/utils.test.js +++ b/packages/gluestick/src/commands/autoUpgrade/__tests__/utils.test.js @@ -5,13 +5,7 @@ jest.mock('fs', () => ({ jest.mock('inquirer', () => ({ prompt: () => Promise.resolve({ confirm: true }), })); -const { - isValidVersion, - replaceFile, - promptModulesUpdate, -} = require('../utils'); -const fs = require('fs'); -const path = require('path'); +const { isValidVersion, promptModulesUpdate } = require('../utils'); describe('autoUpgrade/utils.isValidVersion', () => { it('should return true when version is greater than or equal requiredVersion', () => { @@ -52,23 +46,6 @@ describe('autoUpgrade/utils.isValidVersion', () => { }); }); -describe('autoUpgrade/utils.replaceFile', () => { - it('should replace file', () => { - // $FlowIgnore only info from logger is used - replaceFile( - { - info: jest.fn(), - }, - 'test', - 'data', - ); - expect(fs.writeFileSync.mock.calls[0]).toEqual([ - path.join(process.cwd(), 'test'), - 'data', - ]); - }); -}); - describe('autoUpgrade/utils.promptModulesUpdate', () => { it('should replace file', done => { const mismatchedModules = { diff --git a/packages/gluestick/src/commands/autoUpgrade/checkForMismatch.js b/packages/gluestick/src/commands/autoUpgrade/checkForMismatch.js index 5d7eec9b..9e6c38bf 100644 --- a/packages/gluestick/src/commands/autoUpgrade/checkForMismatch.js +++ b/packages/gluestick/src/commands/autoUpgrade/checkForMismatch.js @@ -4,7 +4,7 @@ import type { MismatchedModules, UpdateDepsPromptResults } from '../../types'; const path = require('path'); const getSingleEntryFromGenerator = require('./getSingleEntryFromGenerator'); const parseConfig = require('gluestick-generators').parseConfig; -const utils = require('./utils'); +const { isValidVersion, promptModulesUpdate } = require('./utils'); const version = require('../../../package.json').version; type ProjectPackage = { @@ -16,6 +16,13 @@ const isFileDependency = (name: string) => { return name.startsWith('file'); }; +const isMismatched = (project, template) => { + return ( + !project || + (!isValidVersion(project, template) && !isFileDependency(project)) + ); +}; + /** * Open the package.json file in both the project as well as the one used by * this command line interface, then compare the versions for shared modules. @@ -44,7 +51,6 @@ const checkForMismatch = ( dev: boolean, ): Promise => { // This is done to keep live reference to mock single function in testing - const { isValidVersion, promptModulesUpdate } = utils; const projectPackage: ProjectPackage = { dependencies: {}, devDependencies: {}, @@ -78,33 +84,20 @@ const checkForMismatch = ( }; }; Object.keys(templatePackage.dependencies).forEach((dep: string): void => { - const projectDependency = projectPackage.dependencies[dep]; - if ( - dev && - dep === 'gluestick' && - !/\d+\.\d+\.\d+.*/.test(projectDependency) - ) { + const project = projectPackage.dependencies[dep]; + const template = templatePackage.dependencies[dep]; + if (dev && dep === 'gluestick' && !/\d+\.\d+\.\d+.*/.test(project)) { return; } - if ( - (!projectDependency || - !isValidVersion( - projectDependency, - templatePackage.dependencies[dep], - )) && - !isFileDependency(projectDependency) - ) { + if (isMismatched(project, template)) { markMissing(dep, 'dependencies'); } }); Object.keys(templatePackage.devDependencies).forEach((dep: string): void => { - if ( - !projectPackage.devDependencies[dep] || - !isValidVersion( - projectPackage.devDependencies[dep], - templatePackage.devDependencies[dep], - ) - ) { + const project = projectPackage.devDependencies[dep]; + const template = templatePackage.devDependencies[dep]; + + if (isMismatched(project, template)) { markMissing(dep, 'devDependencies'); } }); diff --git a/packages/gluestick/src/commands/autoUpgrade/utils.js b/packages/gluestick/src/commands/autoUpgrade/utils.js index b20a1b8c..656e3e16 100644 --- a/packages/gluestick/src/commands/autoUpgrade/utils.js +++ b/packages/gluestick/src/commands/autoUpgrade/utils.js @@ -1,31 +1,14 @@ /* @flow */ import type { - Logger, Question, MismatchedModules, UpdateDepsPromptResults, } from '../../types'; -const path = require('path'); -const fs = require('fs'); const semver = require('semver'); const chalk = require('chalk'); const inquirer = require('inquirer'); -/** - * Let the user know that we are updating the file and copy the contents over. - * - * @param {String} name the name of the file - * @param {String} data the data for the new file - */ -const replaceFile = (logger: Logger, name: string, data: string): void => { - const filePath: string = path.join(process.cwd(), name); - - logger.info(`${name} file is out of date.`); - logger.info(`Updating ${filePath}`); - fs.writeFileSync(filePath, data); -}; - /** * Determine a version meets or exceeds a requirement. * @@ -90,6 +73,5 @@ const promptModulesUpdate = ( module.exports = { isValidVersion, - replaceFile, promptModulesUpdate, };