diff --git a/index.js b/index.js index 34f85eb..0476306 100644 --- a/index.js +++ b/index.js @@ -115,6 +115,14 @@ function initOpts () { } }, + workspaces: { + type: 'string', + prompt: { + message: 'Workspaces:', + filter: parseList + } + }, + type: { type: 'string', prompt: { @@ -300,6 +308,11 @@ async function format (opts, packageInstance) { pkg.scripts = { ...(pkg.scripts || {}), ...opts.scripts }; } + // Workspaces + if (Array.isArray(opts.workspaces) && opts.workspaces.length) { + pkg.workspaces = opts.workspaces; + } + // TODO: to test the empty string, we need to stub git.author() pkg.author = opts.author || ''; pkg.license = opts.license; diff --git a/test/index.js b/test/index.js index ea92a9f..a9f92b1 100644 --- a/test/index.js +++ b/test/index.js @@ -252,6 +252,47 @@ suite('create-package-json', () => { }); }); + suite.only('scaffold workspaces', () => { + test('workspaces input', async () => { + await fix.setup(); + // Empty array is invalid according to npm + const pkg1 = await createPackageJson({ + workspaces: [] + }); + assert.deepStrictEqual(pkg1.workspaces, undefined); + + await fix.setup(); + const pkg2 = await createPackageJson({ + workspaces: ['./lib'] + }); + assert.deepStrictEqual(pkg2.workspaces, ['./lib']); + + await fix.setup(); + const pkg3 = await createPackageJson({ + workspaces: ['./lib/a', './lib/b'] + }); + assert.deepStrictEqual(pkg3.workspaces, ['./lib/a', './lib/b']); + }); + + test('workspaces prompts', async () => { + await fix.setup(); + const pkg1 = await createPackageJson(); + assert.deepStrictEqual(pkg1.workspaces, undefined); + + const pkg2 = await createPackageJson({}, { + promptor: () => { + return async (prompts) => { + console.log(prompts); + return { + workspaces: ['./lib/a', './lib/b'] + }; + }; + } + }); + assert.deepStrictEqual(pkg2.workspaces, ['./lib/a', './lib/b']); + }); + }); + suite('npm init', () => { test('parity', async () => { await fix.setup();