save settings

This commit is contained in:
Timo Hocker 2020-04-16 07:31:08 +02:00
parent 90515c1679
commit 2094287af8

View File

@ -10,34 +10,44 @@ import { FileMapper } from './file_mapper';
import { CopyrightOptions } from './copyright_options'; import { CopyrightOptions } from './copyright_options';
export default class Copyright implements Snippet { export default class Copyright implements Snippet {
private options: CopyrightOptions | null = null;
private cwd: string = '';
async start (cwd: string): Promise<void> { async start (cwd: string): Promise<void> {
const options = (await this.load_options_file (cwd)) this.cwd = cwd;
|| (await this.gather_options ()); await this.load_options_file();
if (!this.options)
await this.gather_options ();
await FileMapper.map_all_files ( await FileMapper.map_all_files (
cwd, this.cwd,
Copyright.fix_file_license, this.fix_file_license.bind(this)
[ options ]
); );
if (await new Confirm({
message: 'should those settings be saved for the next run?'
}).run()) {
this.save_options_file();
}
} }
private async gather_options (): Promise<CopyrightOptions> { private async gather_options (): Promise<void> {
const options = (new CopyrightOptions); this.options = (new CopyrightOptions);
options.author = await new Input ({ message: 'author' }) this.options.author = await new Input ({ message: 'author' })
.run (); .run ();
options.email = await new Input ({ message: 'email' }) this.options.email = await new Input ({ message: 'email' })
.run (); .run ();
options.company = await new Input ({ message: 'company' }) this.options.company = await new Input ({ message: 'company' })
.run (); .run ();
options.software = await new Input ({ message: 'software name' }) this.options.software = await new Input ({ message: 'software name' })
.run (); .run ();
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 ();
if (options.has_license) { if (this.options.has_license) {
options.license = await new AutoComplete ({ this.options.license = await new AutoComplete ({
name: 'license', name: 'license',
message: 'choose a license', message: 'choose a license',
limit: 10, limit: 10,
@ -45,33 +55,28 @@ export default class Copyright implements Snippet {
}) })
.run (); .run ();
} }
return options;
} }
private async load_options_file private async load_options_file (): Promise<void> {
(folder: string): Promise<CopyrightOptions|null> { const file_path = path.join (this.cwd, '.liconfig.json');
const file_path = path.join (folder, '.liconfig.json');
if (await fs.pathExists (file_path)) { if (await fs.pathExists (file_path)) {
return JSON.parse ( this.options = JSON.parse (
await fs.readFile (file_path, 'utf-8') await fs.readFile (file_path, 'utf-8')
); );
} }
return null; this.options = null;
} }
private async save_options_file private async save_options_file(): Promise<void> {
(folder: string, options: CopyrightOptions): Promise<void> { const file_path = path.join (this.cwd, '.liconfig.json');
const file_path = path.join (folder, '.liconfig.json'); await fs.writeFile (file_path, JSON.stringify (this.options, null, 2), 'utf-8');
await fs.writeFile (file_path, JSON.stringify (options, null, 2), 'utf-8');
} }
private static fix_file_license ( private fix_file_license (
data: string, data: string,
filename: string, filename: string
[ options ]: [CopyrightOptions]
): string | null { ): string | null {
const regex = /\/\*\s+\*\sCopyright[\s\S]*?\*\/\n{0,2}/gu; const regex = /\/\*\s+\*\sCopyright[\s\S]*?\*\/\n{0,2}/gu;
const shebang = /^#!.*?\n\n/gu; const shebang = /^#!.*?\n\n/gu;
@ -80,7 +85,7 @@ export default class Copyright implements Snippet {
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] : '') return (shebang_line ? shebang_line[0] : '')
+ CopyrightGenerator.get_copyright_notice (options) + CopyrightGenerator.get_copyright_notice (this.options as CopyrightOptions)
+ data.replace (regex, '') + data.replace (regex, '')
.replace (shebang, ''); .replace (shebang, '');
} }