Skip to content

Commit

Permalink
Skip setting hash if hash was not set previously (#1871)
Browse files Browse the repository at this point in the history
* Skip setting hash if hash was not set previously

* Add end to end test for installing a github package

* Update README.md

* Fix eslint

* Reverse accidental fixture changes

* Add unit tests

* No end to end tests for github installs

Conflicts:
	src/cli/commands/install.js
  • Loading branch information
thomaschaaf authored and bestander committed Nov 16, 2016
1 parent 20eb948 commit ca6a3d2
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 20 deletions.
5 changes: 4 additions & 1 deletion __tests__/commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ test.concurrent('install with arg', (): Promise<void> => {
return runAdd({}, ['is-online'], 'install-with-arg');
});

test.concurrent('install from github', (): Promise<void> => {
return runAdd({}, ['substack/node-mkdirp#master'], 'install-github');
});

test.concurrent('install with --dev flag', (): Promise<void> => {
return runAdd({dev: true}, ['left-pad@1.1.0'], 'add-with-flag', async (config) => {
const lockfile = explodeLockfile(await fs.readFile(path.join(config.cwd, 'yarn.lock')));
Expand Down Expand Up @@ -571,5 +575,4 @@ test.concurrent('add should generate correct integrity file', (): Promise<void>
}
expect(allCorrect).toBe(true);
});

});
4 changes: 4 additions & 0 deletions __tests__/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ test.concurrent('install from git cache', (): Promise<void> => {
});
});

test.concurrent('install from github', (): Promise<void> => {
return runInstall({}, 'install-github');
});

test.concurrent('install should dedupe dependencies avoiding conflicts 0', (): Promise<void> => {
// A@2.0.1 -> B@2.0.0
// B@1.0.0
Expand Down
10 changes: 1 addition & 9 deletions __tests__/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Config from '../../src/config.js';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000;

const stream = require('stream');
const path = require('path');
const os = require('os');

Expand Down Expand Up @@ -48,14 +47,7 @@ async function runLs(
}
}

let out = '';
const stdout = new stream.Writable({
decodeStrings: false,
write(data, encoding, cb) {
out += data;
cb();
},
});
const out = '';

const reporter = new reporters.BufferReporter({stdout: null, stdin: null});

Expand Down
10 changes: 1 addition & 9 deletions __tests__/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000;

const execCommand: $FlowFixMe = require('../../src/util/execute-lifecycle-script').execCommand;

const stream = require('stream');
const path = require('path');
const os = require('os');

Expand Down Expand Up @@ -40,14 +39,7 @@ async function runRun(
}
}

let out = '';
const stdout = new stream.Writable({
decodeStrings: false,
write(data, encoding, cb) {
out += data;
cb();
},
});
const out = '';

const reporter = new reporters.BufferReporter({stdout: null, stdin: null});

Expand Down
Empty file.
5 changes: 5 additions & 0 deletions __tests__/fixtures/install/install-github/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"mkdirp": "substack/node-mkdirp#master"
}
}
115 changes: 115 additions & 0 deletions src/cli/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ import map from '../../util/map.js';
import {sortAlpha} from '../../util/misc.js';

const invariant = require('invariant');
const semver = require('semver');
const emoji = require('node-emoji');
const isCI = require('is-ci');
const path = require('path');

const {version: YARN_VERSION, installationMethod: YARN_INSTALL_METHOD} = require('../../../package.json');
const ONE_DAY = 1000 * 60 * 60 * 24;

