New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@nowa/core

Package Overview
Dependencies
Maintainers
3
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nowa/core - npm Package Compare versions

Comparing version 0.0.6 to 0.0.7

lib/plugins/loadConfig.d.ts

6

index.d.ts

@@ -0,4 +1,6 @@

import { Module } from './lib/core/module';
import { Runner } from './lib/runner';
import { Plugin } from './lib/types/plugin';
declare const createRunner: (plugins: Plugin<Runner>[]) => Promise<Runner>;
export default createRunner;
export declare const createRunner: (plugins: Plugin<Runner>[]) => Promise<Runner>;
export declare const createDefaultRunner: (plugins: Plugin<Runner>[]) => Promise<Runner>;
export { Runner, Module };

@@ -11,22 +11,23 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const module_1 = require("./lib/core/module");
exports.Module = module_1.Module;
const runner_1 = require("./lib/runner");
const loadConfig_1 = require("./lib/builtins/loadConfig");
const loadModules_1 = require("./lib/builtins/loadModules");
const loadPlugins_1 = require("./lib/builtins/loadPlugins");
const loadSolution_1 = require("./lib/builtins/loadSolution");
const parseConfig_1 = require("./lib/builtins/parseConfig");
const parseSolution_1 = require("./lib/builtins/parseSolution");
const createRunner = (plugins) => __awaiter(this, void 0, void 0, function* () {
exports.Runner = runner_1.Runner;
const utils_1 = require("./lib/utils");
const loadConfig_1 = require("./lib/plugins/loadConfig");
const loadModules_1 = require("./lib/plugins/loadModules");
const loadPlugins_1 = require("./lib/plugins/loadPlugins");
const loadSolution_1 = require("./lib/plugins/loadSolution");
const parseConfig_1 = require("./lib/plugins/parseConfig");
const parseSolution_1 = require("./lib/plugins/parseSolution");
exports.createRunner = (plugins) => __awaiter(this, void 0, void 0, function* () {
const runner = new runner_1.Runner();
for (const plugin of plugins) {
plugin.apply(runner);
yield utils_1.awaitable(plugin.apply(runner));
}
yield new loadConfig_1.default().apply(runner);
yield new loadModules_1.default().apply(runner);
yield new loadPlugins_1.default().apply(runner);
yield new loadSolution_1.default().apply(runner);
yield new parseConfig_1.default().apply(runner);
yield new parseSolution_1.default().apply(runner);
return runner;
});
exports.default = createRunner;
exports.createDefaultRunner = (plugins) => __awaiter(this, void 0, void 0, function* () {
const allPlugins = [new loadConfig_1.LoadConfigPlugin(), new loadModules_1.LoadModulesPlugin(), new loadPlugins_1.LoadPluginsPlugin(), new loadSolution_1.LoadSolutionPlugin(), new parseConfig_1.ParseConfigPlugin(), new parseSolution_1.ParseSolutionPlugin(), ...plugins];
return yield exports.createRunner(allPlugins);
});
import { Runnable } from './runnable';
import { Info } from '../types/info';
import { Pluginable } from './pluginable';
export declare namespace Module {
interface BaseInterface {
interface Base {
init(): Promise<void>;
}
abstract class Async<Options extends object = object, PluginGroupName extends string = string> extends Runnable.Async<PluginGroupName> implements BaseInterface {
name: string;
options: Options;
interface Runtime {
info: Info;
commandOptions: object;
moduleOptions: object;
constructor(name: string, options: Options, info: Info, moduleOptions: object);
}
abstract class Async<PluginGroup extends Pluginable.Group> extends Runnable.Async<PluginGroup> implements Base {
$name: string;
$runtime: Runtime;
constructor($name: string, $runtime: Runtime);
abstract init(): Promise<void>;
}
abstract class Callback<Options extends object = object, PluginGroupName extends string = string> extends Runnable.Callback<PluginGroupName> implements BaseInterface {
name: string;
options: Options;
info: Info;
moduleOptions: object;
constructor(name: string, options: Options, info: Info, moduleOptions: object);
abstract class Callback<PluginGroup extends Pluginable.Group> extends Runnable.Callback<PluginGroup> implements Base {
$name: string;
$runtime: Runtime;
constructor($name: string, $runtime: Runtime);
abstract init(): Promise<void>;
}
type Type = Async | Callback;
type Type<PluginGroup extends Pluginable.Group> = Async<PluginGroup> | Callback<PluginGroup>;
}

@@ -7,9 +7,6 @@ "use strict";

class Async extends runnable_1.Runnable.Async {
constructor(name, options, info, moduleOptions) {
constructor($name, $runtime) {
super();
this.name = name;
this.options = options;
this.info = info;
this.moduleOptions = moduleOptions;
return this;
this.$name = $name;
this.$runtime = $runtime;
}

@@ -19,8 +16,6 @@ }

class Callback extends runnable_1.Runnable.Callback {
constructor(name, options, info, moduleOptions) {
constructor($name, $runtime) {
super();
this.name = name;
this.options = options;
this.info = info;
this.moduleOptions = moduleOptions;
this.$name = $name;
this.$runtime = $runtime;
}

@@ -27,0 +22,0 @@ }

export declare namespace Pluginable {
type Handler<Expected = any> = (...params: any[]) => Expected | Promise<Expected>;
interface Registry {
[groupName: string]: Handler[];
}
type Handler<Param, Expected, This> = (this: This, param: Param) => Expected | Promise<Expected>;
type Group = {
[groupName: string]: [any, any];
};
type Registry<This> = {
[groupName in keyof Group]: Pluginable.Handler<Group[groupName][0], Group[groupName][1], This>[];
};
}
export declare abstract class Pluginable<GroupName extends string = string> {
plugins: Pluginable.Registry;
register<Expected = any>(groupName: GroupName, handler: Pluginable.Handler<Expected>): void;
applyPlugin(groupName: GroupName, params?: any[]): Promise<void>;
applyPluginBail<Expected>(groupName: GroupName, params?: any[], reverse?: boolean): Promise<Expected>;
protected applyPluginWaterfall<Expected>(groupName: string, initial: any): Promise<Expected>;
export declare abstract class Pluginable<Group extends Pluginable.Group> {
$plugins: Pluginable.Registry<this>;
$register<GroupName extends keyof Group>(groupName: GroupName, handler: Pluginable.Handler<Group[GroupName][0], Group[GroupName][1], this>): void;
$applyPlugin<GroupName extends keyof Group>(groupName: GroupName, param?: Group[GroupName][0]): Promise<void>;
$applyPluginBail<GroupName extends keyof Group>(groupName: GroupName, param?: Group[GroupName][0], reverse?: boolean): Promise<Group[GroupName][1]>;
$applyPluginWaterfall<GroupName extends keyof Group>(groupName: GroupName, initial: Group[GroupName][0]): Promise<Group[GroupName][1]>;
}

@@ -14,13 +14,13 @@ "use strict";

constructor() {
this.plugins = {};
this.$plugins = {};
}
register(groupName, handler) {
this.plugins[groupName] || (this.plugins[groupName] = []);
this.plugins[groupName].push(handler);
$register(groupName, handler) {
this.$plugins[groupName] || (this.$plugins[groupName] = []);
this.$plugins[groupName].push(handler);
}
applyPlugin(groupName, params = []) {
$applyPlugin(groupName, param) {
return __awaiter(this, void 0, void 0, function* () {
const plugins = this.plugins[groupName];
const plugins = this.$plugins[groupName];
if (plugins) {
yield Promise.all(plugins.map(handler => utils_1.awaitable(handler.call(this, ...params))));
yield Promise.all(plugins.map(handler => utils_1.awaitable(handler.call(null, param))));
}

@@ -30,8 +30,8 @@ return;

}
applyPluginBail(groupName, params = [], reverse = true) {
$applyPluginBail(groupName, param, reverse = true) {
return __awaiter(this, void 0, void 0, function* () {
const plugins = reverse ? this.plugins[groupName] : Array.from(this.plugins[groupName]).reverse();
const plugins = reverse ? this.$plugins[groupName] : Array.from(this.$plugins[groupName]).reverse();
if (plugins) {
for (const handler of plugins) {
const result = yield utils_1.awaitable(handler.call(this, ...params));
const result = yield utils_1.awaitable(handler.call(null, param));
if (result !== undefined) {

@@ -45,9 +45,9 @@ return result;

}
applyPluginWaterfall(groupName, initial) {
$applyPluginWaterfall(groupName, initial) {
return __awaiter(this, void 0, void 0, function* () {
const plugins = this.plugins[groupName];
const plugins = this.$plugins[groupName];
let prevResult = initial;
if (plugins) {
for (const func of plugins) {
const result = yield utils_1.awaitable(func.call(this, initial));
const result = yield utils_1.awaitable(func.call(null, initial));
if (result !== undefined) {

@@ -54,0 +54,0 @@ prevResult = result;

import { Pluginable } from './pluginable';
export declare namespace Runnable {
abstract class Base<PluginGroupName extends string = string> extends Pluginable<PluginGroupName> {
type: 'callback' | 'async';
abstract run(done?: (error?: Error) => void): void | Promise<void>;
interface Base {
$type: 'callback' | 'async';
run(done?: (error?: Error) => void): void | Promise<void>;
}
abstract class Callback<PluginGroupName extends string = string> extends Runnable.Base<PluginGroupName> {
type: 'callback';
abstract class Callback<PluginGroup extends Pluginable.Group> extends Pluginable<PluginGroup> implements Base {
$type: 'callback';
abstract run(done: (error?: Error) => void): void;
}
abstract class Async<PluginGroupName extends string = string> extends Runnable.Base<PluginGroupName> {
type: 'async';
abstract class Async<PluginGroup extends Pluginable.Group> extends Pluginable<PluginGroup> implements Base {
$type: 'async';
abstract run(): Promise<void>;
}
type Type = Callback | Async;
type Type<PluginGroup extends Pluginable.Group> = Callback<PluginGroup> | Async<PluginGroup>;
}

@@ -6,16 +6,13 @@ "use strict";

(function (Runnable) {
class Base extends pluginable_1.Pluginable {
}
Runnable.Base = Base;
class Callback extends Runnable.Base {
class Callback extends pluginable_1.Pluginable {
constructor() {
super(...arguments);
this.type = 'callback';
this.$type = 'callback';
}
}
Runnable.Callback = Callback;
class Async extends Runnable.Base {
class Async extends pluginable_1.Pluginable {
constructor() {
super(...arguments);
this.type = 'async';
this.$type = 'async';
}

@@ -22,0 +19,0 @@ }

import { Runnable } from './core/runnable';
import { Module } from './core/module';
export declare namespace ModuleQueue {
type PluginGroupName = 'init-start' | 'init-error' | 'init-end' | 'run-start' | 'run-error' | 'run-end';
type PluginGroup = {
'init-start': [undefined, void];
'init-end': [undefined, void];
'run-start': [undefined, void];
'run-end': [undefined, void];
'init-error': [{
error: any;
}, void];
'run-error': [{
error: any;
}, void];
};
interface runtime {
loopModules: Map<Module.Callback, number>;
loopModules: Map<Module.Callback<any>, number>;
validLoopID: number;

@@ -11,6 +22,6 @@ done?: (error?: Error) => void;

}
export declare class ModuleQueue extends Runnable.Callback<ModuleQueue.PluginGroupName> {
modules: Module.Type[];
export declare class ModuleQueue extends Runnable.Callback<ModuleQueue.PluginGroup> {
modules: Module.Type<any>[];
runtime: ModuleQueue.runtime;
constructor(modules: Module.Type[]);
constructor(modules: Module.Type<any>[]);
private _initModule(module);

@@ -20,6 +31,6 @@ private _runModule(module, loopID);

private _checkLoopIsValid(loopID);
private _handleInitError(error?);
private _handleRunError(error?);
private _handleInitError(error);
private _handleRunError(error);
init(): Promise<void>;
run(done?: (error?: Error) => void): Promise<void>;
}

@@ -24,4 +24,4 @@ "use strict";

for (const [index, module] of this.modules.entries()) {
debug(`construct ${module.type} module ${module.name}`);
if (module.type === 'callback') {
debug(`construct ${module.$type} module ${module.$name}`);
if (module.$type === 'callback') {
this.runtime.loopModules.set(module, index);

@@ -38,5 +38,3 @@ }

catch (error) {
if (yield this._handleInitError(error)) {
throw error;
}
this._handleInitError(error);
}

@@ -51,4 +49,4 @@ });

}
debug(`call ${module.name}#run`);
if (module.type === 'async') {
debug(`call ${module.$name}#run`);
if (module.$type === 'async') {
try {

@@ -89,11 +87,18 @@ yield module.run();

if (error) {
debug(`${module.name} try to create a new loop but found error`, error);
debug(`${module.$name} try to create a new loop but found error`, error);
this._handleRunError(error);
return;
}
const loopID = this.runtime.validLoopID += 1;
for (const module of this.modules) {
const loopID = (this.runtime.validLoopID += 1);
const currentModuleIndex = this.runtime.loopModules.get(module);
if (currentModuleIndex === undefined) {
this._handleRunError(new Error(`can not locale which module to start`));
return;
}
debug(`continue on module`, this.modules[currentModuleIndex + 1]);
for (const module of this.modules.slice(currentModuleIndex + 1)) {
yield this._runModule(module, loopID);
}
debug('apply run-end');
yield this.applyPlugin('run-end', [this]);
yield this.$applyPlugin('run-end');
this.runtime.done && this.runtime.done();

@@ -106,18 +111,8 @@ });

_handleInitError(error) {
return __awaiter(this, void 0, void 0, function* () {
if (!error) {
return true;
}
debug('apply init-error because of', error);
return yield this.applyPluginBail('init-error', [error, this]);
});
debug('apply init-error because of', error);
this.$applyPlugin('init-error', { error });
}
_handleRunError(error) {
return __awaiter(this, void 0, void 0, function* () {
if (!error) {
return true;
}
debug('apply run-error because of', error);
return yield this.applyPluginBail('run-error', [error, this]);
});
debug('apply run-error because of', error);
this.$applyPlugin('run-error', { error });
}

@@ -127,3 +122,3 @@ init() {

debug('apply init-start');
yield this.applyPlugin('init-start', [this]);
yield this.$applyPlugin('init-start');
for (const module of this.modules) {

@@ -133,3 +128,3 @@ yield this._initModule(module);

debug('apply init-end');
yield this.applyPlugin('init-end', [this]);
yield this.$applyPlugin('init-end');
});

@@ -141,3 +136,3 @@ }

debug('apply run-start');
yield this.applyPlugin('run-start', [this]);
yield this.$applyPlugin('run-start');
const loopID = 1;

@@ -148,3 +143,3 @@ for (const module of this.modules) {

debug('apply run-end');
yield this.applyPlugin('run-end', [this]);
yield this.$applyPlugin('run-end');
this.runtime.done && this.runtime.done();

@@ -151,0 +146,0 @@ });

@@ -6,9 +6,31 @@ import { Runnable } from './core/runnable';

import { ModuleQueue } from './moduleQueue';
import { Plugin } from './types/plugin';
import { Module } from './core/module';
export declare namespace Runner {
type PluginGroupName = 'init-start' | 'load-info' | 'load-config' | 'load-solution' | 'parse-config' | 'parse-solution' | 'load-plugins' | 'get-options' | 'load-modules' | 'init-module-queue' | 'init-end' | 'run-start' | 'run-end' | 'load-plugins';
interface runtime {
}
type PluginGroup = {
'init-start': [undefined, void];
'load-info': [undefined, Info];
'load-config': [Pick<Runner, 'info'>, Runner['rawConfig']];
'parse-config': [Pick<Runner, 'info' | 'rawConfig'>, Runner['parsedConfig']];
'load-solution': [Pick<Runner, 'info' | 'rawConfig'>, Runner['rawSolution']];
'parse-solution': [Pick<Runner, 'info' | 'rawSolution'>, Runner['parsedSolution']];
'load-plugins': [{
info: Runner['info'];
pluginPaths: Config._Nowa._Plugin[];
}, Plugin<Runner>[]];
'get-options': [Pick<Runner, 'info' | 'parsedSolution' | 'parsedConfig'>, Runner['options']];
'load-modules': [Pick<Runner, 'info' | 'options' | 'parsedSolution'>, Runner['modules']];
'init-module-queue': [Pick<Runner, 'info' | 'options' | 'moduleQueue'>, void];
'init-end': [undefined, void];
'run-start': [undefined, void];
'run-end': [undefined, void];
'init-error': [{
error: any;
}, void];
'run-error': [{
error: any;
}, void];
};
}
export declare class Runner extends Runnable.Callback<Runner.PluginGroupName> {
export declare class Runner extends Runnable.Callback<Runner.PluginGroup> {
rawConfig: Config.User;

@@ -19,3 +41,3 @@ parsedConfig: Config.Parsed;

info: Info;
modules: Module.Type[];
modules: Module.Type<{}>[];
moduleQueue: ModuleQueue;

@@ -22,0 +44,0 @@ options: object;

@@ -20,38 +20,44 @@ "use strict";

return __awaiter(this, void 0, void 0, function* () {
debug('apply init-start');
yield this.applyPlugin('init-start');
// load info/config/solution
debug('apply load-info');
this.info = yield this.applyPluginBail('load-info');
debug('apply load-config');
this.rawConfig = yield this.applyPluginBail('load-config', [this.info]);
debug('apply load-solution');
this.rawSolution = yield this.applyPluginBail('load-solution', [this.info, this.rawConfig.solution]);
// parse config/solution
debug('apply parse-config');
this.parsedConfig = yield this.applyPluginWaterfall('parse-config', this.rawConfig);
debug('apply parse-solution');
this.parsedSolution = yield this.applyPluginWaterfall('parse-solution', this.rawSolution);
// load plugins
const rawPlugins = [...((this.parsedConfig.nowa && this.parsedConfig.nowa.plugins) || []), ...((this.parsedSolution.nowa && this.parsedSolution.nowa.plugins) || [])];
debug('apply load-plugins');
const plugins = yield this.applyPluginBail('load-plugins', [rawPlugins]);
debug(`load ${plugins.length} plugin(s) from config and solution`);
for (const plugin of plugins) {
yield utils_1.awaitable(plugin.apply(this));
try {
debug('apply init-start');
yield this.$applyPlugin('init-start');
// load info/config/solution
debug('apply load-info');
this.info = yield this.$applyPluginBail('load-info');
debug('apply load-config');
this.rawConfig = yield this.$applyPluginBail('load-config', this);
debug('apply load-solution');
this.rawSolution = yield this.$applyPluginBail('load-solution', this);
// parse config/solution
debug('apply parse-config');
this.parsedConfig = yield this.$applyPluginWaterfall('parse-config', this);
debug('apply parse-solution');
this.parsedSolution = yield this.$applyPluginWaterfall('parse-solution', this);
// load plugins
const pluginPaths = [...((this.parsedConfig.nowa && this.parsedConfig.nowa.plugins) || []), ...((this.parsedSolution.nowa && this.parsedSolution.nowa.plugins) || [])];
debug('apply load-plugins');
const plugins = yield this.$applyPluginBail('load-plugins', Object.assign({}, this, { pluginPaths }));
debug(`load ${plugins.length} plugin(s) from config and solution`);
for (const plugin of plugins) {
yield utils_1.awaitable(plugin.apply(this));
}
debug(`all custom plugins loaded`);
debug('apply get-options');
this.options = yield this.$applyPluginBail('get-options', this);
debug('apply load-modules');
this.modules = yield this.$applyPluginBail('load-modules', this);
debug(`load ${this.modules.length} module(s) for command nowa ${this.info.command} ${this.info.subCommand}`);
debug('create & init moduleQueue');
console.log(this.modules);
this.moduleQueue = new moduleQueue_1.ModuleQueue(this.modules);
debug('apply init-module-queue');
this.$applyPlugin('init-module-queue', this);
yield this.moduleQueue.init();
debug('apply init-end');
yield this.$applyPlugin('init-end');
}
debug(`all custom plugins loaded`);
debug('apply get-options');
this.options = yield this.applyPluginBail('get-options', [this.info, this.parsedSolution.commandDescriptors, this.parsedConfig.config]);
debug('apply load-modules');
this.modules = yield this.applyPluginBail('load-modules', [this.options, this.info, this.rawSolution.commands]);
debug(`load ${this.modules.length} module(s) for command nowa ${this.info.command} ${this.info.subCommand}`);
debug('create & init moduleQueue');
console.log(this.modules);
this.moduleQueue = new moduleQueue_1.ModuleQueue(this.modules);
debug('apply init-module-queue');
this.applyPlugin('init-module-queue', [this.moduleQueue]);
yield this.moduleQueue.init();
debug('apply init-end');
yield this.applyPlugin('init-end');
catch (e) {
debug(`apply init-error because of ${e}`);
this.$applyPlugin('init-error', { error: e });
}
});

@@ -64,10 +70,10 @@ }

debug('apply run-end');
this.applyPlugin('run-end').then(() => process.exit(1));
this.$applyPlugin('run-end').then(() => process.exit(1));
});
debug('apply run-start');
yield this.applyPlugin('run-start');
yield this.$applyPlugin('run-start');
debug('run modules');
this.moduleQueue.run(() => {
debug('apply run-end');
this.applyPlugin('run-end');
this.$applyPlugin('run-end');
});

@@ -78,12 +84,1 @@ });

exports.Runner = Runner;
// load-info 包括 全局的 info 和 项目本身的 info
// load-config 根据 info 检查 config 位置 并 载入转成 json
// load-solution 根据 info 检查 solution 位置 并 载入转成 json
// parse-config json 内 [@xxx] 关键字 替换。 除module.options内,其余方法 执行并拿到结果
// parse-solution
// instantiate-component
// const
// const runner = new Runner();
// runner.register('start', (ctx: Runner) =>{ console.log(ctx.hello)})
// runner.init();
// runner.run();

@@ -1,4 +0,3 @@

import { Pluginable } from '../core/pluginable';
export interface Plugin<For extends Pluginable = Pluginable> {
export declare type Plugin<For> = {
apply: (pluginable: For) => void | Promise<void>;
}
};
{
"name": "@nowa/core",
"version": "0.0.6",
"version": "0.0.7",
"description": "The nowa core",

@@ -22,3 +22,3 @@ "scripts": {

"@types/mocha": "^2.2.44",
"@types/node": "^8.0.46",
"@types/node": "^8.0.56",
"mocha": "^4.0.1",

@@ -25,0 +25,0 @@ "nyc": "^11.3.0",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc