diff --git a/__tests__/commands/__snapshots__/init.js.snap b/__tests__/commands/__snapshots__/init.js.snap index 0a0ae8c986..0961fd6ab6 100644 --- a/__tests__/commands/__snapshots__/init.js.snap +++ b/__tests__/commands/__snapshots__/init.js.snap @@ -5,11 +5,21 @@ Object { "license": "MIT", "main": "index.js", "name": "hi-github", + "private": false, "repository": "https://github.com/user/repo", "version": "1.0.0", } `; +exports[`init-private-empty 1`] = ` +Object { + "license": "MIT", + "main": "index.js", + "name": "private-empty", + "version": "1.0.0", +} +`; + exports[`init-yes 1`] = ` Object { "license": "MIT", @@ -18,3 +28,13 @@ Object { "version": "1.0.0", } `; + +exports[`init-yes-private 1`] = ` +Object { + "license": "MIT", + "main": "index.js", + "name": "init-yes-private", + "private": true, + "version": "1.0.0", +} +`; diff --git a/__tests__/commands/init.js b/__tests__/commands/init.js index e8f90f314f..32defa527b 100644 --- a/__tests__/commands/init.js +++ b/__tests__/commands/init.js @@ -30,11 +30,36 @@ test.concurrent('init --yes should create package.json with defaults', (): Promi // Name is derived from directory name which is dynamic so check // that separately and then remove from snapshot expect(manifest.name).toEqual(path.basename(cwd)); + expect(manifest.private).toEqual(undefined); expect({...manifest, name: 'init-yes'}).toMatchSnapshot('init-yes'); }, ); }); +test.concurrent('init --yes --private should create package.json with defaults and private true', (): Promise => { + return buildRun( + ConsoleReporter, + fixturesLoc, + (args, flags, config, reporter, lockfile): Promise => { + return runInit(config, reporter, flags, args); + }, + [], + {yes: true, private: true}, + 'init-yes-private', + async (config): Promise => { + const {cwd} = config; + const manifestFile = await fs.readFile(path.join(cwd, 'package.json')); + const manifest = JSON.parse(manifestFile); + + // Name is derived from directory name which is dynamic so check + // that separately and then remove from snapshot + expect(manifest.name).toEqual(path.basename(cwd)); + expect(manifest.private).toEqual(true); + expect({...manifest, name: 'init-yes-private'}).toMatchSnapshot('init-yes-private'); + }, + ); +}); + test.concurrent('init using Github shorthand should resolve to full repository URL', (): Promise => { const questionMap = Object.freeze({ name: 'hi-github', @@ -44,6 +69,7 @@ test.concurrent('init using Github shorthand should resolve to full repository U 'repository url': 'user/repo', author: '', license: '', + private: 'false', }); class TestReporter extends ConsoleReporter { question(question: string, options?: QuestionOptions = {}): Promise { @@ -75,6 +101,46 @@ test.concurrent('init using Github shorthand should resolve to full repository U ); }); +test.concurrent('init and give private empty', (): Promise => { + const questionMap = Object.freeze({ + name: 'private-empty', + version: '', + description: '', + 'entry point': '', + 'repository url': '', + author: '', + license: '', + private: '', + }); + class TestReporter extends ConsoleReporter { + question(question: string, options?: QuestionOptions = {}): Promise { + return new Promise((resolve, reject) => { + const parsedQuestion = question.replace(/ \((.*?)\)/g, ''); + if (parsedQuestion in questionMap) { + resolve(questionMap[parsedQuestion]); + } else { + reject(new Error(`Question not found in question-answer map ${parsedQuestion}`)); + } + }); + } + } + + return buildRun( + TestReporter, + fixturesLoc, + (args, flags, config, reporter, lockfile): Promise => { + return runInit(config, reporter, flags, args); + }, + [], + {}, + 'init-github', + async (config): Promise => { + const manifestFile = await fs.readFile(path.join(config.cwd, 'package.json')); + expect(JSON.parse(manifestFile)).toMatchSnapshot('init-private-empty'); + }, + ); +}); + test.concurrent('getGitConfigInfo should not return the git config val', async (): Promise => { expect('hi seb').toEqual(await getGitConfigInfo('some-info', () => Promise.resolve('hi seb'))); }); diff --git a/__tests__/fixtures/init/init-yes-private/.npmrc b/__tests__/fixtures/init/init-yes-private/.npmrc new file mode 100644 index 0000000000..9465b97ac3 --- /dev/null +++ b/__tests__/fixtures/init/init-yes-private/.npmrc @@ -0,0 +1 @@ +yarn-offline-mirror=./mirror-for-offline diff --git a/package.json b/package.json index e35269c2c4..3ff5137ec0 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "tar-stream": "^1.5.2", "uuid": "^3.0.1", "v8-compile-cache": "^1.1.0", - "validate-npm-package-license": "^3.0.1" + "validate-npm-package-license": "^3.0.1", + "yn": "^2.0.0" }, "devDependencies": { "babel-core": "^6.24.1", diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index 82d68e0dd9..a34055fc0b 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -11,9 +11,11 @@ import * as validate from '../../util/normalize-manifest/validate.js'; const objectPath = require('object-path'); const path = require('path'); +const yn = require('yn'); export function setFlags(commander: Object) { commander.option('-y, --yes', 'use default options'); + commander.option('-p, --private', 'use default options and private true'); } export function hasWrapper(commander: Object, args: Array): boolean { @@ -89,12 +91,18 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg question: 'license', default: String(config.getOption('init-license')), }, + { + key: 'private', + question: 'private', + default: '', + inputFormatter: yn, + }, ]; // get answers const pkg = {}; for (const entry of keys) { - const {yes} = flags; + const {yes, private: privateFlag} = flags; const {key: manifestKey} = entry; let {question, default: def} = entry; @@ -114,8 +122,12 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg def = val; } + if (manifestKey === 'private' && privateFlag) { + def = true; + } + if (def) { - question += ` (${def})`; + question += ` (${def.toString()})`; } let answer; @@ -141,6 +153,9 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg } if (answer) { + if (entry.inputFormatter) { + answer = entry.inputFormatter(answer); + } objectPath.set(pkg, manifestKey, answer); } } diff --git a/yarn.lock b/yarn.lock index b91b697153..96d8bfff16 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4958,3 +4958,7 @@ yargs@~3.10.0: cliui "^2.1.0" decamelize "^1.0.0" window-size "0.1.0" + +yn@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/yn/-/yn-2.0.0.tgz#e5adabc8acf408f6385fc76495684c88e6af689a"