79 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-01-18 21:14:40 +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
*/
2020-01-16 20:35:24 +01:00
/* eslint-disable no-sync */
/* eslint-disable no-console */
2020-02-20 10:57:22 +01:00
'use strict';
2020-01-16 20:35:24 +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
*/
function run (folder, args) {
2020-03-04 12:59:16 +01:00
const is_node = args.length === 1 && (/^node$/ui).test (args[0]);
2020-01-16 20:35:24 +01:00
const template = path.join (
__dirname,
'template',
2020-03-04 12:59:16 +01:00
is_node
2020-01-16 20:35:24 +01:00
? 'node'
: 'general'
);
2020-02-20 10:47:38 +01:00
for (const f of fs.readdirSync (template)) {
2020-01-16 20:35:24 +01:00
fs.copy (
path.join (template, f),
path.join (folder, f),
{ filter: (src, dest) => !fs.existsSync (dest) }
);
2020-02-20 10:47:38 +01:00
}
2020-03-04 12:59:16 +01:00
if (is_node) {
const pkg = path.join (folder, 'package.json');
if (fs.existsSync (pkg)) {
const json = JSON.parse (fs.readFileSync (pkg, 'utf-8'));
json.scripts.ci = 'yarn && node jenkins.js';
fs.writeFileSync (pkg, JSON.stringify (json, null, 2), 'utf-8');
}
}
2020-01-16 20:35:24 +01:00
}
/**
* checks if the arguments meet the requirements
*
* @param {string} folder folder to run in
* @returns {boolean} true if arguments match requirements
*/
function assert (folder) {
const tests = [
{
f: () => (typeof folder === 'string'),
reason: 'cwd is not a folder (internal error)'
},
{
f: () => (fs.existsSync (folder)),
reason: 'cwd does not exist (internal error)'
}
];
2020-02-20 10:47:38 +01:00
for (const test of tests) {
2020-01-16 20:35:24 +01:00
if (!test.f ()) {
console.log (test.reason);
return false;
}
2020-02-20 10:47:38 +01:00
}
2020-01-16 20:35:24 +01:00
return true;
}
module.exports = { run, assert };