adamreisnz / replace-in-file

A simple utility to quickly replace contents in one or more files
580 stars 65 forks source link

replace multiple values with different replacements in different files #119

Closed akaguny closed 4 years ago

akaguny commented 4 years ago

I have config for my replacement:

type UpdateVersionsConfig = {
  someBuisnessLogicProps?: any;
  files: Array<UpdateVersionsFileReplaceConfig>;
};

type UpdateVersionsFileReplaceConfig = {
  path: Array<string>;
  replace: Array<{
    from: string;
    to: string;
  }>;
};

In my case for different files need different replacements. Now i need something like for normalize

{
        files: config.files.flatMap(({ path }) => path),
        from: config.files
          .flatMap(({ replace }) => replace)
          .flatMap(({ from }) => from),
        to: config.files
          .flatMap(({ replace }) => replace)
          .flatMap(({ to }) => to),
        glob: {
          absolute: true,
          cwd: mayBeCloneGitRepository.value,
        },
      }

but in can create bugs because in normalize i simple concat all UpdateVersionsFileReplaceConfig. In this way i have a bug with this conditions:

adamreisnz commented 4 years ago

To the best of my knowledge this is not currently possible or supported.

You can do multiple different replacements, https://github.com/adamreisnz/replace-in-file#multiple-values-with-different-replacements, but on the same files only.

I think the workaround is currently the only option.

akaguny commented 4 years ago

@adamreisnz ,

To the best of my knowledge this is not currently possible or supported.

maybe its for next major realize or minor improvement?

I think the workaround is currently the only option.

maybe add this workaround to readme docs?

adamreisnz commented 4 years ago

Sure, open to a PR with ideas on how to implement it

On Wed, 17 Jun 2020, 00:25 Alexey, notifications@github.com wrote:

@adamreisnz https://github.com/adamreisnz , maybe its for next major realize or minor improvement?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/adamreisnz/replace-in-file/issues/119#issuecomment-644729791, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADXYQVXTT2QZTC6TI7W3DDRW5QDDANCNFSM4NXEOGAQ .

akaguny commented 4 years ago

now i have no enough time for the research for create solution for single run replace in file for different replacements in different files (optimization) correct workaround, someone may be usefully

const normalizes = {
  from: function normalizeFroms(
    configFiles: UpdateVersionsFileReplaceConfig
  ): Array<string | RegExp> {
    return configFiles.replace.flatMap(({ from }) => from);
  },
  to: function normalizeFroms(
    configFiles: UpdateVersionsFileReplaceConfig
  ): Array<string> {
    return configFiles.replace.flatMap(({ to }) => to);
  },
};

export const normalizeConfigs = (
  config,
  globOptions: { cwd: string; absolute: boolean }
): Array<ReplaceInFileConfig> => {
  const normalizedConfigs = [];
  for (const configFile of config.files) {
    normalizedConfigs.push({
      files: configFile.path,
      from: normalizes.from(configFile),
      to: normalizes.to(configFile),
      ...(configFile.encoding ? { encoding: configFile.encoding } : {}),
      glob: { ...globOptions },
    });
  }
  return normalizedConfigs;
};

and run replace file function multiply with arguments =)