Skip to content

Commit 38cc19d

Browse files
authored
feat: workspaces support (#226)
1 parent cd4b569 commit 38cc19d

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ function initOpts () {
115115
}
116116
},
117117

118+
workspaces: {
119+
type: 'string',
120+
prompt: {
121+
message: 'Workspaces:',
122+
filter: parseList
123+
}
124+
},
125+
118126
type: {
119127
type: 'string',
120128
prompt: {
@@ -301,6 +309,11 @@ async function format (opts, packageInstance) {
301309
pkg.scripts = { ...(pkg.scripts || {}), ...opts.scripts };
302310
}
303311

312+
// Workspaces
313+
if (Array.isArray(opts.workspaces) && opts.workspaces.length) {
314+
pkg.workspaces = opts.workspaces;
315+
}
316+
304317
// TODO: to test the empty string, we need to stub git.author()
305318
pkg.author = opts.author || '';
306319
pkg.license = opts.license;

test/index.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ suite('create-package-json', () => {
5656
assert.strictEqual(prompts[4].name, 'repository');
5757
assert.strictEqual(prompts[5].name, 'keywords');
5858
assert.strictEqual(prompts[6].name, 'license');
59-
assert.strictEqual(prompts[7].name, 'type');
60-
assert.strictEqual(prompts[8].name, 'main');
61-
assert.strictEqual(prompts[9].name, 'dependencies');
62-
assert.strictEqual(prompts[10].name, 'devDependencies');
59+
assert.strictEqual(prompts[7].name, 'workspaces');
60+
assert.strictEqual(prompts[8].name, 'type');
61+
assert.strictEqual(prompts[9].name, 'main');
62+
assert.strictEqual(prompts[10].name, 'dependencies');
63+
assert.strictEqual(prompts[11].name, 'devDependencies');
6364

6465
// Set defaults from prompts
6566
const out = await Promise.all(prompts.map(async (p) => {
@@ -262,6 +263,46 @@ suite('create-package-json', () => {
262263
});
263264
});
264265

266+
suite('scaffold workspaces', () => {
267+
test('workspaces input', async () => {
268+
await fix.setup();
269+
// Empty array is invalid according to npm
270+
const pkg1 = await createPackageJson({
271+
workspaces: []
272+
});
273+
assert.deepStrictEqual(pkg1.workspaces, undefined);
274+
275+
await fix.setup();
276+
const pkg2 = await createPackageJson({
277+
workspaces: ['./lib']
278+
});
279+
assert.deepStrictEqual(pkg2.workspaces, ['./lib']);
280+
281+
await fix.setup();
282+
const pkg3 = await createPackageJson({
283+
workspaces: ['./lib/a', './lib/b']
284+
});
285+
assert.deepStrictEqual(pkg3.workspaces, ['./lib/a', './lib/b']);
286+
});
287+
288+
test('workspaces prompts', async () => {
289+
await fix.setup();
290+
const pkg1 = await createPackageJson();
291+
assert.deepStrictEqual(pkg1.workspaces, undefined);
292+
293+
const pkg2 = await createPackageJson({}, {
294+
promptor: () => {
295+
return async (prompts) => {
296+
return {
297+
workspaces: ['./lib/a', './lib/b']
298+
};
299+
};
300+
}
301+
});
302+
assert.deepStrictEqual(pkg2.workspaces, ['./lib/a', './lib/b']);
303+
});
304+
});
305+
265306
suite('npm init', () => {
266307
test('parity', async () => {
267308
await fix.setup();

0 commit comments

Comments
 (0)