Skip to content

Commit c47e97e

Browse files
authored
Merge pull request microsoft#562 from Microsoft/pgonzal/text-lib
Consolidate some text handling functions
2 parents d09563d + 4ce90c0 commit c47e97e

17 files changed

Lines changed: 113 additions & 39 deletions

File tree

‎apps/api-documenter/src/yaml/YamlDocumenter.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import * as fsx from 'fs-extra';
55
import * as path from 'path';
66
import yaml = require('js-yaml');
7-
import { JsonFile, JsonSchema } from '@microsoft/node-core-library';
7+
import { JsonFile, JsonSchema, Text } from '@microsoft/node-core-library';
88
import {
99
MarkupElement,
1010
IApiMethod,
@@ -376,7 +376,7 @@ export class YamlDocumenter {
376376
stringified = `### YamlMime:${yamlMimeType}\n` + stringified;
377377
}
378378

379-
const normalized: string = stringified.split('\n').join('\r\n');
379+
const normalized: string = Text.convertToCrLf(stringified);
380380

381381
fsx.mkdirsSync(path.dirname(filePath));
382382
fsx.writeFileSync(filePath, normalized);

‎apps/api-extractor/src/generators/ApiFileGenerator.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See LICENSE in the project root for license information.
33

44
import * as fs from 'fs';
5+
import { Text } from '@microsoft/node-core-library';
56
import { ExtractorContext } from '../ExtractorContext';
67
import { AstStructuredType } from '../ast/AstStructuredType';
78
import { AstEnum } from '../ast/AstEnum';
@@ -66,7 +67,7 @@ export class ApiFileGenerator extends AstItemVisitor {
6667
this._insideTypeLiteral = 0;
6768
// Normalize to CRLF
6869
this.visit(context.package);
69-
const fileContent: string = this._indentedWriter.toString().replace(/\r?\n/g, '\r\n');
70+
const fileContent: string = Text.convertToCrLf(this._indentedWriter.toString());
7071
return fileContent;
7172
}
7273

‎apps/api-extractor/src/generators/packageTypings/PackageTypingsGenerator.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import * as fs from 'fs';
77
import * as ts from 'typescript';
8+
import { Text } from '@microsoft/node-core-library';
89

910
import { ExtractorContext } from '../../ExtractorContext';
1011
import { IndentedWriter } from '../../utils/IndentedWriter';
@@ -69,7 +70,7 @@ export class PackageTypingsGenerator {
6970
this._generateTypingsFileContent(indentedWriter, dtsKind);
7071

7172
// Normalize to CRLF
72-
const fileContent: string = indentedWriter.toString().replace(/\r?\n/g, '\r\n');
73+
const fileContent: string = Text.convertToCrLf(indentedWriter.toString());
7374

7475
fs.writeFileSync(dtsFilename, fileContent);
7576
}

‎apps/rush-lib/src/cli/logic/InstallManager.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as semver from 'semver';
1010
import * as tar from 'tar';
1111
import * as wordwrap from 'wordwrap';
1212
import globEscape = require('glob-escape');
13-
import { JsonFile, LockFile } from '@microsoft/node-core-library';
13+
import { JsonFile, LockFile, Text } from '@microsoft/node-core-library';
1414

1515
import AsyncRecycler from '../../utilities/AsyncRecycler';
1616
import RushConfiguration, {
@@ -602,8 +602,7 @@ export default class InstallManager {
602602
const pathToDeleteWithoutStar: string = path.join(commonNodeModulesFolder, RushConstants.rushTempNpmScope);
603603
console.log(`Deleting ${pathToDeleteWithoutStar}\\*`);
604604
// Glob can't handle Windows paths
605-
const normalizedpathToDeleteWithoutStar: string
606-
= Utilities.getAllReplaced(pathToDeleteWithoutStar, '\\', '/');
605+
const normalizedpathToDeleteWithoutStar: string = Text.replaceAll(pathToDeleteWithoutStar, '\\', '/');
607606

608607
// Example: "C:/MyRepo/common/temp/node_modules/@rush-temp/*"
609608
for (const tempModulePath of glob.sync(globEscape(normalizedpathToDeleteWithoutStar) + '/*')) {
@@ -736,8 +735,7 @@ export default class InstallManager {
736735
const pathToDeleteWithoutStar: string = path.join(this._rushConfiguration.commonTempFolder,
737736
'node_modules', RushConstants.rushTempNpmScope);
738737
// Glob can't handle Windows paths
739-
const normalizedpathToDeleteWithoutStar: string
740-
= Utilities.getAllReplaced(pathToDeleteWithoutStar, '\\', '/');
738+
const normalizedpathToDeleteWithoutStar: string = Text.replaceAll(pathToDeleteWithoutStar, '\\', '/');
741739

742740
let anyChanges: boolean = false;
743741

‎apps/rush-lib/src/cli/logic/pnpm/PnpmLinkManager.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as os from 'os';
66
import * as path from 'path';
77
import uriEncode = require('strict-uri-encode');
88

9-
import { JsonFile } from '@microsoft/node-core-library';
9+
import { JsonFile, Text } from '@microsoft/node-core-library';
1010

1111
import {
1212
BaseLinkManager,
@@ -135,7 +135,7 @@ export class PnpmLinkManager extends BaseLinkManager {
135135
`${unscopedTempProjectName}.tgz`);
136136

137137
// e.g.: C%3A%2Fwbt%2Fcommon%2Ftemp%2Fprojects%2Fapi-documenter.tgz
138-
const escapedPathToTgzFile: string = uriEncode(pathToTgzFile.split(path.sep).join('/'));
138+
const escapedPathToTgzFile: string = uriEncode(Text.replaceAll(pathToTgzFile, path.sep, '/'));
139139

140140
// tslint:disable-next-line:max-line-length
141141
// e.g.: C:\wbt\common\temp\node_modules\.local\C%3A%2Fwbt%2Fcommon%2Ftemp%2Fprojects%2Fapi-documenter.tgz\node_modules

‎apps/rush-lib/src/data/ApprovedPackagesConfiguration.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import * as fsx from 'fs-extra';
55
import * as path from 'path';
66
import * as os from 'os';
7-
import { JsonFile, JsonSchema } from '@microsoft/node-core-library';
7+
import { JsonFile, JsonSchema, Text } from '@microsoft/node-core-library';
88

99
import Utilities from '../utilities/Utilities';
1010

@@ -162,7 +162,7 @@ export class ApprovedPackagesConfiguration {
162162
body = '// DO NOT ADD COMMENTS IN THIS FILE.'
163163
+ ' They will be lost when the Rush tool resaves it.\n' + body;
164164

165-
body = Utilities.getAllReplaced(body, '\n', '\r\n');
165+
body = Text.convertToCrLf(body);
166166
fsx.writeFileSync(this._jsonFilename, body);
167167
}
168168

‎apps/rush-lib/src/data/test/RushConfiguration.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
/// <reference types='mocha' />
55

66
import { assert } from 'chai';
7+
import { Text } from '@microsoft/node-core-library';
78
import RushConfiguration from '../RushConfiguration';
89
import { ApprovedPackagesPolicy } from '../ApprovedPackagesPolicy';
910
import RushConfigurationProject from '../RushConfigurationProject';
1011
import * as path from 'path';
1112
import Utilities from '../../utilities/Utilities';
1213

1314
function normalizePathForComparison(pathToNormalize: string): string {
14-
return Utilities.getAllReplaced(pathToNormalize, '\\', '/').toUpperCase();
15+
return Text.replaceAll(pathToNormalize, '\\', '/').toUpperCase();
1516
}
1617

1718
function assertPathProperty(validatedPropertyName: string, absolutePath: string, relativePath: string): void {

‎apps/rush-lib/src/utilities/Utilities.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -363,17 +363,6 @@ export default class Utilities {
363363
});
364364
}
365365

366-
/**
367-
* Returns the same thing as targetString.replace(searchValue, replaceValue), except that
368-
* all matches are replaced, rather than just the first match.
369-
* @param targetString - The string to be modified
370-
* @param searchValue - The value to search for
371-
* @param replaceValue - The replacement text
372-
*/
373-
public static getAllReplaced(targetString: string, searchValue: string, replaceValue: string): string {
374-
return targetString.split(searchValue).join(replaceValue);
375-
}
376-
377366
/**
378367
* For strings passed to a shell command, this adds appropriate escaping
379368
* to avoid misinterpretation of spaces or special characters.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "",
5+
"packageName": "@microsoft/api-documenter",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/api-documenter",
10+
"email": "pgonzal@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "",
5+
"packageName": "@microsoft/api-extractor",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor",
10+
"email": "pgonzal@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)