Skip to content

Commit fb84276

Browse files
authored
fix: better logic for when to prompt for workspace root and workspaces (#244)
1 parent 0fb64d2 commit fb84276

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

index.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,35 @@ function initOpts () {
116116
}
117117
},
118118

119-
workspaces: {
120-
type: 'string',
121-
prompt: {
122-
message: 'Workspaces:',
123-
filter: parseList
124-
}
125-
},
126-
127119
workspaceRoot: {
128120
type: 'string',
129121
flag: {
130122
key: 'workspace-root'
131123
},
132124
prompt: {
133125
message: 'Workspace Root:',
126+
default: (promptInput, allInput) => {
127+
return allInput.cwd;
128+
},
134129
when: (promptInput, defaultWhen, allInput) => {
135-
if (allInput.workspaceRoot !== allInput.cwd) {
130+
if (defaultWhen && allInput.workspaceRoot !== allInput.cwd) {
136131
return true;
137132
}
133+
return false;
134+
}
135+
}
136+
},
137+
138+
workspaces: {
139+
type: 'string',
140+
prompt: {
141+
message: 'Workspaces:',
142+
filter: parseList,
143+
when: (promptInput, defaultWhen, allInput) => {
144+
// Only ask this when running from a workspace root
145+
if (allInput.workspaceRoot !== allInput.cwd) {
146+
return false;
147+
}
138148
return defaultWhen;
139149
}
140150
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"fs-extra": "^11.1.1",
4343
"loggerr": "^3.0.0",
4444
"npm-package-arg": "^11.0.1",
45-
"opta": "^1.1.5",
45+
"opta": "^1.1.6",
4646
"safe-parse-list": "^0.1.1",
4747
"validate-npm-package-name": "^5.0.0"
4848
},

test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ suite('create-package-json', () => {
5959
assert.strictEqual(prompts[4].name, 'repository');
6060
assert.strictEqual(prompts[5].name, 'keywords');
6161
assert.strictEqual(prompts[6].name, 'license');
62-
assert.strictEqual(prompts[7].name, 'workspaces');
63-
assert.strictEqual(prompts[8].name, 'workspaceRoot');
62+
assert.strictEqual(prompts[7].name, 'workspaceRoot');
63+
assert.strictEqual(prompts[8].name, 'workspaces');
6464
assert.strictEqual(prompts[9].name, 'type');
6565
assert.strictEqual(prompts[10].name, 'main');
6666
assert.strictEqual(prompts[11].name, 'dependencies');

test/monorepo.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,33 @@ suite('monorepo', () => {
9090
assert.strictEqual(pkg.workspaces, undefined);
9191
assert.strictEqual(pkg.dependencies['english-days'], '^1.0.0');
9292
});
93+
94+
test('create a new workspace package.json in an existing monorepo passing workspaceRoot', async () => {
95+
await fix.setup('monorepo');
96+
const pkg = await createPackageJson(
97+
{
98+
cwd: path.join(fix.TMP, 'baz'),
99+
workspaceRoot: fix.TMP
100+
},
101+
optaUtils.test.promptModule({
102+
prompts: {
103+
workspaceRoot: {
104+
assert: (p) => {
105+
assert.strictEqual(p.name, 'workspaceRoot');
106+
assert.strictEqual(p.when(), false);
107+
}
108+
},
109+
workspaces: {
110+
assert: (p) => {
111+
assert.strictEqual(p.name, 'workspaces');
112+
assert.strictEqual(p.when(), false);
113+
}
114+
}
115+
}
116+
})
117+
);
118+
119+
assert.strictEqual(pkg.name, 'baz');
120+
assert.strictEqual(pkg.workspaces, undefined);
121+
});
93122
});

0 commit comments

Comments
 (0)