149 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-03-25 13:14:59 +01:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
2020-03-25 16:07:54 +01:00
* This file is part of Snippeteer which is released under BSD-3-Clause.
2020-03-25 13:14:59 +01:00
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, March 2020
*/
2020-03-25 16:07:54 +01:00
2020-03-25 13:14:59 +01:00
/* eslint-disable no-sync */
/* eslint-disable no-console */
2020-03-25 15:52:25 +01:00
/* eslint-disable no-await-in-loop */
2020-03-25 13:14:59 +01:00
'use strict';
const fs = require ('fs-extra');
const path = require ('path');
/**
2020-03-25 13:37:27 +01:00
* scan all files and execute a mutation on them
*
2020-03-25 13:14:59 +01:00
* @param {string} folder folder to scan
* @param {Function} func function to execute on file contents
*/
async function map_all_files (folder, func) {
2020-03-25 15:52:25 +01:00
const files = await fs.readdir (folder);
2020-03-25 13:14:59 +01:00
for (const file of files) {
if ([ 'node_modules' ].includes (file))
continue;
const abs_path = path.join (folder, file);
2020-03-25 16:07:54 +01:00
if ((await fs.stat (abs_path)).isDirectory ()) {
2020-03-25 13:14:59 +01:00
map_all_files (abs_path, func);
2020-03-25 16:07:54 +01:00
continue;
}
const data = await fs.readFile (abs_path, 'utf-8');
const res = func (data, file);
2020-03-25 13:14:59 +01:00
if (res === null)
continue;
2020-03-25 16:07:54 +01:00
await fs.writeFile (abs_path, res, 'utf-8');
2020-03-25 13:14:59 +01:00
}
}
/**
2020-03-25 13:37:27 +01:00
* returns a copyright notice
*
2020-03-25 13:14:59 +01:00
* @param {string} license license name
* @param {string} software software name
* @returns {string} copyright notice
*/
function get_copyright_notice (license = '', software = '') {
let notice = '';
2020-03-25 15:52:25 +01:00
const date = (new Date);
const dtf = new Intl.DateTimeFormat ('en', { month: 'long' });
const year = date.getFullYear ();
const month = dtf.format (date);
2020-03-25 13:14:59 +01:00
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;
}
/**
2020-03-25 13:37:27 +01:00
* scans a folder and fixes all copyright notices
*
* @param {string} folder folder to scan
* @param {string} license license name
* @param {string} software software name
2020-03-25 13:14:59 +01:00
*/
2020-03-25 13:37:27 +01:00
async function fix_all_copy (folder, license = '', software = '') {
2020-03-25 16:07:54 +01:00
const regex = /\/\*\s+\*\sCopyright[\s\S]*?\*\/\n{0,2}/gu;
await map_all_files (folder, (data, filename) => {
const shebang = /^#!.*?\n\n/gu;
2020-03-25 16:50:05 +01:00
const shebang_line = shebang.exec (data);
if (!(/\.js$/u).test (filename) && !regex.test (data))
2020-03-25 13:14:59 +01:00
return null;
2020-03-25 16:50:05 +01:00
return (shebang_line ? shebang_line[0] : '')
+ get_copyright_notice (license, software)
+ data.replace (regex, '')
.replace (shebang, '');
2020-03-25 13:14:59 +01:00
});
}
/**
* 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-25 15:52:25 +01:00
fix_all_copy (folder, ...args);
2020-03-25 13:14:59 +01:00
}
/**
* 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 = [
{
2020-03-25 15:52:25 +01:00
f: () => (args.length === 0 || args.length === 2),
reason: 'invalid number of arguments'
2020-03-25 13:14:59 +01:00
},
{
f: () => (args.length === 0 || typeof args[0] === 'string'),
2020-03-25 15:52:25 +01:00
reason: 'license is not a string'
2020-03-25 13:14:59 +01:00
},
{
2020-03-25 15:52:25 +01:00
f: () => (args.length === 0 || typeof args[1] === 'string'),
reason: 'software name is not a string'
2020-03-25 13:14:59 +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)'
}
];
for (const test of tests) {
if (!test.f ()) {
console.log (test.reason);
return false;
}
}
return true;
}
module.exports = { run, assert };