115 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-04-16 07:45:48 +02:00
/*
* 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
*/
2020-04-15 20:21:00 +02:00
/* 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';
2020-04-16 08:02:35 +02:00
import { DialogHandler } from '../../dialog';
2020-04-15 20:21:00 +02:00
import { CopyrightGenerator } from './copyright_generator';
import { FileMapper } from './file_mapper';
import { CopyrightOptions } from './copyright_options';
export default class Copyright implements Snippet {
2020-04-16 07:31:08 +02:00
private options: CopyrightOptions | null = null;
private cwd: string = '';
private loaded_from_config: boolean = false;
2020-04-16 07:31:08 +02:00
2020-04-15 20:21:00 +02:00
async start (cwd: string): Promise<void> {
2020-04-16 07:31:08 +02:00
this.cwd = cwd;
await this.load_options_file();
if (!this.options)
await this.gather_options ();
2020-04-15 20:21:00 +02:00
await FileMapper.map_all_files (
2020-04-16 07:31:08 +02:00
this.cwd,
this.fix_file_license.bind(this)
2020-04-15 20:21:00 +02:00
);
2020-04-16 07:31:08 +02:00
if (!loaded_from_config && await new Confirm({
2020-04-16 07:31:08 +02:00
message: 'should those settings be saved for the next run?'
2020-04-16 08:00:53 +02:00
}).run().catch(DialogHandler.catch)) {
2020-04-16 07:31:08 +02:00
this.save_options_file();
}
2020-04-15 20:21:00 +02:00
}
2020-04-16 07:31:08 +02:00
private async gather_options (): Promise<void> {
this.options = (new CopyrightOptions);
this.options.author = await new Input ({ message: 'author' })
2020-04-16 08:00:53 +02:00
.run ().catch(DialogHandler.catch);
2020-04-16 07:31:08 +02:00
this.options.email = await new Input ({ message: 'email' })
2020-04-16 08:00:53 +02:00
.run ().catch(DialogHandler.catch);
2020-04-16 07:31:08 +02:00
this.options.company = await new Input ({ message: 'company' })
2020-04-16 08:00:53 +02:00
.run ().catch(DialogHandler.catch);
2020-04-16 07:31:08 +02:00
this.options.software = await new Input ({ message: 'software name' })
2020-04-16 08:00:53 +02:00
.run ().catch(DialogHandler.catch);
2020-04-16 07:31:08 +02:00
this.options.has_license = await new Confirm ({
2020-04-15 20:21:00 +02:00
message:
'would you like to specify a license?'
})
2020-04-16 08:00:53 +02:00
.run ().catch(DialogHandler.catch);
2020-04-16 07:31:08 +02:00
if (this.options.has_license) {
this.options.license = await new AutoComplete ({
2020-04-15 20:21:00 +02:00
name: 'license',
message: 'choose a license',
limit: 10,
choices: findLicense ('')
})
2020-04-16 08:00:53 +02:00
.run ().catch(DialogHandler.catch);
2020-04-15 20:21:00 +02:00
}
}
2020-04-16 07:31:08 +02:00
private async load_options_file (): Promise<void> {
const file_path = path.join (this.cwd, '.liconfig.json');
2020-04-16 08:00:53 +02:00
this.options = null;
2020-04-15 20:21:00 +02:00
if (await fs.pathExists (file_path)) {
2020-04-16 08:00:53 +02:00
const options = JSON.parse (
2020-04-15 20:21:00 +02:00
await fs.readFile (file_path, 'utf-8')
);
2020-04-16 08:00:53 +02:00
// eslint-disable-next-line no-console
console.log(`author: ${options.author}
email: ${options.email}
company: ${options.company}
software name: ${options.software}
license: ${options.license}`);
const should_load = await new Confirm({
message: 'should those options be used?'
}).run().catch(DialogHandler.catch);
if (should_load){
2020-04-16 08:00:53 +02:00
this.options = options;
this.loaded_from_config = true;
}
2020-04-15 20:21:00 +02:00
}
}
2020-04-16 07:31:08 +02:00
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');
2020-04-15 20:21:00 +02:00
}
2020-04-16 07:31:08 +02:00
private fix_file_license (
2020-04-15 20:21:00 +02:00
data: string,
2020-04-16 07:31:08 +02:00
filename: string
2020-04-15 20:21:00 +02:00
): 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] : '')
2020-04-16 07:31:08 +02:00
+ CopyrightGenerator.get_copyright_notice (this.options as CopyrightOptions)
2020-04-15 20:21:00 +02:00
+ data.replace (regex, '')
.replace (shebang, '');
}
}