export type InstallCwdRequest = [
DependencyRequestPatterns,
Array<string>,
Expand Down Expand Up @@ -64,6 +69,47 @@ type Flags = {
tilde: boolean,
};

/**
* Try and detect the installation method for Yarn and provide a command to update it with.
*/

function getUpdateCommand(): ?string {
if (YARN_INSTALL_METHOD === 'tar') {
return 'curl -o- -L https://yarnpkg.com/install.sh | bash';
}

if (YARN_INSTALL_METHOD === 'homebrew') {
return 'brew upgrade yarn';
}

if (YARN_INSTALL_METHOD === 'deb') {
return 'sudo apt-get update && sudo apt-get install yarn';
}

if (YARN_INSTALL_METHOD === 'rpm') {
return 'sudo yum install yarn';
}

if (YARN_INSTALL_METHOD === 'npm') {
return 'npm upgrade --global yarn';
}

if (YARN_INSTALL_METHOD === 'chocolatey') {
return 'choco upgrade yarn';
}

return null;
}

function getUpdateInstaller(): ?string {
// Windows
if (YARN_INSTALL_METHOD === 'msi') {
return 'https://yarnpkg.com/latest.msi';
}

return null;
}

function normalizeFlags(config: Config, rawFlags: Object): Flags {
const flags = {
// install
Expand Down Expand Up @@ -279,6 +325,8 @@ export class Install {
*/

async init(): Promise<Array<string>> {
this.checkUpdate();

// warn if we have a shrinkwrap
if (await fs.exists(path.join(this.config.cwd, 'npm-shrinkwrap.json'))) {
this.reporter.error(this.reporter.lang('shrinkwrapWarning'));
Expand Down Expand Up @@ -356,6 +404,7 @@ export class Install {

// fin!
await this.saveLockfileAndIntegrity(patterns);
this.maybeOutputUpdate();
this.config.requestManager.clearCache();
return patterns;
}
Expand Down Expand Up @@ -636,6 +685,72 @@ export class Install {

return request;
}

/**
* Check for updates every day and output a nag message if there's a newer version.
*/

checkUpdate() {
if (!process.stdout.isTTY || isCI) {
// don't show upgrade dialog on CI or non-TTY terminals
return;
}

// only check for updates once a day
const lastUpdateCheck = Number(this.config.getOption('lastUpdateCheck')) || 0;
if (lastUpdateCheck && Date.now() - lastUpdateCheck < ONE_DAY) {
return;
}

// don't bug for updates on tagged releases
if (YARN_VERSION.indexOf('-') >= 0) {
return;
}

this._checkUpdate().catch(() => {
// swallow errors
});
}

async _checkUpdate(): Promise<void> {
let latestVersion = await this.config.requestManager.request({
url: 'https://yarnpkg.com/latest-version',
});
invariant(typeof latestVersion === 'string', 'expected string');
latestVersion = latestVersion.trim();
if (!semver.valid(latestVersion)) {
return;
}

// ensure we only check for updates periodically
this.config.registries.yarn.saveHomeConfig({
lastUpdateCheck: Date.now(),
});

if (semver.gt(latestVersion, YARN_VERSION)) {
this.maybeOutputUpdate = () => {
this.reporter.warn(this.reporter.lang('yarnOutdated', latestVersion, YARN_VERSION));

const command = getUpdateCommand();
if (command) {
this.reporter.info(this.reporter.lang('yarnOutdatedCommand'));
this.reporter.command(command);
} else {
const installer = getUpdateInstaller();
if (installer) {
this.reporter.info(this.reporter.lang('yarnOutdatedInstaller', installer));
}
}
};
}
}

/**
* Method to override with a possible upgrade message.
*/

maybeOutputUpdate() {}
maybeOutputUpdate: any;
}

export function setFlags(commander: Object) {
Expand Down
6 changes: 5 additions & 1 deletion src/package-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ export default class PackageFetcher {
newPkg = res.package;

// update with new remote
ref.remote.hash = res.hash;
// but only if there was a hash previously as the tarball fetcher does not provide a hash.
if (ref.remote.hash) {
ref.remote.hash = res.hash;
}

if (res.resolved) {
ref.remote.resolved = res.resolved;
}
Expand Down

0 comments on commit ca6a3d2

Please sign in to comment.