Skip to content

Commit c41007a

Browse files
committed
fix(tools): lint elements exports, print errors
1 parent 4816ec0 commit c41007a

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

tools/create-element/generator/element.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ import Chalk from 'chalk';
55
import prompts from 'prompts';
66
import { $ } from 'execa';
77

8-
import { fileURLToPath } from 'url';
9-
import { dirname, join, relative } from 'path';
8+
import { fileURLToPath } from 'node:url';
9+
import { dirname, join, relative } from 'node:path';
10+
import * as path from 'node:path';
1011

1112
import { exists, mkdirp, processTemplate, readFile, writeFile } from './files.js';
1213
import { memoize } from './fp.js';
1314

14-
const { blue, green, greenBright } = Chalk;
15+
const { blue, green, greenBright, red, yellow } = Chalk;
1516

1617
const __dirname = dirname(fileURLToPath(import.meta.url));
1718

19+
const $$ = $({ stderr: 'inherit' });
20+
1821
/**
1922
* Available filenames.
2023
*
@@ -142,37 +145,56 @@ async function writeComponentFile(key: FileKey, options: GenerateElementOptions)
142145
}
143146
}
144147

148+
async function getElementPackageJsonPath(options: GenerateElementOptions) {
149+
const abspath = getComponentAbsPath(options);
150+
const { root } = path.parse(abspath);
151+
let packageJsonPath;
152+
let currentdir = abspath;
153+
while (currentdir !== root && !packageJsonPath) {
154+
const possible = join(currentdir, 'package.json');
155+
if (await exists(possible)) {
156+
packageJsonPath = possible;
157+
} else {
158+
currentdir = dirname(currentdir);
159+
}
160+
}
161+
return packageJsonPath;
162+
}
163+
164+
export class PackageJSONError extends Error {}
165+
145166
/**
146167
* Generate an Element
147168
*/
148169
export async function generateElement(options: GenerateElementOptions): Promise<void> {
149-
// ctrl-c
170+
const log = (...args: unknown[]) => void (!options?.silent && console.log(...args));
171+
const start = performance.now();
150172
if (!options || !options.tagName) {
173+
// ctrl-c
151174
return;
152-
}
153-
154-
const log = (...args: unknown[]) => !options.silent && console.log(...args);
155-
156-
const $$ = $({ stderr: 'inherit' });
157-
158-
const packageJsonPath = join(options.directory, 'package.json');
159-
// Quit if trying to scaffold an element in an uninitialized non-monorepo
160-
if (!await exists(packageJsonPath)) {
161-
return console.log('‼️ No package.json found.', '� Scaffold a repository first');
175+
} else if (!await exists(join(options.directory, 'package.json'))) {
176+
// Quit if trying to scaffold an element in an uninitialized non-monorepo
177+
throw new PackageJSONError('‼️ No package.json found. � Scaffold a repository first');
162178
} else if (!await shouldWriteToDir(options)) {
163-
return;
179+
return log(red`Skipping`, 'file write!');
164180
} else {
181+
const packageJsonPath = await getElementPackageJsonPath(options) ?? './**/package.json ./package.json';
182+
if (!await exists(packageJsonPath)) {
183+
throw new PackageJSONError(`Could not find package at ${packageJsonPath}`);
184+
}
165185
log(`\nCreating ${green(options.tagName)} in ${getComponentPathFromDirectoryOption(options)}\n`);
166186
log(blue`Writing`, 'files...');
167187
// $ mkdir -p /Users/alj/jazz-elements/elements/pf-jazz-hands
168188
await mkdirp(getComponentAbsPath(options));
169189
for (const key of Object.keys(FileKey).sort() as FileKey[]) {
170190
await writeComponentFile(key, options);
171191
}
172-
log(blue`Linting`, 'package exports...');
192+
log(blue`Linting`, `${relative(options.directory, packageJsonPath)} for package exports...`);
173193
await $$`npx eslint ${packageJsonPath} --fix`;
174194
log(blue`Analyzing`, 'elements...');
175195
await $$`npm run analyze`;
176-
log(`\n${greenBright('Done!')}`);
196+
const end = performance.now();
197+
const seconds = (end - start) / 1000;
198+
log(`\n${greenBright`Done`} in ${yellow(seconds.toFixed(2))} seconds`);
177199
}
178200
}

tools/create-element/main.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { generateElement } from './generator/element.js';
1+
import { generateElement, PackageJSONError } from './generator/element.js';
22

33
import Chalk from 'chalk';
44
import Yargs from 'yargs';
@@ -38,6 +38,7 @@ interface PackageJSON {
3838
workspaces?: string;
3939
}
4040

41+
/** generated at https://dom111.github.io/image-to-ansi/ */
4142
function banner() {
4243
console.log(`\x1b[49m \x1b[38;5;87;49m▄\x1b[38;5;87;48;5;87m▄\x1b[38;5;87;49m▄\x1b[49m \x1b[m
4344
\x1b[49m \x1b[38;5;81;49m▄\x1b[38;5;81;48;5;87m▄▄\x1b[49m \x1b[38;5;81;48;5;87m▄\x1b[38;5;87;48;5;87m▄\x1b[38;5;81;49m▄\x1b[49m \x1b[m
@@ -140,5 +141,13 @@ export async function main(): Promise<void> {
140141
}))
141142
.then(({ argv }) => argv as GenerateElementOptions)
142143
.then(promptForElementGeneratorOptions)
143-
.then(generateElement);
144+
.then(generateElement)
145+
.catch(e => {
146+
if (e instanceof PackageJSONError) {
147+
console.log(e.message);
148+
process.exit(1);
149+
} else {
150+
throw e;
151+
}
152+
});
144153
}

0 commit comments

Comments
 (0)