Skip to content

Commit 22464c0

Browse files
committed
feat(create): --monorepo flag
1 parent 680b175 commit 22464c0

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

.changeset/tricky-tools-joke.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@patternfly/create-element": minor
3+
---
4+
5+
Adds `--monorepo` flag to generator.
6+
In most cases, this is automatically derived from the root package.json.
7+
Override with `--monorepo` or `--no-monorepo`.

tools/create-element/generator/element.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ const TEMPLATE_FILE_PATHS: Record<FileKey, string> = {
8080
tsconfig: 'tsconfig.json',
8181
};
8282

83+
function isMonorepoFileKey(key: FileKey): boolean {
84+
switch (key) {
85+
case FileKey.package:
86+
case FileKey.cemConfig:
87+
case FileKey.tsconfig:
88+
return true;
89+
default:
90+
return false;
91+
}
92+
}
93+
8394
/** Get output files */
8495
const getFilePathsRelativeToPackageDir =
8596
memoize((options: GenerateElementOptions): Record<FileKey, string> => ({
@@ -163,6 +174,10 @@ async function getTemplate(key: FileKey): Promise<string> {
163174
}
164175

165176
async function writeComponentFile(key: FileKey, options: GenerateElementOptions) {
177+
if (!options.monorepo && isMonorepoFileKey(key)) {
178+
return;
179+
}
180+
166181
const PATH = getOutputFilePath(key, options);
167182
const TEMPLATE = await getTemplate(key);
168183
const DATA = getInterpolations(options);
@@ -204,8 +219,10 @@ async function analyzeElement(options: GenerateElementOptions): Promise<void> {
204219
console.log(`\nAnalyzing ${greenBright(options.tagName)}`);
205220
}
206221

222+
const monorepoArgs = !options.monorepo ? [] : ['--prefix', getComponentAbsPath(options)];
223+
207224
const { stderr, stdout } =
208-
await execa('npm', ['run', 'analyze', '--prefix', getComponentAbsPath(options)], {
225+
await execa('npm', ['run', 'analyze', ...monorepoArgs], {
209226
all: true,
210227
cwd: options.directory,
211228
});
@@ -250,7 +267,9 @@ export async function generateElement(options: GenerateElementOptions): Promise<
250267
} else {
251268
await writeElementFiles(options);
252269
await analyzeElement(options);
253-
await updateTsconfig(options);
254-
await execaCommand('npm install');
270+
if (options.monorepo) {
271+
await updateTsconfig(options);
272+
await execaCommand('npm install');
273+
}
255274
}
256275
}

tools/create-element/main.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import { generateElement } from './generator/element.js';
33
import Chalk from 'chalk';
44
import Yargs from 'yargs';
55
import prompts from 'prompts';
6+
import { readJson } from './generator/files.js';
7+
import { join } from 'node:path';
68

79
export interface BaseOptions {
810
silent: boolean;
911
directory: string;
1012
overwrite: boolean;
13+
monorepo: boolean;
1114
}
1215

1316
export interface AppOptions extends BaseOptions {
@@ -30,6 +33,18 @@ const ERR_BAD_CE_TAG_NAME =
3033

3134
const b = Chalk.cyanBright;
3235

36+
interface PackageJSON {
37+
customElements?: string;
38+
name: string;
39+
version: string;
40+
workspaces?: string;
41+
}
42+
43+
async function isMonorepo() {
44+
const { workspaces } = await readJson<PackageJSON>(join(process.cwd(), 'package.json'));
45+
return !!workspaces;
46+
}
47+
3348
function banner() {
3449
console.log(`${Chalk.cyan(`
3550
${b('`qQQQQQQg.')}
@@ -72,7 +87,7 @@ export async function promptForElementGeneratorOptions(
7287
initial: options?.tagName ?? '',
7388
validate: name => name.includes('-') || ERR_BAD_CE_TAG_NAME,
7489
}, {
75-
type: () => !options?.scope && 'text',
90+
type: () => (!options?.scope && options?.monorepo) ? 'text' : false,
7691
name: 'scope',
7792
message: 'What is the package\'s NPM scope?',
7893
initial: options?.scope ?? ''
@@ -111,6 +126,11 @@ export async function main(): Promise<void> {
111126
default: false,
112127
description: 'Overwrite files without prompting',
113128
})
129+
.option('monorepo', {
130+
type: 'boolean',
131+
default: await isMonorepo(),
132+
description: 'Generate an npm package for the element'
133+
})
114134
.help()
115135
.check(({ name }) => {
116136
if (typeof name === 'string' && !name.includes('-')) {

tools/pfe-tools/11ty/DocsPage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import nunjucks, { Environment } from 'nunjucks';
1414
interface PackageJSON {
1515
customElements?: string;
1616
name: string;
17+
version: string;
18+
workspaces?: string;
1719
}
1820

1921
export interface RenderKwargs {

0 commit comments

Comments
 (0)