Skip to content

Commit

Permalink
feat(compas): run Prettier on file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkdev98 committed Oct 11, 2023
1 parent 0a4745b commit c7d5101
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ export const configLoaderIntegration = {
}
} catch (/** @type {any} */ e) {
if (e.key === "config.resolve.parseError") {
state.logInformation(`Could not reload config due to a syntax error.`);
state.logInformationUnique(
`Could not reload config due to a syntax error.`,
);
} else if (e.key === "config.resolve.validationError") {
state.logInformation(
state.logInformationUnique(
`Could not reload config due to a validation error. Check the docs for supported properties.`,
);
} else {
state.logInformation(
state.logInformationUnique(
`Could not reload the config due to an error. Please check your file.`,
);
}
Expand Down
39 changes: 22 additions & 17 deletions packages/compas/src/main/development/integrations/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { cacheRemoveDynamicAction } from "../cache.js";

const PRETTIER_RUN_ACTION = "prettierRunAction";

let _prettierInterval = undefined;
let _prettierTimeout = undefined;

/**
* @type {import("./base.js").Integration}
Expand All @@ -24,7 +24,7 @@ export const prettierIntegration = {
await prettierDetectInformation(state);

if (state.cache.prettier) {
_prettierInterval = prettierCheckFilesInterval(state);
prettierRefreshBackgroundCheckTimeout(state);
}
},

Expand All @@ -33,7 +33,7 @@ export const prettierIntegration = {
state.dynamicActionCallbacks[PRETTIER_RUN_ACTION] = prettierRunAction;

if (state.cache.prettier) {
_prettierInterval = prettierCheckFilesInterval(state);
prettierRefreshBackgroundCheckTimeout(state);
}
},

Expand All @@ -52,15 +52,16 @@ export const prettierIntegration = {

if (packageJsonChange || configChange) {
await prettierDetectInformation(state);
}

if (state.cache.prettier && isNil(_prettierInterval)) {
_prettierInterval = prettierCheckFilesInterval(state);
} else if (isNil(state.cache.prettier) && !isNil(_prettierInterval)) {
clearInterval(_prettierInterval);
if (packageJsonChange || configChange || filePaths.length > 0) {
if (state.cache.prettier) {
prettierRefreshBackgroundCheckTimeout(state);
} else if (isNil(state.cache.prettier) && !isNil(_prettierTimeout)) {
clearTimeout(_prettierTimeout);
_prettierTimeout = undefined;
}
}

// TODO: Call Prettier only on the changed files
},
};

Expand All @@ -72,7 +73,7 @@ export const prettierIntegration = {
async function prettierRunAction(state, rootDirectory) {
cacheRemoveDynamicAction(state.cache, PRETTIER_RUN_ACTION, rootDirectory);

const prettierConfig = state.cache.prettier[rootDirectory];
const prettierConfig = state.cache.prettier?.[rootDirectory];

if (isNil(prettierConfig)) {
return;
Expand Down Expand Up @@ -125,7 +126,7 @@ async function prettierFormatCommand(state, rootDirectory) {
async function prettierDetectInformation(state) {
state.cache.prettier = {};

for (const rootDirectory of state.cache.rootDirectories) {
for (const rootDirectory of state.cache.rootDirectories ?? []) {
const packageJson = JSON.parse(
await readFile(pathJoin(rootDirectory, "package.json"), "utf-8"),
);
Expand Down Expand Up @@ -158,10 +159,14 @@ async function prettierDetectInformation(state) {
}
}

function prettierCheckFilesInterval(state) {
return setInterval(() => {
state.runTask("prettierBackgroundCheck", prettierBackgroundCheck);
}, 10000);
function prettierRefreshBackgroundCheckTimeout(state) {
if (_prettierTimeout) {
_prettierTimeout.refresh();
} else {
_prettierTimeout = setTimeout(() => {
state.runTask("prettierBackgroundCheck", prettierBackgroundCheck);
}, 200);
}
}

/**
Expand All @@ -170,10 +175,10 @@ function prettierCheckFilesInterval(state) {
*/
async function prettierBackgroundCheck(state) {
await Promise.all(
state.cache.rootDirectories.map(async (rootDirectory) => {
(state.cache.rootDirectories ?? []).map(async (rootDirectory) => {
cacheRemoveDynamicAction(state.cache, PRETTIER_RUN_ACTION, rootDirectory);

const prettierConfig = state.cache.prettier[rootDirectory];
const prettierConfig = state.cache.prettier?.[rootDirectory];

if (isNil(prettierConfig)) {
return;
Expand Down
5 changes: 5 additions & 0 deletions packages/compas/src/main/development/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ export class State {

// === user information ===

// TODO: Rethink log information.
// - We log a lot of duplicate info
// - Information is often not relevant after running an action.
// - In some cases information + action combined gives the full picture.

/**
* @param {string} line
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/store/src/files-jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export function jobFileTransformImage(s3Client) {

if (
file.contentLength === 0 ||
!STORE_FILE_IMAGE_TYPES.includes(file.contentType) ||
!STORE_FILE_IMAGE_TYPES.includes(file.contentType ?? "") ||
file.contentType === "image/svg+xml"
) {
// Empty file is an empty transform, SVG's are not supported
Expand Down

0 comments on commit c7d5101

Please sign in to comment.