insomnia/packages/insomnia-app/app/plugins/create.ts
Dimitri Mitropoulos 5f4c19da35
[TypeScript] Phase 1 & 2 (#3370)
Co-authored-by: Opender Singh <opender.singh@konghq.com>
2021-05-12 18:35:00 +12:00

41 lines
920 B
TypeScript

import fs from 'fs';
import path from 'path';
import mkdirp from 'mkdirp';
import rimraf from 'rimraf';
import { PLUGIN_PATH } from '../common/constants';
export async function createPlugin(
moduleName: string,
version: string,
mainJs: string,
) {
const pluginDir = path.join(PLUGIN_PATH, moduleName);
if (fs.existsSync(pluginDir)) {
throw new Error(`Plugin already exists at "${pluginDir}"`);
}
rimraf.sync(pluginDir);
mkdirp.sync(pluginDir);
// Write package.json
fs.writeFileSync(
path.join(pluginDir, 'package.json'),
JSON.stringify(
{
name: moduleName,
version,
private: true,
insomnia: {
name: moduleName.replace(/^insomnia-plugin-/, ''),
description: '',
},
main: 'main.js',
},
null,
2,
),
);
// Write main JS file
fs.writeFileSync(path.join(pluginDir, 'main.js'), mainJs);
}