From 9a07d73681c519cae06358f9dd955bf7cb72ae19 Mon Sep 17 00:00:00 2001 From: David Brooke <38883189+dmbrooke@users.noreply.github.com> Date: Fri, 22 Sep 2023 13:49:54 -0400 Subject: [PATCH] Wrap into main method --- package-lock.json | 2 + .../atomic/create-atomic-component/index.js | 110 ++++++++++-------- .../create-atomic-result-component/index.js | 98 ++++++++-------- 3 files changed, 117 insertions(+), 93 deletions(-) diff --git a/package-lock.json b/package-lock.json index b6345d1999..ea56568ee8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31409,6 +31409,8 @@ "bin": { "git-lock": "git-lock.mjs", "git-publish-all": "git-publish-all.mjs", + "github-publish": "create-github-release.mjs", + "is-cli-release": "is-cli-release.mjs", "npm-publish": "npm-publish-package.mjs" }, "devDependencies": { diff --git a/packages/ui/atomic/create-atomic-component/index.js b/packages/ui/atomic/create-atomic-component/index.js index 359626d54b..cc9c6eac0b 100755 --- a/packages/ui/atomic/create-atomic-component/index.js +++ b/packages/ui/atomic/create-atomic-component/index.js @@ -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'; @@ -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(); diff --git a/packages/ui/atomic/create-atomic-result-component/index.js b/packages/ui/atomic/create-atomic-result-component/index.js index 1b5ea63ff6..3bab172fa8 100755 --- a/packages/ui/atomic/create-atomic-result-component/index.js +++ b/packages/ui/atomic/create-atomic-result-component/index.js @@ -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();