Skip to content

Commit

Permalink
feat: add ability to delete old .d.ts files for *.scss files
Browse files Browse the repository at this point in the history
This commit adds the ability to remove old .d.ts files while
running the cli with the watch flag (`--watch`). The script leverages
the unlink fs event to know when a file has been deleted and then
remove the analagous .d.ts file. The commit tries to make sure all
of the removal logic is synchronized.
  • Loading branch information
ccortezaguilera authored and skovy committed Oct 28, 2020
1 parent 4f8bc03 commit 3b0d85e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
60 changes: 60 additions & 0 deletions __tests__/core/remove-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import fs from "fs";

import { alerts } from "../../lib/core/alerts";
import { removeFile } from "../../lib/core/remove-file";
import { getTypeDefinitionPath } from "../../lib/typescript";

jest.mock("fs");
jest.mock("../../lib/core/alerts");

describe("removeFile", () => {
const originalTestFile = `${__dirname}/../removable.scss`;
const existingFile = `${__dirname}/../style.scss`;
const existingTypes = getTypeDefinitionPath(originalTestFile);

beforeEach(() => {
(fs.existsSync as jest.Mock).mockImplementation(
(path: fs.PathLike): boolean =>
path === existingTypes || path === existingFile
);
});
afterEach(() => {
jest.clearAllMocks();
});
it("does nothing if *.scss file style exists", async () => {
const existsSyncSpy = fs.existsSync;
const unlinkSyncSpy = fs.unlinkSync;
const existingTypes = getTypeDefinitionPath(existingFile);
removeFile(existingFile);
expect(existsSyncSpy).toBeCalledWith(expect.stringMatching(existingFile));
expect(existsSyncSpy).not.toBeCalledWith(
expect.stringMatching(existingTypes)
);
expect(unlinkSyncSpy).not.toBeCalled();
expect(alerts.success).not.toBeCalled();
});
it("does nothing if types file doesn't exist", async () => {
const existsSyncSpy = fs.existsSync;
const unlinkSyncSpy = fs.unlinkSync;
const nonExistingFile = `${__dirname}/../deleted.scss`;
const nonExistingTypes = getTypeDefinitionPath(nonExistingFile);
removeFile(nonExistingFile);
expect(existsSyncSpy).toBeCalledWith(
expect.stringMatching(nonExistingFile)
);
expect(existsSyncSpy).toBeCalledWith(
expect.stringMatching(nonExistingTypes)
);
expect(unlinkSyncSpy).not.toBeCalled();
expect(alerts.success).not.toBeCalled();
});
it("removes *.scss.d.ts types file for *.scss", () => {
const existsSyncSpy = fs.existsSync;
const unlinkSyncSpy = fs.unlinkSync;
removeFile(originalTestFile);
expect(existsSyncSpy).toBeCalledWith(expect.stringMatching(existingTypes));
expect(unlinkSyncSpy).toBeCalled();
expect(unlinkSyncSpy).toBeCalledWith(expect.stringMatching(existingTypes));
expect(alerts.success).toBeCalled();
});
});
26 changes: 26 additions & 0 deletions lib/core/remove-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from "fs";

import { alerts } from "./alerts";
import { getTypeDefinitionPath } from "../typescript";

/**
* Given a single file remove the generated types if they exist
*
* @param file the SCSS file to generate types for
*/
export const removeFile = (file: string): void => {
if (fs.existsSync(file)) {
return;
}
const path = getTypeDefinitionPath(file);
try {
if (fs.existsSync(path)) {
fs.unlinkSync(path);
alerts.success(`[REMOVED TYPES] ${path}`);
}
} catch (error) {
alerts.error(
`An error occurred removing ${file}:\n${JSON.stringify(error)}`
);
}
};
5 changes: 5 additions & 0 deletions lib/core/watch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chokidar from "chokidar";

import { alerts } from "./alerts";
import { removeFile } from "./remove-file";
import { writeFile } from "./write-file";
import { MainOptions } from "./types";

Expand All @@ -25,5 +26,9 @@ export const watch = (pattern: string, options: MainOptions): void => {
.on("add", (path) => {
alerts.info(`[ADDED] ${path}`);
writeFile(path, options);
})
.on("unlink", (path) => {
alerts.info(`[REMOVED] ${path}`);
removeFile(path);
});
};

0 comments on commit 3b0d85e

Please sign in to comment.