Skip to content

Commit

Permalink
refactor(cli): extract utils for source plugin (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-bompart authored Aug 25, 2022
1 parent 05ac35e commit 4f0a7b4
Show file tree
Hide file tree
Showing 163 changed files with 43,843 additions and 33,569 deletions.
4 changes: 3 additions & 1 deletion .github/actions/e2e-run/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ runs:
shell: bash
working-directory: packages/cli-e2e
run: echo ${{inputs.cliConfigJson}} | base64 --decode | gpg --quiet --batch --yes --decrypt --passphrase="${{ env.E2E_TOKEN_PASSPHRASE }}" --output decrypted
- run: npm ci
- run: |
npm ci
npm run build
shell: bash
- name: End-to-end Setup
shell: bash
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ jobs:
run: npm i -g [email protected]
- name: Setup repo
run: npm ci
- name: Build
run: npm run build
- name: Tests
run: npm run test
e2e-setup-login:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*-error.log
dist
/lib
/node_modules
**/node_modules
/tmp
npm-debug.log
reports/*.xml
Expand Down
76,506 changes: 43,301 additions & 33,205 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@commitlint/config-conventional": "16.2.4",
"@commitlint/lint": "16.2.4",
"@coveo/semantic-monorepo-tools": "1.1.10",
"@coveord/platform-client": "33.8.1",
"@coveord/platform-client": "34.7.0",
"@nrwl/cli": "latest",
"@nrwl/workspace": "latest",
"@types/node": "17.0.45",
Expand Down
2 changes: 1 addition & 1 deletion packages/atomic/create-atomic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"access": "public"
},
"dependencies": {
"@coveord/platform-client": "33.8.1",
"@coveord/platform-client": "34.7.0",
"abortcontroller-polyfill": "1.7.3",
"detect-indent": "7.0.0",
"https-proxy-agent": "5.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@babel/preset-env": "7.18.10",
"@babel/preset-typescript": "7.18.6",
"@coveo/cli": "^1.34.2",
"@coveord/platform-client": "33.8.1",
"@coveord/platform-client": "34.7.0",
"abortcontroller-polyfill": "1.7.3",
"async-retry": "1.3.3",
"axios": "0.27.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-e2e/verdaccio/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ uplinks:
packages:
# All templates packages and the search-token-server are published and fetched from Verdaccio
# No uplink is allowed, so if it ain't published on Verdaccio, it'll fail.
'@coveo/@(angular|vue-cli-plugin-typescript|cra-template|search-token-server|create-atomic|search-token-lambda)':
'@coveo/@(angular|vue-cli-plugin-typescript|cra-template|search-token-server|create-atomic|search-token-lambda|cli-commons-dev|cli-commons)':
access: $all
publish: $all
unpublish: $all
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/commons-dev/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
coverage
tmp
lib
3 changes: 3 additions & 0 deletions packages/cli/commons-dev/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: '../../../base.eslintrc',
};
1 change: 1 addition & 0 deletions packages/cli/commons-dev/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
9 changes: 9 additions & 0 deletions packages/cli/commons-dev/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dist
coverage
tmp
lib

# Autogenerated
CHANGELOG.md
README.md
package-lock.json
5 changes: 5 additions & 0 deletions packages/cli/commons-dev/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const base = require('../../../.prettierrc');

module.exports = {
...base,
};
23 changes: 23 additions & 0 deletions packages/cli/commons-dev/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@coveo/cli-commons-dev",
"version": "1.34.3",
"files": [
"lib/**",
".eslintrc.js",
".prettierrc.js",
"tsconfig.json"
],
"dependencies": {
"fancy-test": "2.0.0",
"tslib": "2.4.0"
},
"devDependencies": {
"@types/jest": "28.1.7",
"typescript": "4.7.4"
},
"scripts": {
"build": "tsc -p tsconfig.build.json",
"prepublish": "npm run build",
"npm:publish:template": "npm publish --access public --registry http://localhost:4873"
}
}
File renamed without changes.
8 changes: 8 additions & 0 deletions packages/cli/commons-dev/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"include": ["src/**/*"]
}
17 changes: 17 additions & 0 deletions packages/cli/commons-dev/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"experimentalDecorators": true,
"declaration": true,
"importHelpers": true,
"moduleResolution": "node",
"module": "commonjs",
"lib": ["ESNext"],
"resolveJsonModule": true,
"strict": true,
"target": "es2019",
"noEmitOnError": false,
"types": ["node", "jest"],
"esModuleInterop": true
}
}
4 changes: 4 additions & 0 deletions packages/cli/commons/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
coverage
tmp
lib
5 changes: 5 additions & 0 deletions packages/cli/commons/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const base = require('@coveo/cli-commons-dev/.eslintrc');

