@grept/node-plugin
Version:
Plugin for @grt/core. Help to manage nodejs versions of your repos
85 lines (63 loc) • 2.4 kB
text/typescript
import { injectable, inject } from 'inversify';
import { BasePlugin, DEFAULT as COMMON_DEFAULT, IOptionsValidatior } from '@grept/common';
import { OctokitService } from '@grept/octokit';
import { IInputOptions, IOutputItem } from './interfaces';
import { DEFAULT } from './constants';
import { NodeVersionOptionsValidator } from './node.version.options.validator';
()
export class NodeVersionPlugin extends BasePlugin<IInputOptions, IOutputItem> {
('octokit')
private octokitService: OctokitService;
(NodeVersionOptionsValidator)
private optionsValidator: IOptionsValidatior<IInputOptions>;
protected applyDefaults(options: IInputOptions = {}): IInputOptions {
return { ...COMMON_DEFAULT, ...DEFAULT, ...options };
}
protected validate(options: IInputOptions) {
this.optionsValidator.validate(options);
}
protected async perform(options: IInputOptions): Promise<IOutputItem[]> {
console.log('Options: ', options);
const { org, user, nvm, engines, token } = options;
const repos = await this.octokitService.getRepos({ org, user, token });
if (!repos) {
return null;
}
const result = await Promise.all(
repos.map(async repo => {
try {
const [nvmVersion, enginesVersion] = await Promise.all([
nvm ? this.getNvmrc(org || user, repo, token).catch(() => null) : null,
engines ? this.getEngines(org || user, repo, token).catch(() => null) : null
]);
const data: IOutputItem = { repo };
if (nvm) {
data.nvmVersion = nvmVersion;
}
if (engines) {
data.enginesVersion = enginesVersion;
}
return data;
}
catch (e) {
return { repo, error: e.code };
}
})
);
return result;
}
private async getNvmrc(owner, repo, token): Promise<string> {
const content = await this.octokitService.getFileContent(owner, repo, '.nvmrc', token);
return content.trim();
}
private async getEngines(owner, repo, token): Promise<string> {
const packageJson = await this.octokitService.getPackageJson(owner, repo, token);
const engines = packageJson.engines;
if (!engines || !engines.node) {
return null;
}
return engines.npm
? `${engines.node} (npm: ${engines.npm})`
: `${engines.node}`;
}
}