Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle invalid directory errors when creating atomic component #1327

Closed
wants to merge 14 commits into from
Closed
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions packages/ui/atomic/create-atomic-component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
unlinkSync,
writeFileSync,
readFileSync,
readdirSync,
} from 'node:fs';
import {cwd} from 'node:process';
import {fileURLToPath} from 'node:url';
Expand All @@ -27,6 +28,20 @@ class SerializableAggregateError extends AggregateError {
};
}
}
class InvalidProjectDirectory extends Error {
name = 'Invalid project directory';
constructor(message) {
super(message);
}

toJSON() {
return {
name: this.name,
message: this.message,
stack: this.stack,
};
}
}

const handleErrors = (error) => {
if (process.channel) {
Expand Down Expand Up @@ -63,6 +78,7 @@ const camelize = (str) =>
str
.replace(/-(.)/g, (_, group) => group.toUpperCase())
.replace(/^./, (match) => match.toUpperCase());

const transform = (transformers) => {
for (const {srcPath, destPath, transform} of transformers) {
if (!srcPath) {
Expand Down Expand Up @@ -120,11 +136,27 @@ const ensureComponentValidity = (tag) => {
}
};

const ensureDirectoryValidity = () => {
const cwdFiles = readdirSync(cwd(), {withFileTypes: true});
const hasPackageInCwd = cwdFiles.some(
(dirent) => dirent.name === 'package.json'
);
if (cwdFiles.length > 0 && !hasPackageInCwd) {
handleErrors(
new InvalidProjectDirectory(
'Current working directory is either not empty or not an npm project (no package.json found). Please try again in an empty directory.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'Current working directory is either not empty or not an npm project (no package.json found). Please try again in an empty directory.'
'Current working directory is either not empty or not an npm project (no package.json found). Please try again in a valid project'

maybe specifying how to initialize an atomic project

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe link to some relevant doc which explains the process e.g https://docs.coveo.com/en/cli/ or something more specific if available

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disagreeing here: The create-atomic-(result)-component packages are initializers that are agnostic of the concept of atomic project.
i.e. you can use them without an atomic project.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think it's valid to refer to it as a valid project as that isn't specific to an atomic project. I think I'll update the link for this description to instead point to the atomic:cmp command documentation as it clearly outlines in what contexts the command will work:
https://docs.coveo.com/en/cli/#coveo-atomiccmp-name

)
);
}
};

/***********************************/
const __dirname = dirname(fileURLToPath(import.meta.url));
const templateRelativeDir = 'template';
const templateDirPath = resolve(__dirname, templateRelativeDir);

ensureDirectoryValidity();

cpSync(templateDirPath, cwd(), {
recursive: true,
});
Expand Down
32 changes: 32 additions & 0 deletions packages/ui/atomic/create-atomic-result-component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
unlinkSync,
writeFileSync,
readFileSync,
readdirSync,
} from 'node:fs';
import {cwd} from 'node:process';
import {fileURLToPath} from 'node:url';
Expand All @@ -28,6 +29,21 @@ class SerializableAggregateError extends AggregateError {
}
}

class InvalidProjectDirectory extends Error {
name = 'Invalid project directory';
constructor(message) {
super(message);
}

toJSON() {
return {
name: this.name,
message: this.message,
stack: this.stack,
};
}
}

const handleErrors = (error) => {
if (process.channel) {
process.send(error);
Expand Down Expand Up @@ -120,11 +136,27 @@ const ensureComponentValidity = (tag) => {
}
};

const ensureDirectoryValidity = () => {
dmbrooke marked this conversation as resolved.
Show resolved Hide resolved
const cwdFiles = readdirSync(cwd(), {withFileTypes: true});
const hasPackageInCwd = cwdFiles.some(
(dirent) => dirent.name === 'package.json'
);
if (cwdFiles.length > 0 && !hasPackageInCwd) {
handleErrors(
new InvalidProjectDirectory(
'Current working directory is either not empty or not an npm project (no package.json found). Please try again in an empty directory.'
)
);
}
};

/***********************************/
const __dirname = dirname(fileURLToPath(import.meta.url));
const templateRelativeDir = 'template';
const templateDirPath = resolve(__dirname, templateRelativeDir);

ensureDirectoryValidity();

cpSync(templateDirPath, cwd(), {
recursive: true,
});
Expand Down