Skip to content

Commit 18a85f3

Browse files
devversionmmalerba
authored andcommitted
build: version placeholder replacement grep alternative (#6290)
For the version placeholde replacement a Grep statement is used as well. For Windows environments there is an alternative called `Findstr` which can be used to avoid having a dependency on ported Unix-tools.
1 parent 0680774 commit 18a85f3

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

tools/package-tools/version-placeholders.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {readFileSync, writeFileSync} from 'fs';
2+
import {platform} from 'os';
23
import {buildConfig} from './build-config';
34
import {spawnSync} from 'child_process';
45

@@ -13,12 +14,9 @@ const versionPlaceholderRegex = new RegExp(versionPlaceholderText, 'g');
1314
* version of Material.
1415
*/
1516
export function replaceVersionPlaceholders(packageDir: string) {
16-
// Resolve files that contain version placeholders using Grep since it's super fast and also
17-
// does have a very simple usage.
18-
const files = spawnSync('grep', ['-ril', versionPlaceholderText, packageDir]).stdout
19-
.toString()
20-
.split('\n')
21-
.filter(String);
17+
// Resolve files that contain version placeholders using Grep or Findstr since those are
18+
// extremely fast and also have a very simple usage.
19+
const files = findFilesWithPlaceholders(packageDir);
2220

2321
// Walk through every file that contains version placeholders and replace those with the current
2422
// version of the root package.json file.
@@ -30,3 +28,27 @@ export function replaceVersionPlaceholders(packageDir: string) {
3028
writeFileSync(filePath, fileContent);
3129
});
3230
}
31+
32+
/** Finds all files in the specified package dir where version placeholders are included. */
33+
function findFilesWithPlaceholders(packageDir: string): string[] {
34+
const findCommand = buildPlaceholderFindCommand(packageDir);
35+
return spawnSync(findCommand.binary, findCommand.args).stdout
36+
.toString()
37+
.split(/[\n\r]/)
38+
.filter(String);
39+
}
40+
41+
/** Builds the command that will be executed to find all files containing version placeholders. */
42+
function buildPlaceholderFindCommand(packageDir: string) {
43+
if (platform() === 'win32') {
44+
return {
45+
binary: 'findstr',
46+
args: ['/msi', `/c:${versionPlaceholderText}`, `${packageDir}\\*`]
47+
};
48+
} else {
49+
return {
50+
binary: 'grep',
51+
args: ['-ril', versionPlaceholderText, packageDir]
52+
};
53+
}
54+
}

0 commit comments

Comments
 (0)