-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to delete old .d.ts files for *.scss files
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
1 parent
4f8bc03
commit 3b0d85e
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}` | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters