Skip to content

Commit 6f0ec7b

Browse files
committed
Update various projects to use the new Text API instead of ad hoc code
1 parent 511928d commit 6f0ec7b

9 files changed

Lines changed: 17 additions & 36 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/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.

‎libraries/node-core-library/src/FileDiffTest.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as path from 'path';
55
import * as fsx from 'fs-extra';
66

77
import { PackageJsonLookup } from './PackageJsonLookup';
8+
import { Text } from './Text';
89

910
/**
1011
* Implements a unit testing strategy that generates output files, and then
@@ -84,7 +85,7 @@ export class FileDiffTest {
8485
}
8586

8687
private static _getNormalizedContent(s: string): string {
87-
return s.replace(/\r\n/g, '\n').replace(/\r/g, '') // convert to Unix-style newlines
88+
return Text.convertToLf(s) // convert to Unix-style newlines
8889
.replace(/\s+\n/g, '\n') // strip spaces from end of line
8990
.replace(/\n+$/g, ''); // strip newlines from end of file
9091
}

‎libraries/node-core-library/src/JsonFile.ts‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as os from 'os';
66
import * as jju from 'jju';
77

88
import { JsonSchema, IJsonSchemaErrorInfo, IJsonSchemaValidateOptions } from './JsonSchema';
9+
import { Text } from './Text';
910

1011
/**
1112
* Options for JsonFile.stringify()
@@ -92,7 +93,7 @@ export class JsonFile {
9293
if (options && options.unixNewlines) {
9394
return stringified;
9495
} else {
95-
return JsonFile._getAllReplaced(stringified, '\n', '\r\n');
96+
return Text.convertToCrLf(stringified);
9697
}
9798

9899
}
@@ -198,15 +199,4 @@ export class JsonFile {
198199
}
199200
return result;
200201
}
201-
202-
/**
203-
* Returns the same thing as targetString.replace(searchValue, replaceValue), except that
204-
* all matches are replaced, rather than just the first match.
205-
* @param targetString The string to be modified
206-
* @param searchValue The value to search for
207-
* @param replaceValue The replacement text
208-
*/
209-
private static _getAllReplaced(targetString: string, searchValue: string, replaceValue: string): string {
210-
return targetString.split(searchValue).join(replaceValue);
211-
}
212202
}

0 commit comments

Comments
 (0)