102 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-04-18 17:24:41 +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.
2020-05-07 18:40:35 +02:00
* Created by Timo Hocker <timo@scode.ovh>, May 2020
2020-04-18 17:24:41 +02:00
*/
import path from 'path';
2020-04-23 17:51:19 +02:00
import { Input, Confirm } from 'enquirer';
2020-05-06 08:36:48 +02:00
import { scripts as standard_scripts } from '@sapphirecode/standard';
2020-04-18 17:24:41 +02:00
import { Snippet } from '../../Snippet';
import { apply_template, modify_json, run_command } from '../../Helper';
2020-04-23 17:58:04 +02:00
import {
2020-05-07 18:40:35 +02:00
eslintrc, gitignore,
2020-05-04 12:23:06 +02:00
tsconfig, eslintrc_ts, eslintignore
2020-04-23 17:58:04 +02:00
} from './Assets';
2020-04-18 17:24:41 +02:00
2020-05-04 12:23:06 +02:00
/**
* initialize the package.json
*
* @param {string} folder folder
2020-05-04 12:31:38 +02:00
* @param {boolean} use_ts use_ts
* @param {boolean} use_tests use_tests
2020-05-04 12:23:06 +02:00
*/
2020-05-04 12:31:38 +02:00
async function init_package (
folder: string,
use_ts: boolean,
use_tests: boolean
): Promise<void> {
2020-05-04 12:23:06 +02:00
await modify_json ((obj: Record<string, unknown>) => {
const scripts
= {
2020-05-04 12:31:38 +02:00
lint: standard_scripts.lint,
test: standard_scripts.test.common,
compile: standard_scripts.compile.common
2020-05-04 12:23:06 +02:00
};
const files = [ 'LICENSE' ];
2020-05-04 12:31:38 +02:00
2020-05-04 12:23:06 +02:00
if (use_ts) {
2020-05-04 12:31:38 +02:00
scripts.compile = standard_scripts.compile.ts;
scripts.test = standard_scripts.test.ts;
2020-05-04 12:23:06 +02:00
files.push ('/dist/');
}
else {
files.push ('*.js', '*.d.ts');
}
2020-05-04 12:31:38 +02:00
if (!use_tests)
scripts.test = standard_scripts.test.no;
2020-05-04 12:23:06 +02:00
obj.scripts = scripts;
obj.files = files;
return obj;
}, path.join (folder, 'package.json'));
}
2020-04-18 19:00:27 +02:00
export default class Node implements Snippet {
2020-04-18 17:24:41 +02:00
public async start (): Promise<void> {
const folder = await new Input (
{ message: 'project name (leave empty for current folder):' }
)
.run ();
2020-04-23 17:51:19 +02:00
const use_ts = await new Confirm ({
2020-04-23 16:19:31 +02:00
message: 'use typescript?',
initial: false
})
.run ();
2020-05-04 12:31:38 +02:00
const use_tests = await new Confirm ({
message: 'use tests?',
initial: true
})
.run ();
2020-04-18 17:24:41 +02:00
await apply_template (eslintrc, path.join (folder, '.eslintrc.js'));
await apply_template (gitignore, path.join (folder, '.gitignore'));
2020-05-04 12:23:06 +02:00
await apply_template (
eslintignore,
path.join (folder, '.eslintignore')
);
2020-04-23 17:30:12 +02:00
if (use_ts) {
2020-04-23 16:19:31 +02:00
await apply_template (tsconfig, path.join (folder, 'tsconfig.json'));
2020-04-23 17:30:12 +02:00
await apply_template (
eslintrc_ts,
path.join (folder, 'lib', '.eslintrc.js')
);
}
2020-04-18 19:00:27 +02:00
run_command ('git init', folder);
run_command ('yarn init -y', folder);
run_command (
2020-05-07 18:40:35 +02:00
`yarn add --dev @sapphirecode/eslint-config${use_ts
2020-04-23 16:19:31 +02:00
? '-ts typescript @ava/typescript'
: ''} eslint nyc ava`,
2020-04-18 17:24:41 +02:00
folder
);
2020-05-04 12:31:38 +02:00
await init_package (folder, use_ts, use_tests);
2020-04-18 17:24:41 +02:00
}
}