Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should delete all online rich menus if using force', async () => {
const ctx = setup(true);
LineClient.connect().getRichMenuList.mockResolvedValueOnce([
{
richMenuId: '1234567890',
},
{
richMenuId: '0987654321',
},
]);
await deleteLineMenu(ctx);
expect(LineClient.connect().deleteRichMenu.mock.calls.length).toBe(2);
expect(print.mock.calls.length).toBe(1);
});
it('should not call deleteRichMenu when online rich menus and local rich menus are same', async () => {
const ctx = setup();
LineClient.connect().getRichMenuList.mockResolvedValueOnce([
{
richMenuId: '1234567890',
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
it('should exit when failed to find rich menu', async () => {
const ctx = setup(false);
LineClient.connect().getRichMenuList.mockResolvedValueOnce(null);
await deleteLineMenu(ctx);
expect(error).toBeCalled();
expect(process.exit).toBeCalled();
});
it('should print rich menus', async () => {
const ctx = setup();
LineClient.connect().getRichMenuList.mockResolvedValueOnce([
{
richMenuId: '1234567890',
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=sell&itemid=321',
},
},
],
},
]);
await setLineMenus(ctx);
expect(LineClient.connect().deleteRichMenu.mock.calls.length).toBe(2);
expect(LineClient.connect().createRichMenu.mock.calls.length).toBe(1);
expect(print.mock.calls.length).toBe(1);
expect(log.mock.calls.length).toBe(1);
});
});
y: 0,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=sell&itemid=321',
},
},
],
},
]);
await setLineMenus(ctx);
expect(LineClient.connect().deleteRichMenu.mock.calls.length).toBe(2);
expect(LineClient.connect().createRichMenu.mock.calls.length).toBe(1);
expect(print.mock.calls.length).toBe(1);
expect(log.mock.calls.length).toBe(1);
});
});
export async function setLineMenus(_: CliContext) {
try {
const config = getConfig('line');
const { richMenus: localRichMenus } = config;
invariant(config.accessToken, 'accessToken is not found in config file');
const accessToken = config.accessToken;
const client = LineClient.connect(accessToken);
const onlineRichMenus = await client.getRichMenuList();
invariant(
onlineRichMenus,
`Failed to get ${bold('LINE rich menu')} response.`
);
const existedRichMenus = onlineRichMenus.map(richMenu =>
omit(richMenu, 'richMenuId')
);
const shouldDeleteRichMenus = differenceWith(
existedRichMenus,
localRichMenus,
isEqual
);
constructor(options: ConstructorOptions) {
const {
mapDestinationToAccessToken,
shouldBatch,
sendMethod,
skipProfile,
} = options;
if ('client' in options) {
this._client = options.client;
this._channelSecret = '';
} else {
const { accessToken, channelSecret, origin } = options;
this._client = LineClient.connect({
accessToken,
channelSecret,
origin,
});
this._channelSecret = channelSecret;
}
this._mapDestinationToAccessToken = mapDestinationToAccessToken || null;
this._shouldBatch = typeof shouldBatch === 'boolean' ? shouldBatch : true;
warning(
!sendMethod || sendMethod === 'reply' || sendMethod === 'push',
'sendMethod should be one of `reply` or `push`'
);
this._sendMethod = sendMethod || 'reply';
export async function deleteLineMenu(ctx: CliContext) {
const argv = getSubArgs(ctx.argv, {
'--force': Boolean,
'-f': '--force',
});
const force = argv['--force'];
try {
const config = getConfig('line');
invariant(config.accessToken, 'accessToken is not found in config file');
const accessToken = config.accessToken;
const client = LineClient.connect(accessToken);
const richMenus = await client.getRichMenuList();
invariant(richMenus, `Failed to get ${bold('LINE rich menu')} response.`);
if (force) {
await pMap(
richMenus,
async richMenu => {
await client.deleteRichMenu(richMenu.richMenuId);
},
{ concurrency: 5 }
);
print(`Successfully delete ${bold('all')} LINE rich menu.`);
} else {