typescript, interactive

This commit is contained in:
2020-04-15 20:21:00 +02:00
parent 93862eff16
commit bc06152359
36 changed files with 2730 additions and 654 deletions

View File

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

View File

@ -0,0 +1,8 @@
export class CopyrightOptions {
public has_license = false;
public license = '';
public author = '';
public company = '';
public email = '';
public software = '';
}

View File

@ -0,0 +1,27 @@
/* eslint-disable no-await-in-loop */
import path from 'path';
import fs from 'fs-extra';
export class FileMapper {
public static async map_all_files (
folder: string,
mutator: Function,
args: Array<unknown> = []
): Promise<void> {
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 ()) {
await FileMapper.map_all_files (abs_path, mutator, args);
continue;
}
const data = await fs.readFile (abs_path, 'utf-8');
const res = mutator (data, file, args);
if (res === null)
continue;
await fs.writeFile (abs_path, res, 'utf-8');
}
}
}

View File

@ -0,0 +1,87 @@
/* eslint-disable no-await-in-loop */
import path from 'path';
import fs from 'fs-extra';
import { Confirm, Input, AutoComplete } from 'enquirer';
// eslint-disable-next-line id-match
import { findLicense } from 'license';
import { Snippet } from '../../Snippet';
import { CopyrightGenerator } from './copyright_generator';
import { FileMapper } from './file_mapper';
import { CopyrightOptions } from './copyright_options';
export default class Copyright implements Snippet {
async start (cwd: string): Promise<void> {
const options = (await this.load_options_file (cwd))
|| (await this.gather_options ());
await FileMapper.map_all_files (
cwd,
Copyright.fix_file_license,
[ options ]
);
}
private async gather_options (): Promise<CopyrightOptions> {
const options = (new CopyrightOptions);
options.author = await new Input ({ message: 'author' })
.run ();
options.email = await new Input ({ message: 'email' })
.run ();
options.company = await new Input ({ message: 'company' })
.run ();
options.software = await new Input ({ message: 'software name' })
.run ();
options.has_license = await new Confirm ({
message:
'would you like to specify a license?'
})
.run ();
if (options.has_license) {
options.license = await new AutoComplete ({
name: 'license',
message: 'choose a license',
limit: 10,
choices: findLicense ('')
})
.run ();
}
return options;
}
private async load_options_file
(folder: string): Promise<CopyrightOptions|null> {
const file_path = path.join (folder, '.liconfig.json');
if (await fs.pathExists (file_path)) {
return JSON.parse (
await fs.readFile (file_path, 'utf-8')
);
}
return null;
}
private async save_options_file
(folder: string, options: CopyrightOptions): Promise<void> {
const file_path = path.join (folder, '.liconfig.json');
await fs.writeFile (file_path, JSON.stringify (options, null, 2), 'utf-8');
}
private static fix_file_license (
data: string,
filename: string,
[ options ]: [CopyrightOptions]
): string | null {
const regex = /\/\*\s+\*\sCopyright[\s\S]*?\*\/\n{0,2}/gu;
const shebang = /^#!.*?\n\n/gu;
const shebang_line = shebang.exec (data);
if (!(/\.(?:js|ts|mjs)$/u).test (filename) && !regex.test (data))
return null;
return (shebang_line ? shebang_line[0] : '')
+ CopyrightGenerator.get_copyright_notice (options)
+ data.replace (regex, '')
.replace (shebang, '');
}
}