Skip to content

Commit

Permalink
Removing debuggers (#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
scalvert authored May 21, 2021
1 parent ac1d0f5 commit b195640
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 53 deletions.
11 changes: 8 additions & 3 deletions packages/cli/__tests__/__utils__/fake-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ import type { Answers } from 'inquirer';
import { generatePlugin, generateTask } from './generator-utils';

export class FakeProject extends CheckupProject {
async addPlugin(options: helpers.Dictionary<any> = {}, prompts: Answers = {}) {
let pluginDir = await generatePlugin(options, prompts, join(this.baseDir, 'node_modules'));
symlinkCorePackage(baseDir: string = this.baseDir) {
let source = join(__dirname, '../../..', 'core');
let target = join(pluginDir, 'node_modules', '@checkup', 'core');
let target = join(baseDir, 'node_modules', '@checkup', 'core');

// we create a self-referential link to the core package within the generated plugin. This
// allows us to generate plugins, and test the APIs for the latest version of core that is
// referenced via the plugins' node_modules.
this.symlinkPackage(source, target);
}

async addPlugin(options: helpers.Dictionary<any> = {}, prompts: Answers = {}) {
let pluginDir = await generatePlugin(options, prompts, join(this.baseDir, 'node_modules'));

this.symlinkCorePackage(pluginDir);

return pluginDir;
}
Expand Down
114 changes: 66 additions & 48 deletions packages/cli/__tests__/cli-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});

let result = await run(['run', '--list-tasks']);
Expand Down Expand Up @@ -238,11 +234,7 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});

project.write({
Expand Down Expand Up @@ -279,11 +271,7 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});

let fixturePath = resolve(__dirname, '__fixtures__', 'checkup-formatter-test');
Expand Down Expand Up @@ -312,11 +300,7 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});

let result = await run(['run', '.', '--task', 'fake/file-count', '--format', 'pretty']);
Expand Down Expand Up @@ -356,11 +340,7 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});

let result = await run(['run', '.', '--format', 'pretty'], {
Expand Down Expand Up @@ -389,11 +369,7 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});

let result = await run([
Expand Down Expand Up @@ -451,11 +427,7 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});

let result = await run(['run', '.', '--category', 'files', '--format', 'pretty']);
Expand Down Expand Up @@ -500,11 +472,7 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});

let result = await run([
Expand Down Expand Up @@ -564,11 +532,7 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});

let result = await run(['run', '.', '--group', 'group1', '--format', 'pretty']);
Expand Down Expand Up @@ -613,11 +577,7 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});

let result = await run([
Expand Down Expand Up @@ -670,9 +630,6 @@ describe('cli-test', () => {
);

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: { 'fake/file-count': 'off' },
});
Expand Down Expand Up @@ -857,7 +814,7 @@ describe('cli-test', () => {
expect(result.stderr).toContain('Cannot find the foo task.');
});

it('can load plugins from pluginBaseDir, if provided', async () => {
it('can load plugins from pluginBaseDir with a node_modules', async () => {
let newProject = new FakeProject('random-app', '0.0.0', () => {});
newProject.files['index.js'] = 'module.exports = {};';
newProject.files['index.hbs'] = '<div>Random App</div>';
Expand All @@ -874,11 +831,7 @@ describe('cli-test', () => {
newProject.writeSync();

project.addCheckupConfig({
$schema:
'https://raw.githubusercontent.com/checkupjs/checkup/master/packages/core/src/schemas/config-schema.json',
excludePaths: [],
plugins: ['checkup-plugin-fake'],
tasks: {},
});
project.chdir();

Expand All @@ -887,6 +840,71 @@ describe('cli-test', () => {
expect(result.stdout).toMatch('✔ foo');
});

it('can load plugins from nested (non-node_modules) pluginBaseDir', async () => {
project.addCheckupConfig({
plugins: ['checkup-plugin-nested'],
});

project.write({
lib: {
'checkup-plugin-nested': {
'index.js': `
const FooTask = require('./tasks/foo-task');
module.exports = {
register: function(args) {
let pluginName = 'checkup-plugin-nested';
args.register.task(new FooTask(pluginName, args.context));
}
}
`,
'package.json': `{
"name": "checkup-plugin-nested",
"description": "",
"version": "0.0.1",
"dependencies": {
"@checkup/core": "*"
},
"devDependencies": {},
"keywords": [
"checkup-plugin"
]
}
`,
tasks: {
'foo-task.js': `const { BaseTask } = require('@checkup/core');
module.exports = class FooTask extends BaseTask {
taskName = 'foo';
taskDisplayName = 'Foo';
category = 'best practices';
async run() {
return [
{
message: { text: 'foo' },
ruleId: this.taskName,
properties: {
taskDisplayName: this.taskDisplayName,
category: this.category,
},
},
];
}
}
`,
},
},
},
});

project.symlinkCorePackage();

let result = await run(['run', '.', '--plugin-base-dir', join(project.baseDir, 'lib')]);
expect(result.exitCode).toEqual(0);
expect(result.stdout).toMatch('✔ foo');
});

function run(args: string[], options: execa.Options = {}) {
let defaults = {
reject: false,
Expand Down
18 changes: 16 additions & 2 deletions packages/cli/src/api/checkup-task-runner.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from 'path';
import {
Action,
CheckupError,
Expand Down Expand Up @@ -192,10 +193,23 @@ export default class CheckupTaskRunner {
let pluginBaseDir = this.options.pluginBaseDir || this.options.cwd;

for (let pluginName of this.config.plugins) {
this.debug('Loading plugin from %s', pluginName);
let pluginDir;

try {
// We first attempt to resolve from a node_modules location, which is either
// the project's node_modules directory or an alternative location that contains
// a node_modules.
pluginDir = resolve.sync(pluginName, { basedir: pluginBaseDir });
} catch {
// If we're trying to load from a pluginBaseDir that doesn't contain a node_modules,
// we assume we're trying to load plugins from a simple directory, and therefore
// simply require the entry point file.
pluginDir = join(pluginBaseDir, pluginName);
}

let { register } = require(resolve.sync(pluginName, { basedir: pluginBaseDir }));
this.debug('Loading plugin from %s', pluginDir);

let { register } = require(pluginDir);
await register(registrationArgs);
}
}
Expand Down

0 comments on commit b195640

Please sign in to comment.