Skip to content

Commit

Permalink
🏗 Add @types/fs-extra and fix related typing issues (#36087)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrozenberg committed Sep 20, 2021
1 parent ffe1481 commit 9b97955
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 31 deletions.
5 changes: 4 additions & 1 deletion build-system/common/update-packages.js
Expand Up @@ -17,7 +17,10 @@ const {runNpmChecks} = require('./npm-checks');
* @param {string} file Contents to write
*/
function writeIfUpdated(patchedName, file) {
if (!fs.existsSync(patchedName) || fs.readFileSync(patchedName) != file) {
if (
!fs.existsSync(patchedName) ||
fs.readFileSync(patchedName, 'utf8') != file
) {
fs.writeFileSync(patchedName, file);
logLocalDev('Patched', cyan(patchedName));
}
Expand Down
2 changes: 1 addition & 1 deletion build-system/common/utils.js
Expand Up @@ -102,7 +102,7 @@ function getFilesFromFileList() {
if (!argv.filelist) {
return [];
}
return fs.readFileSync(argv.filelist, {encoding: 'utf8'}).trim().split(',');
return fs.readFileSync(argv.filelist, 'utf8').trim().split(',');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion build-system/compile/post-closure-babel.js
Expand Up @@ -64,7 +64,7 @@ async function postClosureBabel(file) {
const {compressed, terserMap} = await terserMinify(code);
await fs.outputFile(file, compressed);

const closureMap = await fs.readJson(`${file}.map`, 'utf-8');
const closureMap = await fs.readJson(`${file}.map`, {encoding: 'utf8'});
const sourceMap = remapping(
[terserMap, babelMap, closureMap],
() => null,
Expand Down
4 changes: 2 additions & 2 deletions build-system/compile/pre-closure-babel.js
Expand Up @@ -81,7 +81,7 @@ async function preClosureBabel(file, outputFilename, options) {
}
const transformedFile = path.join(outputDir, file);
if (!filesToTransform.includes(file)) {
if (!(await fs.exists(transformedFile))) {
if (!(await fs.pathExists(transformedFile))) {
await fs.copy(file, transformedFile);
}
return transformedFile;
Expand All @@ -96,7 +96,7 @@ async function preClosureBabel(file, outputFilename, options) {
const {contents, hash} = await batchedRead(file, optionsHash);
const cachedPromise = transformCache.get(hash);
if (cachedPromise) {
if (!(await fs.exists(transformedFile))) {
if (!(await fs.pathExists(transformedFile))) {
await fs.outputFile(transformedFile, await cachedPromise);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion build-system/pr-check/utils.js
Expand Up @@ -327,7 +327,7 @@ function generateCircleCiShardTestFileList(globs) {
)
.trim()
.replace(/\s+/g, ',');
fs.writeFileSync(TEST_FILES_LIST_FILE_NAME, fileList, {encoding: 'utf8'});
fs.writeFileSync(TEST_FILES_LIST_FILE_NAME, fileList, 'utf8');
logWithoutTimestamp(
'Stored list of',
cyan(fileList.split(',').length),
Expand Down
2 changes: 1 addition & 1 deletion build-system/tasks/check-exact-versions.js
Expand Up @@ -12,7 +12,7 @@ const {log, logLocalDev, logWithoutTimestamp} = require('../common/logging');
* @return {boolean}
*/
function check(file) {
const json = fs.readJsonSync(file, 'utf8');
const json = fs.readJsonSync(file, {encoding: 'utf8'});

// We purposfully ignore peerDependencies here, because that's that's for the
// consumer to decide.
Expand Down
2 changes: 1 addition & 1 deletion build-system/tasks/dep-check.js
Expand Up @@ -208,7 +208,7 @@ async function getModuleGraph(entryPointModule) {

/**
* @param {string} extensionFolder
* @return {!Array<!ModuleDef>}
* @return {!Array<string>}
*/
function getEntryPoint(extensionFolder) {
const extension = path.basename(extensionFolder);
Expand Down
31 changes: 8 additions & 23 deletions build-system/tasks/make-extension/index.js
Expand Up @@ -111,31 +111,20 @@ async function writeFromTemplateDir(
await fs.mkdirp(path.dirname(destination));

// Skip if the destination file already exists
let fileHandle;
try {
fileHandle = await fs.open(destination, 'wx');
} catch (e) {
if (e.code !== 'EEXIST') {
throw e;
}
if (!argv.overwrite) {
logLocalDev(
yellow('WARNING:'),
'Skipping existing file',
cyan(destination)
);
continue;
}
if (await fs.pathExists(destination)) {
logLocalDev(
yellow('WARNING:'),
'Overwriting existing file',
argv.overwrite ? 'Overwriting' : 'Skipping',
'existing file',
cyan(destination)
);
if (!argv.overwrite) {
continue;
}
}

const template = await fs.readFile(templatePath, 'utf8');
await fs.write(fileHandle, replace(template, replacements));
await fs.close(fileHandle);
await fs.writeFile(destination, replace(template, replacements));

logLocalDev(green('SUCCESS:'), 'Created', cyan(destination));

Expand All @@ -155,11 +144,7 @@ async function insertExtensionBundlesConfig(
bundle,
destination = extensionBundlesJson
) {
let extensionBundles = [];
try {
extensionBundles = await fs.readJson(destination, {throws: false});
} catch (_) {}

const extensionBundles = fs.readJsonSync(destination, {throws: false}) ?? [];
const existingOrNull = extensionBundles.find(
({name}) => name === bundle.name
);
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -62,6 +62,7 @@
"@types/chai": "4.2.21",
"@types/eslint": "7.28.0",
"@types/minimist": "1.2.2",
"@types/fs-extra": "9.0.12",
"@types/mocha": "8.2.3",
"@types/node": "14.17.12",
"acorn-globals": "6.0.0",
Expand Down

0 comments on commit 9b97955

Please sign in to comment.