100 lines
3.0 KiB
TypeScript

/*
* Copyright (C) SapphireCode - All Rights Reserved
* This file is part of Snippeteer which is released under BSD-3-Clause.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, April 2020
*/
/* 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 {
private options: CopyrightOptions | null = null;
private cwd: string = '';
async start (cwd: string): Promise<void> {
this.cwd = cwd;
await this.load_options_file();
if (!this.options)
await this.gather_options ();
await FileMapper.map_all_files (
this.cwd,
this.fix_file_license.bind(this)
);
if (await new Confirm({
message: 'should those settings be saved for the next run?'
}).run()) {
this.save_options_file();
}
}
private async gather_options (): Promise<void> {
this.options = (new CopyrightOptions);
this.options.author = await new Input ({ message: 'author' })
.run ();
this.options.email = await new Input ({ message: 'email' })
.run ();
this.options.company = await new Input ({ message: 'company' })
.run ();
this.options.software = await new Input ({ message: 'software name' })
.run ();
this.options.has_license = await new Confirm ({
message:
'would you like to specify a license?'
})
.run ();
if (this.options.has_license) {
this.options.license = await new AutoComplete ({
name: 'license',
message: 'choose a license',
limit: 10,
choices: findLicense ('')
})
.run ();
}
}
private async load_options_file (): Promise<void> {
const file_path = path.join (this.cwd, '.liconfig.json');
if (await fs.pathExists (file_path)) {
this.options = JSON.parse (
await fs.readFile (file_path, 'utf-8')
);
}
this.options = null;
}
private async save_options_file(): Promise<void> {
const file_path = path.join (this.cwd, '.liconfig.json');
await fs.writeFile (file_path, JSON.stringify (this.options, null, 2), 'utf-8');
}
private fix_file_license (
data: string,
filename: string
): 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 (this.options as CopyrightOptions)
+ data.replace (regex, '')
.replace (shebang, '');
}
}