Skip to content

Commit 155ebfd

Browse files
committed
chore: add eslint (closes #10)
1 parent 884dd9e commit 155ebfd

20 files changed

+596
-68
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.eslintrc.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
parser: "@typescript-eslint/parser",
3+
parserOptions: {
4+
ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
5+
sourceType: "module", // Allows for the use of imports
6+
// ecmaFeatures: {
7+
// jsx: true // Allows for the parsing of JSX
8+
// }
9+
},
10+
extends: [
11+
"eslint:recommended",
12+
"plugin:@typescript-eslint/eslint-recommended",
13+
"plugin:@typescript-eslint/recommended",
14+
"prettier/@typescript-eslint", // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
15+
"plugin:prettier/recommended", // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
16+
],
17+
};

.prettierrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
semi: true,
3+
trailingComma: "all",
4+
singleQuote: true,
5+
printWidth: 120,
6+
tabWidth: 2
7+
};

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"build": "yarn clean:dist && tsc --project ./",
1313
"clean:dist": "rimraf dist",
1414
"test": "nyc ava",
15+
"lint": "eslint . --ext .ts",
1516
"prepublish": "yarn build"
1617
},
1718
"author": "Ossama Edbali <[email protected]>",
@@ -26,8 +27,14 @@
2627
"@types/rimraf": "^3.0.0",
2728
"@types/yargs": "^15.0.11",
2829
"@types/yarnpkg__lockfile": "^1.1.4",
30+
"@typescript-eslint/eslint-plugin": "^4.10.0",
31+
"@typescript-eslint/parser": "^4.10.0",
2932
"ava": "^3.13.0",
33+
"eslint": "^7.15.0",
34+
"eslint-config-prettier": "^7.0.0",
35+
"eslint-plugin-prettier": "^3.3.0",
3036
"nyc": "^15.1.0",
37+
"prettier": "^2.2.1",
3138
"rimraf": "^3.0.2",
3239
"ts-node": "^9.0.0",
3340
"typescript": "^4.0.5"

