77 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-01-20 09:12:01 +01:00
/*
* 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 */
2020-02-20 10:57:22 +01:00
'use strict';
2020-01-20 09:12:01 +01:00
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) {
2020-02-26 20:52:39 +01:00
const template = path.join (
__dirname,
args[0] === 'csv' ? 'csv.template.js' : 'template.js'
);
const target_name = args[0] === 'csv' ? '000001_csv_ingress' : args[0];
const dest = path.join (folder, `${target_name}.js`);
2020-01-20 09:12:01 +01:00
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'
},
{
2020-03-09 10:11:38 +01:00
f: () => ((/^(?:[0-9]+[-_][a-z-_]+|csv)$/iu).test (args[0])),
reason: 'name has to match /^[0-9]+[-_][a-z-_]+$/'
2020-01-20 09:12:01 +01:00
},
{
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: 'migration already exists'
}
];
2020-02-20 10:47:38 +01:00
for (const test of tests) {
2020-01-20 09:12:01 +01:00
if (!test.f ()) {
console.log (test.reason);
return false;
}
2020-02-20 10:47:38 +01:00
}
2020-01-20 09:12:01 +01:00
return true;
}
module.exports = { run, assert };