complete copyright snippet

This commit is contained in:
Timo Hocker 2020-03-25 15:52:25 +01:00
parent e55aa6bb08
commit 0118737289

View File

@ -7,6 +7,7 @@
/* eslint-disable no-sync */
/* eslint-disable no-console */
/* eslint-disable no-await-in-loop */
'use strict';
@ -20,12 +21,12 @@ const path = require ('path');
* @param {Function} func function to execute on file contents
*/
async function map_all_files (folder, func) {
const files = await fs.readDir (folder);
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)
if ((await fs.stat (abs_path)).isDirectory ())
map_all_files (abs_path, func);
const data = await fs.readFile (abs_path);
@ -45,6 +46,11 @@ async function map_all_files (folder, func) {
*/
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
@ -74,7 +80,7 @@ function get_copyright_notice (license = '', software = '') {
* @param {string} software software name
*/
async function fix_all_copy (folder, license = '', software = '') {
const regex = /\/\*\s+\*\sCopyright[\s\S]*?\*\//g;
const regex = /\/\*\s+\*\sCopyright[\s\S]*?\*\//gu;
await map_all_files (folder, (data) => {
if (!regex.test (data))
return null;
@ -89,23 +95,7 @@ async function fix_all_copy (folder, license = '', software = '') {
* @param {Array} args function arguments
*/
function run (folder, args) {
const snip_folder_path = [ folder ];
if (args.length > 0)
snip_folder_path.push (args[0]);
const snip_folder = path.join (...snip_folder_path);
const template = path.join (__dirname, 'template');
if (!fs.existsSync (snip_folder))
fs.mkdir (snip_folder);
for (const f of fs.readdirSync (template)) {
fs.copy (
path.join (template, f),
path.join (snip_folder, f),
{
recursive: true,
filter: (src, dest) => !fs.existsSync (dest)
}
);
}
fix_all_copy (folder, ...args);
}
/**
@ -118,16 +108,16 @@ function run (folder, args) {
function assert (folder, args) {
const tests = [
{
f: () => (args.length < 2),
reason: 'too many arguments'
f: () => (args.length === 0 || args.length === 2),
reason: 'invalid number of arguments'
},
{
f: () => (args.length === 0 || typeof args[0] === 'string'),
reason: 'name is not a string'
reason: 'license is not a string'
},
{
f: () => (args.length === 0 || (/^[a-z]+$/iu).test (args[0])),
reason: 'name can only contain [a-z]'
f: () => (args.length === 0 || typeof args[1] === 'string'),
reason: 'software name is not a string'
},
{
f: () => (typeof folder === 'string'),
@ -136,15 +126,6 @@ function assert (folder, args) {
{
f: () => (fs.existsSync (folder)),
reason: 'cwd does not exist (internal error)'
},
{
f: () => (args.length === 1 || fs.readdirSync (folder).length === 0),
reason: 'folder is not empty'
},
{
f: () => (args.length === 0
|| !fs.existsSync (path.join (folder, args[0]))),
reason: 'folder already exists'
}
];
for (const test of tests) {