mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-23 01:58:29 +00:00
In generator-aspnetcore-spa, offer user the choice of whether to include tests
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as yeoman from 'yeoman-generator';
|
import * as yeoman from 'yeoman-generator';
|
||||||
import * as uuid from 'node-uuid';
|
import * as uuid from 'node-uuid';
|
||||||
@@ -9,15 +10,35 @@ const yosay = require('yosay');
|
|||||||
const toPascalCase = require('to-pascal-case');
|
const toPascalCase = require('to-pascal-case');
|
||||||
const isWindows = /^win/.test(process.platform);
|
const isWindows = /^win/.test(process.platform);
|
||||||
|
|
||||||
|
// Paths matching these regexes will only be included if the user wants tests
|
||||||
|
const testSpecificPaths = [
|
||||||
|
/\.spec.ts$/, // Files ending '.spec.ts'
|
||||||
|
/(^|\/|\\)test($|\/|\\)/ // Files under any directory called 'test'
|
||||||
|
];
|
||||||
|
|
||||||
|
// These NPM dependencies will only be included if the user wants tests
|
||||||
|
const testSpecificNpmPackages = [
|
||||||
|
"@types/chai",
|
||||||
|
"@types/jasmine",
|
||||||
|
"chai",
|
||||||
|
"jasmine-core",
|
||||||
|
"karma",
|
||||||
|
"karma-chai",
|
||||||
|
"karma-chrome-launcher",
|
||||||
|
"karma-cli",
|
||||||
|
"karma-jasmine",
|
||||||
|
"karma-webpack"
|
||||||
|
];
|
||||||
|
|
||||||
type YeomanPrompt = (opt: yeoman.IPromptOptions | yeoman.IPromptOptions[], callback: (answers: any) => void) => void;
|
type YeomanPrompt = (opt: yeoman.IPromptOptions | yeoman.IPromptOptions[], callback: (answers: any) => void) => void;
|
||||||
const optionOrPrompt: YeomanPrompt = require('yeoman-option-or-prompt');
|
const optionOrPrompt: YeomanPrompt = require('yeoman-option-or-prompt');
|
||||||
|
|
||||||
const templates = [
|
const templates = [
|
||||||
{ value: 'angular-2', name: 'Angular 2' },
|
{ value: 'angular-2', name: 'Angular 2', tests: true },
|
||||||
{ value: 'aurelia', name: 'Aurelia' },
|
{ value: 'aurelia', name: 'Aurelia', tests: false },
|
||||||
{ value: 'knockout', name: 'Knockout' },
|
{ value: 'knockout', name: 'Knockout', tests: false },
|
||||||
{ value: 'react', name: 'React' },
|
{ value: 'react', name: 'React', tests: false },
|
||||||
{ value: 'react-redux', name: 'React with Redux' }
|
{ value: 'react-redux', name: 'React with Redux', tests: false }
|
||||||
];
|
];
|
||||||
|
|
||||||
class MyGenerator extends yeoman.Base {
|
class MyGenerator extends yeoman.Base {
|
||||||
@@ -35,25 +56,41 @@ class MyGenerator extends yeoman.Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
prompting() {
|
prompting() {
|
||||||
const done = this.async();
|
|
||||||
|
|
||||||
this.option('projectguid');
|
this.option('projectguid');
|
||||||
|
|
||||||
|
const done = this.async();
|
||||||
this._optionOrPrompt([{
|
this._optionOrPrompt([{
|
||||||
type: 'list',
|
type: 'list',
|
||||||
name: 'framework',
|
name: 'framework',
|
||||||
message: 'Framework',
|
message: 'Framework',
|
||||||
choices: templates
|
choices: templates
|
||||||
}, {
|
}], frameworkAnswer => {
|
||||||
|
const frameworkChoice = templates.filter(t => t.value === frameworkAnswer.framework)[0];
|
||||||
|
const furtherQuestions = [{
|
||||||
type: 'input',
|
type: 'input',
|
||||||
name: 'name',
|
name: 'name',
|
||||||
message: 'Your project name',
|
message: 'Your project name',
|
||||||
default: this.appname
|
default: this.appname
|
||||||
}], answers => {
|
}];
|
||||||
|
|
||||||
|
if (frameworkChoice.tests) {
|
||||||
|
furtherQuestions.unshift({
|
||||||
|
type: 'confirm',
|
||||||
|
name: 'tests',
|
||||||
|
message: 'Do you want to include unit tests?',
|
||||||
|
default: true as any
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
this._optionOrPrompt(furtherQuestions, answers => {
|
||||||
|
answers.framework = frameworkAnswer.framework;
|
||||||
this._answers = answers;
|
this._answers = answers;
|
||||||
|
this._answers.framework = frameworkAnswer.framework;
|
||||||
this._answers.namePascalCase = toPascalCase(answers.name);
|
this._answers.namePascalCase = toPascalCase(answers.name);
|
||||||
this._answers.projectGuid = this.options['projectguid'] || uuid.v4();
|
this._answers.projectGuid = this.options['projectguid'] || uuid.v4();
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
writing() {
|
writing() {
|
||||||
@@ -79,11 +116,27 @@ class MyGenerator extends yeoman.Base {
|
|||||||
outputFn = path.join(path.dirname(fn), 'node_modules', '_placeholder.txt');
|
outputFn = path.join(path.dirname(fn), 'node_modules', '_placeholder.txt');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exclude test-specific files (unless the user has said they want tests)
|
||||||
|
const isTestSpecificFile = testSpecificPaths.some(regex => regex.test(outputFn));
|
||||||
|
if (this._answers.tests || !isTestSpecificFile) {
|
||||||
|
const inputFullPath = path.join(templateRoot, fn);
|
||||||
|
if (path.basename(fn) === 'package.json') {
|
||||||
|
// Special handling for package.json, because we rewrite it dynamically
|
||||||
|
this.fs.writeJSON(
|
||||||
|
this.destinationPath(outputFn),
|
||||||
|
rewritePackageJson(JSON.parse(fs.readFileSync(inputFullPath, 'utf8')), this._answers.tests),
|
||||||
|
/* replacer */ null,
|
||||||
|
/* space */ 2
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Regular file - copy as template
|
||||||
this.fs.copyTpl(
|
this.fs.copyTpl(
|
||||||
path.join(templateRoot, fn),
|
inputFullPath,
|
||||||
this.destinationPath(outputFn),
|
this.destinationPath(outputFn),
|
||||||
this._answers
|
this._answers
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,5 +178,34 @@ function assertNpmVersionIsAtLeast(minVersion: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function rewritePackageJson(contents, includeTests) {
|
||||||
|
if (!includeTests) {
|
||||||
|
// Delete any test-specific packages from dependencies and devDependencies
|
||||||
|
['dependencies', 'devDependencies'].forEach(dependencyListName => {
|
||||||
|
var packageList = contents[dependencyListName];
|
||||||
|
if (packageList) {
|
||||||
|
testSpecificNpmPackages.forEach(packageToRemove => {
|
||||||
|
delete packageList[packageToRemove];
|
||||||
|
});
|
||||||
|
|
||||||
|
if (Object.getOwnPropertyNames(packageList).length === 0) {
|
||||||
|
delete contents[dependencyListName];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Delete any script called 'test'
|
||||||
|
const scripts = contents.scripts;
|
||||||
|
if (scripts.test) {
|
||||||
|
delete scripts.test;
|
||||||
|
if (Object.getOwnPropertyNames(scripts).length === 0) {
|
||||||
|
delete contents.scripts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return contents;
|
||||||
|
}
|
||||||
|
|
||||||
declare var module: any;
|
declare var module: any;
|
||||||
(module).exports = MyGenerator;
|
(module).exports = MyGenerator;
|
||||||
|
|||||||
Reference in New Issue
Block a user