Skip to content

Commit

Permalink
Feature: Support save-exact true (#4471)
Browse files Browse the repository at this point in the history
**Summary**

Fixes #4343. Currently there is no way to remove the package prefix inside `.yarnrc` file, this PR add support for `save-exact` in `.yarnrc` as discussed in #4343. Full credit goes to @jambonrose

```
save-exact true
```

One small thing, should `yarn` be backwards compatible with the old behavior which is `save-prefix ''`? We can just add an extra check here for do this. What do you think @BYK?


```js
} else if (exact || Boolean(this.config.getOption('save-exact')) || Boolean(this.config.getOption('save-prefix'))) {

```

**Test plan**

New unit test.
  • Loading branch information
ahmedelgabri authored and BYK committed Sep 17, 2017
1 parent 96c215c commit 0e16ee9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 11 additions & 0 deletions __tests__/commands/add.js
Expand Up @@ -223,6 +223,17 @@ test.concurrent('add save-prefix should not expand ~ to home dir', (): Promise<v
});
});

test.concurrent('add save-exact should make all package.json strict', (): Promise<void> => {
return runAdd(['left-pad'], {}, 'install-strict-all', async config => {
const lockfile = explodeLockfile(await fs.readFile(path.join(config.cwd, 'yarn.lock')));

expect(lockfile[0]).toMatch(/^left-pad@\d+\.\d+\.\d+:$/);
expect(JSON.parse(await fs.readFile(path.join(config.cwd, 'package.json'))).dependencies['left-pad']).toMatch(
/^\d+\.\d+\.\d+$/,
);
});
});

test.concurrent('add with new dependency should be deterministic 3', (): Promise<void> => {
return runAdd([], {}, 'install-should-cleanup-when-package-json-changed-3', async (config, reporter) => {
// expecting yarn check after installation not to fail
Expand Down
2 changes: 2 additions & 0 deletions __tests__/fixtures/add/install-strict-all/.yarnrc
@@ -0,0 +1,2 @@
yarn-offline-mirror "./mirror-for-offline"
save-exact true
6 changes: 4 additions & 2 deletions src/cli/commands/add.js
Expand Up @@ -69,7 +69,9 @@ export class Add extends Install {
* returns version for a pattern based on Manifest
*/
getPatternVersion(pattern: string, pkg: Manifest): string {
const {exact, tilde} = this.flags;
const tilde = this.flags.tilde;
const configPrefix = String(this.config.getOption('save-prefix'));
const exact = this.flags.exact || Boolean(this.config.getOption('save-exact')) || configPrefix === '';
const {hasVersion, range} = normalizePattern(pattern);
let version;

Expand All @@ -86,7 +88,7 @@ export class Add extends Install {
} else if (exact) {
prefix = '';
} else {
prefix = String(this.config.getOption('save-prefix')) || '^';
prefix = configPrefix || '^';
}

version = `${prefix}${pkg.version}`;
Expand Down

0 comments on commit 0e16ee9

Please sign in to comment.