Skip to content

Commit f6aec09

Browse files
authored
Merge pull request microsoft#727 from Microsoft/nickpape/add-file-system
Create a FileSystem API and remove references to fs/fsx
2 parents 93387ba + 6669cb6 commit f6aec09

88 files changed

Lines changed: 1237 additions & 459 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/api-documenter/package.json‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
"@microsoft/node-core-library": "1.4.1",
2020
"@microsoft/ts-command-line": "4.2.0",
2121
"colors": "~1.2.1",
22-
"fs-extra": "~5.0.0",
2322
"js-yaml": "~3.9.1"
2423
},
2524
"devDependencies": {
2625
"@microsoft/node-library-build": "4.3.47",
27-
"@types/fs-extra": "5.0.1",
2826
"@types/js-yaml": "3.9.1",
2927
"@types/node": "8.5.8",
3028
"gulp": "~3.9.1",

‎apps/api-documenter/src/cli/BaseAction.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import * as fsx from 'fs-extra';
54
import * as path from 'path';
65

76
import {
87
CommandLineAction,
98
CommandLineStringParameter
109
} from '@microsoft/ts-command-line';
1110

11+
import { FileSystem } from '@microsoft/node-core-library';
12+
1213
import { DocItemSet } from '../utils/DocItemSet';
1314

1415
export abstract class BaseAction extends CommandLineAction {
@@ -41,14 +42,14 @@ export abstract class BaseAction extends CommandLineAction {
4142
const docItemSet: DocItemSet = new DocItemSet();
4243

4344
this.inputFolder = this._inputFolderParameter.value || './input';
44-
if (!fsx.existsSync(this.inputFolder)) {
45+
if (!FileSystem.exists(this.inputFolder)) {
4546
throw new Error('The input folder does not exist: ' + this.inputFolder);
4647
}
4748

4849
this.outputFolder = this._outputFolderParameter.value || `./${this.actionName}`;
49-
fsx.mkdirsSync(this.outputFolder);
50+
FileSystem.ensureFolder(this.outputFolder);
5051

51-
for (const filename of fsx.readdirSync(this.inputFolder)) {
52+
for (const filename of FileSystem.readFolder(this.inputFolder)) {
5253
if (filename.match(/\.api\.json$/i)) {
5354
console.log(`Reading ${filename}`);
5455
const filenamePath: string = path.join(this.inputFolder, filename);

‎apps/api-documenter/src/markdown/MarkdownDocumenter.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import * as fsx from 'fs-extra';
54
import * as path from 'path';
65

7-
import { Text, PackageName } from '@microsoft/node-core-library';
6+
import {
7+
PackageName,
8+
FileSystem,
9+
NewlineKind
10+
} from '@microsoft/node-core-library';
811
import {
912
IApiClass,
1013
IApiEnum,
@@ -597,7 +600,9 @@ export class MarkdownDocumenter {
597600
}
598601
});
599602

600-
fsx.writeFileSync(filename, Text.convertToCrLf(content));
603+
FileSystem.writeFile(filename, content, {
604+
convertLineEndings: NewlineKind.CrLf
605+
});
601606
}
602607

603608
private _getFilenameForDocItem(docItem: DocItem): string {
@@ -614,6 +619,6 @@ export class MarkdownDocumenter {
614619

615620
private _deleteOldOutputFiles(): void {
616621
console.log('Deleting old output from ' + this._outputFolder);
617-
fsx.emptyDirSync(this._outputFolder);
622+
FileSystem.ensureEmptyFolder(this._outputFolder);
618623
}
619624
}

‎apps/api-documenter/src/utils/test/MarkdownRenderer.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import * as fsx from 'fs-extra';
54
import * as path from 'path';
6-
import { FileDiffTest } from '@microsoft/node-core-library';
5+
import { FileDiffTest, FileSystem } from '@microsoft/node-core-library';
76
import { IMarkupPage, Markup } from '@microsoft/api-extractor';
87

98
import { MarkdownRenderer } from '../MarkdownRenderer';
@@ -66,7 +65,7 @@ describe('MarkdownPageRenderer', () => {
6665
]);
6766

6867
const outputFilename: string = path.join(outputFolder, 'ActualOutput.md');
69-
fsx.writeFileSync(outputFilename, MarkdownRenderer.renderElements([markupPage], { }));
68+
FileSystem.writeFile(outputFilename, MarkdownRenderer.renderElements([markupPage], { }));
7069

7170
FileDiffTest.assertEqual(outputFilename, path.join(__dirname, 'ExpectedOutput.md'));
7271

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
// See LICENSE in the project root for license information.
33

44
import * as colors from 'colors';
5-
import * as fsx from 'fs-extra';
65
import * as path from 'path';
76
import yaml = require('js-yaml');
87

98
import { DocItemSet } from '../utils/DocItemSet';
109
import { IYamlTocItem } from './IYamlTocFile';
1110
import { IYamlItem } from './IYamlApiFile';
1211
import { YamlDocumenter } from './YamlDocumenter';
13-
import { Text } from '@microsoft/node-core-library';
12+
import { Text, FileSystem } from '@microsoft/node-core-library';
1413

1514
interface ISnippetsFile {
1615
/**
@@ -44,7 +43,8 @@ export class OfficeYamlDocumenter extends YamlDocumenter {
4443
const snippetsFilePath: string = path.join(inputFolder, 'snippets.yaml');
4544

4645
console.log('Loading snippets from ' + snippetsFilePath);
47-
const snippetsContent: string = fsx.readFileSync(snippetsFilePath).toString();
46+
47+
const snippetsContent: string = FileSystem.readFile(snippetsFilePath);
4848
this._snippets = yaml.load(snippetsContent, { filename: snippetsFilePath });
4949
}
5050

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import * as fsx from 'fs-extra';
54
import * as path from 'path';
65
import * as colors from 'colors';
76

87
import yaml = require('js-yaml');
9-
import { JsonFile, JsonSchema, Text, PackageName } from '@microsoft/node-core-library';
8+
import {
9+
JsonFile,
10+
JsonSchema,
11+
PackageName,
12+
FileSystem,
13+
NewlineKind
14+
} from '@microsoft/node-core-library';
1015
import {
1116
MarkupElement,
1217
IApiMethod,
@@ -413,10 +418,10 @@ export class YamlDocumenter {
413418
stringified = `### YamlMime:${yamlMimeType}\n` + stringified;
414419
}
415420

416-
const normalized: string = Text.convertToCrLf(stringified);
417-
418-
fsx.mkdirsSync(path.dirname(filePath));
419-
fsx.writeFileSync(filePath, normalized);
421+
FileSystem.writeFile(filePath, stringified, {
422+
convertLineEndings: NewlineKind.CrLf,
423+
ensureFolderExists: true
424+
});
420425

421426
if (schema) {
422427
schema.validateObject(dataObject, filePath);
@@ -576,6 +581,6 @@ export class YamlDocumenter {
576581

577582
private _deleteOldOutputFiles(): void {
578583
console.log('Deleting old output from ' + this._outputFolder);
579-
fsx.emptyDirSync(this._outputFolder);
584+
FileSystem.ensureEmptyFolder(this._outputFolder);
580585
}
581586
}

‎apps/api-extractor/package.json‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@
3333
"dependencies": {
3434
"@microsoft/node-core-library": "1.4.1",
3535
"@microsoft/ts-command-line": "4.2.0",
36-
"@types/fs-extra": "5.0.1",
3736
"@types/node": "8.5.8",
3837
"@types/z-schema": "3.16.31",
3938
"colors": "~1.2.1",
40-
"fs-extra": "~5.0.0",
4139
"jju": "~1.3.0",
4240
"lodash": "~4.15.0",
4341
"typescript": "~2.4.1",

‎apps/api-extractor/src/DocItemLoader.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import * as fsx from 'fs-extra';
5-
import * as os from 'os';
4+
import * as os from 'os';
65
import * as path from 'path';
7-
import { JsonFile } from '@microsoft/node-core-library';
6+
import {
7+
JsonFile,
8+
FileSystem
9+
} from '@microsoft/node-core-library';
810

911
import {
1012
ApiItem,
@@ -38,7 +40,7 @@ export class DocItemLoader implements IReferenceResolver {
3840
* that we are compiling.
3941
*/
4042
constructor(projectFolder: string) {
41-
if (!fsx.existsSync(path.join(projectFolder, 'package.json'))) {
43+
if (!FileSystem.exists(path.join(projectFolder, 'package.json'))) {
4244
throw new Error(`An NPM project was not found in the specified folder: ${projectFolder}`);
4345
}
4446

@@ -198,7 +200,7 @@ export class DocItemLoader implements IReferenceResolver {
198200
`dist/${apiDefinitionRef.packageName}.api.json`
199201
);
200202

201-
if (!fsx.existsSync(path.join(apiJsonFilePath))) {
203+
if (!FileSystem.exists(path.join(apiJsonFilePath))) {
202204
// Error should be handled by the caller
203205
return undefined;
204206
}

‎apps/api-extractor/src/ExternalApiHelper.ts‎

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

44
import * as path from 'path';
5-
import * as fs from 'fs';
5+
import { FileSystem } from '@microsoft/node-core-library';
6+
67
import { Extractor } from './extractor/Extractor';
78

89
/**
@@ -37,15 +38,8 @@ export class ExternalApiHelper {
3738
rootDir: entryPointFolder
3839
};
3940

40-
let outputPath: string = path.resolve(rootDir, libFolder);
41-
if (!fs.existsSync(outputPath)) {
42-
fs.mkdirSync(outputPath);
43-
}
44-
45-
outputPath = path.join(outputPath, 'external-api-json');
46-
if (!fs.existsSync(outputPath)) {
47-
fs.mkdirSync(outputPath);
48-
}
41+
const outputPath: string = path.resolve(rootDir, libFolder, 'external-api-json');
42+
FileSystem.ensureFolder(outputPath);
4943

5044
const extractor: Extractor = new Extractor({
5145
compiler: {

‎apps/api-extractor/src/ExtractorContext.ts‎

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

44
import * as ts from 'typescript';
5-
import * as fsx from 'fs-extra';
65
import * as path from 'path';
7-
import { PackageJsonLookup, IPackageJson, PackageName, IParsedPackageName } from '@microsoft/node-core-library';
6+
import {
7+
PackageJsonLookup,
8+
IPackageJson,
9+
PackageName,
10+
IParsedPackageName,
11+
FileSystem
12+
} from '@microsoft/node-core-library';
813

914
import { AstPackage } from './ast/AstPackage';
1015
import { DocItemLoader } from './DocItemLoader';
@@ -160,14 +165,13 @@ export class ExtractorContext {
160165
return;
161166
}
162167

163-
const files: string[] = fsx.readdirSync(externalJsonCollectionPath);
164-
files.forEach(file => {
168+
FileSystem.readFolder(externalJsonCollectionPath, {
169+
absolutePaths: true
170+
}).forEach(file => {
165171
if (path.extname(file) === '.json') {
166-
const externalJsonFilePath: string = path.join(externalJsonCollectionPath, file);
167-
168172
// Example: "C:\Example\my-package.json" --> "my-package"
169173
const packageName: string = path.parse(file).name;
170-
this.docItemLoader.loadPackageIntoCache(externalJsonFilePath, packageName);
174+
this.docItemLoader.loadPackageIntoCache(file, packageName);
171175
}
172176
});
173177
}

0 commit comments

Comments
 (0)