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

chore: add eslinter, and run lint #539

Merged
merged 2 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/lint-client.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Lint Client

on:
push:
branches:
- main
- release/*
paths:
- "clients/js/**"
- ".github/workflows/lint-client.yml"
pull_request:
branches:
- main
paths:
- "clients/js/**"
- ".github/workflows/lint-client.yml"
types: [opened, reopened]

workflow_dispatch:

jobs:
starshipjs-tests:
runs-on: ubuntu-latest

defaults:
run:
working-directory: clients/js

steps:
- name: Checkout Repository 🛎️
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'

- name: Install Dependencies
run: yarn install

- name: Build Project
run: yarn build

- name: Run lint
run: yarn run lint
2 changes: 1 addition & 1 deletion clients/js/.prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": false
"singleQuote": true
}
7 changes: 5 additions & 2 deletions clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/node": "^20.12.7",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^29.3.1",
"copyfiles": "^2.4.1",
"del-cli": "^5.1.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-unused-imports": "^3.0.0",
"jest": "^29.6.2",
Expand Down
75 changes: 75 additions & 0 deletions clients/js/packages/cli/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module.exports = {
parser: '@typescript-eslint/parser', // Specify the ESLint parser
plugins: ['@typescript-eslint', 'prettier'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'plugin:prettier/recommended'
],
parserOptions: {
ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
sourceType: 'module', // Allows for the use of imports
ecmaFeatures: {
jsx: true // Allows for the parsing of JSX
}
},
env: {
es6: true,
browser: true,
node: true,
jest: true
},
rules: {
'@typescript-eslint/no-explicit-any': 'off', // Disable the rule entirely
'no-debugger': 2,
'no-alert': 2,
'no-await-in-loop': 0,
'no-prototype-builtins': 0,
'no-return-assign': ['error', 'except-parens'],
'no-restricted-syntax': [
2,
'ForInStatement',
'LabeledStatement',
'WithStatement'
],
'no-unused-vars': [
0,
{
ignoreSiblings: true,
argsIgnorePattern: 'React|res|next|^_'
}
],
'prefer-const': [
'error',
{
destructuring: 'all'
}
],
'no-unused-expressions': [
2,
{
allowTaggedTemplates: true
}
],
'no-console': 'off',
'comma-dangle': 2,
'jsx-quotes': [2, 'prefer-double'],
'linebreak-style': ['error', 'unix'],
quotes: [
2,
'single',
{
avoidEscape: true,
allowTemplateLiterals: true
}
],
'prettier/prettier': [
'error',
{
trailingComma: 'none',
singleQuote: true,
printWidth: 80
}
]
}
};
30 changes: 15 additions & 15 deletions clients/js/packages/cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
babelConfig: false,
tsconfig: "tsconfig.json",
},
],
},
transformIgnorePatterns: [`/node_modules/*`],
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
modulePathIgnorePatterns: ["dist/*"]
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
babelConfig: false,
tsconfig: 'tsconfig.json'
}
]
},
transformIgnorePatterns: [`/node_modules/*`],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
modulePathIgnorePatterns: ['dist/*']
};
18 changes: 12 additions & 6 deletions clients/js/packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { StarshipClient, StarshipInstaller } from '@starship-ci/client'; // Adju
import { Inquirerer, type Question } from 'inquirerer';
import minimist from 'minimist';

import { displayUsage, displayVersion, loadConfig, params,usageText } from './utils';
import {
displayUsage,
displayVersion,
loadConfig,
params,
usageText
} from './utils';

const argv = minimist(process.argv.slice(2), {
alias: {
Expand All @@ -24,11 +30,11 @@ const prompter = new Inquirerer({
noTty: !argv.tty
});

const questions: Question[] = params.map(name => ({ name, type: 'text' }));
const questions: Question[] = params.map((name) => ({ name, type: 'text' }));

// Filter questions based on the command
function getQuestionsForCommand(command: string): Question[] {
const commonQuestions = questions.filter(q => q.name !== 'config');
const commonQuestions = questions.filter((q) => q.name !== 'config');
if (['start', 'deploy', 'start-ports', 'wait-for-pods'].includes(command)) {
return questions; // Include all questions, including config
} else {
Expand Down Expand Up @@ -63,8 +69,8 @@ async function main() {
switch (command) {
case 'install':
installer.checkAndInstallDependencies().catch((err: any) => {
console.error('An error occurred during start:', err);
process.exit(1);
console.error('An error occurred during start:', err);
process.exit(1);
});
break;
case 'start':
Expand Down Expand Up @@ -112,7 +118,7 @@ async function main() {
}

// Improved error handling
main().catch(err => {
main().catch((err) => {
console.error('An error occurred:', err);
prompter.close();
process.exit(1);
Expand Down
8 changes: 4 additions & 4 deletions clients/js/packages/cli/src/package.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync,readFileSync } from "fs";
import { dirname,join } from "path";
import { existsSync, readFileSync } from 'fs';
import { dirname, join } from 'path';

// need to search due to the dist/ folder and src/, etc.
// need to search due to the dist/ folder and src/, etc.
function findPackageJson(currentDir: string): any {
const filePath = join(currentDir, 'package.json');

Expand Down Expand Up @@ -30,4 +30,4 @@ export function readAndParsePackageJson() {
const str = readFileSync(pkgPath, 'utf8');
const pkg = JSON.parse(str);
return pkg;
}
}
16 changes: 13 additions & 3 deletions clients/js/packages/cli/src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ export class Inquirerer {
}

// Method to prompt for missing parameters
public async prompt<T extends object>(params: T, questions: Question[], usageText?: string): Promise<T> {
public async prompt<T extends object>(
params: T,
questions: Question[],
usageText?: string
): Promise<T> {
const obj: any = { ...params };

if (usageText && Object.values(params).some(value => value === undefined) && !this.noTty) {
if (
usageText &&
Object.values(params).some((value) => value === undefined) &&
!this.noTty
) {
console.log(usageText);
}

Expand All @@ -37,7 +45,9 @@ export class Inquirerer {
this.rl.question(`Enter ${question.name}: `, resolve);
});
} else {
throw new Error("No TTY available and a readline interface is missing.");
throw new Error(
'No TTY available and a readline interface is missing.'
);
}
} else {
// Optionally handle noTty cases, e.g., set defaults or throw errors
Expand Down
56 changes: 30 additions & 26 deletions clients/js/packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import {defaultStarshipContext, StarshipConfig, StarshipContext} from '@starship-ci/client'; // Adjust the import path as necessary
import {
defaultStarshipContext,
StarshipConfig,
StarshipContext
} from '@starship-ci/client'; // Adjust the import path as necessary
import chalk from 'chalk';
import {readFileSync} from 'fs';
import { readFileSync } from 'fs';
import * as yaml from 'js-yaml';
import { resolve} from 'path';
import { resolve } from 'path';

import {readAndParsePackageJson} from './package';
import { readAndParsePackageJson } from './package';

// Function to display the version information
export function displayVersion() {
const pkg = readAndParsePackageJson();
console.log(chalk.green(`Name: ${pkg.name}`));
console.log(chalk.blue(`Version: ${pkg.version}`));
const pkg = readAndParsePackageJson();
console.log(chalk.green(`Name: ${pkg.name}`));
console.log(chalk.blue(`Version: ${pkg.version}`));
}


const resolvePath = (filename: string) =>
filename.startsWith('/') ? filename : resolve((process.cwd(), filename));


const loadYaml = (filename: string): any => {
const path = resolvePath(filename);
const fileContents = readFileSync(path, 'utf8');
return yaml.load(fileContents);
}
};

export interface Config {
context: StarshipContext,
starship: StarshipConfig
context: StarshipContext;
starship: StarshipConfig;
}

export const params: string[] = [
Expand All @@ -36,36 +38,38 @@ export const params: string[] = [
'repo',
'repoUrl',
'chart',
'namespace',
]
'namespace'
];

export const loadConfig = (argv: any): Config => {
console.log("argv: ", argv);
let context: StarshipContext = { ...defaultStarshipContext } as StarshipContext;
console.log('argv: ', argv);
const context: StarshipContext = {
...defaultStarshipContext
} as StarshipContext;
let starship: StarshipConfig = {} as StarshipConfig;

console.log("context", context);
console.log('context', context);

// Override context with command-line arguments dynamically based on StarshipContext keys
params.forEach(key => {
params.forEach((key) => {
if (argv[key] !== undefined) {
console.log("key: ", key, " argv[key]: ", argv[key]);
// @ts-ignore
console.log('key: ', key, ' argv[key]: ', argv[key]);
// @ts-expect-error - dynamic assignment
context[key] = argv[key];
}
});

if (context.config) {
context.config = resolvePath(context.config);
starship = loadYaml(context.config) as StarshipConfig
starship = loadYaml(context.config) as StarshipConfig;
}

console.log("starship: ", starship);
console.log('starship: ', starship);

return {context, starship};
}
return { context, starship };
};

export const usageText =`
export const usageText = `
Usage: starship <command> [options]

Commands:
Expand Down Expand Up @@ -106,4 +110,4 @@ Additional Help:

export function displayUsage() {
console.log(usageText);
};
}
Loading