2020-03-25 15:52:25 +01:00

142 lines
3.6 KiB
JavaScript

/*
* 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>, March 2020
*/
/* eslint-disable no-sync */
/* eslint-disable no-console */
/* eslint-disable no-await-in-loop */
'use strict';
const fs = require ('fs-extra');
const path = require ('path');
/**
* scan all files and execute a mutation on them
*
* @param {string} folder folder to scan
* @param {Function} func function to execute on file contents
*/
async function map_all_files (folder, func) {
const files = await fs.readdir (folder);
for (const file of files) {
if ([ 'node_modules' ].includes (file))
continue;
const abs_path = path.join (folder, file);
if ((await fs.stat (abs_path)).isDirectory ())
map_all_files (abs_path, func);
const data = await fs.readFile (abs_path);
const res = func (data);
if (res === null)
continue;
await fs.writeFile (abs_path, res);
}
}
/**
* returns a copyright notice
*
* @param {string} license license name
* @param {string} software software name
* @returns {string} copyright notice
*/
function get_copyright_notice (license = '', software = '') {
let notice = '';
const date = (new Date);
const dtf = new Intl.DateTimeFormat ('en', { month: 'long' });
const year = date.getFullYear ();
const month = dtf.format (date);
if (license) {
notice = `${'/*'}
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of ${software} which is released under ${license}.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, ${month} ${year}
*/
`;
}
else {
notice = `${'/*'}
* Copyright (C) Sapphirecode - All Rights Reserved
* Created by Timo Hocker <timo@scode.ovh>, ${month} ${year}
*/
`;
}
return notice;
}
/**
* scans a folder and fixes all copyright notices
*
* @param {string} folder folder to scan
* @param {string} license license name
* @param {string} software software name
*/
async function fix_all_copy (folder, license = '', software = '') {
const regex = /\/\*\s+\*\sCopyright[\s\S]*?\*\//gu;
await map_all_files (folder, (data) => {
if (!regex.test (data))
return null;
return get_copyright_notice (license, software) + data.replace (regex, '');
});
}
/**
* 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) {
fix_all_copy (folder, ...args);
}
/**
* 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 === 0 || args.length === 2),
reason: 'invalid number of arguments'
},
{
f: () => (args.length === 0 || typeof args[0] === 'string'),
reason: 'license is not a string'
},
{
f: () => (args.length === 0 || typeof args[1] === 'string'),
reason: 'software name is not a string'
},
{
f: () => (typeof folder === 'string'),
reason: 'cwd is not a folder (internal error)'
},
{
f: () => (fs.existsSync (folder)),
reason: 'cwd does not exist (internal error)'
}
];
for (const test of tests) {
if (!test.f ()) {
console.log (test.reason);
return false;
}
}
return true;
}
module.exports = { run, assert };