Skip to content

Commit

Permalink
fix: clear out old types for empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
skovy committed Aug 6, 2023
1 parent 5d322d9 commit ce5d9a1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
47 changes: 47 additions & 0 deletions __tests__/core/write-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ describeAllImplementations((implementation) => {
// Avoid creating new directories while running tests.
jest.spyOn(fs, "mkdirSync").mockImplementation();

// Test removing existing types.
jest.spyOn(fs, "unlinkSync").mockImplementation();

console.log = jest.fn();
});

Expand Down Expand Up @@ -74,6 +77,50 @@ describeAllImplementations((implementation) => {
);
});

describe("when a file already exists with type definitions", () => {
const testFile = path.resolve(__dirname, "..", "dummy-styles/empty.scss");
const existingTypes = path.join(
process.cwd(),
"__tests__/dummy-styles/empty.scss.d.ts"
);
const originalExistsSync = fs.existsSync;

beforeEach(() => {
jest
.spyOn(fs, "existsSync")
.mockImplementation((p) =>
p === existingTypes ? true : originalExistsSync(p)
);
});

afterEach(() => {
(fs.existsSync as jest.Mock).mockRestore();
});

test("it removes existing type definitions if no classes are found", async () => {
await writeFile(testFile, {
banner: "",
watch: false,
ignoreInitial: false,
exportType: "named",
exportTypeName: "ClassNames",
exportTypeInterface: "Styles",
listDifferent: false,
ignore: [],
implementation,
quoteType: "single",
updateStaleOnly: false,
logLevel: "verbose",
outputFolder: null,
});

expect(fs.unlinkSync).toBeCalledWith(existingTypes);
expect(console.log).toBeCalledWith(
expect.stringContaining(`[REMOVED] ${existingTypes}`)
);
});
});

describe("when outputFolder is passed", () => {
it("should write to the correct path", async () => {
const testFile = path.resolve(
Expand Down
16 changes: 12 additions & 4 deletions lib/core/write-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "../typescript";
import { fileToClassNames } from "../sass";
import { CLIOptions } from "./types";
import { removeSCSSTypeDefinitionFile } from "./remove-file";

/**
* Given a single file generate the proper types.
Expand All @@ -24,17 +25,24 @@ export const writeFile = (file: string, options: CLIOptions): Promise<void> => {
...options,
});

const typesPath = getTypeDefinitionPath(file, options);
const typesExist = fs.existsSync(typesPath);

// Avoid outputting empty type definition files.
// If the file exists and the type definition is now empty, remove the file.
if (!typeDefinition) {
alerts.notice(`[NO GENERATED TYPES] ${file}`);
if (typesExist) {
removeSCSSTypeDefinitionFile(file, options);
} else {
alerts.notice(`[NO GENERATED TYPES] ${file}`);
}
return;
}

const typesPath = getTypeDefinitionPath(file, options);

// Avoid re-writing the file if it hasn't changed.
// First by checking the file modification time, then
// by comparing the file contents.
if (options.updateStaleOnly && fs.existsSync(typesPath)) {
if (options.updateStaleOnly && typesExist) {
const fileModified = fs.statSync(file).mtime;
const typeDefinitionModified = fs.statSync(typesPath).mtime;

Expand Down

0 comments on commit ce5d9a1

Please sign in to comment.