New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Listing files. #67
Comments
Here is an example of what I am doing now (prompts, etc. not included). This code decides whether to copy over the // All template filenames are prefixed with at least one underscore so that they
// are ignored by tools that care about special files. We remove or replace the
// prefix when we are ready to give the file special meaning.
const unprefix = (name) => {
const hiddenFilePrefix = '__';
return name.startsWith(hiddenFilePrefix) ?
name.replace(hiddenFilePrefix, '.') :
name.substring('_'.length);
};
module.exports = class extends Generator {
writing() {
const templates = [this.sourceRoot()];
if (!this.props.cli) {
templates.push('!**/_cli.js');
}
this.fs.copyTpl(
templates,
this.destinationRoot(),
this.props
);
const filesToMove = [
'__editorconfig',
'__gitattributes',
'__gitignore',
'_CONTRIBUTING.md',
'_LICENSE',
'_README.md',
'_circle.yml',
'_index.js',
'_package.json',
'_test.js'
];
if (this.props.cli) {
filesToMove.push('_cli.js');
}
filesToMove.forEach((name) => {
this.fs.move(
this.destinationPath(name),
this.destinationPath(unprefix(name))
);
});
}
}; |
Looping through the files is the correct approach and what we should be doing in The current implementation is quite naive in which it does only match files and bypass folder levels operations. |
Oh also, it is currently possible to loop over the in-memory files https://github.com/SBoudrias/mem-fs#iterating-over-the-file-system And I just commited a fix to master for copy and delete operations not working on in-memory directories. I'll be closing this issue as I believe both these things covers/fix the issue and the question you had. |
@SBoudrias I don't think we're on the same page. I'll try to explain my feature request better. Assume I have a very basic Yeoman generator that has some templates for The problem I am trying to solve is:
|
For this use case, I think registering a transform stream with |
Hmm okay that is a solid step in the right direction! I got pretty close with this: this.registerTransformStream(new stream.Transform({
objectMode : true,
transform(file, encoding, callback) {
const repaired = path.join(file.dirname, unprefix(file.basename));
file.path = repaired;
callback(null, file);
}
})); The trouble is, I unexpectedly receive Solutions I can think of:
Semantically speaking, I want a transform stream like this but only for my templates. Also, should I be cloning the files in the stream? Not sure if it matters. |
Oh, no, solution Even Here is solution this.registerTransformStream(new stream.Transform({
objectMode : true,
transform(file, encoding, callback) {
// Only process template files.
if (file.basename.startsWith('_')) {
file.path = path.join(file.dirname, unprefix(file.basename));
}
callback(null, file);
}
})); |
I think registerTransformStream can't be used in many cases because of yeoman/generator#819 |
Code that moves files individually is unfortunate. I would prefer to avoid hardcoding filenames into my generator.
One thing that would help is if I could programmatically get a list of the filenames within the
destinationRoot()
directory. Then I can have conventions around how files get renamed and do it in a loop based on whatever files were actually copied over. E.g. all filenames with a given prefix have that prefix removed.In the generator, it would look something like this:
The text was updated successfully, but these errors were encountered: