140 lines
3.1 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-09-24 12:08:31 +02:00
import {
scripts as standard_scripts,
files as standard_files
2020-09-24 08:11:44 +02:00
} 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-09-24 12:08:31 +02:00
const packages = {
common: [ '@sapphirecode/eslint-config' ],
test: [
'nyc',
'jasmine',
'@types/jasmine'
],
test_ts: [
'jasmine-ts',
'ts-node'
],
ts: [
'typescript',
'@sapphirecode/eslint-config-ts'
]
};
2020-05-04 12:23:06 +02:00
/**
* initialize the package.json
*
2020-05-08 09:58:49 +02:00
* @param folder - folder
* @param use_ts - use typescript
* @param 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-09-24 12:08:31 +02:00
run_command ('yarn init -y', folder);
const bundle = [
...(use_ts ? packages.ts : packages.common),
...(use_tests ? packages.test : []),
...(use_ts && use_tests ? packages.test_ts : [])
];
run_command (
`yarn add --dev ${bundle.join (' ')}`,
folder
);
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-05-11 12:01:39 +02:00
public is_active (): boolean {
return true;
}
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);
2020-09-24 12:08:31 +02:00
if (use_tests) {
apply_template (
standard_files.jasmine,
path.join (folder, 'jasmine.json')
);
}
2020-04-18 17:24:41 +02:00
2020-05-04 12:31:38 +02:00
await init_package (folder, use_ts, use_tests);
2020-04-18 17:24:41 +02:00
}
}