Skip to content

Commit

Permalink
fix: set up the dtslint temp folder only when needed (#449)
Browse files Browse the repository at this point in the history
Previously this was done already when the module was required (which
was: always, as there is a re-export from index.ts).
  • Loading branch information
akudev authored Apr 25, 2024
1 parent 805e218 commit 2a4f38e
Showing 1 changed file with 55 additions and 41 deletions.
96 changes: 55 additions & 41 deletions packages/dts-generator/src/checkDtslint/check-dtslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,50 +74,63 @@ const mFileContents: { [fileName: string]: string[] } = {};
const MIN_TYPESCRIPT_VERSION = "5.0"; // the lowest TypeScript version which should be used by dtslint for testing

// make sure there is an empty temp directory
const tempDir = path.join(
process.cwd(),
"/temp-dtslint",
"DefinitelyTyped",
"types",
"openui5",
);
const tempDefinitelyTypedDir = path.join(
process.cwd(),
"/temp-dtslint",
"DefinitelyTyped",
); // parent of the above
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
} else {
const previousFiles = fs.readdirSync(tempDir);
for (const file of previousFiles) {
fs.unlinkSync(path.join(tempDir, file));
function prepareTempDir(): string {
const tempDir = path.join(
process.cwd(),
"/temp-dtslint",
"DefinitelyTyped",
"types",
"openui5",
);

const tempDefinitelyTypedDir = path.join(
process.cwd(),
"/temp-dtslint",
"DefinitelyTyped",
); // parent of the above

// create the directory or delete its contents if needed, so it is an existing empty directory
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
} else {
const previousFiles = fs.readdirSync(tempDir);
for (const file of previousFiles) {
fs.unlinkSync(path.join(tempDir, file));
}
}
}
// fill it with the config files required for dtslint (dtslint is very strict about checking its environment and even the content of the files it expects)
[
"tsconfig.json",
".eslintrc.json",
"package.json",
".npm___ignore", // renamed to avoid it being used when npm releases the dts-generator package
"openui5-tests.ts",
].forEach((file) => {

// fill it with the config files required for dtslint (dtslint is very strict about checking its environment and even the content of the files it expects)
[
"tsconfig.json",
".eslintrc.json",
"package.json",
".npm___ignore", // renamed to avoid it being used when npm releases the dts-generator package
"openui5-tests.ts",
].forEach((file) => {
fs.copyFileSync(
new URL("dtslintConfig/" + file, import.meta.url),
path.join(tempDir, file === ".npm___ignore" ? ".npmignore" : file),
);
});

// some files need to go one level above
fs.copyFileSync(
new URL("dtslintConfig/" + file, import.meta.url),
path.join(tempDir, file === ".npm___ignore" ? ".npmignore" : file),
new URL(
"dtslintConfig/forDefinitelyTypedDir/notNeededPackages.json",
import.meta.url,
),
path.join(tempDefinitelyTypedDir, "notNeededPackages.json"),
);
});
fs.copyFileSync(
new URL(
"dtslintConfig/forDefinitelyTypedDir/notNeededPackages.json",
import.meta.url,
),
path.join(tempDefinitelyTypedDir, "notNeededPackages.json"),
);
fs.copyFileSync(
new URL("dtslintConfig/forDefinitelyTypedDir/.eslintrc.cjs", import.meta.url),
path.join(tempDefinitelyTypedDir, ".eslintrc.cjs"),
);
fs.copyFileSync(
new URL(
"dtslintConfig/forDefinitelyTypedDir/.eslintrc.cjs",
import.meta.url,
),
path.join(tempDefinitelyTypedDir, ".eslintrc.cjs"),
);

return tempDir;
}

const MIN_TYPESCRIPT_VERSION_LINES = `/**
* Copyright (c) 2024 SAP SE or an SAP affiliate company and OpenUI5 contributors.
Expand Down Expand Up @@ -175,6 +188,7 @@ export default function checkDtslint(
" is not an existing d.ts file or directory containing such files",
);
}
const tempDir = prepareTempDir(); // existing and empty now TODO: consider making this a parameter

// copy the files to test to the temp folder
const aFiles = fs.lstatSync(dtsFileOrDir).isFile()
Expand Down

0 comments on commit 2a4f38e

Please sign in to comment.