
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
node6+
・メッセージ内容の簡易取得
scripts/app.js
const RobotSpec = require('robot-spec');
module.exports = (robot) => {
const robot2 = new RobotSpec(robot);
robot2.hear(/PING$/i, ({ res, msg }) => {
res.send('.PONG > ' + msg);
});
robot2.hear('text', ({ res, msg }) => {
res.send('.TEXT > ' + msg);
});
robot2.hear('select', ({ res }) => {
res.send('.SELECT-STAMP ');
});
robot2.respond(/ECHO (.*)_(.*)/i, ({ res, msg, msgs }) => {
res.send('.ECHO INPUT >\n' + msgs.join('\n'));
});
};
・メソッドのキャッシュと呼び出し
scripts/app.js
const RobotSpec = require('robot-spec');
module.exports = (robot) => {
const robot2 = new RobotSpec(robot).config();
const spec = robot2.spec;
robot2.hear('text', ({ res, msg }) => spec.scenario.textReceive({ res, msg }));
robot2.hear('select', ({ res }) => spec.scenario.selectReceive({ res }));
robot2.hear('stamp', ({ res }) => spec.scenario.stampReceive({ res }));
robot.hear(/PING$/i, (res) => {
res.send('PONG');
});
robot2.respond(/TEST (.*)_(.*)/i, ({ res, msg, msgs }) => {
res.send('test!\n' + msgs.join('\n'));
});
robot2.hear(/TEST (.*)_(.*)/i, ({ res, msg, msgs }) => {
res.send('test!!\n' + msgs.join('\n'));
console.log(msg, msgs);
});
};
・メソッドの初期化を行う(constructorの実行)
initialize
const robot2 = new RobotSpec(robot).config().initialize();
const spec = robot2.spec;
キャッシュしたメソッドの利用
scripts/app.js
const robot2 = new RobotSpec(robot).config();
const spec = robot2.spec;
spec.scenario.method();
// or
robot2.hear('stamp', ({ res }) => spec.scenario.method());
別のモジュールではもう一度呼び出して使用することも可能
const spec = require('robot-spec').Spec();
spec.scenario.method();
ディレクトリ構造
scripts/app.js
src
├── components.js
コアメソッドのキャッシュ
src/components.js
'use strict';
const components = (robot, config) => ({
robot,
config,
// scenario
scenario: './scenario/common',
task: './scenario/task'
});
module.exports = components;
メソッドのキャッシュ
ディレクトリ構造
scripts/app.js
src
├── scenario
│ ├── common
│ │ ├── index.js // *
│ │ ├── join-home.js
│ │ ├── select-action-admin.js
│ │ ├── select-receive.js
│ │ ├── stamp-receive.js
│ │ ├── text-action-admin.js
│ │ └── text-receive.js
scenario/common/index.js
'use strict';
class Common {
constructor(components) {
components.mixin(this, __dirname, {
joinHome: './join-home',
selectActionAdmin: './select-action-admin',
selectReceive: './select-receive',
stampReceive: './stamp-receive',
textActionAdmin: './text-action-admin',
textReceive: './text-receive',
});
}
}
module.exports = Common;
クラス
'use strict';
class Schedules {
constructor(spec) {
this.spec = spec;
}
add(args) {
return this.spec.apiSchedule.add(args);
}
}
module.exports = Schedules;
メソッド
scenario/common/join-home.js
'use strict';
const joinHome = ({ messages }) => {
return ({ res }) => {
return messages.joinUser({ res });
};
};
module.exports = joinHome;
クラス、メソッド共にモジュールから読み込む方法でも可能
(再掲)
const spec = require('robot-spec').Spec();
spec.scenario.method();
クラス、メソッドのキャッシュ
scripts/app.js
new RobotSpec(robot).config(opts);
opts
の設定を変更することで、各設置ディレクトリの変更が可能です。
opts
デフォルト
opts = {
root: 'src/',
config: 'src/config',
components: 'src/components'
};
config.js
は定数を定義し、グローバル利用できます。
利用しない場合は、false
を設定します。
opts = {
root: 'src/',
config: false,
components: 'src/components'
};
config.js
を利用し、components.js
(メソッドキャッシュ)を利用しない場合は、
下記のように、components
にfalse
を設定します。
opts = {
root: 'src/',
config: 'src/config',
components: false
};
または、components
のみ設定します。
opts = {
components: false
};
グローバル定数
src/config.js
const g = {};
g.ADMIN_USER = 'xxxxxxxx';
g.ADMIN_PASS = 'xxxxxxxx';
module.exports = g;
FAQs
customize robot events hear and response. apply robot and spec.
The npm package robot-spec receives a total of 1 weekly downloads. As such, robot-spec popularity was classified as not popular.
We found that robot-spec demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.