Skip to content

Commit

Permalink
feat: fallback to tsconfig.json if autobarrel not present
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Jan 30, 2022
1 parent e53425d commit af8404b
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
test/testing
test/tsTesting
node_modules
yarn-error.log
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/**
* @type {import('@jest/types').Config.ProjectConfig}
*/
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
watchPathIgnorePatterns: ["<rootDir>/test/testing"],
testPathIgnorePatterns: ["<rootDir>/test/tsFixtures"],
}
32 changes: 30 additions & 2 deletions src/autobarrel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,40 @@ export async function autobarrelWatch(configData: AutoBarrelWatchData) {
return watcher
}

async function resolveAutobarrelFromTsconfig(configPath: string, e: any) {
try {
const tsConfigPath = configPath.replace(
path.basename(configPath),
`tsconfig.json`
)
const tsConfigData = JSON.parse(await readFileAsync(tsConfigPath, "utf8"))
const toInclude = Array.isArray(tsConfigData?.include)
? tsConfigData?.include
: tsConfigData?.compilerOptions?.rootDir
? [`${tsConfigData?.compilerOptions?.rootDir}/**`]
: null

if (toInclude) {
return {
cwd: path.dirname(configPath),
paths: toInclude,
ignore: tsConfigData?.exclude ?? [],
}
}
} catch {}
throw e
}

