103 lines
2.9 KiB
TypeScript
103 lines
2.9 KiB
TypeScript
(async () => {
|
|
const serviceName = prompt('Service name? (This is used by the router to determine the route prefix)')?.replace('-service', '');
|
|
if (!serviceName) return;
|
|
type permissionShorts = 'r' | 'w' | 'x' | 'n';
|
|
type permissionNames = 'read' | 'write' | 'execute' | 'run' | 'net';
|
|
|
|
const getPermissionByNameOrShort = (name: permissionNames | permissionShorts | string) => {
|
|
switch (name) {
|
|
case "read":
|
|
case "r":
|
|
return {
|
|
name: 'read',
|
|
denoPerm: '--allow-read'
|
|
};
|
|
case "write":
|
|
case "w":
|
|
return {
|
|
name: 'write',
|
|
denoPerm: '--allow-write'
|
|
};
|
|
case "execute":
|
|
case "run":
|
|
case "x":
|
|
return {
|
|
name: 'run',
|
|
denoPerm: '--allow-run'
|
|
};
|
|
case "net":
|
|
case "n":
|
|
return {
|
|
name: 'net',
|
|
denoPerm: '--allow-net'
|
|
};
|
|
default:
|
|
return {
|
|
name: 'custom-' + name,
|
|
denoPerm: '--allow-' + name
|
|
}
|
|
}
|
|
}
|
|
|
|
const perms = [];
|
|
|
|
if (Deno.args[0]) {
|
|
const permShort = Deno.args[0].split('') as permissionShorts[];
|
|
for (const short of permShort) {
|
|
perms.push(getPermissionByNameOrShort(short));
|
|
}
|
|
} else if (confirm('Permissions not specified, would you like to add specific permissions?')) {
|
|
let permsPrompt: string | null = '';
|
|
while (!permsPrompt) {
|
|
permsPrompt = prompt('Please enter the permission short code or names of permissions you would like');
|
|
if (!permsPrompt) continue
|
|
let permNames;
|
|
if (permsPrompt?.match(' ')) {
|
|
permNames = permsPrompt.split(' ');
|
|
} else {
|
|
permNames = permsPrompt.split('');
|
|
}
|
|
|
|
for (const perm of permNames) {
|
|
perms.push(getPermissionByNameOrShort(perm));
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`Service will run with following permissions: ${perms.map(p => p.name).join()}`);
|
|
if (!perms.find(p => p.name === 'net')) {
|
|
if (!confirm('Service does not have permissions to "net", is this correct?'))
|
|
perms.push(getPermissionByNameOrShort('net'));
|
|
}
|
|
|
|
const serviceFile = `./${serviceName}-service/`;
|
|
await Deno.mkdir(serviceFile);
|
|
await Deno.writeTextFile(serviceFile + 'index.ts', `
|
|
import { CGGService } from 'cgg/Application.ts';
|
|
import { Router } from 'oak';
|
|
|
|
const app = new CGGService({ prefix: '/${serviceName}' });
|
|
|
|
app.route(new Router());
|
|
|
|
app.start();
|
|
console.log('User service running on ' + Deno.args.at(0));
|
|
`);
|
|
await Deno.writeTextFile(serviceFile + 'perms', perms.map(p => p.denoPerm).join('\n'));
|
|
await Deno.writeTextFile(serviceFile + 'prefix', serviceName);
|
|
|
|
if (confirm('Does this service need DB access?'))
|
|
await Deno.writeTextFile(serviceFile + 'data.ts', `
|
|
import mongoose from 'mongoose';
|
|
import { configDatabase } from 'lib/data.ts';
|
|
|
|
configDatabase(mongoose);
|
|
`)
|
|
|
|
Deno.run({
|
|
cmd: [
|
|
'mplayer',
|
|
'./chimes.wav'
|
|
]
|
|
})
|
|
})(); |