mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
Workaround for Yeoman generators not producing .gitignore files due to Yeoman issue #1862
This commit is contained in:
7
.gitignore
vendored
7
.gitignore
vendored
@@ -29,3 +29,10 @@ project.lock.json
|
|||||||
.vs/
|
.vs/
|
||||||
npm-debug.log
|
npm-debug.log
|
||||||
/.build/
|
/.build/
|
||||||
|
|
||||||
|
# The templates can't contain their own .gitignore files, because Yeoman has strange default handling for
|
||||||
|
# files with that name (https://github.com/npm/npm/issues/1862). So, each template instead has a template_gitignore
|
||||||
|
# file which gets renamed after the files are copied. And so any files that need to be excluded in the source
|
||||||
|
# repo have to be excluded here.
|
||||||
|
/templates/*/node_modules/
|
||||||
|
/templates/*/wwwroot/dist/
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import * as _ from 'lodash';
|
|||||||
import * as mkdirp from 'mkdirp';
|
import * as mkdirp from 'mkdirp';
|
||||||
import * as rimraf from 'rimraf';
|
import * as rimraf from 'rimraf';
|
||||||
|
|
||||||
const textFileExtensions = ['.gitignore', '.config', '.cs', '.cshtml', 'Dockerfile', '.html', '.js', '.json', '.jsx', '.md', '.ts', '.tsx', '.xproj'];
|
const textFileExtensions = ['.gitignore', 'template_gitignore', '.config', '.cs', '.cshtml', 'Dockerfile', '.html', '.js', '.json', '.jsx', '.md', '.ts', '.tsx', '.xproj'];
|
||||||
|
|
||||||
const templates = {
|
const templates = {
|
||||||
'angular-2': '../../templates/Angular2Spa/',
|
'angular-2': '../../templates/Angular2Spa/',
|
||||||
@@ -33,12 +33,15 @@ function isTextFile(filename: string): boolean {
|
|||||||
|
|
||||||
function writeFileEnsuringDirExists(root: string, filename: string, contents: string | Buffer) {
|
function writeFileEnsuringDirExists(root: string, filename: string, contents: string | Buffer) {
|
||||||
let fullPath = path.join(root, filename);
|
let fullPath = path.join(root, filename);
|
||||||
mkdirp.sync(path.dirname(fullPath));
|
mkdirp.sync(path.dirname(fullPath));
|
||||||
fs.writeFileSync(fullPath, contents);
|
fs.writeFileSync(fullPath, contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
function listFilesExcludingGitignored(root: string): string[] {
|
function listFilesExcludingGitignored(root: string): string[] {
|
||||||
let gitIgnorePath = path.join(root, '.gitignore');
|
// Note that the gitignore files, prior to be written by the generator, are called 'template_gitignore'
|
||||||
|
// instead of '.gitignore'. This is a workaround for Yeoman doing strange stuff with .gitignore files
|
||||||
|
// (it renames them to .npmignore, which is not helpful).
|
||||||
|
let gitIgnorePath = path.join(root, 'template_gitignore');
|
||||||
let gitignoreEvaluator = fs.existsSync(gitIgnorePath)
|
let gitignoreEvaluator = fs.existsSync(gitIgnorePath)
|
||||||
? gitignore.compile(fs.readFileSync(gitIgnorePath, 'utf8'))
|
? gitignore.compile(fs.readFileSync(gitIgnorePath, 'utf8'))
|
||||||
: { accepts: () => true };
|
: { accepts: () => true };
|
||||||
@@ -49,36 +52,36 @@ function listFilesExcludingGitignored(root: string): string[] {
|
|||||||
function writeTemplate(sourceRoot: string, destRoot: string) {
|
function writeTemplate(sourceRoot: string, destRoot: string) {
|
||||||
listFilesExcludingGitignored(sourceRoot).forEach(fn => {
|
listFilesExcludingGitignored(sourceRoot).forEach(fn => {
|
||||||
let sourceContent = fs.readFileSync(path.join(sourceRoot, fn));
|
let sourceContent = fs.readFileSync(path.join(sourceRoot, fn));
|
||||||
|
|
||||||
// For text files, replace hardcoded values with template tags
|
// For text files, replace hardcoded values with template tags
|
||||||
if (isTextFile(fn)) {
|
if (isTextFile(fn)) {
|
||||||
let sourceText = sourceContent.toString('utf8');
|
let sourceText = sourceContent.toString('utf8');
|
||||||
contentReplacements.forEach(replacement => {
|
contentReplacements.forEach(replacement => {
|
||||||
sourceText = sourceText.replace(replacement.from, replacement.to);
|
sourceText = sourceText.replace(replacement.from, replacement.to);
|
||||||
});
|
});
|
||||||
|
|
||||||
sourceContent = new Buffer(sourceText, 'utf8');
|
sourceContent = new Buffer(sourceText, 'utf8');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also apply replacements in filenames
|
// Also apply replacements in filenames
|
||||||
filenameReplacements.forEach(replacement => {
|
filenameReplacements.forEach(replacement => {
|
||||||
fn = fn.replace(replacement.from, replacement.to);
|
fn = fn.replace(replacement.from, replacement.to);
|
||||||
});
|
});
|
||||||
|
|
||||||
writeFileEnsuringDirExists(destRoot, fn, sourceContent);
|
writeFileEnsuringDirExists(destRoot, fn, sourceContent);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function copyRecursive(sourceRoot: string, destRoot: string, matchGlob: string) {
|
function copyRecursive(sourceRoot: string, destRoot: string, matchGlob: string) {
|
||||||
glob.sync(matchGlob, { cwd: sourceRoot, dot: true, nodir: true })
|
glob.sync(matchGlob, { cwd: sourceRoot, dot: true, nodir: true })
|
||||||
.forEach(fn => {
|
.forEach(fn => {
|
||||||
const sourceContent = fs.readFileSync(path.join(sourceRoot, fn));
|
const sourceContent = fs.readFileSync(path.join(sourceRoot, fn));
|
||||||
writeFileEnsuringDirExists(destRoot, fn, sourceContent);
|
writeFileEnsuringDirExists(destRoot, fn, sourceContent);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const outputRoot = './generator-aspnetcore-spa';
|
const outputRoot = './generator-aspnetcore-spa';
|
||||||
const outputTemplatesRoot = path.join(outputRoot, 'app/templates');
|
const outputTemplatesRoot = path.join(outputRoot, 'app/templates');
|
||||||
rimraf.sync(outputTemplatesRoot);
|
rimraf.sync(outputTemplatesRoot);
|
||||||
|
|
||||||
// Copy template files
|
// Copy template files
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ class MyGenerator extends yeoman.Base {
|
|||||||
|
|
||||||
prompting() {
|
prompting() {
|
||||||
const done = this.async();
|
const done = this.async();
|
||||||
|
|
||||||
this.prompt([{
|
this.prompt([{
|
||||||
type: 'list',
|
type: 'list',
|
||||||
name: 'framework',
|
name: 'framework',
|
||||||
@@ -40,13 +40,18 @@ class MyGenerator extends yeoman.Base {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
writing() {
|
writing() {
|
||||||
var templateRoot = this.templatePath(this._answers.framework);
|
var templateRoot = this.templatePath(this._answers.framework);
|
||||||
glob.sync('**/*', { cwd: templateRoot, dot: true, nodir: true }).forEach(fn => {
|
glob.sync('**/*', { cwd: templateRoot, dot: true, nodir: true }).forEach(fn => {
|
||||||
// Token replacement in filenames
|
// Token replacement in filenames
|
||||||
let outputFn = fn.replace(/tokenreplace\-([^\.\/]*)/g, (substr, token) => this._answers[token]);
|
let outputFn = fn.replace(/tokenreplace\-([^\.\/]*)/g, (substr, token) => this._answers[token]);
|
||||||
|
|
||||||
|
// Rename template_gitignore to .gitignore in output
|
||||||
|
if (path.basename(fn) === 'template_gitignore') {
|
||||||
|
outputFn = path.join(path.dirname(fn), '.gitignore');
|
||||||
|
}
|
||||||
|
|
||||||
this.fs.copyTpl(
|
this.fs.copyTpl(
|
||||||
path.join(templateRoot, fn),
|
path.join(templateRoot, fn),
|
||||||
this.destinationPath(outputFn),
|
this.destinationPath(outputFn),
|
||||||
@@ -54,7 +59,7 @@ class MyGenerator extends yeoman.Base {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
installingDeps() {
|
installingDeps() {
|
||||||
this.installDependencies({
|
this.installDependencies({
|
||||||
npm: true,
|
npm: true,
|
||||||
@@ -62,7 +67,7 @@ class MyGenerator extends yeoman.Base {
|
|||||||
callback: () => {
|
callback: () => {
|
||||||
this.spawnCommandSync('dotnet', ['restore']);
|
this.spawnCommandSync('dotnet', ['restore']);
|
||||||
this.spawnCommandSync('./node_modules/.bin/webpack', ['--config', 'webpack.config.vendor.js']);
|
this.spawnCommandSync('./node_modules/.bin/webpack', ['--config', 'webpack.config.vendor.js']);
|
||||||
this.spawnCommandSync('./node_modules/.bin/webpack');
|
this.spawnCommandSync('./node_modules/.bin/webpack');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "generator-aspnetcore-spa",
|
"name": "generator-aspnetcore-spa",
|
||||||
"version": "0.1.5",
|
"version": "0.1.6",
|
||||||
"description": "Single-Page App templates for ASP.NET Core",
|
"description": "Single-Page App templates for ASP.NET Core",
|
||||||
"author": "Microsoft",
|
"author": "Microsoft",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user