src/app.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default class App {
5555
const appConfigFileName = this.getAppConfigFileName();
5656
const defaultAppConfig: AppConfig = {
5757
cacheExpiry: '5h',
58-
lastInvalidation: null
58+
lastInvalidation: null,
5959
};
6060

6161
if (this.options.forceRun) {
@@ -79,14 +79,16 @@ export default class App {
7979
shouldInvalidate(): boolean {
8080
const now = new Date();
8181

82-
return this.appConfig.lastInvalidation === null
83-
|| compareAsc(now, parseRelativeTime(this.appConfig.cacheExpiry, this.appConfig.lastInvalidation)) === 1;
82+
return (
83+
this.appConfig.lastInvalidation === null ||
84+
compareAsc(now, parseRelativeTime(this.appConfig.cacheExpiry, this.appConfig.lastInvalidation)) === 1
85+
);
8486
}
8587

86-
updateLastInvalidation(lastInvalidation: Date) {
88+
updateLastInvalidation(lastInvalidation: Date): void {
8789
this.appConfig = {
8890
...this.appConfig,
89-
lastInvalidation
91+
lastInvalidation,
9092
};
9193

9294
fs.writeFileSync(this.getAppConfigFileName(), this.serializeAppConfig());
@@ -95,9 +97,7 @@ export default class App {
9597
private serializeAppConfig(): string {
9698
return JSON.stringify({
9799
...this.appConfig,
98-
lastInvalidation: this.appConfig.lastInvalidation !== null
99-
? formatISO(this.appConfig.lastInvalidation)
100-
: null
100+
lastInvalidation: this.appConfig.lastInvalidation !== null ? formatISO(this.appConfig.lastInvalidation) : null,
101101
});
102102
}
103103

@@ -106,9 +106,7 @@ export default class App {
106106

107107
this.appConfig = {
108108
...appConfigObject,
109-
lastInvalidation: appConfigObject.lastInvalidation
110-
? new Date(appConfigObject.lastInvalidation)
111-
: null
109+
lastInvalidation: appConfigObject.lastInvalidation ? new Date(appConfigObject.lastInvalidation) : null,
112110
};
113111
}
114112
}

src/cli.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ async function run() {
1212
description: 'Whether to show debug information in the cli',
1313
type: 'boolean',
1414
default: false,
15-
alias: 'v'
15+
alias: 'v',
1616
},
1717
force: {
1818
description: 'By-pass the cache',
1919
type: 'boolean',
2020
default: false,
21-
alias: 'f'
22-
}
21+
alias: 'f',
22+
},
2323
}).argv;
2424

2525
const loggingLevel = argv.verbose ? 'debug' : 'info';
@@ -29,7 +29,7 @@ async function run() {
2929
printMainHeading();
3030

3131
const options: Options = {
32-
forceRun: argv.force
32+
forceRun: argv.force,
3333
};
3434

3535
const discoveryOutput = await discover(options);

src/context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { ProcessingContext } from './types';
33
export default function getContext(): ProcessingContext {
44
return {
55
rootDirectory: process.cwd(),
6-
vfPackagePrefix: '@visual-framework'
6+
vfPackagePrefix: '@visual-framework',
77
};
88
}

src/git-client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import fs from 'fs';
22
import http from 'isomorphic-git/http/node';
33
import git from 'isomorphic-git';
44

5-
export async function cloneRepository(url: string, directory: string) {
5+
export async function cloneRepository(url: string, directory: string): Promise<void> {
66
return await git.clone({
77
fs,
88
http,
99
dir: directory,
1010
url,
1111
singleBranch: true,
12-
depth: 1
12+
depth: 1,
1313
});
1414
}

src/index.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ export async function discover(options: Options): Promise<DiscoveryItem[]> {
2424

2525
logger.debug('Running service discovery');
2626

27-
const pipeline = await Promise.all(fs.readdirSync(path.join(__dirname, 'pipeline'))
28-
.filter(f => f.endsWith('.js'))
29-
.map(async (fileName) => (await import(`./pipeline/${fileName}`)).default));
30-
31-
const discoveryItems = await asyncFlow(...pipeline) as DiscoveryItem[];
27+
const pipeline = await Promise.all(
28+
fs
29+
.readdirSync(path.join(__dirname, 'pipeline'))
30+
.filter((fileName) => fileName.endsWith('.js'))
31+
.map(async (fileName) => (await import(`./pipeline/${fileName}`)).default),
32+
);
33+
34+
const discoveryItems = (await asyncFlow(...pipeline)) as DiscoveryItem[];
3235

3336
return discoveryItems;
3437
}

src/logger.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import winston from 'winston';
22

33
let logger: winston.Logger;
44

5-
export function registerLogger(level: string) {
5+
export function registerLogger(level: string): void {
66
logger = winston.createLogger({
77
level,
88
format: winston.format.simple(),
99
transports: [
1010
new winston.transports.File({ filename: 'vf-core-service-discovery.log', level }),
11-
new winston.transports.Console()
12-
]
11+
new winston.transports.Console(),
12+
],
1313
});
1414
}
1515

src/parse-lock-file.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ export default function (rootDirectory: string): LockObject {
1818
if (fs.existsSync(yarnLockFileName)) {
1919
const yarnLockFile: LockObject = parseYarnLockFile(fs.readFileSync(yarnLockFileName, 'utf-8')).object;
2020

21-
return Object.entries(yarnLockFile).reduce((obj, [pkg, lockItem]) => ({
22-
...obj,
23-
[pkg.split('@').slice(0, -1).join('@')]: lockItem
24-
}), {});
21+
return Object.entries(yarnLockFile).reduce(
22+
(obj, [pkg, lockItem]) => ({
23+
...obj,
24+
[pkg.split('@').slice(0, -1).join('@')]: lockItem,
25+
}),
26+
{},
27+
);
2528
}
2629

2730
throw new FileNotFoundError(`${npmLockFileName} and ${yarnLockFileName}`);

src/pipeline/01-get-components.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ export default function getComponentsFromPackageJson(): Promise<string[]> {
2121

2222
const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonFile, 'utf-8'));
2323

24-
const dependencies: string[] = Object.keys(packageJson.dependencies || {}).filter(dep => dep.startsWith(context.vfPackagePrefix));
24+
const dependencies: string[] = Object.keys(packageJson.dependencies || {}).filter((dep) =>
25+
dep.startsWith(context.vfPackagePrefix),
26+
);
2527

26-
const devDependencies: string[] = Object.keys(packageJson.devDependencies || {}).filter(dep => dep.startsWith(context.vfPackagePrefix));
28+
const devDependencies: string[] = Object.keys(packageJson.devDependencies || {}).filter((dep) =>
29+
dep.startsWith(context.vfPackagePrefix),
30+
);
2731

2832
const components: string[] = [...dependencies, ...devDependencies];
2933

@@ -32,6 +36,6 @@ export default function getComponentsFromPackageJson(): Promise<string[]> {
3236
}
3337

3438
// TODO: fix this once we know where vf-form is
35-
resolve(components.filter(c => !c.includes('vf-form')));
39+
resolve(components.filter((c) => !c.includes('vf-form')));
3640
});
3741
}

src/pipeline/02-get-component-exact-versions.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import parseLockFile from '../parse-lock-file';
44
import { DiscoveryItem, LockObject } from '../types';
55

66
export default function getComponentsExactVersion(components: string[]): Promise<DiscoveryItem[]> {
7-
return new Promise<DiscoveryItem[]>((resolve, reject) => {
7+
return new Promise<DiscoveryItem[]>((resolve) => {
88
const logger = getLogger();
99
const context = getContext();
1010

@@ -19,11 +19,14 @@ export default function getComponentsExactVersion(components: string[]): Promise
1919
}
2020
}
2121

22-
const discoveryItems = Object.entries(componentsMap).map(([component, version]) => ({
23-
name: component,
24-
nameWithoutPrefix: component.replace(`${context.vfPackagePrefix}/`, ''),
25-
version
26-
} as DiscoveryItem));
22+
const discoveryItems = Object.entries(componentsMap).map(
23+
([component, version]) =>
24+
({
25+
name: component,
26+
nameWithoutPrefix: component.replace(`${context.vfPackagePrefix}/`, ''),
27+
version,
28+
} as DiscoveryItem),
29+
);
2730

2831
resolve(discoveryItems);
2932
});

src/pipeline/03-get-components-package-json.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ function getComponentPackageJson(discoveryItem: DiscoveryItem): PackageJson {
1313
}
1414

1515
export default function extendWithComponentPackageJson(discoveryItems: DiscoveryItem[]): Promise<DiscoveryItem[]> {
16-
return new Promise<DiscoveryItem[]>((resolve, reject) => {
16+
return new Promise<DiscoveryItem[]>((resolve) => {
1717
const logger = getLogger();
1818

1919
logger.debug('Retrieving latest packages information');
2020

2121
const processedDiscoveryItems = discoveryItems.map((discoveryItem) => ({
2222
...discoveryItem,
23-
packageJson: getComponentPackageJson(discoveryItem)
23+
packageJson: getComponentPackageJson(discoveryItem),
2424
}));
2525

2626
resolve(processedDiscoveryItems);

src/pipeline/04-get-component-config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ function getComponentConfig(discoveryItem: DiscoveryItem): ComponentConfig {
1818
}
1919

2020
export default function extendWithComponentConfig(discoveryItems: DiscoveryItem[]): Promise<DiscoveryItem[]> {
21-
return new Promise<DiscoveryItem[]>((resolve, reject) => {
21+
return new Promise<DiscoveryItem[]>((resolve) => {
2222
const logger = getLogger();
2323

2424
logger.debug('Retrieving lastest packages configuration');
2525

2626
const processedDiscoveryItems = discoveryItems.map((discoveryItem) => ({
2727
...discoveryItem,
28-
config: getComponentConfig(discoveryItem)
28+
config: getComponentConfig(discoveryItem),
2929
}));
3030

3131
resolve(processedDiscoveryItems);

src/pipeline/05-get-changelog.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function getComponentCumulativeChangelog(discoveryItem: DiscoveryItem): Changelo
3131

3232
changelogItem = {
3333
version,
34-
changes: []
34+
changes: [],
3535
};
3636
} else if (line.startsWith('*') && changelogItem) {
3737
changelogItem.changes.push(line.replace(/^\*/, '').trim());
@@ -42,10 +42,10 @@ function getComponentCumulativeChangelog(discoveryItem: DiscoveryItem): Changelo
4242
}
4343

4444
export default function extendWithCumulativeChangelog(discoveryItems: DiscoveryItem[]): Promise<DiscoveryItem[]> {
45-
return new Promise<DiscoveryItem[]>((resolve, reject) => {
45+
return new Promise<DiscoveryItem[]>((resolve) => {
4646
const processedDiscoveryItems = discoveryItems.map((discoveryItem) => ({
4747
...discoveryItem,
48-
changelog: getComponentCumulativeChangelog(discoveryItem)
48+
changelog: getComponentCumulativeChangelog(discoveryItem),
4949
}));
5050

5151
resolve(processedDiscoveryItems);

src/reporters/cli-reporter.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,38 @@ import chalk from 'chalk';
22
import boxen from 'boxen';
33
import { DiscoveryItem } from '../types';
44

5-
export function printMainHeading() {
5+
export function printMainHeading(): void {
66
console.log('\n');
77
console.log(chalk.bold(boxen('vf-core-service-discovery\n\nVersion 0.1.0-alpha.0', { padding: 1 })));
88
console.log('\n');
99
}
1010

11-
export function report(discoveryItems: DiscoveryItem[]) {
11+
export function report(discoveryItems: DiscoveryItem[]): void {
1212
for (const discoveryItem of discoveryItems) {
1313
const isOldVersion = discoveryItem.version !== discoveryItem.packageJson.version;
1414

1515
console.log(chalk.bold(`${discoveryItem.nameWithoutPrefix} (${discoveryItem.config.title})\n`));
1616
console.log(` Used version\t\t${isOldVersion ? chalk.red(discoveryItem.version) : discoveryItem.version}`);
17-
console.log(` Latest version\t${isOldVersion ? chalk.green(discoveryItem.packageJson.version) : discoveryItem.packageJson.version}`);
17+
console.log(
18+
` Latest version\t${
19+
isOldVersion ? chalk.green(discoveryItem.packageJson.version) : discoveryItem.packageJson.version
20+
}`,
21+
);
1822
console.log(` Status\t\t${discoveryItem.config.status}`);
1923

2024
if (discoveryItem.changelog.length > 0) {
2125
console.log(` Changelog`);
22-
console.log(`${discoveryItem.changelog.map(item => `\n\t\t\t${item.version}${item.changes.map(c => `\n\t\t\t ${c}`).join('')}`).join('')}`);
26+
console.log(
27+
`${discoveryItem.changelog
28+
.map((item) => `\n\t\t\t${item.version}${item.changes.map((c) => `\n\t\t\t ${c}`).join('')}`)
29+
.join('')}`,
30+
);
2331
}
2432

2533
console.log(` Dependents${!discoveryItem.dependents?.length ? `\t\t${chalk.red('None')}` : ''}`);
2634

2735
if (discoveryItem.dependents?.length > 0) {
28-
console.log(`${discoveryItem.dependents.map(dep => `\n\t\t\t${dep}`).join('')}`);
36+
console.log(`${discoveryItem.dependents.map((dep) => `\n\t\t\t${dep}`).join('')}`);
2937
}
3038

3139
console.log(`\n\n`);

src/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface Options {
1010
export interface PackageJson {
1111
version: string;
1212
dependencies?: { [key: string]: string };
13-
devDependencies?: { [key: string]: string }
13+
devDependencies?: { [key: string]: string };
1414
}
1515

1616
export interface ComponentConfig {
@@ -44,8 +44,8 @@ export interface LockItem {
4444
resolved: string;
4545
integrity: string;
4646
dev?: boolean;
47-
dependencies?: object;
48-
requires?: object;
47+
dependencies?: Record<string, string>;
48+
requires?: Record<string, string>;
4949
}
5050

5151
export interface LockObject {

0 commit comments

Comments
 (0)