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

[kbn/plugin-helpers] typescript-ify #66513

Merged
merged 12 commits into from
May 15, 2020
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
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ module.exports = {
'.eslintrc.js',
'**/webpackShims/**/*.js',
'packages/kbn-plugin-generator/**/*.js',
'packages/kbn-plugin-helpers/**/*.js',
'packages/kbn-eslint-import-resolver-kibana/**/*.js',
'packages/kbn-eslint-plugin-eslint/**/*',
'x-pack/gulpfile.js',
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-plugin-helpers/bin/plugin-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ if (nodeMajorVersion < 6) {
process.exit(1);
}

require('../cli');
require('../target/cli');
26 changes: 0 additions & 26 deletions packages/kbn-plugin-helpers/lib/index.d.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/kbn-plugin-helpers/lib/index.js

This file was deleted.

58 changes: 0 additions & 58 deletions packages/kbn-plugin-helpers/lib/run.test.js

This file was deleted.

24 changes: 0 additions & 24 deletions packages/kbn-plugin-helpers/lib/win_cmd.js

This file was deleted.

27 changes: 20 additions & 7 deletions packages/kbn-plugin-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
{
"name": "@kbn/plugin-helpers",
"private": true,
"version": "9.0.2",
"private": true,
"description": "Just some helpers for kibana plugin devs.",
"main": "lib/index.js",
"license": "Apache-2.0",
"main": "target/lib/index.js",
"scripts": {
"kbn:bootstrap": "tsc"
},
"bin": {
"plugin-helpers": "bin/plugin-helpers.js"
},
"author": "Spencer Alger <[email protected]>",
"license": "Apache-2.0",
"peerDependencies": {
"@kbn/babel-preset": "1.0.0"
},
"dependencies": {
"@babel/core": "^7.9.0",
"argv-split": "^2.0.1",
Expand All @@ -27,6 +26,20 @@
"node-sass": "^4.13.1",
"through2": "^2.0.3",
"through2-map": "^3.0.0",
"vinyl": "^2.2.0",
"vinyl-fs": "^3.0.3"
},
"devDependencies": {
"@types/gulp-rename": "^0.0.33",
"@types/gulp-zip": "^4.0.1",
"@types/inquirer": "^6.5.0",
"@types/node-sass": "^4.11.0",
"@types/through2": "^2.0.35",
"@types/through2-map": "^3.0.0",
"@types/vinyl": "^2.0.4",
"typescript": "3.7.2"
},
"peerDependencies": {
"@kbn/babel-preset": "1.0.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
* under the License.
*/

const program = require('commander');
import Fs from 'fs';
import Path from 'path';

const pkg = require('./package.json');
const createCommanderAction = require('./lib/commander_action');
const docs = require('./lib/docs');
const enableCollectingUnknownOptions = require('./lib/enable_collecting_unknown_options');
import program from 'commander';

import { createCommanderAction } from './lib/commander_action';
import { docs } from './lib/docs';
import { enableCollectingUnknownOptions } from './lib/enable_collecting_unknown_options';

const pkg = JSON.parse(Fs.readFileSync(Path.resolve(__dirname, '../package.json'), 'utf8'));
program.version(pkg.version);

enableCollectingUnknownOptions(
Expand Down Expand Up @@ -55,7 +58,7 @@ program
buildVersion: command.buildVersion,
kibanaVersion: command.kibanaVersion,
skipArchive: Boolean(command.skipArchive),
files: files,
files,
}))
);

