Skip to content

Commit 1afb1de

Browse files
committed
feat: workspaces support
1 parent 6e1df67 commit 1afb1de

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
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: {
@@ -300,6 +308,11 @@ async function format (opts, packageInstance) {
300308
pkg.scripts = { ...(pkg.scripts || {}), ...opts.scripts };
301309
}
302310

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

test/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,47 @@ suite('create-package-json', () => {
252252
});
253253
});
254254

255+
suite.only('scaffold workspaces', () => {
256+
test('workspaces input', async () => {
257+
await fix.setup();
258+
// Empty array is invalid according to npm
259+
const pkg1 = await createPackageJson({
260+
workspaces: []
261+
});
262+
assert.deepStrictEqual(pkg1.workspaces, undefined);
263+
264+
await fix.setup();
265+
const pkg2 = await createPackageJson({
266+
workspaces: ['./lib']
267+
});
268+
assert.deepStrictEqual(pkg2.workspaces, ['./lib']);
269+
270+
await fix.setup();
271+
const pkg3 = await createPackageJson({
272+
workspaces: ['./lib/a', './lib/b']
273+
});
274+
assert.deepStrictEqual(pkg3.workspaces, ['./lib/a', './lib/b']);
275+
});
276+
277+
test('workspaces prompts', async () => {
278+
await fix.setup();
279+
const pkg1 = await createPackageJson();
280+
assert.deepStrictEqual(pkg1.workspaces, undefined);
281+
282+
const pkg2 = await createPackageJson({}, {
283+
promptor: () => {
284+
return async (prompts) => {
285+
console.log(prompts);
286+
return {
287+
workspaces: ['./lib/a', './lib/b']
288+
};
289+
};
290+
}
291+
});
292+
assert.deepStrictEqual(pkg2.workspaces, ['./lib/a', './lib/b']);
293+
});
294+
});
295+
255296
suite('npm init', () => {
256297
test('parity', async () => {
257298
await fix.setup();

0 commit comments

Comments
 (0)