module.exports = {
...base,
};
1 change: 1 addition & 0 deletions packages/cli/commons/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
9 changes: 9 additions & 0 deletions packages/cli/commons/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dist
coverage
tmp
lib

# Autogenerated
CHANGELOG.md
README.md
package-lock.json
5 changes: 5 additions & 0 deletions packages/cli/commons/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const base = require('@coveo/cli-commons-dev/.prettierrc');

module.exports = {
...base,
};
27 changes: 27 additions & 0 deletions packages/cli/commons/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@coveo/cli-commons",
"version": "1.34.3",
"files": [
"lib/**"
],
"devDependencies": {
"@coveo/cli-commons-dev": "1.34.3",
"@types/jest": "^28.1.7",
"typescript": "4.7.4"
},
"dependencies": {
"@coveord/platform-client": "34.7.0",
"@oclif/core": "1.14.2",
"abortcontroller-polyfill": "1.7.3",
"fs-extra": "10.1.0",
"https-proxy-agent": "5.0.1",
"isomorphic-fetch": "3.0.0",
"semver": "7.3.7",
"ts-dedent": "2.2.0"
},
"scripts": {
"build": "tsc",
"prepublish": "npm run build",
"npm:publish:template": "npm publish --access public --registry http://localhost:4873"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
} from 'fs-extra';
import {join} from 'path';
import dedent from 'ts-dedent';
import {defaultConfiguration} from '../../__stub__/configuration';
import {defaultConfiguration} from '@coveo/cli-commons-dev/lib/testUtils/stub/configuration';
import {PlatformEnvironment} from '../platform/environment';
import {Config} from './config';
import {IncompatibleConfigurationError} from './configErrors';
import {fancyIt} from '../../__test__/it';
import {fancyIt} from '@coveo/cli-commons-dev/lib/testUtils/it';
import {CurrentSchemaVersion} from './configSchemaVersion';

const mockedSemverSatisifies = jest.mocked(satisfies);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {coerce, gt, lt} from 'semver';
import dedent from 'ts-dedent';
import {CLIBaseError} from '../errors/CLIBaseError';
import {CLIBaseError} from '../errors/cliBaseError';
import {CurrentSchemaVersion} from './configSchemaVersion';

export class IncompatibleConfigurationError extends CLIBaseError {
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/commons/src/config/stub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Region} from '@coveord/platform-client';
import type {Config, Configuration} from './config';
import {PlatformEnvironment} from '../platform/environment';

export const defaultConfiguration = {
environment: PlatformEnvironment.Dev,
organization: 'my-org',
region: Region.US,
accessToken: 'some-api-key-or-token',
anonymous: false,
} as Configuration;

export const configurationMock: (
configuration?: Configuration
) => () => Config =
(configuration = defaultConfiguration) =>
() =>
({
get: () => configuration,
} as Config);
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import {PrintableError, SeverityLevel} from './printableError';

export enum PreconditionErrorCategory {
MissingBin = 'Missing Bin',
InvalidBinInstallation = 'Invalid Bin installation',
InvalidBinVersionRange = 'Invalid Bin Range',
MissingPlatformPrivilege = 'Missing Platform Privilege',
Authentication = 'Authentication',
}

export interface PreconditionErrorOptions {
category?: PreconditionErrorCategory;
category?: string;
level?: SeverityLevel;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CliUx} from '@oclif/core';
import {CLIBaseError} from './CLIBaseError';
import {CLIBaseError} from './cliBaseError';

export enum SeverityLevel {
Info = 'info',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
castEnvironmentToPlatformClient,
PlatformEnvironment,
} from './environment';
import {fancyIt} from '../../__test__/it';
import {fancyIt} from '@coveo/cli-commons-dev/lib/testUtils/it';
import globalConfig from '../config/globalConfig';
const mockConfig = jest.mocked(Config);
const mockPlatformClient = jest.mocked(PlatformClient);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Environment, Region} from '@coveord/platform-client';
import {fancyIt} from '../../__test__/it';
import {fancyIt} from '@coveo/cli-commons-dev/lib/testUtils/it';
import {
castEnvironmentToPlatformClient,
PlatformEnvironment,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Region} from '@coveord/platform-client';
import {fancyIt} from '../../__test__/it';
import {fancyIt} from '@coveo/cli-commons-dev/lib/testUtils/it';