Expand Down Expand Up @@ -84,7 +87,7 @@ program
.on('--help', docs('test/mocha'))
.action(
createCommanderAction('testMocha', (command, files) => ({
files: files,
files,
}))
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
* under the License.
*/

/*eslint-env jest*/
import { createCommanderAction } from './commander_action';
import { run } from './run';

const createCommanderAction = require('./commander_action');
const run = require('./run');

jest.mock('./run', () => jest.fn());
jest.mock('./run');

const STACK_TRACE_RE = /\n(?:\s+at .+(?:\n|$))+/g;
expect.addSnapshotSerializer({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
* under the License.
*/

const run = require('./run');
import { run } from './run';
import { Tasks } from './tasks';

module.exports = function createCommanderAction(taskName, getOptions = () => {}) {
return async (...args) => {
type GetOptions = (command: any, ...args: string[]) => any;

export function createCommanderAction(taskName: keyof Tasks, getOptions: GetOptions = () => {}) {
return async (...args: string[]) => {
try {
// command is the last arg passed by commander, but we move it to the front of the list
const command = args.pop();
Expand All @@ -30,4 +33,4 @@ module.exports = function createCommanderAction(taskName, getOptions = () => {})
process.exit(1);
}
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,34 @@
* under the License.
*/

const resolve = require('path').resolve;
const readFileSync = require('fs').readFileSync;
import { resolve } from 'path';
import { readFileSync } from 'fs';

const configFiles = ['.kibana-plugin-helpers.json', '.kibana-plugin-helpers.dev.json'];
const configCache = {};
const configFileNames = ['.kibana-plugin-helpers.json', '.kibana-plugin-helpers.dev.json'];

module.exports = function(root) {
if (!root) root = process.cwd();
interface Config {
[key: string]: unknown;
}

if (configCache[root]) {
return configCache[root];
const configCache = new Map<string, Config>();

export function configFile(root: string = process.cwd()) {
if (configCache.has(root)) {
return configCache.get(root)!;
}

// config files to read from, in the order they are merged together
let config = (configCache[root] = {});

configFiles.forEach(function(configFile) {
let config: Config = {};
for (const name of configFileNames) {
try {
const content = JSON.parse(readFileSync(resolve(root, configFile)));
config = Object.assign(config, content);
config = JSON.parse(readFileSync(resolve(root, name), 'utf8'));
} catch (e) {
// rethrow error unless it's complaining about file not existing
if (e.code !== 'ENOENT') {
throw e;
}
}
});
}

const deprecationMsg =
'has been removed from `@kbn/plugin-helpers`. ' +
Expand All @@ -59,8 +60,10 @@ module.exports = function(root) {
}

// use resolve to ensure correct resolution of paths
const { includePlugins } = config;
if (includePlugins) config.includePlugins = includePlugins.map(path => resolve(root, path));
if (Array.isArray(config.includePlugins)) {
config.includePlugins = config.includePlugins.map((path: string) => resolve(root, path));
}

configCache.set(root, config);
return config;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@
* under the License.
*/

const resolve = require('path').resolve;
const readFileSync = require('fs').readFileSync;
import { resolve } from 'path';
import { readFileSync } from 'fs';

function indent(txt, n) {
function indent(txt: string, n: number) {
const space = new Array(n + 1).join(' ');
return space + txt.split('\n').join('\n' + space);
}

module.exports = function docs(name) {
const md = readFileSync(resolve(__dirname, '../tasks', name, 'README.md'), 'utf8');
export function docs(name: string) {
const md = readFileSync(resolve(__dirname, '../../src/tasks', name, 'README.md'), 'utf8');

return function() {
console.log('\n Docs:');
console.log('');
console.log(indent(md, 4));
console.log('');
/* eslint-disable-next-line no-console */
console.log(`\n Docs:\n\n${indent(md, 4)}\n\n`);
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
* under the License.
*/

module.exports = function enableCollectingUnknownOptions(command) {
import { Command } from 'commander';

export function enableCollectingUnknownOptions(command: Command) {
const origParse = command.parseOptions;
command.allowUnknownOption();
command.parseOptions = function(argv) {
command.parseOptions = function(argv: string[]) {
const opts = origParse.call(this, argv);
this.unknownOptions = opts.unknown;
return opts;
};
};
}
Loading