carry previous date string

This commit is contained in:
Timo Hocker 2020-06-27 19:36:35 +02:00
parent 6ff99c827d
commit d7d30fd417
2 changed files with 37 additions and 32 deletions

View File

@ -9,13 +9,18 @@ import { CopyrightOptions } from './copyright_options';
export class CopyrightGenerator { export class CopyrightGenerator {
public static get_copyright_notice ( public static get_copyright_notice (
opt: CopyrightOptions opt: CopyrightOptions,
date_str?: string
): string { ): string {
let notice = ''; let notice = '';
const date = (new Date); let date_string = date_str;
const dtf = new Intl.DateTimeFormat ('en', { month: 'long' }); if (typeof date_str === 'undefined') {
const year = date.getFullYear (); const date = (new Date);
const month = dtf.format (date); const dtf = new Intl.DateTimeFormat ('en', { month: 'long' });
const year = date.getFullYear ();
const month = dtf.format (date);
date_string = `${month} ${year}`;
}
if (opt.has_license) { if (opt.has_license) {
notice = `${'/*'} notice = `${'/*'}
@ -23,7 +28,7 @@ export class CopyrightGenerator {
* This file is part of ${opt.software} which is released under ${ * This file is part of ${opt.software} which is released under ${
opt.license}. opt.license}.
* See file 'LICENSE' for full license details. * See file 'LICENSE' for full license details.
* Created by ${opt.author} <${opt.email}>, ${month} ${year} * Created by ${opt.author} <${opt.email}>, ${date_string}
*/ */
`; `;
@ -31,7 +36,7 @@ export class CopyrightGenerator {
else { else {
notice = `${'/*'} notice = `${'/*'}
* Copyright (C) ${opt.company || opt.author} - All Rights Reserved * Copyright (C) ${opt.company || opt.author} - All Rights Reserved
* Created by ${opt.author} <${opt.email}>, ${month} ${year} * Created by ${opt.author} <${opt.email}>, ${date_string}
*/ */
`; `;

View File

@ -42,9 +42,7 @@ export default class Copyright implements Snippet {
await modify_json ((json) => { await modify_json ((json) => {
json.author = `${options.author} <${options.email}>`; json.author = `${options.author} <${options.email}>`;
json.license = options.has_license json.license = options.has_license ? options.license : 'UNLICENSED';
? options.license
: 'UNLICENSED';
return json; return json;
}); });
@ -59,11 +57,14 @@ export default class Copyright implements Snippet {
); );
} }
if (!this._loaded_from_config && await new Confirm ( if (
{ message: 'should those settings be saved for the next run?' } !this._loaded_from_config
&& (await new Confirm (
{ message: 'should those settings be saved for the next run?' }
)
.run ()
.catch (DialogHandler.catch))
) )
.run ()
.catch (DialogHandler.catch))
this.save_options_file (); this.save_options_file ();
} }
@ -81,10 +82,9 @@ export default class Copyright implements Snippet {
this._options.software = await new Input ({ message: 'software name' }) this._options.software = await new Input ({ message: 'software name' })
.run () .run ()
.catch (DialogHandler.catch); .catch (DialogHandler.catch);
this._options.has_license = await new Confirm ({ this._options.has_license = await new Confirm (
message: { message: 'would you like to specify a license?' }
'would you like to specify a license?' )
})
.run () .run ()
.catch (DialogHandler.catch); .catch (DialogHandler.catch);
if (this._options.has_license) { if (this._options.has_license) {
@ -104,9 +104,7 @@ export default class Copyright implements Snippet {
this._options = null; this._options = null;
if (await fs.pathExists (file_path)) { if (await fs.pathExists (file_path)) {
const options = JSON.parse ( const options = JSON.parse (await fs.readFile (file_path, 'utf-8'));
await fs.readFile (file_path, 'utf-8')
);
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log (`author: ${options.author} console.log (`author: ${options.author}
@ -135,21 +133,23 @@ license: ${options.license}`);
); );
} }
private fix_file_license ( private fix_file_license (data: string, filename: string): string | null {
data: string, // eslint-disable-next-line max-len
filename: string const regex = /\/\*\s+\*\sCopyright[\s\S]*?(?:Created by.*?, (?<date>[a-z]+ [0-9]+)[\s\S]*?|\s)\*\/\n{0,2}/gu;
): string | null {
const regex = /\/\*\s+\*\sCopyright[\s\S]*?\*\/\n{0,2}/gu;
const shebang = /^#!.*?\n\n/gu; const shebang = /^#!.*?\n\n/gu;
const shebang_line = shebang.exec (data); const shebang_line = shebang.exec (data);
if (!(/\.(?:js|ts|mjs)$/u).test (filename) && !regex.test (data)) if (!(/\.(?:js|ts|mjs)$/u).test (filename) && !regex.test (data))
return null; return null;
return (shebang_line ? shebang_line[0] : '') const res = regex.exec (data);
+ CopyrightGenerator.get_copyright_notice ( return (
this._options as CopyrightOptions (shebang_line ? shebang_line[0] : '')
) + CopyrightGenerator.get_copyright_notice (
+ data.replace (regex, '') this._options as CopyrightOptions,
.replace (shebang, ''); res?.groups?.date
)
+ data.replace (regex, '')
.replace (shebang, '')
);
} }
} }