From e0e119e9195cb6fd0b885f61b0a9f7a7e5d0326a Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Fri, 30 Jun 2017 22:24:33 -0500 Subject: [PATCH] Do not save lockfile when no dependencies (#3395) --- __tests__/commands/install/lockfiles.js | 8 +++----- __tests__/index.js | 2 +- src/cli/commands/install.js | 7 ++++++- src/cli/index.js | 2 +- src/reporters/lang/en.js | 2 ++ 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/__tests__/commands/install/lockfiles.js b/__tests__/commands/install/lockfiles.js index 3df100326f..c0be7e17be 100644 --- a/__tests__/commands/install/lockfiles.js +++ b/__tests__/commands/install/lockfiles.js @@ -44,15 +44,13 @@ test.concurrent("writes new lockfile if existing one isn't satisfied", async (): }); }); -test.concurrent('writes a lockfile even when there are no dependencies', (): Promise => { - // https://github.com/yarnpkg/yarn/issues/679 +test.concurrent("doesn't write a lockfile when there are no dependencies", (): Promise => { return runInstall({}, 'install-without-dependencies', async config => { const lockfileExists = await fs.exists(path.join(config.cwd, 'yarn.lock')); const installedDepFiles = await fs.walk(path.join(config.cwd, 'node_modules')); - expect(lockfileExists).toEqual(true); - // 1 for integrity file (located in node_modules) - expect(installedDepFiles).toHaveLength(1); + expect(lockfileExists).toEqual(false); + expect(installedDepFiles).toHaveLength(0); }); }); diff --git a/__tests__/index.js b/__tests__/index.js index f30edee001..24073233ab 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -208,7 +208,7 @@ test.concurrent('should install if no args', async () => { test.concurrent('should install if first arg looks like a flag', async () => { const stdout = await execCommand('--json', [], 'run-add', true); - expect(stdout[stdout.length - 1]).toEqual('{"type":"success","data":"Saved lockfile."}'); + expect(stdout[stdout.length - 1]).toEqual('{"type":"info","data":"Lockfile not saved, no dependencies."}'); }); test.concurrent('should not output JSON activity/progress if given --no-progress option', async () => { diff --git a/src/cli/commands/install.js b/src/cli/commands/install.js index b48bdc629e..79964e00f3 100644 --- a/src/cli/commands/install.js +++ b/src/cli/commands/install.js @@ -503,7 +503,12 @@ export class Install { } // fin! - await this.saveLockfileAndIntegrity(topLevelPatterns, workspaceLayout); + // The second condition is to make sure lockfile can be updated when running `remove` command. + if (topLevelPatterns.length || (await fs.exists(path.join(this.config.cwd, constants.LOCKFILE_FILENAME)))) { + await this.saveLockfileAndIntegrity(topLevelPatterns, workspaceLayout); + } else { + this.reporter.info(this.reporter.lang('notSavedLockfileNoDependencies')); + } this.maybeOutputUpdate(); this.config.requestManager.clearCache(); return flattenedTopLevelPatterns; diff --git a/src/cli/index.js b/src/cli/index.js index 3563284c2d..2f6bbcaf35 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -29,7 +29,7 @@ const args = process.argv.slice(2, doubleDashIndex === -1 ? process.argv.length const endArgs = doubleDashIndex === -1 ? [] : process.argv.slice(doubleDashIndex + 1, process.argv.length); // set global options -commander.version(version); +commander.version(version, '--version'); commander.usage('[command] [flags]'); commander.option('--verbose', 'output verbose messages on internal operations'); commander.option('--offline', 'trigger an error if any required dependencies are not available in local cache'); diff --git a/src/reporters/lang/en.js b/src/reporters/lang/en.js index 038c4d9810..14c3c81a2d 100644 --- a/src/reporters/lang/en.js +++ b/src/reporters/lang/en.js @@ -182,6 +182,8 @@ const messages = { foundWarnings: 'Found $0 warnings.', foundErrors: 'Found $0 errors.', + notSavedLockfileNoDependencies: 'Lockfile not saved, no dependencies.', + savedLockfile: 'Saved lockfile.', noRequiredLockfile: 'No lockfile in this directory. Run `yarn install` to generate one.', noLockfileFound: 'No lockfile found.',