add requestor template

This commit is contained in:
Timo Hocker 2020-01-21 09:50:05 +01:00
parent 692f48883c
commit e4b2a53344
2 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,69 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of snippeteer which is released under BSD-3-Clause.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, January 2020
*/
/* eslint-disable no-sync */
/* eslint-disable no-console */
const fs = require ('fs-extra');
const path = require ('path');
/**
* copies the full template to a new folder named after arg[0]
*
* @param {string} folder folder to run in
* @param {Array} args function arguments
*/
async function run (folder, args) {
const template = path.join (__dirname, 'template.js');
const dest = path.join (folder, `${args[0]}.js`);
await fs.writeFile (dest, await fs.readFile (template));
}
/**
* checks if the arguments meet the requirements
*
* @param {string} folder folder to run in
* @param {Array} args function arguments
* @returns {boolean} true if arguments match requirements
*/
function assert (folder, args) {
const tests = [
{
f: () => (args.length < 2),
reason: 'too many arguments'
},
{
f: () => (args.length === 1 && typeof args[0] === 'string'),
reason: 'name is not a string'
},
{
f: () => ((/^[a-z]+-[a-z]+$/iu).test (args[0])),
reason: 'name has to match /^[a-z]+-[a-z]+$/'
},
{
f: () => (typeof folder === 'string'),
reason: 'cwd is not a folder (internal error)'
},
{
f: () => (fs.existsSync (folder)),
reason: 'cwd does not exist (internal error)'
},
{
f: () => (!fs.existsSync (path.join (folder, args[0]))),
reason: 'file already exists'
}
];
for (const test of tests)
if (!test.f ()) {
console.log (test.reason);
return false;
}
return true;
}
module.exports = { run, assert };

View File

@ -0,0 +1,8 @@
module.exports = function handler (req, res, next, opts) {
if (req.headers.example === 'abc')
res.writeHead (1);
else
res.writeHead (2);
res.end (opts.text);
};