Skip to content
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

Trying to replace text in files but they are not copied yet. #1263

Closed
y-chen opened this issue Jan 12, 2021 · 1 comment
Closed

Trying to replace text in files but they are not copied yet. #1263

y-chen opened this issue Jan 12, 2021 · 1 comment

Comments

@y-chen
Copy link

y-chen commented Jan 12, 2021

I'm writing a generator for a personalised angular template. On init I ask for the author name, project name and selector because I've some placeholder in the template where I want to replace them with the input value. My generator code looks like this:

  writing() {
    this.fs.copy(this.templatePath("**/*"), this.destinationPath());

    const { author, appName, selector } = this.props;
    const capitalAppName = Case.capital(appName);
    const kebabAppName = Case.kebab(appName);
    const kebabSelector = Case.kebab(selector);
    const destinationPath = this.destinationPath();

    const filesToEdit = [
      `${destinationPath}/angular.json`,
      `${destinationPath}/jest.config.json`,
      `${destinationPath}/package-lock.json`,
      `${destinationPath}/package.json`,
      `${destinationPath}/index.html`,
      `${destinationPath}/app.component.spec.ts`,
      `${destinationPath}/app.component.ts`
    ];

    for (const file of filesToEdit) {
      let content = this.fs.read(file);
      content = content
        .replace(/__author__/g, author)
        .replace(/__kebab-app-name__/g, kebabAppName)
        .replace(/__capital-case-name__/g, capitalAppName)
        .replace(/__selector__/g, kebabSelector);

      this.fs.write(file, content);
    }
  }

But when the generator runs the files are not copied yet. How can I wait for the file to be copied before trying to replace the placeholders?

This is the output of the command you wanted me to run:

darwin {
  node: '15.2.0',
  v8: '8.6.395.17-node.17',
  uv: '1.40.0',
  zlib: '1.2.11',
  brotli: '1.0.9',
  ares: '1.16.1',
  modules: '88',
  nghttp2: '1.41.0',
  napi: '7',
  llhttp: '2.1.3',
  openssl: '1.1.1g',
  cldr: '37.0',
  icu: '67.1',
  tz: '2019c',
  unicode: '13.0'
}
@y-chen y-chen closed this as completed Jan 20, 2021
@y-chen
Copy link
Author

y-chen commented Jan 20, 2021

Solved.

yeoman copies in memory first so the files were not actually on the file system yet.
You can achieve what I was trying to do in this way:

    this.fs.copy(this.templatePath("**/*"), destinationPath, {
      process: function(content) {
        let text = content.toString();
        return text.replace(/__placeholder__/g, textToReplace);
      }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant