Skip to content

Commit

Permalink
use faster fs methods (#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke authored Aug 5, 2023
1 parent c702f80 commit b0dfbd1
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 42 deletions.
16 changes: 7 additions & 9 deletions .github/bin/format-package-json.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { promises as fsp, constants } from 'fs';
import fs from 'fs';
import path from 'path';

async function postcssPeerDependencyVersion() {
function postcssPeerDependencyVersion() {
// "postcss-tape" is our reference for PostCSS versions.
// This package is our test suite.
// If CI passes it means the plugin is compatible with the lower version in "postcss-tape".
const packageJSONInfoForPostCSS_Tape = JSON.parse(await fsp.readFile('../../packages/postcss-tape/package.json', 'utf8'));
const packageJSONInfoForPostCSS_Tape = JSON.parse(fs.readFileSync('../../packages/postcss-tape/package.json', 'utf8'));

// "postcss-tape" package.json:
//
Expand All @@ -24,7 +24,7 @@ async function postcssPeerDependencyVersion() {
return lowerPostCSS_Version.split('~')[1];
}

const packageJSONInfo = JSON.parse(await fsp.readFile('./package.json', 'utf8'));
const packageJSONInfo = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const packageJSONInfoCopy = JSON.stringify(packageJSONInfo, null, '\t');
const formatted = {};

Expand Down Expand Up @@ -146,7 +146,7 @@ const formatted = {};
delete packageJSONInfo.peerDependencies;

if (formatted.peerDependencies['postcss']) {
formatted.peerDependencies['postcss'] = '^' + (await postcssPeerDependencyVersion());
formatted.peerDependencies['postcss'] = '^' + postcssPeerDependencyVersion();
}
}

Expand Down Expand Up @@ -203,9 +203,7 @@ const formatted = {};
}
delete packageJSONInfo.scripts;

try {
await fsp.access('./stryker.conf.json', constants.R_OK | constants.W_OK);
} catch (_) {
if (!fs.existsSync('./stryker.conf.json')) {
delete formatted.scripts.stryker;
}
}
Expand Down Expand Up @@ -257,4 +255,4 @@ if (process.env.GITHUB_ACTIONS && JSON.stringify(formatted, null, '\t') !== pack
process.exit(1);
}

await fsp.writeFile('./package.json', JSON.stringify(formatted, null, '\t') + '\n');
fs.writeFileSync('./package.json', JSON.stringify(formatted, null, '\t') + '\n');
8 changes: 4 additions & 4 deletions .github/bin/generate-docs/install.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { promises as fsp } from 'fs';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const template = await fsp.readFile(path.join(path.dirname(fileURLToPath(import.meta.url)), './install-template.md'), 'utf8');
const packageJSONInfo = JSON.parse(await fsp.readFile('./package.json', 'utf8'));
const template = fs.readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), './install-template.md'), 'utf8');
const packageJSONInfo = JSON.parse(fs.readFileSync('./package.json', 'utf8'));

let installDoc = template.toString();
installDoc = installDoc.replaceAll('<exportName>', packageJSONInfo.csstools.exportName);
Expand All @@ -21,4 +21,4 @@ if (packageJSONInfo?.csstools?.assumesToProcessBundledCSS) {
installDoc = installDoc.replaceAll('<otherPluginsInWebpack>', `// Other plugins`);
}

await fsp.writeFile('./INSTALL.md', installDoc);
fs.writeFileSync('./INSTALL.md', installDoc);
14 changes: 7 additions & 7 deletions .github/bin/generate-docs/readme.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { promises as fsp } from 'fs';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const template = await fsp.readFile(path.join('docs', './README.md'), 'utf8');
const corsTemplate = await fsp.readFile(path.join(path.dirname(fileURLToPath(import.meta.url)), './cors-template.md'), 'utf8');
const packageJSONInfo = JSON.parse(await fsp.readFile('./package.json', 'utf8'));
const template = fs.readFileSync(path.join('docs', './README.md'), 'utf8');
const corsTemplate = fs.readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), './cors-template.md'), 'utf8');
const packageJSONInfo = JSON.parse(fs.readFileSync('./package.json', 'utf8'));

