Timo Hocker da8c39c91b
All checks were successful
continuous-integration/drone/push Build is passing
switch node template to jasmine
2020-09-24 12:08:31 +02:00

140 lines
3.1 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>, May 2020
*/
import path from 'path';
import { Input, Confirm } from 'enquirer';
import {
scripts as standard_scripts,
files as standard_files
} from '@sapphirecode/standard';
import { Snippet } from '../../Snippet';
import { apply_template, modify_json, run_command } from '../../Helper';
import {
eslintrc, gitignore,
tsconfig, eslintrc_ts, eslintignore
} from './Assets';
const packages = {
common: [ '@sapphirecode/eslint-config' ],
test: [
'nyc',
'jasmine',
'@types/jasmine'
],
test_ts: [
'jasmine-ts',
'ts-node'
],
ts: [
'typescript',
'@sapphirecode/eslint-config-ts'
]
};
/**
* initialize the package.json
*
* @param folder - folder
* @param use_ts - use typescript
* @param use_tests - use tests
*/
async function init_package (
folder: string,
use_ts: boolean,
use_tests: boolean
): Promise<void> {
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
);
await modify_json ((obj: Record<string, unknown>) => {
const scripts
= {
lint: standard_scripts.lint,
test: standard_scripts.test.common,
compile: standard_scripts.compile.common
};
const files = [ 'LICENSE' ];
if (use_ts) {
scripts.compile = standard_scripts.compile.ts;
scripts.test = standard_scripts.test.ts;
files.push ('/dist/');
}
else {
files.push ('*.js', '*.d.ts');
}
if (!use_tests)
scripts.test = standard_scripts.test.no;
obj.scripts = scripts;
obj.files = files;
return obj;
}, path.join (folder, 'package.json'));
}
export default class Node implements Snippet {
public is_active (): boolean {
return true;
}
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 ();
const use_tests = await new Confirm ({
message: 'use tests?',
initial: true
})
.run ();
await apply_template (eslintrc, path.join (folder, '.eslintrc.js'));
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);
if (use_tests) {
apply_template (
standard_files.jasmine,
path.join (folder, 'jasmine.json')
);
}
await init_package (folder, use_ts, use_tests);
}
}