complete copyright snippet
This commit is contained in:
parent
e55aa6bb08
commit
0118737289
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
/* eslint-disable no-sync */
|
/* eslint-disable no-sync */
|
||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
|
/* eslint-disable no-await-in-loop */
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
@ -20,12 +21,12 @@ const path = require ('path');
|
|||||||
* @param {Function} func function to execute on file contents
|
* @param {Function} func function to execute on file contents
|
||||||
*/
|
*/
|
||||||
async function map_all_files (folder, func) {
|
async function map_all_files (folder, func) {
|
||||||
const files = await fs.readDir (folder);
|
const files = await fs.readdir (folder);
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
if ([ 'node_modules' ].includes (file))
|
if ([ 'node_modules' ].includes (file))
|
||||||
continue;
|
continue;
|
||||||
const abs_path = path.join (folder, file);
|
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);
|
map_all_files (abs_path, func);
|
||||||
|
|
||||||
const data = await fs.readFile (abs_path);
|
const data = await fs.readFile (abs_path);
|
||||||
@ -45,6 +46,11 @@ async function map_all_files (folder, func) {
|
|||||||
*/
|
*/
|
||||||
function get_copyright_notice (license = '', software = '') {
|
function get_copyright_notice (license = '', software = '') {
|
||||||
let notice = '';
|
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) {
|
if (license) {
|
||||||
notice = `${'/*'}
|
notice = `${'/*'}
|
||||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
@ -74,7 +80,7 @@ function get_copyright_notice (license = '', software = '') {
|
|||||||
* @param {string} software software name
|
* @param {string} software software name
|
||||||
*/
|
*/
|
||||||
async function fix_all_copy (folder, license = '', software = '') {
|
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) => {
|
await map_all_files (folder, (data) => {
|
||||||
if (!regex.test (data))
|
if (!regex.test (data))
|
||||||
return null;
|
return null;
|
||||||
@ -89,23 +95,7 @@ async function fix_all_copy (folder, license = '', software = '') {
|
|||||||
* @param {Array} args function arguments
|
* @param {Array} args function arguments
|
||||||
*/
|
*/
|
||||||
function run (folder, args) {
|
function run (folder, args) {
|
||||||
const snip_folder_path = [ folder ];
|
fix_all_copy (folder, ...args);
|
||||||
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)
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -118,16 +108,16 @@ function run (folder, args) {
|
|||||||
function assert (folder, args) {
|
function assert (folder, args) {
|
||||||
const tests = [
|
const tests = [
|
||||||
{
|
{
|
||||||
f: () => (args.length < 2),
|
f: () => (args.length === 0 || args.length === 2),
|
||||||
reason: 'too many arguments'
|
reason: 'invalid number of arguments'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
f: () => (args.length === 0 || typeof args[0] === 'string'),
|
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])),
|
f: () => (args.length === 0 || typeof args[1] === 'string'),
|
||||||
reason: 'name can only contain [a-z]'
|
reason: 'software name is not a string'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
f: () => (typeof folder === 'string'),
|
f: () => (typeof folder === 'string'),
|
||||||
@ -136,15 +126,6 @@ function assert (folder, args) {
|
|||||||
{
|
{
|
||||||
f: () => (fs.existsSync (folder)),
|
f: () => (fs.existsSync (folder)),
|
||||||
reason: 'cwd does not exist (internal error)'
|
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) {
|
for (const test of tests) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user