export async function resolveAutobarrelConfig(config: AutoBarrelConfig) {
let configPath = path.isAbsolute(config.path)
? config.path
: path.join(process.cwd(), config.path)
const stat = await statAsync(configPath)

let stat: fs.Stats
try {
stat = await statAsync(configPath)
} catch (e) {
return await resolveAutobarrelFromTsconfig(configPath, e)
}
if (!stat.isFile()) {
throw new Error(`Autobarrel config ${configPath} is not a file`)
}
Expand Down
5 changes: 4 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ const confPath = argv.config
: path.join(process.cwd(), argv.config)
: path.join(process.cwd(), "autobarrel.json")

if (!fs.existsSync(confPath)) {
if (
!fs.existsSync(confPath) &&
!fs.existsSync(confPath.replace(path.basename(confPath), "tsconfig.json"))
) {
throw new Error(
`Could not find autobarrel.json file at ${confPath}, specify a valid path via --config`
)
Expand Down
25 changes: 25 additions & 0 deletions test/__snapshots__/autobarrel.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`autobarrel autobarrel (tsconfig.json) tests 1`] = `
Array [
"tsTesting",
"tsTesting/src",
"tsTesting/src/file.ts",
"tsTesting/src/ignored",
"tsTesting/src/ignored/some-file.test.ts",
"tsTesting/src/index.ts",
"tsTesting/tsconfig.json",
]
`;

exports[`autobarrel autobarrel (tsconfig.json) tests: Paths Written 1`] = `
Array [
"src/index.ts",
]
`;

exports[`autobarrel autobarrel (tsconfig.json) tests: tsTesting/src/index.ts 1`] = `
"// created by autobarrel, do not modify directly
export * from './file'
"
`;

exports[`autobarrel autobarrel tests 1`] = `
Array [
"testing",
Expand Down
27 changes: 22 additions & 5 deletions test/__snapshots__/cli.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`autobarrel cli (tsconfig.json) should run the CLI 1`] = `
Array [
"tsTesting",
"tsTesting/src",
"tsTesting/src/file.ts",
"tsTesting/src/ignored",
"tsTesting/src/ignored/some-file.test.ts",
"tsTesting/src/index.ts",
"tsTesting/tsconfig.json",
]
`;

exports[`autobarrel cli should run the CLI 1`] = `
Array [
"testing",
Expand Down Expand Up @@ -48,37 +60,42 @@ Array [
`;

exports[`watch mode dir remove should run --watch mode, watch dir remove: After dir remove 1`] = `
"// created by autobarrel, do not modify directly
"/* eslint-disable */
// created by autobarrel, do not modify directly
export * from './watch'
"
`;

exports[`watch mode dir remove should run --watch mode, watch dir remove: Before dir remove 1`] = `
"// created by autobarrel, do not modify directly
"/* eslint-disable */
// created by autobarrel, do not modify directly
export * from './addedDir/'
export * from './watch'
"
`;

exports[`watch mode should run --watch mode, watch file add: After file add 1`] = `
"// created by autobarrel, do not modify directly
"/* eslint-disable */
// created by autobarrel, do not modify directly
export * from './added'
export * from './watch'
"
`;

exports[`watch mode should run --watch mode, watch file add: Before file add 1`] = `
"// created by autobarrel, do not modify directly
"/* eslint-disable */
// created by autobarrel, do not modify directly
export * from './watch'
"
`;

exports[`watch mode should run --watch mode, watch file remove: Before file remove 1`] = `
"// created by autobarrel, do not modify directly
"/* eslint-disable */
// created by autobarrel, do not modify directly
export * from './watch'
"
Expand Down
31 changes: 31 additions & 0 deletions test/autobarrel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ describe("autobarrel", () => {
path.join(__dirname, "fixtures"),
path.join(__dirname, "testing")
)
try {
fs.rmdirSync(path.join(__dirname, "tsTesting"), { recursive: true })
} catch {}
fs.copySync(
path.join(__dirname, "tsFixtures"),
path.join(__dirname, "tsTesting")
)
})

afterEach(() => {
try {
fs.rmdirSync(path.join(__dirname, "testing"), { recursive: true })
} catch {}
try {
fs.rmdirSync(path.join(__dirname, "tsTesting"), { recursive: true })
} catch {}
})

test("autobarrel tests", async () => {
Expand All @@ -40,4 +50,25 @@ describe("autobarrel", () => {

expect(pathsWritten).toMatchSnapshot("Paths Written")
})

test("autobarrel (tsconfig.json) tests", async () => {
const pathsWritten = await autobarrel(
await resolveAutobarrelConfig({
path: path.join(__dirname, "tsTesting", "autobarrel.json"),
})
)
const globFiles = glob.sync("tsTesting/**", {
cwd: __dirname,
})
expect(globFiles).toMatchSnapshot()
const indexFiles = globFiles.filter((f) => f.endsWith("index.ts"))

indexFiles.map((f) => {
expect(fs.readFileSync(path.join(__dirname, f), "utf8")).toMatchSnapshot(
f
)
})

expect(pathsWritten).toMatchSnapshot("Paths Written")
})
})
27 changes: 27 additions & 0 deletions test/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ beforeEach(() => {
fs.rmdirSync(path.join(__dirname, "testing"), { recursive: true })
} catch {}
fs.copySync(path.join(__dirname, "fixtures"), path.join(__dirname, "testing"))
try {
fs.rmdirSync(path.join(__dirname, "tsTesting"), { recursive: true })
} catch {}
fs.copySync(
path.join(__dirname, "tsFixtures"),
path.join(__dirname, "tsTesting")
)
})

afterEach(() => {
try {
fs.rmdirSync(path.join(__dirname, "testing"), { recursive: true })
} catch {}
try {
fs.rmdirSync(path.join(__dirname, "tsTesting"), { recursive: true })
} catch {}
})

describe("autobarrel cli", () => {
Expand All @@ -37,6 +47,23 @@ describe("autobarrel cli", () => {
})
})

describe("autobarrel cli (tsconfig.json)", () => {
test("should run the CLI", async () => {
childProcess.spawnSync(
"./cli.js",
["--config", path.join(__dirname, "./tsTesting", "autobarrel.json")],
{
cwd: path.join(__dirname, "../dist"),
stdio: "inherit",
}
)
const globbed = await globAsync("tsTesting/**", {
cwd: __dirname,
})
expect(globbed).toMatchSnapshot()
})
})

describe("watch mode", () => {
const watchArgs = [
"--config",
Expand Down
1 change: 1 addition & 0 deletions test/tsFixtures/src/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const x = 1
Empty file.
8 changes: 8 additions & 0 deletions test/tsFixtures/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"noEmit": true
},
"exclude": ["src/**/*.test.ts"]
}

0 comments on commit af8404b

Please sign in to comment.