insomnia/packages/insomnia-app/app/plugins/create.ts

42 lines
921 B
TypeScript
Raw Normal View History

import fs from 'fs';
import mkdirp from 'mkdirp';
2021-07-22 23:04:56 +00:00
import path from 'path';
import rimraf from 'rimraf';
2021-07-22 23:04:56 +00:00
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);
}