2020-05-04 12:23:06 +02:00

85 lines
2.3 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
*/
import path from 'path';
import { Input, Confirm } from 'enquirer';
import { Snippet } from '../../Snippet';
import { apply_template, modify_json, run_command } from '../../Helper';
import {
eslintrc, gitignore, npmrc,
tsconfig, eslintrc_ts, eslintignore
} from './Assets';
/**
* initialize the package.json
*
* @param {string} folder folder
*/
async function init_package (folder: string): Promise<void> {
await modify_json ((obj: Record<string, unknown>) => {
const scripts
= {
lint: 'eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs',
test: 'nyc ava',
compile: 'tsc --allowJs --declaration --emitDeclarationOnly index.js'
};
const files = [ 'LICENSE' ];
if (use_ts) {
scripts.compile = 'tsc';
scripts.test = 'tsc && nyc ava';
files.push ('/dist/');
}
else {
files.push ('*.js', '*.d.ts');
}
obj.scripts = scripts;
obj.files = files;
return obj;
}, path.join (folder, 'package.json'));
}
export default class Node implements Snippet {
public async start (): Promise<void> {
const folder = await new Input (
{ message: 'project name (leave empty for current folder):' }
)
.run ();
const use_ts = await new Confirm ({
message: 'use typescript?',
initial: false
})
.run ();
await apply_template (eslintrc, path.join (folder, '.eslintrc.js'));
await apply_template (npmrc, path.join (folder, '.npmrc'));
await apply_template (gitignore, path.join (folder, '.gitignore'));
await apply_template (
eslintignore,
path.join (folder, '.eslintignore')
);
if (use_ts) {
await apply_template (tsconfig, path.join (folder, 'tsconfig.json'));
await apply_template (
eslintrc_ts,
path.join (folder, 'lib', '.eslintrc.js')
);
}
run_command ('git init', folder);
run_command ('yarn init -y', folder);
run_command (
`yarn add --dev @scode/eslint-config${use_ts
? '-ts typescript @ava/typescript'
: ''} eslint nyc ava`,
folder
);
await init_package (folder);
}
}