Skip to content

Commit

Permalink
Merge branch 'master' of github.com:yarnpkg/yarn
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/constants.js
  • Loading branch information
Sebastian McKenzie committed Oct 12, 2016
2 parents 71c050b + 7bb8278 commit 76c559a
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
git:
depth: 1
depth: 10
language: generic

sudo: false
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<a href="https://travis-ci.org/yarnpkg/yarn"><img alt="Travis Status" src="https://travis-ci.com/yarnpkg/yarn.svg?token=DxqWAqRqs3zWAF8EhBHy"></a>
<a href="https://circleci.com/gh/yarnpkg/yarn"><img alt="Circle Status" src="https://circleci.com/gh/yarnpkg/yarn.svg?style=svg&circle-token=5f0a78473b0f440afb218bf2b82323cc6b3cb43f"></a>
<a href="https://ci.appveyor.com/project/yarnpkg/yarn/branch/master"><img alt="Appveyor Status" src="https://ci.appveyor.com/api/projects/status/rhcdj4980ccy7su3/branch/master?svg=true"></a>
<a href="https://discord.gg/yarnpkg"><img alt="Discord Status" src="https://discordapp.com/api/guilds/226791405589233664/widget.png"></a>
<a href="https://discord.gg/yarnpkg"><img alt="Discord Chat" src="https://discordapp.com/api/guilds/226791405589233664/widget.png"></a>
</p>

---
Expand Down
81 changes: 81 additions & 0 deletions __tests__/commands/_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* @flow */

import {Reporter} from '../../src/reporters/index.js';
import * as reporters from '../../src/reporters/index.js';
import * as fs from '../../src/util/fs.js';
import {run as init} from '../../src/cli/commands/init.js';
import Config from '../../src/config.js';

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

const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'init');

export function runInit(
flags: Object,
name: string,
checkInitialized?: ?(config: Config) => ?Promise<void>,
): Promise<void> {
return run(() => {
return init;
}, flags, path.join(fixturesLoc, name), checkInitialized);
}

export async function run(
factory: () => (config: Config, reporter: Reporter, flags: Object, args: Array<string>) => Promise<void>,
flags: Object,
dir: string,
checkInitialized: ?(config: Config) => ?Promise<void>,
): Promise<void> {
const cwd = path.join(
os.tmpdir(),
`yarn-${path.basename(dir)}-${Math.random()}`,
);
await fs.unlink(cwd);
await fs.copy(dir, cwd);

for (const {basename, absolute} of await fs.walk(cwd)) {
if (basename.toLowerCase() === '.ds_store') {
await fs.unlink(absolute);
}
}

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

const reporter = new reporters.ConsoleReporter({stdout, stderr: stdout});

// create directories
await fs.mkdirp(path.join(cwd, '.yarn'));
await fs.mkdirp(path.join(cwd, 'node_modules'));

try {
const config = new Config(reporter);
await config.init({
cwd,
globalFolder: path.join(cwd, '.yarn/.global'),
packagesRoot: path.join(cwd, '.yarn'),
linkFolder: path.join(cwd, '.yarn/.link'),
});

await init(config, reporter, flags, []);

try {
if (checkInitialized) {
await checkInitialized(config);
}
} finally {
// clean up
await fs.unlink(cwd);
}
} catch (err) {
throw new Error(`${err && err.stack} \nConsole output:\n ${out}`);
}
}
23 changes: 23 additions & 0 deletions __tests__/commands/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* @flow */

import {runInit} from './_init.js';
import {DEFAULTS} from '../../src/registries/yarn-registry.js';
import * as fs from '../../src/util/fs.js';
import assert from 'assert';

const path = require('path');

jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

test.concurrent('init --yes should create package.json with defaults', (): Promise<void> => {
return runInit({yes: true}, 'init-yes', async (config): Promise<void> => {
const {cwd} = config;
const manifestFile = await fs.readFile(path.join(cwd, 'package.json'));
const manifest = JSON.parse(manifestFile);

assert.equal(manifest.name, path.basename(cwd));
assert.equal(manifest.main, 'index.js');
assert.equal(manifest.version, DEFAULTS['init-version']);
assert.equal(manifest.license, DEFAULTS['init-license']);
});
});
1 change: 1 addition & 0 deletions __tests__/fixtures/init/init-yes/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn-offline-mirror=./mirror-for-offline
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"testPathIgnorePatterns": [
"__tests__/(fixtures|__mocks__)/",
"updates/",
"/_(temp|mock|install).js$"
"/_(temp|mock|install|init).js$"
]
}
}
14 changes: 12 additions & 2 deletions src/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import * as fs from '../../util/fs.js';
const objectPath = require('object-path');
const path = require('path');

export const noArguments = true;
export function setFlags(commander: Object) {
commander.option('-y, --yes', 'use default options');
}

export async function run(
config: Config,
Expand Down Expand Up @@ -84,6 +86,7 @@ export async function run(
// get answers
const pkg = {};
for (const entry of keys) {
const {yes} = flags;
const {key: manifestKey} = entry;
let {question, default: def} = entry;

Expand All @@ -100,7 +103,14 @@ export async function run(
question += ` (${def})`;
}

const answer = (await reporter.question(question)) || def;
let answer;

if (yes) {
answer = def;
} else {
answer = (await reporter.question(question)) || def;
}

if (answer) {
objectPath.set(pkg, manifestKey, answer);
}
Expand Down
10 changes: 9 additions & 1 deletion src/registries/yarn-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ export default class YarnRegistry extends NpmRegistry {
homeConfig: Object;

getOption(key: string): mixed {
return this.config[key] || this.registries.npm.getOption(npmMap[key] || key);
let val = this.config[key] || this.registries.npm.getOption(npmMap[key]);

// if we have no yarn option for this or have used a default then use the npm
// value if it exists
if (!val || val === DEFAULTS[key]) {
val = this.registries.npm.getOption(key) || val;
}

return val;
}

async loadConfig(): Promise<void> {
Expand Down
6 changes: 5 additions & 1 deletion src/reporters/console/console-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ export default class ConsoleReporter extends BaseReporter {
input: this.stdin,
}, (err, answer) => {
if (err) {
reject(err);
if (err.message === 'canceled') {
process.exit(1);
} else {
reject(err);
}
} else {
if (!answer && options.required) {
this.error(this.lang('answerRequired'));
Expand Down

0 comments on commit 76c559a

Please sign in to comment.