jest.mock('./environment');
import {PlatformEnvironment, platformUrl} from './environment';
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jest.mock('../../config/config');
jest.mock('../../config/globalConfig');

import {Interfaces} from '@oclif/core';
import {fancyIt} from '../../../__test__/it';
import {fancyIt} from '@coveo/cli-commons-dev/lib/testUtils/it';
import {Config} from '../../config/config';
import globalConfig from '../../config/globalConfig';
import {AuthenticatedClient} from '../../platform/authenticatedClient';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ import PlatformClient, {
PrivilegeModel,
} from '@coveord/platform-client';
import {Command} from '@oclif/core';
import {Config} from '../../config/config';
import globalConfig from '../../config/globalConfig';
import {
PreconditionError,
PreconditionErrorCategory,
} from '../../errors/preconditionError';
import {AuthenticatedClient} from '../../platform/authenticatedClient';
import {Config} from '../config/config';
import globalConfig from '../config/globalConfig';
import {PreconditionError} from '../errors/preconditionError';
import {AuthenticatedClient} from '../platform/authenticatedClient';
import {PlatformPrivilege} from './platformPrivilege';

const PRECONDITION_ERROR_CATEGORY = 'Missing Platform Privilege';

export function HasNecessaryCoveoPrivileges(
...privileges: PlatformPrivilege[]
) {
return async function (
this: Command,
command: Command
): Promise<void | never> {
const {flags} = hasGetFlagMethod(this)
const {flags}: {flags: {organization?: string}} = hasGetFlagMethod(this)
? {flags: await this.getFlags()}
: await this.parse(command.ctor);
: await this.parse<{organization?: string}, unknown, {}>(command.ctor);
const authenticatedClient = new AuthenticatedClient();
const client = await authenticatedClient.getClient();
const {organization: target, anonymous} = await getConfiguration();
Expand All @@ -34,7 +33,7 @@ export function HasNecessaryCoveoPrivileges(
Boolean(anonymous)
);
throw new PreconditionError(message, {
category: PreconditionErrorCategory.MissingPlatformPrivilege,
category: PRECONDITION_ERROR_CATEGORY,
});
}
})
Expand Down Expand Up @@ -63,8 +62,8 @@ async function getConfiguration() {
return config.get();
}

function hasGetFlagMethod(
candidate: any
): candidate is Command & {getFlags: () => Promise<unknown>} {
function hasGetFlagMethod(candidate: any): candidate is Command & {
getFlags: () => Promise<{organization?: string | undefined}>;
} {
return Boolean(candidate?.getFlags);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
jest.mock('../../platform/authenticatedClient');

import {fancyIt} from '../../../__test__/it';
import {fancyIt} from '@coveo/cli-commons-dev/lib/testUtils/it';
import {PreconditionError} from '../../errors/preconditionError';
import {
AuthenticationStatus,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import {
PreconditionError,
PreconditionErrorCategory,
} from '../../errors/preconditionError';
import {PreconditionError} from '../errors/preconditionError';
import {
AuthenticationStatus,
getAuthenticationStatus,
} from '../../platform/authenticatedClient';
} from '../platform/authenticatedClient';

const PRECONDITION_ERROR_CATEGORY = 'Authentication';

export function IsAuthenticated() {
return async function () {
const status = await getAuthenticationStatus();
if (status === AuthenticationStatus.LOGGED_OUT) {
throw new PreconditionError(
'Not currently logged in. Run coveo auth:login first.',
{category: PreconditionErrorCategory.Authentication}
{category: PRECONDITION_ERROR_CATEGORY}
);
}

if (status === AuthenticationStatus.EXPIRED) {
throw new PreconditionError(
'Authentication token is expired. Run coveo auth:login first.',
{category: PreconditionErrorCategory.Authentication}
{category: PRECONDITION_ERROR_CATEGORY}
);
}
};
Expand Down
Loading

0 comments on commit 4f0a7b4

Please sign in to comment.