let exampleFilePaths = [];

try {
exampleFilePaths = await fsp.readdir(path.join('test', 'examples'));
exampleFilePaths = fs.readdirSync(path.join('test', 'examples'));
} catch(error) {
// No examples
}
Expand Down Expand Up @@ -106,8 +106,8 @@ readmeDoc = readmeDoc.replaceAll('<specUrl>', packageJSONInfo.csstools.specUrl);
for (const exampleFilePath of exampleFilePaths) {
readmeDoc = readmeDoc.replaceAll(
`<${exampleFilePath}>`,
(await fsp.readFile(path.join('test', 'examples', exampleFilePath), 'utf8')).toString().slice(0, -1), // trim final newline
(fs.readFileSync(path.join('test', 'examples', exampleFilePath), 'utf8')).toString().slice(0, -1), // trim final newline
);
}

await fsp.writeFile('./README.md', readmeDoc);
fs.writeFileSync('./README.md', readmeDoc);
6 changes: 3 additions & 3 deletions .github/bin/license/check-license.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { promises as fsp } from 'fs';
import fs from 'fs';
import { getFiles } from '../util/get-files.mjs';
import { parseLicenseField } from './parse-license-field.mjs';
import { licenseIsOkByException, osiApprovedWithCCO } from './allowed.mjs';
Expand Down Expand Up @@ -52,7 +52,7 @@ const allFiles = (await getFiles('./')).filter((file) => {
});

for (const file of allDistFiles) {
const contents = await fsp.readFile(file, 'utf8');
const contents = fs.readFileSync(file, 'utf8');
const lines = contents.split('\n');

for (const line of lines) {
Expand Down Expand Up @@ -83,7 +83,7 @@ const allFiles = (await getFiles('./')).filter((file) => {

// Read all "package.json" files and try to locate the license field
for (const file of packageJsonFiles) {
const data = await fsp.readFile(file, 'utf8');
const data = fs.readFileSync(file, 'utf8');
const packageJson = JSON.parse(data);

if (!packageJson.license && !packageJson.licenses) {
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: setup node
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20
cache: 'npm'

- run: npm ci --ignore-scripts
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
fetch-depth: 1
- uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20

- name: npm ci
run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ jobs:
node: [16, 18, 20]
include:
- os: macos-latest
node: 18
node: 20
- os: windows-latest
node: 18
node: 20
- os: ubuntu-latest
is_base_os_version: true
- node: 18
- node: 20
is_base_node_version: true
steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"test:browser": "npm run test:browser --workspaces --if-present"
},
"volta": {
"node": "20.2.0",
"npm": "9.6.7"
"node": "20.5.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function runTestCustomMedia(source, testPath, assertEqual, expectGeneralE
fs.writeFileSync(path.join(process.cwd(), `./test/cases/${testPath}.expect.json`), resultAST_JSON);
fs.writeFileSync(path.join(process.cwd(), `./test/cases/${testPath}.result.json`), resultAST_JSON);
} else {
const expectData = JSON.parse(fs.readFileSync(path.join(process.cwd(), `./test/cases/${testPath}.expect.json`)).toString());
const expectData = JSON.parse(fs.readFileSync(path.join(process.cwd(), `./test/cases/${testPath}.expect.json`), 'utf-8').toString());
if (resultAST === false) {
assertEqual(resultAST, expectData);
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/media-query-list-parser/test/util/run-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function runTest(source, testPath, assertEqual, expectGeneralEnclosed = 0
fs.writeFileSync(path.join(process.cwd(), `./test/cases/${testPath}.expect.json`), resultAST_JSON);
fs.writeFileSync(path.join(process.cwd(), `./test/cases/${testPath}.result.json`), resultAST_JSON);
} else {
const expectData = JSON.parse(fs.readFileSync(path.join(process.cwd(), `./test/cases/${testPath}.expect.json`)).toString());
const expectData = JSON.parse(fs.readFileSync(path.join(process.cwd(), `./test/cases/${testPath}.expect.json`), 'utf-8').toString());

assertEqual(
resultAST.map((x) => x.toString()).join(','),
Expand Down
Loading

0 comments on commit b0dfbd1

Please sign in to comment.