From c8ec266a1389c0a971cc82717beb4c47d4b711d7 Mon Sep 17 00:00:00 2001 From: Ben Coveney Date: Sun, 8 Oct 2017 15:04:55 +0100 Subject: [PATCH] Built with line endings fix --- bin/builder.js | 130 ++++++++-------- bin/builder.test.js | 258 ++++++++++++++++---------------- bin/builders/fileSystem.js | 134 ++++++++--------- bin/builders/fileSystem.test.js | 94 ++++++------ bin/builders/flat.js | 22 +-- bin/builders/flat.test.js | 180 +++++++++++----------- bin/destinations.js | 92 ++++++------ bin/destinations.test.js | 124 +++++++-------- bin/fileTree.js | 88 +++++------ bin/fileTree.test.js | 176 +++++++++++----------- bin/index.js | 36 ++--- bin/modules.js | 128 ++++++++-------- bin/modules.test.js | 192 ++++++++++++------------ bin/options.js | 136 ++++++++--------- bin/options.test.js | 102 ++++++------- bin/purge.js | 46 +++--- bin/purge.test.js | 92 ++++++------ bin/testUtilities.js | 242 +++++++++++++++--------------- bin/utilities.js | 20 +-- bin/utilities.test.js | 82 +++++----- 20 files changed, 1187 insertions(+), 1187 deletions(-) diff --git a/bin/builder.js b/bin/builder.js index 424d5306..966f45f8 100644 --- a/bin/builder.js +++ b/bin/builder.js @@ -1,66 +1,66 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const fs = require("fs"); -const path = require("path"); -const utilities_1 = require("./utilities"); -const fileSystem_1 = require("./builders/fileSystem"); -const flat_1 = require("./builders/flat"); -const modules_1 = require("./modules"); -function buildBarrels(destinations, options) { - let builder; - switch (options.structure) { - default: - case "flat": - builder = flat_1.buildFlatBarrel; - break; - case "filesystem": - builder = fileSystem_1.buildFileSystemBarrel; - break; - } - // Build the barrels. - destinations.forEach((destination) => buildBarrel(destination, builder, options)); -} -exports.buildBarrels = buildBarrels; -// Build a barrel for the specified directory. -function buildBarrel(directory, builder, options) { - options.logger(`Building barrel @ ${directory.path}`); - const content = builder(directory, modules_1.loadDirectoryModules(directory, options), options); - const destination = path.join(directory.path, options.barrelName); - fs.writeFileSync(destination, content); - // Update the file tree model with the new barrel. - if (!directory.files.some((file) => file.name === options.barrelName)) { - const convertedPath = utilities_1.convertPathSeparator(destination); - const barrel = { - name: options.barrelName, - path: convertedPath, - }; - options.logger(`Updating model barrel @ ${convertedPath}`); - directory.files.push(barrel); - directory.barrel = barrel; - } -} -/** Builds the TypeScript */ -function buildImportPath(directory, target) { - // Get the route from the current directory to the module. - const relativePath = path.relative(directory.path, target.path); - // Get the route and ensure it's relative - let directoryPath = path.dirname(relativePath); - if (directoryPath !== ".") { - directoryPath = `.${path.sep}${directoryPath}`; - } - // Strip off the .ts or .tsx from the file name. - const fileName = getBasename(relativePath); - // Build the final path string. Use posix-style seperators. - const location = `${directoryPath}${path.sep}${fileName}`; - return utilities_1.convertPathSeparator(location); -} -exports.buildImportPath = buildImportPath; -/** Strips the .ts or .tsx file extension from a path and returns the base filename. */ -function getBasename(relativePath) { - const strippedTsPath = path.basename(relativePath, ".ts"); - const strippedTsxPath = path.basename(relativePath, ".tsx"); - // Return whichever path is shorter. If they're the same length then nothing was stripped. - return strippedTsPath.length < strippedTsxPath.length ? strippedTsPath : strippedTsxPath; -} -exports.getBasename = getBasename; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const fs = require("fs"); +const path = require("path"); +const utilities_1 = require("./utilities"); +const fileSystem_1 = require("./builders/fileSystem"); +const flat_1 = require("./builders/flat"); +const modules_1 = require("./modules"); +function buildBarrels(destinations, options) { + let builder; + switch (options.structure) { + default: + case "flat": + builder = flat_1.buildFlatBarrel; + break; + case "filesystem": + builder = fileSystem_1.buildFileSystemBarrel; + break; + } + // Build the barrels. + destinations.forEach((destination) => buildBarrel(destination, builder, options)); +} +exports.buildBarrels = buildBarrels; +// Build a barrel for the specified directory. +function buildBarrel(directory, builder, options) { + options.logger(`Building barrel @ ${directory.path}`); + const content = builder(directory, modules_1.loadDirectoryModules(directory, options), options); + const destination = path.join(directory.path, options.barrelName); + fs.writeFileSync(destination, content); + // Update the file tree model with the new barrel. + if (!directory.files.some((file) => file.name === options.barrelName)) { + const convertedPath = utilities_1.convertPathSeparator(destination); + const barrel = { + name: options.barrelName, + path: convertedPath, + }; + options.logger(`Updating model barrel @ ${convertedPath}`); + directory.files.push(barrel); + directory.barrel = barrel; + } +} +/** Builds the TypeScript */ +function buildImportPath(directory, target) { + // Get the route from the current directory to the module. + const relativePath = path.relative(directory.path, target.path); + // Get the route and ensure it's relative + let directoryPath = path.dirname(relativePath); + if (directoryPath !== ".") { + directoryPath = `.${path.sep}${directoryPath}`; + } + // Strip off the .ts or .tsx from the file name. + const fileName = getBasename(relativePath); + // Build the final path string. Use posix-style seperators. + const location = `${directoryPath}${path.sep}${fileName}`; + return utilities_1.convertPathSeparator(location); +} +exports.buildImportPath = buildImportPath; +/** Strips the .ts or .tsx file extension from a path and returns the base filename. */ +function getBasename(relativePath) { + const strippedTsPath = path.basename(relativePath, ".ts"); + const strippedTsxPath = path.basename(relativePath, ".tsx"); + // Return whichever path is shorter. If they're the same length then nothing was stripped. + return strippedTsPath.length < strippedTsxPath.length ? strippedTsPath : strippedTsxPath; +} +exports.getBasename = getBasename; //# sourceMappingURL=builder.js.map \ No newline at end of file diff --git a/bin/builder.test.js b/bin/builder.test.js index 514dd69b..2e67a160 100644 --- a/bin/builder.test.js +++ b/bin/builder.test.js @@ -1,130 +1,130 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const chai_1 = require("chai"); -const fs = require("fs"); -const MockFs = require("mock-fs"); -const Sinon = require("sinon"); -const Builder = require("./builder"); -const FileSystem = require("./builders/fileSystem"); -const Flat = require("./builders/flat"); -const Modules = require("./modules"); -const TestUtilities = require("./testUtilities"); -// Gets a location from a list by name. -function getLocationByName(locations, name) { - return locations.filter((location) => location.name === name)[0]; -} -describe("builder/builder module has a", () => { - describe("buildBarrels function that", () => { - let directory; - let spySandbox; - let logger; - const runBuilder = (structure) => { - logger = spySandbox.spy(); - Builder.buildBarrels(directory.directories, { - barrelName: "barrel.ts", - logger, - quoteCharacter: "\"", - rootPath: ".", - structure, - }); - }; - beforeEach(() => { - MockFs(TestUtilities.mockFsConfiguration()); - directory = TestUtilities.mockDirectoryTree(); - spySandbox = Sinon.sandbox.create(); - spySandbox.stub(FileSystem, "buildFileSystemBarrel").returns("fileSystemContent"); - spySandbox.stub(Flat, "buildFlatBarrel").returns("flatContent"); - spySandbox.stub(Modules, "loadDirectoryModules").returns("loadedModules"); - }); - afterEach(() => { - MockFs.restore(); - spySandbox.restore(); - }); - describe("uses the structure option and", () => { - const testStructure = (structure, isFlat) => { - runBuilder(structure); - // TODO: Test arguments for barrel builder & loadDirectoryModules - if (isFlat) { - Sinon.assert.calledTwice(Flat.buildFlatBarrel); - Sinon.assert.notCalled(FileSystem.buildFileSystemBarrel); - } - else { - Sinon.assert.notCalled(Flat.buildFlatBarrel); - Sinon.assert.calledTwice(FileSystem.buildFileSystemBarrel); - } - }; - it("should use the flat builder if in flat mode", () => { - testStructure("flat", true); - }); - it("should use the filesystem builder if in filesystem mode", () => { - testStructure("filesystem", false); - }); - it("should use the flat builder if no mode is specified", () => { - testStructure(undefined, true); - }); - }); - it("should write each barrel's content to disk", () => { - runBuilder("flat"); - const checkContent = (address) => { - const result = fs.readFileSync(address, "utf8"); - chai_1.assert.equal(result, "flatContent"); - }; - checkContent("directory1/directory2/barrel.ts"); - checkContent("directory1/directory3/barrel.ts"); - }); - it("should update the directory structure with the new barrel", () => { - runBuilder("flat"); - directory.directories.forEach((subDirectory) => { - chai_1.assert.equal(subDirectory.barrel.name, "barrel.ts"); - }); - }); - it("should log useful information to the logger", () => { - runBuilder("flat"); - const messages = [ - "Building barrel @ directory1/directory2", - "Updating model barrel @ directory1/directory2/barrel.ts", - "Building barrel @ directory1/directory3", - "Updating model barrel @ directory1/directory3/barrel.ts", - ]; - chai_1.assert.equal(logger.callCount, messages.length); - messages.forEach((message, barrel) => { - chai_1.assert.equal(logger.getCall(barrel).args[0], message); - }); - }); - }); - describe("buildImportPath function that", () => { - let directory; - beforeEach(() => { - directory = TestUtilities.mockDirectoryTree(); - }); - it("should correctly build a path to a file in the same directory", () => { - const target = getLocationByName(directory.files, "index.ts"); - const result = Builder.buildImportPath(directory, target); - chai_1.assert.equal(result, "./index"); - }); - it("should correctly build a path to a file in a child directory", () => { - const childDirectory = getLocationByName(directory.directories, "directory2"); - const target = getLocationByName(childDirectory.files, "script.ts"); - const result = Builder.buildImportPath(directory, target); - chai_1.assert.equal(result, "./directory2/script"); - }); - }); - describe("getBasename function that", () => { - it("should correctly strip .ts from the filename", () => { - const fileName = "./random/path/file.ts"; - const result = Builder.getBasename(fileName); - chai_1.assert.equal(result, "file"); - }); - it("should correctly strip .tsx from the filename", () => { - const fileName = "./random/path/file.tsx"; - const result = Builder.getBasename(fileName); - chai_1.assert.equal(result, "file"); - }); - it("should not strip extensions from non-typescript filenames", () => { - const fileName = "./random/path/file.cs"; - const result = Builder.getBasename(fileName); - chai_1.assert.equal(result, "file.cs"); - }); - }); -}); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const chai_1 = require("chai"); +const fs = require("fs"); +const MockFs = require("mock-fs"); +const Sinon = require("sinon"); +const Builder = require("./builder"); +const FileSystem = require("./builders/fileSystem"); +const Flat = require("./builders/flat"); +const Modules = require("./modules"); +const TestUtilities = require("./testUtilities"); +// Gets a location from a list by name. +function getLocationByName(locations, name) { + return locations.filter((location) => location.name === name)[0]; +} +describe("builder/builder module has a", () => { + describe("buildBarrels function that", () => { + let directory; + let spySandbox; + let logger; + const runBuilder = (structure) => { + logger = spySandbox.spy(); + Builder.buildBarrels(directory.directories, { + barrelName: "barrel.ts", + logger, + quoteCharacter: "\"", + rootPath: ".", + structure, + }); + }; + beforeEach(() => { + MockFs(TestUtilities.mockFsConfiguration()); + directory = TestUtilities.mockDirectoryTree(); + spySandbox = Sinon.sandbox.create(); + spySandbox.stub(FileSystem, "buildFileSystemBarrel").returns("fileSystemContent"); + spySandbox.stub(Flat, "buildFlatBarrel").returns("flatContent"); + spySandbox.stub(Modules, "loadDirectoryModules").returns("loadedModules"); + }); + afterEach(() => { + MockFs.restore(); + spySandbox.restore(); + }); + describe("uses the structure option and", () => { + const testStructure = (structure, isFlat) => { + runBuilder(structure); + // TODO: Test arguments for barrel builder & loadDirectoryModules + if (isFlat) { + Sinon.assert.calledTwice(Flat.buildFlatBarrel); + Sinon.assert.notCalled(FileSystem.buildFileSystemBarrel); + } + else { + Sinon.assert.notCalled(Flat.buildFlatBarrel); + Sinon.assert.calledTwice(FileSystem.buildFileSystemBarrel); + } + }; + it("should use the flat builder if in flat mode", () => { + testStructure("flat", true); + }); + it("should use the filesystem builder if in filesystem mode", () => { + testStructure("filesystem", false); + }); + it("should use the flat builder if no mode is specified", () => { + testStructure(undefined, true); + }); + }); + it("should write each barrel's content to disk", () => { + runBuilder("flat"); + const checkContent = (address) => { + const result = fs.readFileSync(address, "utf8"); + chai_1.assert.equal(result, "flatContent"); + }; + checkContent("directory1/directory2/barrel.ts"); + checkContent("directory1/directory3/barrel.ts"); + }); + it("should update the directory structure with the new barrel", () => { + runBuilder("flat"); + directory.directories.forEach((subDirectory) => { + chai_1.assert.equal(subDirectory.barrel.name, "barrel.ts"); + }); + }); + it("should log useful information to the logger", () => { + runBuilder("flat"); + const messages = [ + "Building barrel @ directory1/directory2", + "Updating model barrel @ directory1/directory2/barrel.ts", + "Building barrel @ directory1/directory3", + "Updating model barrel @ directory1/directory3/barrel.ts", + ]; + chai_1.assert.equal(logger.callCount, messages.length); + messages.forEach((message, barrel) => { + chai_1.assert.equal(logger.getCall(barrel).args[0], message); + }); + }); + }); + describe("buildImportPath function that", () => { + let directory; + beforeEach(() => { + directory = TestUtilities.mockDirectoryTree(); + }); + it("should correctly build a path to a file in the same directory", () => { + const target = getLocationByName(directory.files, "index.ts"); + const result = Builder.buildImportPath(directory, target); + chai_1.assert.equal(result, "./index"); + }); + it("should correctly build a path to a file in a child directory", () => { + const childDirectory = getLocationByName(directory.directories, "directory2"); + const target = getLocationByName(childDirectory.files, "script.ts"); + const result = Builder.buildImportPath(directory, target); + chai_1.assert.equal(result, "./directory2/script"); + }); + }); + describe("getBasename function that", () => { + it("should correctly strip .ts from the filename", () => { + const fileName = "./random/path/file.ts"; + const result = Builder.getBasename(fileName); + chai_1.assert.equal(result, "file"); + }); + it("should correctly strip .tsx from the filename", () => { + const fileName = "./random/path/file.tsx"; + const result = Builder.getBasename(fileName); + chai_1.assert.equal(result, "file"); + }); + it("should not strip extensions from non-typescript filenames", () => { + const fileName = "./random/path/file.cs"; + const result = Builder.getBasename(fileName); + chai_1.assert.equal(result, "file.cs"); + }); + }); +}); //# sourceMappingURL=builder.test.js.map \ No newline at end of file diff --git a/bin/builders/fileSystem.js b/bin/builders/fileSystem.js index 78937cd6..e4398be0 100644 --- a/bin/builders/fileSystem.js +++ b/bin/builders/fileSystem.js @@ -1,73 +1,73 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const path = require("path"); -const utilities_1 = require("../utilities"); -const builder_1 = require("../builder"); -function stringify(structure, previousIndentation) { - const nextIndentation = previousIndentation + utilities_1.indentation; - let content = ""; - for (const key of Object.keys(structure).sort()) { +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const path = require("path"); +const utilities_1 = require("../utilities"); +const builder_1 = require("../builder"); +function stringify(structure, previousIndentation) { + const nextIndentation = previousIndentation + utilities_1.indentation; + let content = ""; + for (const key of Object.keys(structure).sort()) { content += ` -${nextIndentation}${key}: `; - const exported = structure[key]; - if (typeof exported === "string") { - content += exported; - } - else { - content += stringify(exported, nextIndentation); - } - content += ","; - } +${nextIndentation}${key}: `; + const exported = structure[key]; + if (typeof exported === "string") { + content += exported; + } + else { + content += stringify(exported, nextIndentation); + } + content += ","; + } return `{${content} -${previousIndentation}}`; -} -function buildStructureSubsection(structure, pathParts, name, reference) { - const pathPart = pathParts.shift(); - let subsection = pathPart === "." ? structure : structure[pathPart]; - if (!subsection) { - subsection = {}; - structure[pathPart] = subsection; - } - if (pathParts.length === 0) { - subsection[name] = reference; - } - else { - buildStructureSubsection(subsection, pathParts, name, reference); - } -} -// Comparator for alphabetically sorting imports by path. -// Does not need to check for equality, will only be used on distinct paths. -function compareImports(a, b) { - return a.path < b.path ? -1 : 1; -} -function buildFileSystemBarrel(directory, modules, options) { - const structure = {}; - let content = ""; - modules - .map((module) => ({ module, path: builder_1.buildImportPath(directory, module) })) - .sort(compareImports) - .forEach((imported) => { - const relativePath = path.relative(directory.path, imported.module.path); - const directoryPath = path.dirname(relativePath); - const parts = directoryPath.split(path.sep); - const alias = relativePath.replace(utilities_1.nonAlphaNumeric, ""); +${previousIndentation}}`; +} +function buildStructureSubsection(structure, pathParts, name, reference) { + const pathPart = pathParts.shift(); + let subsection = pathPart === "." ? structure : structure[pathPart]; + if (!subsection) { + subsection = {}; + structure[pathPart] = subsection; + } + if (pathParts.length === 0) { + subsection[name] = reference; + } + else { + buildStructureSubsection(subsection, pathParts, name, reference); + } +} +// Comparator for alphabetically sorting imports by path. +// Does not need to check for equality, will only be used on distinct paths. +function compareImports(a, b) { + return a.path < b.path ? -1 : 1; +} +function buildFileSystemBarrel(directory, modules, options) { + const structure = {}; + let content = ""; + modules + .map((module) => ({ module, path: builder_1.buildImportPath(directory, module) })) + .sort(compareImports) + .forEach((imported) => { + const relativePath = path.relative(directory.path, imported.module.path); + const directoryPath = path.dirname(relativePath); + const parts = directoryPath.split(path.sep); + const alias = relativePath.replace(utilities_1.nonAlphaNumeric, ""); content += `import * as ${alias} from ${options.quoteCharacter}${imported.path}${options.quoteCharacter}; -`; - const fileName = path.basename(imported.module.name, ".ts"); - buildStructureSubsection(structure, parts, fileName, alias); - }); - for (const key of Object.keys(structure).sort()) { - const exported = structure[key]; - if (typeof exported === "string") { +`; + const fileName = path.basename(imported.module.name, ".ts"); + buildStructureSubsection(structure, parts, fileName, alias); + }); + for (const key of Object.keys(structure).sort()) { + const exported = structure[key]; + if (typeof exported === "string") { content += `export {${exported} as ${key}}; -`; - } - else { +`; + } + else { content += `export const ${key} = ${stringify(exported, "")}; -`; - } - } - return content; -} -exports.buildFileSystemBarrel = buildFileSystemBarrel; +`; + } + } + return content; +} +exports.buildFileSystemBarrel = buildFileSystemBarrel; //# sourceMappingURL=fileSystem.js.map \ No newline at end of file diff --git a/bin/builders/fileSystem.test.js b/bin/builders/fileSystem.test.js index 42e3c222..4deac62e 100644 --- a/bin/builders/fileSystem.test.js +++ b/bin/builders/fileSystem.test.js @@ -1,23 +1,23 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const path = require("path"); -const TestUtilities = require("../testUtilities"); -const FileSystem = require("./fileSystem"); -describe("builder/fileSystem module has a", () => { - describe("buildFileSystemBarrel function that", () => { - describe("when using double quotes", () => { - let output; - const options = { - barrelName: "index.ts", - logger: () => void 0, - quoteCharacter: "\"", - rootPath: path.resolve("./"), - }; - beforeEach(() => { - const rootDirectory = TestUtilities.mockDirectoryTree(); - output = FileSystem.buildFileSystemBarrel(rootDirectory, TestUtilities.mockModules(rootDirectory), options); - }); - it("should produce the correct output", () => { +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const path = require("path"); +const TestUtilities = require("../testUtilities"); +const FileSystem = require("./fileSystem"); +describe("builder/fileSystem module has a", () => { + describe("buildFileSystemBarrel function that", () => { + describe("when using double quotes", () => { + let output; + const options = { + barrelName: "index.ts", + logger: () => void 0, + quoteCharacter: "\"", + rootPath: path.resolve("./"), + }; + beforeEach(() => { + const rootDirectory = TestUtilities.mockDirectoryTree(); + output = FileSystem.buildFileSystemBarrel(rootDirectory, TestUtilities.mockModules(rootDirectory), options); + }); + it("should produce the correct output", () => { TestUtilities.assertMultiLine(output, `import * as barrelts from "./barrel"; import * as directory2directory4deeplyNestedts from "./directory2/directory4/deeplyNested"; import * as directory2scriptts from "./directory2/script"; @@ -34,26 +34,26 @@ export const directory3 = { program: directory3programts, }; export {indexts as index}; -`); - }); - it("should produce output compatible with the recommended tslint ruleset", () => { - TestUtilities.tslint(output, options); - }); - }); - }); - describe("when using single quotes", () => { - let output; - const options = { - barrelName: "index.ts", - logger: () => void 0, - quoteCharacter: "'", - rootPath: path.resolve("./"), - }; - beforeEach(() => { - const rootDirectory = TestUtilities.mockDirectoryTree(); - output = FileSystem.buildFileSystemBarrel(rootDirectory, TestUtilities.mockModules(rootDirectory), options); - }); - it("should produce the correct output", () => { +`); + }); + it("should produce output compatible with the recommended tslint ruleset", () => { + TestUtilities.tslint(output, options); + }); + }); + }); + describe("when using single quotes", () => { + let output; + const options = { + barrelName: "index.ts", + logger: () => void 0, + quoteCharacter: "'", + rootPath: path.resolve("./"), + }; + beforeEach(() => { + const rootDirectory = TestUtilities.mockDirectoryTree(); + output = FileSystem.buildFileSystemBarrel(rootDirectory, TestUtilities.mockModules(rootDirectory), options); + }); + it("should produce the correct output", () => { TestUtilities.assertMultiLine(output, `import * as barrelts from './barrel'; import * as directory2directory4deeplyNestedts from './directory2/directory4/deeplyNested'; import * as directory2scriptts from './directory2/script'; @@ -70,11 +70,11 @@ export const directory3 = { program: directory3programts, }; export {indexts as index}; -`); - }); - it("should produce output compatible with the recommended tslint ruleset", () => { - TestUtilities.tslint(output, options); - }); - }); -}); +`); + }); + it("should produce output compatible with the recommended tslint ruleset", () => { + TestUtilities.tslint(output, options); + }); + }); +}); //# sourceMappingURL=fileSystem.test.js.map \ No newline at end of file diff --git a/bin/builders/flat.js b/bin/builders/flat.js index 4b3ae3d5..8e61d040 100644 --- a/bin/builders/flat.js +++ b/bin/builders/flat.js @@ -1,13 +1,13 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const builder_1 = require("../builder"); -function buildFlatBarrel(directory, modules, options) { - return modules.reduce((previous, current) => { - const importPath = builder_1.buildImportPath(directory, current); - options.logger(`Including path ${importPath}`); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const builder_1 = require("../builder"); +function buildFlatBarrel(directory, modules, options) { + return modules.reduce((previous, current) => { + const importPath = builder_1.buildImportPath(directory, current); + options.logger(`Including path ${importPath}`); return previous += `export * from ${options.quoteCharacter}${importPath}${options.quoteCharacter}; -`; - }, ""); -} -exports.buildFlatBarrel = buildFlatBarrel; +`; + }, ""); +} +exports.buildFlatBarrel = buildFlatBarrel; //# sourceMappingURL=flat.js.map \ No newline at end of file diff --git a/bin/builders/flat.test.js b/bin/builders/flat.test.js index 15a9c833..e2a3e29e 100644 --- a/bin/builders/flat.test.js +++ b/bin/builders/flat.test.js @@ -1,101 +1,101 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const chai_1 = require("chai"); -const Sinon = require("sinon"); -const TestUtilities = require("../testUtilities"); -const Flat = require("./flat"); -describe("builder/flat module has a", () => { - describe("buildFlatBarrel function that", () => { - describe("when using double quotes", () => { - let output; - let spySandbox; - let logger; - let options; - beforeEach(() => { - const directory = TestUtilities.mockDirectoryTree(); - spySandbox = Sinon.sandbox.create(); - logger = spySandbox.spy(); - options = { - barrelName: "barrel.ts", - logger, - quoteCharacter: "\"", - rootPath: ".", - }; - output = Flat.buildFlatBarrel(directory, TestUtilities.mockModules(directory), options); - }); - afterEach(() => { - spySandbox.restore(); - }); - it("should produce the correct output", () => { +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const chai_1 = require("chai"); +const Sinon = require("sinon"); +const TestUtilities = require("../testUtilities"); +const Flat = require("./flat"); +describe("builder/flat module has a", () => { + describe("buildFlatBarrel function that", () => { + describe("when using double quotes", () => { + let output; + let spySandbox; + let logger; + let options; + beforeEach(() => { + const directory = TestUtilities.mockDirectoryTree(); + spySandbox = Sinon.sandbox.create(); + logger = spySandbox.spy(); + options = { + barrelName: "barrel.ts", + logger, + quoteCharacter: "\"", + rootPath: ".", + }; + output = Flat.buildFlatBarrel(directory, TestUtilities.mockModules(directory), options); + }); + afterEach(() => { + spySandbox.restore(); + }); + it("should produce the correct output", () => { TestUtilities.assertMultiLine(output, `export * from "./barrel"; export * from "./index"; export * from "./directory2/script"; export * from "./directory2/directory4/deeplyNested"; export * from "./directory3/program"; -`); - }); - it("should log useful information to the logger", () => { - const messages = [ - "Including path ./barrel", - "Including path ./index", - "Including path ./directory2/script", - "Including path ./directory2/directory4/deeplyNested", - "Including path ./directory3/program", - ]; - chai_1.assert.equal(logger.callCount, messages.length); - messages.forEach((message, index) => { - chai_1.assert.equal(logger.getCall(index).args[0], message); - }); - }); - it("should produce output compatible with the recommended tslint ruleset", () => { - TestUtilities.tslint(output, options); - }); - }); - describe("when using single quotes", () => { - let output; - let spySandbox; - let logger; - let options; - beforeEach(() => { - const directory = TestUtilities.mockDirectoryTree(); - spySandbox = Sinon.sandbox.create(); - logger = spySandbox.spy(); - options = { - barrelName: "barrel.ts", - logger, - quoteCharacter: "'", - rootPath: ".", - }; - output = Flat.buildFlatBarrel(directory, TestUtilities.mockModules(directory), options); - }); - afterEach(() => { - spySandbox.restore(); - }); - it("should produce the correct output", () => { +`); + }); + it("should log useful information to the logger", () => { + const messages = [ + "Including path ./barrel", + "Including path ./index", + "Including path ./directory2/script", + "Including path ./directory2/directory4/deeplyNested", + "Including path ./directory3/program", + ]; + chai_1.assert.equal(logger.callCount, messages.length); + messages.forEach((message, index) => { + chai_1.assert.equal(logger.getCall(index).args[0], message); + }); + }); + it("should produce output compatible with the recommended tslint ruleset", () => { + TestUtilities.tslint(output, options); + }); + }); + describe("when using single quotes", () => { + let output; + let spySandbox; + let logger; + let options; + beforeEach(() => { + const directory = TestUtilities.mockDirectoryTree(); + spySandbox = Sinon.sandbox.create(); + logger = spySandbox.spy(); + options = { + barrelName: "barrel.ts", + logger, + quoteCharacter: "'", + rootPath: ".", + }; + output = Flat.buildFlatBarrel(directory, TestUtilities.mockModules(directory), options); + }); + afterEach(() => { + spySandbox.restore(); + }); + it("should produce the correct output", () => { TestUtilities.assertMultiLine(output, `export * from './barrel'; export * from './index'; export * from './directory2/script'; export * from './directory2/directory4/deeplyNested'; export * from './directory3/program'; -`); - }); - it("should log useful information to the logger", () => { - const messages = [ - "Including path ./barrel", - "Including path ./index", - "Including path ./directory2/script", - "Including path ./directory2/directory4/deeplyNested", - "Including path ./directory3/program", - ]; - chai_1.assert.equal(logger.callCount, messages.length); - messages.forEach((message, index) => { - chai_1.assert.equal(logger.getCall(index).args[0], message); - }); - }); - it("should produce output compatible with the recommended tslint ruleset", () => { - TestUtilities.tslint(output, options); - }); - }); - }); -}); +`); + }); + it("should log useful information to the logger", () => { + const messages = [ + "Including path ./barrel", + "Including path ./index", + "Including path ./directory2/script", + "Including path ./directory2/directory4/deeplyNested", + "Including path ./directory3/program", + ]; + chai_1.assert.equal(logger.callCount, messages.length); + messages.forEach((message, index) => { + chai_1.assert.equal(logger.getCall(index).args[0], message); + }); + }); + it("should produce output compatible with the recommended tslint ruleset", () => { + TestUtilities.tslint(output, options); + }); + }); + }); +}); //# sourceMappingURL=flat.test.js.map \ No newline at end of file diff --git a/bin/destinations.js b/bin/destinations.js index 7cba38c4..99c95ecf 100644 --- a/bin/destinations.js +++ b/bin/destinations.js @@ -1,47 +1,47 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const fileTree_1 = require("./fileTree"); -/** Assess which directories in the tree should contain barrels. */ -function getDestinations(rootTree, options) { - let destinations; - switch (options.location) { - case "top": - default: - destinations = [rootTree]; - break; - case "below": - destinations = rootTree.directories; - break; - case "all": - destinations = []; - fileTree_1.walkTree(rootTree, (directory) => { - destinations.push(directory); - }); - break; - case "replace": - destinations = []; - fileTree_1.walkTree(rootTree, (directory) => { - if (directory.files.some((location) => location.name === options.barrelName)) { - destinations.push(directory); - } - }); - break; - case "branch": - destinations = []; - fileTree_1.walkTree(rootTree, (directory) => { - if (directory.directories.length > 0) { - destinations.push(directory); - } - }); - break; - } - // Sort by length. This means barrels will be created deepest first. - destinations = destinations.sort((a, b) => { - return b.path.length - a.path.length; - }); - options.logger("Destinations:"); - destinations.forEach((destination) => options.logger(destination.path)); - return destinations; -} -exports.getDestinations = getDestinations; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const fileTree_1 = require("./fileTree"); +/** Assess which directories in the tree should contain barrels. */ +function getDestinations(rootTree, options) { + let destinations; + switch (options.location) { + case "top": + default: + destinations = [rootTree]; + break; + case "below": + destinations = rootTree.directories; + break; + case "all": + destinations = []; + fileTree_1.walkTree(rootTree, (directory) => { + destinations.push(directory); + }); + break; + case "replace": + destinations = []; + fileTree_1.walkTree(rootTree, (directory) => { + if (directory.files.some((location) => location.name === options.barrelName)) { + destinations.push(directory); + } + }); + break; + case "branch": + destinations = []; + fileTree_1.walkTree(rootTree, (directory) => { + if (directory.directories.length > 0) { + destinations.push(directory); + } + }); + break; + } + // Sort by length. This means barrels will be created deepest first. + destinations = destinations.sort((a, b) => { + return b.path.length - a.path.length; + }); + options.logger("Destinations:"); + destinations.forEach((destination) => options.logger(destination.path)); + return destinations; +} +exports.getDestinations = getDestinations; //# sourceMappingURL=destinations.js.map \ No newline at end of file diff --git a/bin/destinations.test.js b/bin/destinations.test.js index 566fbff4..71453590 100644 --- a/bin/destinations.test.js +++ b/bin/destinations.test.js @@ -1,63 +1,63 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const chai_1 = require("chai"); -const Destinations = require("./destinations"); -const TestUtilities = require("./testUtilities"); -describe("destinations module has a", () => { - describe("getDestinations function that", () => { - let directory; - let destinations; - let options; - let logged; - const testMode = (mode, getExpectedDestinations, expectedLogs) => { - describe(`when in '${mode}' mode`, () => { - beforeEach(() => { - options.location = mode; - destinations = Destinations.getDestinations(directory, options); - }); - it("should select the correct destinations", () => { - chai_1.assert.deepEqual(destinations, getExpectedDestinations()); - }); - it("should log useful information to the logger", () => { - chai_1.assert.deepEqual(logged, expectedLogs); - }); - }); - }; - beforeEach(() => { - directory = TestUtilities.mockDirectoryTree(); - logged = []; - options = TestUtilities.mockOptions(logged); - }); - testMode("top", () => [directory], [ - "Destinations:", - "./directory1", - ]); - testMode("below", () => directory.directories, [ - "Destinations:", - "directory1/directory2", - "directory1/directory3", - ]); - testMode("all", () => [ - directory.directories[0].directories[0], - directory.directories[0], - directory.directories[1], - directory, - ], [ - "Destinations:", - "directory1/directory2/directory4", - "directory1/directory2", - "directory1/directory3", - "./directory1", - ]); - testMode("replace", () => [directory], [ - "Destinations:", - "./directory1", - ]); - testMode("branch", () => [directory.directories[0], directory], [ - "Destinations:", - "directory1/directory2", - "./directory1", - ]); - }); -}); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const chai_1 = require("chai"); +const Destinations = require("./destinations"); +const TestUtilities = require("./testUtilities"); +describe("destinations module has a", () => { + describe("getDestinations function that", () => { + let directory; + let destinations; + let options; + let logged; + const testMode = (mode, getExpectedDestinations, expectedLogs) => { + describe(`when in '${mode}' mode`, () => { + beforeEach(() => { + options.location = mode; + destinations = Destinations.getDestinations(directory, options); + }); + it("should select the correct destinations", () => { + chai_1.assert.deepEqual(destinations, getExpectedDestinations()); + }); + it("should log useful information to the logger", () => { + chai_1.assert.deepEqual(logged, expectedLogs); + }); + }); + }; + beforeEach(() => { + directory = TestUtilities.mockDirectoryTree(); + logged = []; + options = TestUtilities.mockOptions(logged); + }); + testMode("top", () => [directory], [ + "Destinations:", + "./directory1", + ]); + testMode("below", () => directory.directories, [ + "Destinations:", + "directory1/directory2", + "directory1/directory3", + ]); + testMode("all", () => [ + directory.directories[0].directories[0], + directory.directories[0], + directory.directories[1], + directory, + ], [ + "Destinations:", + "directory1/directory2/directory4", + "directory1/directory2", + "directory1/directory3", + "./directory1", + ]); + testMode("replace", () => [directory], [ + "Destinations:", + "./directory1", + ]); + testMode("branch", () => [directory.directories[0], directory], [ + "Destinations:", + "directory1/directory2", + "./directory1", + ]); + }); +}); //# sourceMappingURL=destinations.test.js.map \ No newline at end of file diff --git a/bin/fileTree.js b/bin/fileTree.js index 095b7fd4..a0301ad1 100644 --- a/bin/fileTree.js +++ b/bin/fileTree.js @@ -1,45 +1,45 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const fs = require("fs"); -const path = require("path"); -const utilities_1 = require("./utilities"); -/** Build directory information recursively. */ -function buildTree(directory, options) { - options.logger(`Building directory tree for ${utilities_1.convertPathSeparator(directory)}`); - const names = fs.readdirSync(directory); - const result = { - directories: [], - files: [], - name: path.basename(directory), - path: utilities_1.convertPathSeparator(directory), - }; - names.forEach((name) => { - const fullPath = path.join(directory, name); - if (fs.statSync(fullPath).isDirectory()) { - result.directories.push(buildTree(fullPath, options)); - } - else { - const convertedPath = utilities_1.convertPathSeparator(fullPath); - const file = { - name, - path: convertedPath, - }; - result.files.push(file); - if (file.name === options.barrelName) { - options.logger(`Found existing barrel @ ${convertedPath}`); - result.barrel = file; - } - } - }); - return result; -} -exports.buildTree = buildTree; -/** Walk an entire directory tree recursively. */ -function walkTree(directory, callback) { - callback(directory); - for (const name of Object.keys(directory.directories)) { - walkTree(directory.directories[name], callback); - } -} -exports.walkTree = walkTree; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const fs = require("fs"); +const path = require("path"); +const utilities_1 = require("./utilities"); +/** Build directory information recursively. */ +function buildTree(directory, options) { + options.logger(`Building directory tree for ${utilities_1.convertPathSeparator(directory)}`); + const names = fs.readdirSync(directory); + const result = { + directories: [], + files: [], + name: path.basename(directory), + path: utilities_1.convertPathSeparator(directory), + }; + names.forEach((name) => { + const fullPath = path.join(directory, name); + if (fs.statSync(fullPath).isDirectory()) { + result.directories.push(buildTree(fullPath, options)); + } + else { + const convertedPath = utilities_1.convertPathSeparator(fullPath); + const file = { + name, + path: convertedPath, + }; + result.files.push(file); + if (file.name === options.barrelName) { + options.logger(`Found existing barrel @ ${convertedPath}`); + result.barrel = file; + } + } + }); + return result; +} +exports.buildTree = buildTree; +/** Walk an entire directory tree recursively. */ +function walkTree(directory, callback) { + callback(directory); + for (const name of Object.keys(directory.directories)) { + walkTree(directory.directories[name], callback); + } +} +exports.walkTree = walkTree; //# sourceMappingURL=fileTree.js.map \ No newline at end of file diff --git a/bin/fileTree.test.js b/bin/fileTree.test.js index a105d2cb..445801f8 100644 --- a/bin/fileTree.test.js +++ b/bin/fileTree.test.js @@ -1,89 +1,89 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const chai_1 = require("chai"); -const MockFs = require("mock-fs"); -const TestUtilities = require("./testUtilities"); -const FileTree = require("./fileTree"); -describe("fileTree module has a", () => { - describe("buildTree function that", () => { - let result; - let logged; - beforeEach(() => { - MockFs(TestUtilities.mockFsConfiguration()); - logged = []; - const logger = (message) => logged.push(message); - result = FileTree.buildTree("./directory1", { - barrelName: "barrel.ts", - logger, - quoteCharacter: "\"", - rootPath: "some/path", - }); - }); - afterEach(() => { - MockFs.restore(); - }); - it("should build a tree structure matching the file system directories", () => { - // Check the current directory. - chai_1.assert.equal(result.path, "./directory1"); - chai_1.assert.equal(result.name, "directory1"); - // Check for a child. - chai_1.assert.lengthOf(result.directories, 2); - const subDirectory = result.directories[0]; - // Check the child directory. - chai_1.assert.equal(subDirectory.path, "directory1/directory2"); - chai_1.assert.equal(subDirectory.name, "directory2"); - }); - it("should enumerate each file in a directory", () => { - chai_1.assert.lengthOf(result.files, 3); - const testFile = (name) => { - const files = result.files.filter((file) => file.name === name); - chai_1.assert.lengthOf(files, 1); - const firstFile = files[0]; - chai_1.assert.equal(firstFile.path, `directory1/${name}`); - chai_1.assert.equal(firstFile.name, name); - }; - testFile("index.ts"); - testFile("barrel.ts"); - testFile("ignore.txt"); - }); - it("should identify existing barrels in a directory", () => { - chai_1.assert.isNotNull(result.barrel); - const barrel = result.barrel; - // Test the barrel. - chai_1.assert.equal(barrel.name, "barrel.ts"); - chai_1.assert.equal(barrel.path, "directory1/barrel.ts"); - // Test it is in the files list. - chai_1.assert.notEqual(result.files.indexOf(barrel), -1); - // Check for a child. - chai_1.assert.lengthOf(result.directories, 2); - const subDirectory = result.directories[0]; - // Child shouldn't have a barrel. - chai_1.assert.isUndefined(subDirectory.barrel); - }); - it("should log useful information to the logger", () => { - chai_1.assert.sameMembers(logged, [ - "Building directory tree for ./directory1", - "Found existing barrel @ directory1/barrel.ts", - "Building directory tree for directory1/directory2", - "Building directory tree for directory1/directory2/directory4", - "Building directory tree for directory1/directory3", - ]); - }); - }); - describe("walkTree function that", () => { - it("should should call the callback once for each directory in the tree", () => { - const fakeTree = TestUtilities.mockDirectoryTree(); - // Build a collection all all directories. - let allDirectories = [fakeTree]; - fakeTree.directories.forEach((directory) => { - // Child/grandchild directories. - allDirectories = allDirectories.concat([directory]).concat(directory.directories); - }); - const calledDirectories = []; - const callback = (directory) => calledDirectories.push(directory); - FileTree.walkTree(fakeTree, callback); - chai_1.assert.deepEqual(allDirectories, calledDirectories); - }); - }); -}); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const chai_1 = require("chai"); +const MockFs = require("mock-fs"); +const TestUtilities = require("./testUtilities"); +const FileTree = require("./fileTree"); +describe("fileTree module has a", () => { + describe("buildTree function that", () => { + let result; + let logged; + beforeEach(() => { + MockFs(TestUtilities.mockFsConfiguration()); + logged = []; + const logger = (message) => logged.push(message); + result = FileTree.buildTree("./directory1", { + barrelName: "barrel.ts", + logger, + quoteCharacter: "\"", + rootPath: "some/path", + }); + }); + afterEach(() => { + MockFs.restore(); + }); + it("should build a tree structure matching the file system directories", () => { + // Check the current directory. + chai_1.assert.equal(result.path, "./directory1"); + chai_1.assert.equal(result.name, "directory1"); + // Check for a child. + chai_1.assert.lengthOf(result.directories, 2); + const subDirectory = result.directories[0]; + // Check the child directory. + chai_1.assert.equal(subDirectory.path, "directory1/directory2"); + chai_1.assert.equal(subDirectory.name, "directory2"); + }); + it("should enumerate each file in a directory", () => { + chai_1.assert.lengthOf(result.files, 3); + const testFile = (name) => { + const files = result.files.filter((file) => file.name === name); + chai_1.assert.lengthOf(files, 1); + const firstFile = files[0]; + chai_1.assert.equal(firstFile.path, `directory1/${name}`); + chai_1.assert.equal(firstFile.name, name); + }; + testFile("index.ts"); + testFile("barrel.ts"); + testFile("ignore.txt"); + }); + it("should identify existing barrels in a directory", () => { + chai_1.assert.isNotNull(result.barrel); + const barrel = result.barrel; + // Test the barrel. + chai_1.assert.equal(barrel.name, "barrel.ts"); + chai_1.assert.equal(barrel.path, "directory1/barrel.ts"); + // Test it is in the files list. + chai_1.assert.notEqual(result.files.indexOf(barrel), -1); + // Check for a child. + chai_1.assert.lengthOf(result.directories, 2); + const subDirectory = result.directories[0]; + // Child shouldn't have a barrel. + chai_1.assert.isUndefined(subDirectory.barrel); + }); + it("should log useful information to the logger", () => { + chai_1.assert.sameMembers(logged, [ + "Building directory tree for ./directory1", + "Found existing barrel @ directory1/barrel.ts", + "Building directory tree for directory1/directory2", + "Building directory tree for directory1/directory2/directory4", + "Building directory tree for directory1/directory3", + ]); + }); + }); + describe("walkTree function that", () => { + it("should should call the callback once for each directory in the tree", () => { + const fakeTree = TestUtilities.mockDirectoryTree(); + // Build a collection all all directories. + let allDirectories = [fakeTree]; + fakeTree.directories.forEach((directory) => { + // Child/grandchild directories. + allDirectories = allDirectories.concat([directory]).concat(directory.directories); + }); + const calledDirectories = []; + const callback = (directory) => calledDirectories.push(directory); + FileTree.walkTree(fakeTree, callback); + chai_1.assert.deepEqual(allDirectories, calledDirectories); + }); + }); +}); //# sourceMappingURL=fileTree.test.js.map \ No newline at end of file diff --git a/bin/index.js b/bin/index.js index 0e3ac9a7..65ace48c 100755 --- a/bin/index.js +++ b/bin/index.js @@ -1,19 +1,19 @@ -#! /usr/bin/env node -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const builder_1 = require("./builder"); -const destinations_1 = require("./destinations"); -const fileTree_1 = require("./fileTree"); -const options_1 = require("./options"); -const purge_1 = require("./purge"); -// Get the launch options/arguments. -const options = options_1.getOptions(); -// Build the directory tree. -const rootTree = fileTree_1.buildTree(options.rootPath, options); -// Work out which directories should have barrels. -const destinations = destinations_1.getDestinations(rootTree, options); -// Potentially there are some existing barrels that need removing. -purge_1.purge(rootTree, options); -// Create the barrels. -builder_1.buildBarrels(destinations, options); +#! /usr/bin/env node +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const builder_1 = require("./builder"); +const destinations_1 = require("./destinations"); +const fileTree_1 = require("./fileTree"); +const options_1 = require("./options"); +const purge_1 = require("./purge"); +// Get the launch options/arguments. +const options = options_1.getOptions(); +// Build the directory tree. +const rootTree = fileTree_1.buildTree(options.rootPath, options); +// Work out which directories should have barrels. +const destinations = destinations_1.getDestinations(rootTree, options); +// Potentially there are some existing barrels that need removing. +purge_1.purge(rootTree, options); +// Create the barrels. +builder_1.buildBarrels(destinations, options); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/bin/modules.js b/bin/modules.js index afbdaa2c..48cde5d7 100644 --- a/bin/modules.js +++ b/bin/modules.js @@ -1,65 +1,65 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const utilities_1 = require("./utilities"); -// Get any typescript modules contained at any depth in the current directory. -function getModules(directory, options) { - options.logger(`Getting modules @ ${directory.path}`); - if (directory.barrel) { - // If theres a barrel then use that as it *should* contain descendant modules. - options.logger(`Found existing barrel @ ${directory.barrel.path}`); - return [directory.barrel]; - } - const files = [].concat(directory.files); - directory.directories.forEach((childDirectory) => { - // Recurse. - files.push(...getModules(childDirectory, options)); - }); - // Only return files that look like TypeScript modules. - return files.filter((file) => file.name.match(utilities_1.isTypeScriptFile)); -} -function buildFilters(options) { - // Filter a set of modules down to those matching the include/exclude rules. - function buildRegexList(patterns) { - if (!Array.isArray(patterns)) { - return []; - } - return patterns.map((pattern) => new RegExp(pattern)); - } - return { - blacklists: buildRegexList(options.exclude), - whitelists: buildRegexList(options.include), - }; -} -function filterModules(filters, locations, options) { - let result = locations; - if (filters.whitelists.length > 0) { - result = result.filter((location) => { - return filters.whitelists.some((test) => { - const isMatch = !!location.path.match(test); - if (isMatch) { - options.logger(`${location.path} is included by ${test}`); - } - return isMatch; - }); - }); - } - if (filters.blacklists.length > 0) { - result = result.filter((location) => { - return !filters.blacklists.some((test) => { - const isMatch = !!location.path.match(test); - if (isMatch) { - options.logger(`${location.path} is excluded by ${test}`); - } - return isMatch; - }); - }); - } - return result; -} -function loadDirectoryModules(directory, options) { - const modules = getModules(directory, options); - const filters = buildFilters(options); - return filterModules(filters, modules, options); -} -exports.loadDirectoryModules = loadDirectoryModules; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const utilities_1 = require("./utilities"); +// Get any typescript modules contained at any depth in the current directory. +function getModules(directory, options) { + options.logger(`Getting modules @ ${directory.path}`); + if (directory.barrel) { + // If theres a barrel then use that as it *should* contain descendant modules. + options.logger(`Found existing barrel @ ${directory.barrel.path}`); + return [directory.barrel]; + } + const files = [].concat(directory.files); + directory.directories.forEach((childDirectory) => { + // Recurse. + files.push(...getModules(childDirectory, options)); + }); + // Only return files that look like TypeScript modules. + return files.filter((file) => file.name.match(utilities_1.isTypeScriptFile)); +} +function buildFilters(options) { + // Filter a set of modules down to those matching the include/exclude rules. + function buildRegexList(patterns) { + if (!Array.isArray(patterns)) { + return []; + } + return patterns.map((pattern) => new RegExp(pattern)); + } + return { + blacklists: buildRegexList(options.exclude), + whitelists: buildRegexList(options.include), + }; +} +function filterModules(filters, locations, options) { + let result = locations; + if (filters.whitelists.length > 0) { + result = result.filter((location) => { + return filters.whitelists.some((test) => { + const isMatch = !!location.path.match(test); + if (isMatch) { + options.logger(`${location.path} is included by ${test}`); + } + return isMatch; + }); + }); + } + if (filters.blacklists.length > 0) { + result = result.filter((location) => { + return !filters.blacklists.some((test) => { + const isMatch = !!location.path.match(test); + if (isMatch) { + options.logger(`${location.path} is excluded by ${test}`); + } + return isMatch; + }); + }); + } + return result; +} +function loadDirectoryModules(directory, options) { + const modules = getModules(directory, options); + const filters = buildFilters(options); + return filterModules(filters, modules, options); +} +exports.loadDirectoryModules = loadDirectoryModules; //# sourceMappingURL=modules.js.map \ No newline at end of file diff --git a/bin/modules.test.js b/bin/modules.test.js index bb123f23..bf46b56b 100644 --- a/bin/modules.test.js +++ b/bin/modules.test.js @@ -1,97 +1,97 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const chai_1 = require("chai"); -const Modules = require("./modules"); -const TestUtilities = require("./testUtilities"); -describe("builder/modules module has a", () => { - describe("loadDirectoryModules function that", () => { - let directory; - let logged; - let options; - beforeEach(() => { - directory = TestUtilities.mockDirectoryTree(); - logged = []; - options = TestUtilities.mockOptions(logged); - }); - it("should identify modules from directories recursively", () => { - const result = Modules.loadDirectoryModules(directory.directories[0], options); - chai_1.assert.lengthOf(result, 2); - chai_1.assert.deepEqual(result[0], { - name: "script.ts", - path: "directory1/directory2/script.ts", - }); - chai_1.assert.deepEqual(result[1], { - name: "deeplyNested.ts", - path: "directory1/directory2/directory4/deeplyNested.ts", - }); - }); - it("should identify directories that already contain a barrel", () => { - // Set up a barrel. - const targetDirectory = directory.directories[0]; - targetDirectory.barrel = targetDirectory.files[0]; - const result = Modules.loadDirectoryModules(directory.directories[0], options); - chai_1.assert.lengthOf(result, 1); - chai_1.assert.deepEqual(result[0], { - name: "script.ts", - path: "directory1/directory2/script.ts", - }); - }); - it("should only include TypeScript files", () => { - const result = Modules.loadDirectoryModules(directory, options); - result.forEach((location) => chai_1.assert.notEqual(location.name, "ignore.txt")); - }); - it("should only include files matching a whitelist option when specified", () => { - options.include = ["directory2"]; - const result = Modules.loadDirectoryModules(directory, options); - chai_1.assert.lengthOf(result, 2); - chai_1.assert.deepEqual(result[0], { - name: "script.ts", - path: "directory1/directory2/script.ts", - }); - chai_1.assert.deepEqual(result[1], { - name: "deeplyNested.ts", - path: "directory1/directory2/directory4/deeplyNested.ts", - }); - }); - it("should exclude files matching a blacklist option when specified", () => { - options.exclude = ["directory2"]; - const result = Modules.loadDirectoryModules(directory, options); - chai_1.assert.lengthOf(result, 3); - chai_1.assert.deepEqual(result[0], { - name: "barrel.ts", - path: "directory1/barrel.ts", - }); - chai_1.assert.deepEqual(result[1], { - name: "index.ts", - path: "directory1/index.ts", - }); - chai_1.assert.deepEqual(result[2], { - name: "program.ts", - path: "directory1/directory3/program.ts", - }); - }); - it("should correctly handle both whitelist and blacklist options being set", () => { - options.include = ["directory2"]; - options.exclude = ["directory4"]; - const result = Modules.loadDirectoryModules(directory, options); - chai_1.assert.lengthOf(result, 1); - chai_1.assert.deepEqual(result[0], { - name: "script.ts", - path: "directory1/directory2/script.ts", - }); - }); - it("should log useful information to the logger", () => { - // Set up a barrel. - const indexedDirectory = directory.directories[0]; - indexedDirectory.barrel = indexedDirectory.files[0]; - Modules.loadDirectoryModules(directory, options); - chai_1.assert.deepEqual(logged, [ - "Getting modules @ ./directory1", - "Getting modules @ directory1/directory2", - "Found existing barrel @ directory1/directory2/script.ts", - "Getting modules @ directory1/directory3", - ]); - }); - }); -}); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const chai_1 = require("chai"); +const Modules = require("./modules"); +const TestUtilities = require("./testUtilities"); +describe("builder/modules module has a", () => { + describe("loadDirectoryModules function that", () => { + let directory; + let logged; + let options; + beforeEach(() => { + directory = TestUtilities.mockDirectoryTree(); + logged = []; + options = TestUtilities.mockOptions(logged); + }); + it("should identify modules from directories recursively", () => { + const result = Modules.loadDirectoryModules(directory.directories[0], options); + chai_1.assert.lengthOf(result, 2); + chai_1.assert.deepEqual(result[0], { + name: "script.ts", + path: "directory1/directory2/script.ts", + }); + chai_1.assert.deepEqual(result[1], { + name: "deeplyNested.ts", + path: "directory1/directory2/directory4/deeplyNested.ts", + }); + }); + it("should identify directories that already contain a barrel", () => { + // Set up a barrel. + const targetDirectory = directory.directories[0]; + targetDirectory.barrel = targetDirectory.files[0]; + const result = Modules.loadDirectoryModules(directory.directories[0], options); + chai_1.assert.lengthOf(result, 1); + chai_1.assert.deepEqual(result[0], { + name: "script.ts", + path: "directory1/directory2/script.ts", + }); + }); + it("should only include TypeScript files", () => { + const result = Modules.loadDirectoryModules(directory, options); + result.forEach((location) => chai_1.assert.notEqual(location.name, "ignore.txt")); + }); + it("should only include files matching a whitelist option when specified", () => { + options.include = ["directory2"]; + const result = Modules.loadDirectoryModules(directory, options); + chai_1.assert.lengthOf(result, 2); + chai_1.assert.deepEqual(result[0], { + name: "script.ts", + path: "directory1/directory2/script.ts", + }); + chai_1.assert.deepEqual(result[1], { + name: "deeplyNested.ts", + path: "directory1/directory2/directory4/deeplyNested.ts", + }); + }); + it("should exclude files matching a blacklist option when specified", () => { + options.exclude = ["directory2"]; + const result = Modules.loadDirectoryModules(directory, options); + chai_1.assert.lengthOf(result, 3); + chai_1.assert.deepEqual(result[0], { + name: "barrel.ts", + path: "directory1/barrel.ts", + }); + chai_1.assert.deepEqual(result[1], { + name: "index.ts", + path: "directory1/index.ts", + }); + chai_1.assert.deepEqual(result[2], { + name: "program.ts", + path: "directory1/directory3/program.ts", + }); + }); + it("should correctly handle both whitelist and blacklist options being set", () => { + options.include = ["directory2"]; + options.exclude = ["directory4"]; + const result = Modules.loadDirectoryModules(directory, options); + chai_1.assert.lengthOf(result, 1); + chai_1.assert.deepEqual(result[0], { + name: "script.ts", + path: "directory1/directory2/script.ts", + }); + }); + it("should log useful information to the logger", () => { + // Set up a barrel. + const indexedDirectory = directory.directories[0]; + indexedDirectory.barrel = indexedDirectory.files[0]; + Modules.loadDirectoryModules(directory, options); + chai_1.assert.deepEqual(logged, [ + "Getting modules @ ./directory1", + "Getting modules @ directory1/directory2", + "Found existing barrel @ directory1/directory2/script.ts", + "Getting modules @ directory1/directory3", + ]); + }); + }); +}); //# sourceMappingURL=modules.test.js.map \ No newline at end of file diff --git a/bin/options.js b/bin/options.js index 95949792..975bfc66 100644 --- a/bin/options.js +++ b/bin/options.js @@ -1,69 +1,69 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const path = require("path"); -const Yargs = require("yargs"); -const utilities_1 = require("./utilities"); -function setUpArguments() { - return Yargs - .usage("Usage: barrelsby [options]") - .example("barrelsby", "Run barrelsby") - .config("c") - .alias("c", "config") - .describe("c", "The location of the config file.") - .string("d") - .alias("d", "directory") - .nargs("d", 1) - .describe("d", "The directory to create barrels for.") - .default("d", "./") - .boolean("D") - .alias("D", "delete") - .describe("D", "Delete existing barrel files.") - .default("D", false) - .array("e") - .alias("e", "exclude") - .describe("e", "Excludes any files whose paths match any of the regular expressions.") - .help("h") - .alias("h", "help") - .default("h", false) - .array("i") - .alias("i", "include") - .describe("i", "Only include files whose paths match any of the regular expressions.") - .string("l") - .alias("l", "location") - .describe("l", "The mode for picking barrel file locations") - .choices("l", ["top", "below", "all", "replace", "branch"]) - .default("l", "top") - .string("n") - .alias("n", "name") - .describe("n", "The name to give barrel files") - .default("n", "index") - .string("s") - .alias("s", "structure") - .describe("s", "The mode for structuring barrel file exports") - .choices("s", ["flat", "filesystem"]) - .default("s", "flat") - .boolean("q") - .alias("q", "singleQuotes") - .describe("q", "Use single quotes for paths instead of the default double quotes") - .default("q", false) - .version() - .alias("v", "version") - .default("v", false) - .boolean("V") - .alias("V", "verbose") - .describe("V", "Display additional logging information") - .default("V", false); -} -function getOptions() { - const options = setUpArguments().argv; - options.logger = options.verbose ? console.log : new Function("return void(0);"); - options.rootPath = path.resolve(options.directory); - options.quoteCharacter = options.singleQuotes ? "'" : "\""; - // Resolve barrel name. - const nameArgument = options.name; - options.barrelName = nameArgument.match(utilities_1.isTypeScriptFile) ? nameArgument : `${nameArgument}.ts`; - options.logger(`Using name ${options.barrelName}`); - return options; -} -exports.getOptions = getOptions; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const path = require("path"); +const Yargs = require("yargs"); +const utilities_1 = require("./utilities"); +function setUpArguments() { + return Yargs + .usage("Usage: barrelsby [options]") + .example("barrelsby", "Run barrelsby") + .config("c") + .alias("c", "config") + .describe("c", "The location of the config file.") + .string("d") + .alias("d", "directory") + .nargs("d", 1) + .describe("d", "The directory to create barrels for.") + .default("d", "./") + .boolean("D") + .alias("D", "delete") + .describe("D", "Delete existing barrel files.") + .default("D", false) + .array("e") + .alias("e", "exclude") + .describe("e", "Excludes any files whose paths match any of the regular expressions.") + .help("h") + .alias("h", "help") + .default("h", false) + .array("i") + .alias("i", "include") + .describe("i", "Only include files whose paths match any of the regular expressions.") + .string("l") + .alias("l", "location") + .describe("l", "The mode for picking barrel file locations") + .choices("l", ["top", "below", "all", "replace", "branch"]) + .default("l", "top") + .string("n") + .alias("n", "name") + .describe("n", "The name to give barrel files") + .default("n", "index") + .string("s") + .alias("s", "structure") + .describe("s", "The mode for structuring barrel file exports") + .choices("s", ["flat", "filesystem"]) + .default("s", "flat") + .boolean("q") + .alias("q", "singleQuotes") + .describe("q", "Use single quotes for paths instead of the default double quotes") + .default("q", false) + .version() + .alias("v", "version") + .default("v", false) + .boolean("V") + .alias("V", "verbose") + .describe("V", "Display additional logging information") + .default("V", false); +} +function getOptions() { + const options = setUpArguments().argv; + options.logger = options.verbose ? console.log : new Function("return void(0);"); + options.rootPath = path.resolve(options.directory); + options.quoteCharacter = options.singleQuotes ? "'" : "\""; + // Resolve barrel name. + const nameArgument = options.name; + options.barrelName = nameArgument.match(utilities_1.isTypeScriptFile) ? nameArgument : `${nameArgument}.ts`; + options.logger(`Using name ${options.barrelName}`); + return options; +} +exports.getOptions = getOptions; //# sourceMappingURL=options.js.map \ No newline at end of file diff --git a/bin/options.test.js b/bin/options.test.js index 54e673cd..b9df0931 100644 --- a/bin/options.test.js +++ b/bin/options.test.js @@ -1,52 +1,52 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const chai_1 = require("chai"); -const Yargs = require("yargs"); -const Options = require("./options"); -describe("options module has a", () => { - describe("getOptions function that", () => { - it("should load the configuration options", () => { - Yargs([ - "--delete", - "--directory", - "./test", - "--exclude", - "zeta.ts$", - "--include", - "a.ts$", - "--location", - "top", - "--name", - "barrel", - "--structure", - "filesystem", - "--verbose", - ]); - const options = Options.getOptions(); - chai_1.assert.equal(options.logger, console.log); - chai_1.assert.match(options.rootPath, /test$/); - chai_1.assert.equal(options.barrelName, "barrel.ts"); - // From yargs - chai_1.assert.isUndefined(options.config); - chai_1.assert.equal(options.delete, true); - chai_1.assert.equal(options.directory, "./test"); - chai_1.assert.sameMembers(options.exclude, ["zeta.ts$"]); - chai_1.assert.sameMembers(options.include, ["a.ts$"]); - chai_1.assert.equal(options.location, "top"); - chai_1.assert.equal(options.name, "barrel"); - chai_1.assert.equal(options.structure, "filesystem"); - chai_1.assert.equal(options.verbose, true); - }); - it("should not use the console if logging is disabled", () => { - Yargs([]); - const options = Options.getOptions(); - chai_1.assert.notEqual(options.logger, console.log); - }); - it("should not append .ts to the name option if already present", () => { - Yargs(["--name", "barrel.ts"]); - const options = Options.getOptions(); - chai_1.assert.equal(options.barrelName, "barrel.ts"); - }); - }); -}); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const chai_1 = require("chai"); +const Yargs = require("yargs"); +const Options = require("./options"); +describe("options module has a", () => { + describe("getOptions function that", () => { + it("should load the configuration options", () => { + Yargs([ + "--delete", + "--directory", + "./test", + "--exclude", + "zeta.ts$", + "--include", + "a.ts$", + "--location", + "top", + "--name", + "barrel", + "--structure", + "filesystem", + "--verbose", + ]); + const options = Options.getOptions(); + chai_1.assert.equal(options.logger, console.log); + chai_1.assert.match(options.rootPath, /test$/); + chai_1.assert.equal(options.barrelName, "barrel.ts"); + // From yargs + chai_1.assert.isUndefined(options.config); + chai_1.assert.equal(options.delete, true); + chai_1.assert.equal(options.directory, "./test"); + chai_1.assert.sameMembers(options.exclude, ["zeta.ts$"]); + chai_1.assert.sameMembers(options.include, ["a.ts$"]); + chai_1.assert.equal(options.location, "top"); + chai_1.assert.equal(options.name, "barrel"); + chai_1.assert.equal(options.structure, "filesystem"); + chai_1.assert.equal(options.verbose, true); + }); + it("should not use the console if logging is disabled", () => { + Yargs([]); + const options = Options.getOptions(); + chai_1.assert.notEqual(options.logger, console.log); + }); + it("should not append .ts to the name option if already present", () => { + Yargs(["--name", "barrel.ts"]); + const options = Options.getOptions(); + chai_1.assert.equal(options.barrelName, "barrel.ts"); + }); + }); +}); //# sourceMappingURL=options.test.js.map \ No newline at end of file diff --git a/bin/purge.js b/bin/purge.js index 77180748..4d66b754 100644 --- a/bin/purge.js +++ b/bin/purge.js @@ -1,24 +1,24 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const fs = require("fs"); -const fileTree_1 = require("./fileTree"); -function purge(rootTree, options) { - // Delete any existing barrels. - if (options.delete) { - fileTree_1.walkTree(rootTree, (directory) => { - directory.files - .filter((file) => { - return file.name === options.barrelName; - }) - .forEach((file) => { - options.logger(`Deleting existing barrel @ ${file.path}`); - // Delete barrel file and clean up tree model. - fs.unlinkSync(file.path); - directory.files.splice(directory.files.indexOf(file), 1); - directory.barrel = undefined; - }); - }); - } -} -exports.purge = purge; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const fs = require("fs"); +const fileTree_1 = require("./fileTree"); +function purge(rootTree, options) { + // Delete any existing barrels. + if (options.delete) { + fileTree_1.walkTree(rootTree, (directory) => { + directory.files + .filter((file) => { + return file.name === options.barrelName; + }) + .forEach((file) => { + options.logger(`Deleting existing barrel @ ${file.path}`); + // Delete barrel file and clean up tree model. + fs.unlinkSync(file.path); + directory.files.splice(directory.files.indexOf(file), 1); + directory.barrel = undefined; + }); + }); + } +} +exports.purge = purge; //# sourceMappingURL=purge.js.map \ No newline at end of file diff --git a/bin/purge.test.js b/bin/purge.test.js index 1fed6b89..5e73a2dd 100644 --- a/bin/purge.test.js +++ b/bin/purge.test.js @@ -1,47 +1,47 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const chai_1 = require("chai"); -const fs = require("fs"); -const MockFs = require("mock-fs"); -const TestUtilities = require("./testUtilities"); -const Purge = require("./purge"); -describe("purge module has a", () => { - describe("purge function that", () => { - let directory; - let options; - let logged; - beforeEach(() => { - MockFs(TestUtilities.mockFsConfiguration()); - directory = TestUtilities.mockDirectoryTree(); - logged = []; - options = TestUtilities.mockOptions(logged); - }); - afterEach(() => { - MockFs.restore(); - }); - it("should delete existing barrels if the delete flag is enabled", () => { - options.delete = true; - Purge.purge(directory, options); - // Check directory has been manipulated. - chai_1.assert.lengthOf(directory.files, 2); - chai_1.assert.lengthOf(directory.files.filter((file) => file.name === "barrel.ts"), 0); - // Check FS has been manipulated. - chai_1.assert.isNotOk(fs.existsSync("directory1/barrel.ts")); - }); - it("should do nothing if the delete flag is disabled", () => { - options.delete = false; - Purge.purge(directory, options); - // Check directory has not been manipulated. - chai_1.assert.lengthOf(directory.files, 3); - chai_1.assert.lengthOf(directory.files.filter((file) => file.name === "barrel.ts"), 1); - // Check FS has not been manipulated. - chai_1.assert.isOk(fs.existsSync("directory1/barrel.ts")); - }); - it("should log useful information to the logger", () => { - options.delete = true; - Purge.purge(directory, options); - chai_1.assert.sameMembers(logged, ["Deleting existing barrel @ directory1/barrel.ts"]); - }); - }); -}); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const chai_1 = require("chai"); +const fs = require("fs"); +const MockFs = require("mock-fs"); +const TestUtilities = require("./testUtilities"); +const Purge = require("./purge"); +describe("purge module has a", () => { + describe("purge function that", () => { + let directory; + let options; + let logged; + beforeEach(() => { + MockFs(TestUtilities.mockFsConfiguration()); + directory = TestUtilities.mockDirectoryTree(); + logged = []; + options = TestUtilities.mockOptions(logged); + }); + afterEach(() => { + MockFs.restore(); + }); + it("should delete existing barrels if the delete flag is enabled", () => { + options.delete = true; + Purge.purge(directory, options); + // Check directory has been manipulated. + chai_1.assert.lengthOf(directory.files, 2); + chai_1.assert.lengthOf(directory.files.filter((file) => file.name === "barrel.ts"), 0); + // Check FS has been manipulated. + chai_1.assert.isNotOk(fs.existsSync("directory1/barrel.ts")); + }); + it("should do nothing if the delete flag is disabled", () => { + options.delete = false; + Purge.purge(directory, options); + // Check directory has not been manipulated. + chai_1.assert.lengthOf(directory.files, 3); + chai_1.assert.lengthOf(directory.files.filter((file) => file.name === "barrel.ts"), 1); + // Check FS has not been manipulated. + chai_1.assert.isOk(fs.existsSync("directory1/barrel.ts")); + }); + it("should log useful information to the logger", () => { + options.delete = true; + Purge.purge(directory, options); + chai_1.assert.sameMembers(logged, ["Deleting existing barrel @ directory1/barrel.ts"]); + }); + }); +}); //# sourceMappingURL=purge.test.js.map \ No newline at end of file diff --git a/bin/testUtilities.js b/bin/testUtilities.js index 8a684928..8e0653dd 100644 --- a/bin/testUtilities.js +++ b/bin/testUtilities.js @@ -1,122 +1,122 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const chai_1 = require("chai"); -const tslint_1 = require("tslint"); -function mockFsConfiguration() { - return { - "code.ts": "export const code = 'Hello Saturn!'", - "directory1": { - "barrel.ts": "export const code = 'Hello Graham!'", - "directory2": { - "directory4": { - "deeplyNested.ts": "export const code = 'Heya Wurrl", - }, - "script.ts": "export const code = 'Hello Detroit!'", - }, - "directory3": { - "program.ts": "export const code = 'Hello Detroit!'", - }, - "ignore.txt": "export const code = 'Goodbye World!'", - "index.ts": "export const code = 'Hello World!'", - }, - }; -} -exports.mockFsConfiguration = mockFsConfiguration; -function mockDirectoryTree() { - return { - directories: [ - { - directories: [ - { - directories: [], - files: [ - { - name: "deeplyNested.ts", - path: "directory1/directory2/directory4/deeplyNested.ts", - }, - ], - name: "directory4", - path: "directory1/directory2/directory4", - }, - ], - files: [ - { - name: "script.ts", - path: "directory1/directory2/script.ts", - }, - ], - name: "directory2", - path: "directory1/directory2", - }, - { - directories: [], - files: [ - { - name: "program.ts", - path: "directory1/directory3/program.ts", - }, - ], - name: "directory3", - path: "directory1/directory3", - }, - ], - files: [ - { - name: "barrel.ts", - path: "directory1/barrel.ts", - }, - { - name: "ignore.txt", - path: "directory1/ignore.txt", - }, - { - name: "index.ts", - path: "directory1/index.ts", - }, - ], - name: "directory1", - path: "./directory1", - }; -} -exports.mockDirectoryTree = mockDirectoryTree; -function mockModules(rootDirectory) { - const getModules = (directory) => directory.directories.reduce((previous, current) => { - return previous.concat(getModules(current)); - }, directory.files); - return getModules(rootDirectory).filter((module) => module.name.indexOf(".ts") >= 0); -} -exports.mockModules = mockModules; -// Gets a mock Options object. -function mockOptions(loggerTarget) { - return { - barrelName: "barrel.ts", - logger: (message) => loggerTarget.push(message), - quoteCharacter: "\"", - rootPath: "some/path", - }; -} -exports.mockOptions = mockOptions; -// Multiline string assertion to give more useful output messages. -function assertMultiLine(actual, expected) { - const actualParts = actual.split("\n"); - const expectParts = expected.split("\n"); - chai_1.assert.equal(actualParts.length, expectParts.length, "Different numbers of lines"); - actualParts.forEach((actualPart, index) => { - chai_1.assert.equal(actualPart, expectParts[index]); - }); -} -exports.assertMultiLine = assertMultiLine; -// Runs tslint against the specified file and checks there are no errors. -function tslint(content, options) { - const linter = new tslint_1.Linter({ fix: false, formatter: "json" }); - const configuration = tslint_1.Configuration.loadConfigurationFromPath("./tslint.json"); - if (options.quoteCharacter === "'") { - configuration.rules.set("quotemark", { ruleArguments: ["single", "avoid-escape"] }); - } - console.info(configuration.rules.get("quotemark")); //tslint:disable-line - linter.lint("test_output.ts", content, configuration); - const failures = linter.getResult().failures.map((failure) => `${failure.getRuleName()} ${failure.getStartPosition().getLineAndCharacter().line}`); - chai_1.assert.deepEqual(failures, []); -} -exports.tslint = tslint; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const chai_1 = require("chai"); +const tslint_1 = require("tslint"); +function mockFsConfiguration() { + return { + "code.ts": "export const code = 'Hello Saturn!'", + "directory1": { + "barrel.ts": "export const code = 'Hello Graham!'", + "directory2": { + "directory4": { + "deeplyNested.ts": "export const code = 'Heya Wurrl", + }, + "script.ts": "export const code = 'Hello Detroit!'", + }, + "directory3": { + "program.ts": "export const code = 'Hello Detroit!'", + }, + "ignore.txt": "export const code = 'Goodbye World!'", + "index.ts": "export const code = 'Hello World!'", + }, + }; +} +exports.mockFsConfiguration = mockFsConfiguration; +function mockDirectoryTree() { + return { + directories: [ + { + directories: [ + { + directories: [], + files: [ + { + name: "deeplyNested.ts", + path: "directory1/directory2/directory4/deeplyNested.ts", + }, + ], + name: "directory4", + path: "directory1/directory2/directory4", + }, + ], + files: [ + { + name: "script.ts", + path: "directory1/directory2/script.ts", + }, + ], + name: "directory2", + path: "directory1/directory2", + }, + { + directories: [], + files: [ + { + name: "program.ts", + path: "directory1/directory3/program.ts", + }, + ], + name: "directory3", + path: "directory1/directory3", + }, + ], + files: [ + { + name: "barrel.ts", + path: "directory1/barrel.ts", + }, + { + name: "ignore.txt", + path: "directory1/ignore.txt", + }, + { + name: "index.ts", + path: "directory1/index.ts", + }, + ], + name: "directory1", + path: "./directory1", + }; +} +exports.mockDirectoryTree = mockDirectoryTree; +function mockModules(rootDirectory) { + const getModules = (directory) => directory.directories.reduce((previous, current) => { + return previous.concat(getModules(current)); + }, directory.files); + return getModules(rootDirectory).filter((module) => module.name.indexOf(".ts") >= 0); +} +exports.mockModules = mockModules; +// Gets a mock Options object. +function mockOptions(loggerTarget) { + return { + barrelName: "barrel.ts", + logger: (message) => loggerTarget.push(message), + quoteCharacter: "\"", + rootPath: "some/path", + }; +} +exports.mockOptions = mockOptions; +// Multiline string assertion to give more useful output messages. +function assertMultiLine(actual, expected) { + const actualParts = actual.split("\n"); + const expectParts = expected.split("\n"); + chai_1.assert.equal(actualParts.length, expectParts.length, "Different numbers of lines"); + actualParts.forEach((actualPart, index) => { + chai_1.assert.equal(actualPart, expectParts[index]); + }); +} +exports.assertMultiLine = assertMultiLine; +// Runs tslint against the specified file and checks there are no errors. +function tslint(content, options) { + const linter = new tslint_1.Linter({ fix: false, formatter: "json" }); + const configuration = tslint_1.Configuration.loadConfigurationFromPath("./tslint.json"); + if (options.quoteCharacter === "'") { + configuration.rules.set("quotemark", { ruleArguments: ["single", "avoid-escape"] }); + } + console.info(configuration.rules.get("quotemark")); //tslint:disable-line + linter.lint("test_output.ts", content, configuration); + const failures = linter.getResult().failures.map((failure) => `${failure.getRuleName()} ${failure.getStartPosition().getLineAndCharacter().line}`); + chai_1.assert.deepEqual(failures, []); +} +exports.tslint = tslint; //# sourceMappingURL=testUtilities.js.map \ No newline at end of file diff --git a/bin/utilities.js b/bin/utilities.js index 3939a93a..dd0b77c7 100644 --- a/bin/utilities.js +++ b/bin/utilities.js @@ -1,11 +1,11 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -/** Convert path separator from windows to unix */ -function convertPathSeparator(path) { - return path.replace(/\\+/g, "/"); -} -exports.convertPathSeparator = convertPathSeparator; -exports.isTypeScriptFile = /\.tsx?$/m; -exports.nonAlphaNumeric = /\W+/g; -exports.indentation = " "; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +/** Convert path separator from windows to unix */ +function convertPathSeparator(path) { + return path.replace(/\\+/g, "/"); +} +exports.convertPathSeparator = convertPathSeparator; +exports.isTypeScriptFile = /\.tsx?$/m; +exports.nonAlphaNumeric = /\W+/g; +exports.indentation = " "; //# sourceMappingURL=utilities.js.map \ No newline at end of file diff --git a/bin/utilities.test.js b/bin/utilities.test.js index 1450c6ab..106d1c26 100644 --- a/bin/utilities.test.js +++ b/bin/utilities.test.js @@ -1,42 +1,42 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const chai_1 = require("chai"); -const Utilities = require("./utilities"); -describe("utilities module has a", () => { - describe("isTypeScriptFile regular expression that", () => { - it("should match a typescript file", () => { - chai_1.assert.notEqual("instructions.ts".search(Utilities.isTypeScriptFile), -1); - }); - it("should match a typescript file in a directory", () => { - chai_1.assert.notEqual("src/code/scripts/instructions.ts".search(Utilities.isTypeScriptFile), -1); - }); - it("should match a typescript definition file", () => { - chai_1.assert.notEqual("definitions.d.ts".search(Utilities.isTypeScriptFile), -1); - }); - it("should match a typescript jsx (.tsx) file", () => { - chai_1.assert.notEqual("other.tsx".search(Utilities.isTypeScriptFile), -1); - }); - it("should not match a non-typescript file", () => { - chai_1.assert.equal("other.cs".search(Utilities.isTypeScriptFile), -1); - }); - }); - describe("nonAlphaNumeric regular expression that", () => { - it("should match any non-alpha-numeric characters", () => { - const input = "aA1!bB2\"cC3£dD4$"; - const output = input.replace(Utilities.nonAlphaNumeric, ""); - chai_1.assert.equal(output, "aA1bB2cC3dD4"); - }); - }); - describe("indentation constant that", () => { - it("is only whitespace", () => { - chai_1.assert.equal(Utilities.indentation.trim(), ""); - }); - }); - describe("convertPathSeparator function that", () => { - it("should window path seperators with unix ones", () => { - const result = Utilities.convertPathSeparator("my\\long/path"); - chai_1.assert.equal(result, "my/long/path"); - }); - }); -}); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const chai_1 = require("chai"); +const Utilities = require("./utilities"); +describe("utilities module has a", () => { + describe("isTypeScriptFile regular expression that", () => { + it("should match a typescript file", () => { + chai_1.assert.notEqual("instructions.ts".search(Utilities.isTypeScriptFile), -1); + }); + it("should match a typescript file in a directory", () => { + chai_1.assert.notEqual("src/code/scripts/instructions.ts".search(Utilities.isTypeScriptFile), -1); + }); + it("should match a typescript definition file", () => { + chai_1.assert.notEqual("definitions.d.ts".search(Utilities.isTypeScriptFile), -1); + }); + it("should match a typescript jsx (.tsx) file", () => { + chai_1.assert.notEqual("other.tsx".search(Utilities.isTypeScriptFile), -1); + }); + it("should not match a non-typescript file", () => { + chai_1.assert.equal("other.cs".search(Utilities.isTypeScriptFile), -1); + }); + }); + describe("nonAlphaNumeric regular expression that", () => { + it("should match any non-alpha-numeric characters", () => { + const input = "aA1!bB2\"cC3£dD4$"; + const output = input.replace(Utilities.nonAlphaNumeric, ""); + chai_1.assert.equal(output, "aA1bB2cC3dD4"); + }); + }); + describe("indentation constant that", () => { + it("is only whitespace", () => { + chai_1.assert.equal(Utilities.indentation.trim(), ""); + }); + }); + describe("convertPathSeparator function that", () => { + it("should window path seperators with unix ones", () => { + const result = Utilities.convertPathSeparator("my\\long/path"); + chai_1.assert.equal(result, "my/long/path"); + }); + }); +}); //# sourceMappingURL=utilities.test.js.map \ No newline at end of file