diff --git a/.changeset/little-apes-appear.md b/.changeset/little-apes-appear.md new file mode 100644 index 0000000..7d2540a --- /dev/null +++ b/.changeset/little-apes-appear.md @@ -0,0 +1,5 @@ +--- +'gitemo-cli': minor +--- + +authentication for cli diff --git a/.github/workflows/build_and_publish.yaml b/.github/workflows/build_and_publish.yaml index b15eab1..813bc1a 100644 --- a/.github/workflows/build_and_publish.yaml +++ b/.github/workflows/build_and_publish.yaml @@ -23,6 +23,7 @@ jobs: run: | export CLIENT_URL=${{ secrets.CLIENT_URL }} export API_KEY=${{ secrets.API_KEY }} + export KEY_FILENAME=${{ secrets.KEY_FILENAME }} npm install --frozen-lockfile npm run build --if-present - name: Create Release Pull Request or Publish @@ -31,6 +32,7 @@ jobs: with: publish: npm run release title: "📦 Version packages" + commit: "📦 package: Version packages" env: GITHUB_TOKEN: ${{ secrets.REPO_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/src/utils/findGitemoCommands.js b/src/utils/findGitemoCommands.js index 32c0d0a..8ef2eec 100644 --- a/src/utils/findGitemoCommands.js +++ b/src/utils/findGitemoCommands.js @@ -1,4 +1,6 @@ import FLAGS from '@constants/flag.js'; +import requireLogin from './requireLogin'; +import chalk from 'chalk'; const isSupportedCommand = (command, options) => { return Object.keys(options).includes(command); @@ -38,6 +40,19 @@ const getOptionsForCommand = (command, flags, input, type) => { const findGitemoCommand = (cli, options) => { const { command, type } = determineCommand(cli.flags, cli.input, options); + var key = requireLogin(command); + + if (command === FLAGS.LOGIN) { + if (key && key !== 1) { + console.log(chalk.bold.green(`\n\nYou are already logged in\n\n`)); + return; + } + } + + if (!key || key === undefined) { + return; + } + if (!command || !isSupportedCommand(command, options)) { return cli.showHelp(); } diff --git a/src/utils/requireLogin.js b/src/utils/requireLogin.js new file mode 100644 index 0000000..ad0300f --- /dev/null +++ b/src/utils/requireLogin.js @@ -0,0 +1,25 @@ +import os from 'os'; +import path from 'path'; +import chalk from 'chalk'; +import { readFileSync } from 'fs'; +import dotenv from 'dotenv'; +dotenv.config(); + +function requireLogin(command) { + try { + const FILENAME = process.env.KEY_FILENAME; + const homeDir = os.homedir(); + const filePath = path.join(homeDir, FILENAME); + const fileData = readFileSync(filePath, 'utf8'); + const { key } = JSON.parse(fileData); + return key; + } catch (error) { + if (command === 'login') { + return 1; + } + console.log(chalk.red(`No key found! Please login first\n\n`)); + console.log(`use ${chalk.cyan('gitemo login')} to login\n`); + } +} + +export default requireLogin;