Skip to content

Commit

Permalink
Wrap into main method
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbrooke committed Sep 22, 2023
1 parent f4a80f4 commit 9a07d73
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 93 deletions.
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.

110 changes: 64 additions & 46 deletions packages/ui/atomic/create-atomic-component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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,
};
}
}

class InvalidProjectDirectory extends Error {
name = 'Invalid project directory';
Expand Down Expand Up @@ -140,52 +154,56 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
const templateRelativeDir = 'template';
const templateDirPath = resolve(__dirname, templateRelativeDir);

const cwdFiles = readdirSync(cwd(), {withFileTypes: true});
if (cwdFiles.length > 0) {
if (cwdFiles.some((dirent) => dirent.name === 'package.json')) {
return;
} else {
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 main = () => {
const cwdFiles = readdirSync(cwd(), {withFileTypes: true});
if (cwdFiles.length > 0) {
if (cwdFiles.some((dirent) => dirent.name === 'package.json')) {
return;
} else {
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.'
)
);
}
}
}

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

let componentName = process.argv[2];
if (componentName) {
ensureComponentValidity(componentName);
const transformers = [
{
srcPath: 'src/components/sample-component',
destPath: `src/components/${componentName}`,
},
{
srcPath: `src/components/${componentName}/src/sample-component.tsx`,
destPath: `src/components/${componentName}/src/${componentName}.tsx`,
transform: (text) =>
text
.replaceAll(/sample-component/g, componentName)
.replaceAll(/SampleComponent/g, camelize(componentName)),
},
{
srcPath: `src/components/${componentName}/src/sample-component.css`,
destPath: `src/components/${componentName}/src/${componentName}.css`,
},
{
srcPath: `src/components/${componentName}/package.json`,
destPath: `src/components/${componentName}/package.json`,
transform: (text) =>
text.replaceAll(/(@coveo\/)?sample-component/g, componentName),
},
];

transform(transformers);
}
cpSync(templateDirPath, cwd(), {
recursive: true,
});

let componentName = process.argv[2];
if (componentName) {
ensureComponentValidity(componentName);
const transformers = [
{
srcPath: 'src/components/sample-component',
destPath: `src/components/${componentName}`,
},
{
srcPath: `src/components/${componentName}/src/sample-component.tsx`,
destPath: `src/components/${componentName}/src/${componentName}.tsx`,
transform: (text) =>
text
.replaceAll(/sample-component/g, componentName)
.replaceAll(/SampleComponent/g, camelize(componentName)),
},
{
srcPath: `src/components/${componentName}/src/sample-component.css`,
destPath: `src/components/${componentName}/src/${componentName}.css`,
},
{
srcPath: `src/components/${componentName}/package.json`,
destPath: `src/components/${componentName}/package.json`,
transform: (text) =>
text.replaceAll(/(@coveo\/)?sample-component/g, componentName),
},
];

transform(transformers);
}

successMessage();
};

successMessage();
main();
98 changes: 51 additions & 47 deletions packages/ui/atomic/create-atomic-result-component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,52 +140,56 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
const templateRelativeDir = 'template';
const templateDirPath = resolve(__dirname, templateRelativeDir);

/*const cwdFiles = readdirSync(cwd(), {withFileTypes: true});
if (cwdFiles.length > 0) {
if (cwdFiles.some((dirent) => dirent.name === 'package.json')) {
return;
} else {
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 main = () => {
const cwdFiles = readdirSync(cwd(), {withFileTypes: true});
if (cwdFiles.length > 0) {
if (cwdFiles.some((dirent) => dirent.name === 'package.json')) {
return;
} else {
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.'
)
);
}
}
}*/

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

let componentName = process.argv[2];
if (componentName) {
ensureComponentValidity(componentName);
const transformers = [
{
srcPath: 'src/components/sample-result-component',
destPath: `src/components/${componentName}`,
},
{
srcPath: `src/components/${componentName}/src/sample-result-component.tsx`,
destPath: `src/components/${componentName}/src/${componentName}.tsx`,
transform: (text) =>
text
.replaceAll(/sample-result-component/g, componentName)
.replaceAll(/SampleResultComponent/g, camelize(componentName)),
},
{
srcPath: `src/components/${componentName}/src/sample-result-component.css`,
destPath: `src/components/${componentName}/src/${componentName}.css`,
},
{
srcPath: `src/components/${componentName}/package.json`,
destPath: `src/components/${componentName}/package.json`,
transform: (text) =>
text.replaceAll(/(@coveo\/)?sample-result-component/g, componentName),
},
];

transform(transformers);
}

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

let componentName = process.argv[2];
if (componentName) {
ensureComponentValidity(componentName);
const transformers = [
{
srcPath: 'src/components/sample-result-component',
destPath: `src/components/${componentName}`,
},
{
srcPath: `src/components/${componentName}/src/sample-result-component.tsx`,
destPath: `src/components/${componentName}/src/${componentName}.tsx`,
transform: (text) =>
text
.replaceAll(/sample-result-component/g, componentName)
.replaceAll(/SampleResultComponent/g, camelize(componentName)),
},
{
srcPath: `src/components/${componentName}/src/sample-result-component.css`,
destPath: `src/components/${componentName}/src/${componentName}.css`,
},
{
srcPath: `src/components/${componentName}/package.json`,
destPath: `src/components/${componentName}/package.json`,
transform: (text) =>
text.replaceAll(/(@coveo\/)?sample-result-component/g, componentName),
},
];

transform(transformers);
}

successMessage();
};

main();

0 comments on commit 9a07d73

Please sign in to comment.