Skip to content

Commit

Permalink
Merge branch 'master' into greenkeeper/jasmine-3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KalleV committed May 24, 2019
2 parents 90dd649 + 567d1ef commit d0d8ea6
Show file tree
Hide file tree
Showing 15 changed files with 329 additions and 240 deletions.
70 changes: 70 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,73 @@
## [2.4.1](https://github.com/LabShare/lsc/compare/v2.4.0...v2.4.1) (2019-05-24)


### Bug Fixes

* **pkg:** resolve Snyk vulnerabilities ([602a756](https://github.com/LabShare/lsc/commit/602a756))

# [2.4.0](https://github.com/LabShare/lsc/compare/v2.3.1...v2.4.0) (2019-02-13)


### Bug Fixes

* format global method removed ([f03de87](https://github.com/LabShare/lsc/commit/f03de87))


### Features

* fluent logger and winston upgrade ([295c2e1](https://github.com/LabShare/lsc/commit/295c2e1))

## [2.3.1](https://github.com/LabShare/lsc/compare/v2.3.0...v2.3.1) (2018-11-01)


### Bug Fixes

* **logs:** update log file name to "app.log" instead of "lsc.log" ([7f89f7a](https://github.com/LabShare/lsc/commit/7f89f7a))

# [2.3.0](https://github.com/LabShare/lsc/compare/v2.2.7...v2.3.0) (2018-10-25)


### Features

* **api:** export logger and config loader classes ([2f0e680](https://github.com/LabShare/lsc/commit/2f0e680))

## [2.2.7](https://github.com/LabShare/lsc/compare/v2.2.6...v2.2.7) (2018-10-24)


### Bug Fixes

* **docs:** update install command after renaming package to [@labshare](https://github.com/labshare)/lsc ([03c7d9d](https://github.com/LabShare/lsc/commit/03c7d9d))
* **pkg:** rename package to [@labshare](https://github.com/labshare)/lsc to fix publishing errors ([abe8e76](https://github.com/LabShare/lsc/commit/abe8e76))

## [2.2.6](https://github.com/LabShare/lsc/compare/v2.2.5...v2.2.6) (2018-10-01)


### Bug Fixes

* **pkg:** remove deprecated prepublish hook ([1ae0acf](https://github.com/LabShare/lsc/commit/1ae0acf))

## [2.2.5](https://github.com/LabShare/lsc/compare/v2.2.4...v2.2.5) (2018-09-20)


### Bug Fixes

* **loader:** resolves "callback already called" exception SHELL-1614 ([0c8a48e](https://github.com/LabShare/lsc/commit/0c8a48e))

## [2.2.4](https://github.com/LabShare/lsc/compare/v2.2.3...v2.2.4) (2018-09-20)


### Bug Fixes

* **package:** update [@types](https://github.com/types)/glob to version 7.1.0 ([9dbb3a1](https://github.com/LabShare/lsc/commit/9dbb3a1))
* **package:** update [@types](https://github.com/types)/yargs to version 12.0.0 ([de5322b](https://github.com/LabShare/lsc/commit/de5322b))

## [2.2.3](https://github.com/LabShare/lsc/compare/v2.2.2...v2.2.3) (2018-09-12)


### Bug Fixes

* package.json to reduce vulnerabilities ([d9ce207](https://github.com/LabShare/lsc/commit/d9ce207))

## [2.2.2](https://github.com/LabShare/lsc/compare/v2.2.1...v2.2.2) (2018-08-02)


Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -6,7 +6,7 @@
# LSC

## Install
`npm i -g lsc`
`npm i -g @labshare/lsc`

## Usage
Run `lsc help` to display a list of available commands.
Expand Down
2 changes: 0 additions & 2 deletions cli/package.ts
@@ -1,7 +1,6 @@
'use strict';

import * as gulp from 'gulp'
import * as conflict from 'gulp-conflict'
import * as template from 'gulp-template'
import * as rename from 'gulp-rename'
import {PackageUpdate} from '../lib/package/update'
Expand Down Expand Up @@ -74,7 +73,6 @@ module.exports = {
file.basename = '.' + file.basename.slice(1);
}
}))
.pipe(conflict('./'))
.pipe(gulp.dest('./'))
.on('end', () => {
this.log.info(`Successfully created LabShare ${answers.projectType} package...`);
Expand Down
10 changes: 7 additions & 3 deletions index.ts
@@ -1,3 +1,7 @@
module.exports = {
CLI: require('./lib/cli')
};
import {Logger} from "./lib/log";
import {ConfigLoader} from "./lib/config";

export {
Logger,
ConfigLoader
}
37 changes: 20 additions & 17 deletions lib/cli/init.ts
Expand Up @@ -15,28 +15,31 @@ import labShare from '../labshare';
const lscRoot = path.join(__dirname, '..');

export function init(): void {
let argv = yargs.options({
configFile: {
alias: ['config', 'conf'],
describe: 'A path to a local configuration file',
type: 'string',
'default': null
}
}).argv,
config = configLoaderSync({
main: process.cwd(),
directories: [lscRoot],
configFilePath: argv.configFile
});
const argv = yargs.options({
configFile: {
alias: ['config', 'conf'],
describe: 'A path to a local configuration file',
type: 'string',
'default': null
}
}).argv;
const config = configLoaderSync({
cwd: process.cwd(),
directories: [lscRoot],
configFilePath: argv.configFile
});

global.LabShare = global.LabShare || labShare;
global.LabShare.Config = config;

let logDirectory: string = _.get(config, 'lsc.Log.Path'),
fluentD = _.get(config, 'lsc.Log.FluentD', {});
let logDirectory: string = _.get(config, 'lsc.Log.Path');
let fluentD = _.get(config, 'lsc.Log.FluentD', {});
let format = _.get(config, 'lsc.Log.Format', {});

global.LabShare.Logger = new Logger({
// default logger
global.LabShare.Logger = Logger({
logDirectory,
fluentD
fluentD,
format
});
}
5 changes: 3 additions & 2 deletions lib/cli/loader-plugin.ts
Expand Up @@ -23,7 +23,7 @@
*
* {Object} options
* {String} [options.pattern] - The pattern used to match files that contain cli commands (e.g. 'src/cli/*.js')
* {String} [options.main] - A relative or absolute path to a directory containing a LabShare package with 0 or more dependencies to other LabShare packages. Default: ''
* {String} [options.cwd] - A relative or absolute path to a directory containing a LabShare package with 0 or more dependencies to other LabShare packages. Default: ''
* {Array} [options.directories] - A list of paths to LabShare packages that should be searched for CLI commands. Each directory
* must contain a package.json to be considered valid. Default: []
* @param {Number} [options.timeout] - How long to wait for the optional callback to be called in CLI package init functions
Expand Down Expand Up @@ -51,7 +51,8 @@ exports.init = async function init(done) {

done();
} catch (error) {
done(error);
// TODO: workaround for async/await incompatibility with callbacks in the Flatiron CLI library
console.error(error);
}
};

Expand Down
20 changes: 10 additions & 10 deletions lib/cli/loader.ts
Expand Up @@ -3,7 +3,7 @@
*
* How to use:
* - Construct a CliLoader with a valid Flatiron app. An options object can be passed as a
* second argument to the constructor. To locate commands, specify options.main
* second argument to the constructor. To locate commands, specify options.cwd
* and/or a list of package directories in options.directories. View the constructor documentation below
* for more details on the available options.
* - Call .load() to locate and load all the cli commands and run the list of "initFunctions" passed to
Expand Down Expand Up @@ -38,7 +38,7 @@ function createCommandHeader(name) {

interface ICliLoaderOptions {
configFilePath?: string
main?: string
cwd?: string
packageDirectory?: string
directories?: string | string[]
timeout?: number
Expand All @@ -60,7 +60,7 @@ export class CliLoader {
* @param {Object} app - An initialized Flatiron app with command storage and logging capabilities
* @param {Object} options - Overrides default settings
* @param {String} [options.pattern] - The pattern used to match files that contain cli commands. Default: 'cli/*.js'
* @param {String} [options.main] - A relative or absolute path to a directory containing a LabShare packages. Default: ''
* @param {String} [options.cwd] - A relative or absolute path to a directory containing a LabShare packages. Default: ''
* @param {Array} [options.directories] - A list of paths to LabShare packages that should be searched for CLI commands. Each directory
* must contain a package.json to be considered valid. Default: []
* @param {Number} [options.timeout] - How long to wait for the optional callback to be called in CLI package init functions
Expand All @@ -75,9 +75,9 @@ export class CliLoader {
this._commands = {}; // format: {packageName1: {cmd1Name: 'path/to/cmd1Name', cmd2Name: 'path/to/cmd2Name', ...}, packageName2: ...}
this.initFunctions = options.initFunctions || [];

if (_.get(options, 'main')) {
assert.ok(_.isString(options.main), '`options.main` must be a string');
options.main = path.resolve(options.main);
if (_.get(options, 'cwd')) {
assert.ok(_.isString(options.cwd), '`options.cwd` must be a string');
options.cwd = path.resolve(options.cwd);
}

if (_.get(options, 'directories')) {
Expand All @@ -88,7 +88,7 @@ export class CliLoader {
}

this.options = _.defaults(options || {}, {
main: '',
cwd: '',
pattern: 'cli/*.js',
directories: [],
timeout: INIT_TIMEOUT
Expand All @@ -97,7 +97,7 @@ export class CliLoader {

/**
* @description Synchronously loads and caches all the LabShare package CLI command modules
* found in the dependencies of options.main and/or options.directories.
* found in the dependencies of options.cwd and/or options.directories.
*
* An error is logged if two different packages try to load a command with the same name, a command could not be loaded, or
* if a command module does not contain help text.
Expand All @@ -109,8 +109,8 @@ export class CliLoader {
try {
_.each(this.options.directories, this.storeCommands.bind(this));

if (this.options.main) {
applyToNodeModulesSync(this.options.main, this.storeCommands.bind(this));
if (this.options.cwd) {
applyToNodeModulesSync(this.options.cwd, this.storeCommands.bind(this));
}
} catch (error) {
throw new Error(`Failed to load CLI commands: ${error.message}: ${error.stack}`);
Expand Down
15 changes: 7 additions & 8 deletions lib/cli/start.ts
Expand Up @@ -8,13 +8,12 @@ import labShare from '../labshare'
import loaderPlugin = require('./loader-plugin')

const {app} = flatiron;
const cwd = process.cwd();
const lscRoot = path.join(__dirname, '..', '..');

export interface IStartOptions {
directories?: string[]
pattern?: string
main?: string
cwd?: string
initFunctions?: ((error?: Error) => any)[]
}

Expand All @@ -25,13 +24,13 @@ interface IPackageJson {

/**
* @description Bootstraps the CLI
* @param {string} main - Root project location
* @param {string} cwd - Root project location
* @param {Array<string>} [directories] - Additional project directories to search for CLI commands
* @param {string} pattern - The CLI module pattern to search for (glob syntax)
* @param {Array<Function>} initModules - Array of custom initializer functions
*/
export async function start({
main = cwd,
cwd = process.cwd(),
directories = [lscRoot],
pattern = '{src/cli,cli}/*.js',
initFunctions = []
Expand All @@ -40,11 +39,11 @@ export async function start({

checkVersion({name: 'lsc', logger: app.log});

if (isPackageSync(main)) {
if (isPackageSync(cwd)) {
app.config.file({
file: path.join(main, 'config.json')
file: path.join(cwd, 'config.json')
});
pkg = require(path.join(main, 'package.json'));
pkg = require(path.join(cwd, 'package.json'));
} else {
pkg = require(path.join(lscRoot, 'package.json'));
app.config.file({
Expand All @@ -68,7 +67,7 @@ export async function start({

app.use(require('flatiron-cli-config'));
app.use(loaderPlugin, {
main,
cwd,
directories,
pattern,
initFunctions
Expand Down
27 changes: 9 additions & 18 deletions lib/config/loader.ts
Expand Up @@ -50,46 +50,37 @@ function loadConfig(directory: string, config): void {

interface ConfigLoaderOptions {
configFilePath?: string
main?: string
packageDirectory?: string
cwd?: string
directories?: string[]
}

/**
* @throws an exception if invalid options are provided.
*
* @param {Object} options
* @param {string} [options.packageDirectory] - A relative or absolute path to a directory containing LabShare
* packages as subdirectories. Default: ''
* @param {string} [options.main] - The current, top-level project directory
* @param {string} [options.cwd] - The current, top-level project directory
* @param {Array} [directories] - A list of paths to LabShare packages that should be searched for CLI commands. Each directory
* must contain a package.json to be considered valid. Default: []
* @param {string} [configFilePath] - A path to a configuration file. Configuration data in the file will be loaded
* first then any configuration files found in 'main' or 'directories' will be loaded next.
* first then any configuration files found in 'cwd' or 'directories' will be loaded next.
* @constructor
*/
export class ConfigLoader {
private options: ConfigLoaderOptions;

constructor(options: ConfigLoaderOptions = {
configFilePath: null,
main: null,
packageDirectory: null,
cwd: null,
directories: []
}) {
if (options.configFilePath) {
assert.ok(_.isString(options.configFilePath), '`options.configFilePath` must be a string');
options.configFilePath = path.resolve(untildify(options.configFilePath));
}

if (options.main) {
assert.ok(_.isString(options.main), '`options.main` must be a string');
options.main = path.resolve(options.main);
}

if (options.packageDirectory) {
assert.ok(_.isString(options.packageDirectory), '`options.packageDirectory` must be a string');
options.packageDirectory = path.resolve(options.packageDirectory);
if (options.cwd) {
assert.ok(_.isString(options.cwd), '`options.cwd` must be a string');
options.cwd = path.resolve(options.cwd);
}

if (options.directories) {
Expand Down Expand Up @@ -117,8 +108,8 @@ export class ConfigLoader {
}

try {
if (this.options.main) {
applyToNodeModulesSync(this.options.main, directory => {
if (this.options.cwd) {
applyToNodeModulesSync(this.options.cwd, directory => {
loadConfig(directory, config);
});
}
Expand Down

0 comments on commit d0d8ea6

Please sign in to comment.