Skip to content

Commit

Permalink
refactor: use fs, fs-extra, remove sander (#4319)
Browse files Browse the repository at this point in the history
* refactor: use fs.readFileSync

* refactor: use fs.existsSync

* refactor: use fs.writeFileSync

* refactor: use fs.unlinkSync

* refactor: use fs.readdirSync

* refactor: use fs-extra.removeSync

* refactor: use fs-extra.copy/copySync

* remove sander

* fix test
  • Loading branch information
dnalborczyk committed Jan 29, 2022
1 parent 7194b88 commit 042933a
Show file tree
Hide file tree
Showing 37 changed files with 1,264 additions and 1,343 deletions.
89 changes: 44 additions & 45 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -87,6 +87,7 @@
"eslint-plugin-prettier": "^4.0.0",
"execa": "^5.1.1",
"fixturify": "^2.1.1",
"fs-extra": "^10.0.0",
"hash.js": "^1.1.7",
"husky": "^7.0.4",
"is-reference": "^3.0.0",
Expand All @@ -105,7 +106,6 @@
"rollup-plugin-string": "^3.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-thatworks": "^1.0.4",
"sander": "^0.6.0",
"shx": "^0.3.4",
"signal-exit": "^3.0.6",
"source-map": "^0.7.3",
Expand Down
4 changes: 2 additions & 2 deletions scripts/load-perf-config.js
@@ -1,12 +1,12 @@
const fs = require('fs');
const { accessSync, constants } = require('fs');
const path = require('path');
const rollup = require('../dist/rollup.js');

exports.targetDir = path.resolve(__dirname, '..', 'perf');
const configFile = path.resolve(exports.targetDir, 'rollup.config.js');

try {
fs.accessSync(configFile, fs.constants.R_OK);
accessSync(configFile, constants.R_OK);
} catch (e) {
console.error(
`No valid "rollup.config.js" in ${exports.targetDir}. Did you "npm run perf:init"?`
Expand Down
8 changes: 4 additions & 4 deletions scripts/perf-init.js
@@ -1,9 +1,9 @@
/* eslint-disable no-console */

const fs = require('fs');
const { accessSync, constants } = require('fs');
const path = require('path');
const execa = require('execa');
const sander = require('sander');
const { removeSync } = require('fs-extra');
const repoWithBranch = process.argv[2];

const TARGET_DIR = path.resolve(__dirname, '..', 'perf');
Expand All @@ -17,7 +17,7 @@ if (process.argv.length !== 3 || !VALID_REPO.test(repoWithBranch)) {
process.exit(1);
}
console.error(`Cleaning up '${TARGET_DIR}'...`);
sander.rimrafSync(TARGET_DIR);
removeSync(TARGET_DIR);

const [, repo, , branch] = VALID_REPO.exec(repoWithBranch);

Expand All @@ -43,7 +43,7 @@ async function setupNewRepo(repo, branch) {
gitArgs.push(`https://github.com/${repo}.git`, TARGET_DIR);
await execWithOutput('git', gitArgs);
try {
fs.accessSync(path.resolve(TARGET_DIR, 'rollup.config.js'), fs.constants.R_OK);
accessSync(path.resolve(TARGET_DIR, 'rollup.config.js'), constants.R_OK);
} catch (e) {
throw new Error('The repository needs to have a file "rollup.config.js" at the top level.');
}
Expand Down
6 changes: 3 additions & 3 deletions scripts/perf.js
@@ -1,6 +1,6 @@
/* global gc */

const fs = require('fs');
const { readFileSync, writeFileSync } = require('fs');
const path = require('path');
const colorette = require('colorette');
const prettyBytes = require('pretty-bytes');
Expand Down Expand Up @@ -137,7 +137,7 @@ function clearLines(numberOfLines) {

function getExistingTimings() {
try {
const timings = JSON.parse(fs.readFileSync(perfFile, 'utf8'));
const timings = JSON.parse(readFileSync(perfFile, 'utf8'));
console.info(
colorette.bold(
`Comparing with ${colorette.cyan(perfFile)}. Delete this file to create a new base line.`
Expand All @@ -151,7 +151,7 @@ function getExistingTimings() {

function persistTimings(timings) {
try {
fs.writeFileSync(perfFile, JSON.stringify(timings, null, 2), 'utf8');
writeFileSync(perfFile, JSON.stringify(timings, null, 2), 'utf8');
console.info(
colorette.bold(
`Saving performance information to new reference file ${colorette.cyan(perfFile)}.`
Expand Down
13 changes: 7 additions & 6 deletions scripts/update-snapshots.js
@@ -1,7 +1,8 @@
#!/usr/bin/env node

const { readdirSync } = require('fs');
const { resolve, join } = require('path');
const { readdirSync, copydirSync, copyFileSync, rimrafSync } = require('sander');
const { copySync, removeSync } = require('fs-extra');

const basePath = resolve(__dirname, '../test');

Expand All @@ -14,10 +15,10 @@ for (const dir of formDirsToHandle) {
formDirsToHandle.push(...testFiles.map(filename => join(dir, filename)));
} else if (testFiles.includes('_actual')) {
const expectedPath = join(testPath, '_expected');
rimrafSync(expectedPath);
copydirSync(join(testPath, '_actual')).to(expectedPath);
removeSync(expectedPath);
copySync(join(testPath, '_actual'), expectedPath);
} else if (testFiles.includes('_actual.js')) {
copyFileSync(join(testPath, '_actual.js')).to(join(testPath, '_expected.js'));
copySync(join(testPath, '_actual.js'), join(testPath, '_expected.js'));
} else {
throw new Error(`Could not find test output in ${testPath}`);
}
Expand All @@ -32,8 +33,8 @@ for (const dir of chunkingDirsToHandle) {
chunkingDirsToHandle.push(...testFiles.map(filename => join(dir, filename)));
} else if (testFiles.includes('_actual')) {
const expectedPath = join(testPath, '_expected');
rimrafSync(expectedPath);
copydirSync(join(testPath, '_actual')).to(expectedPath);
removeSync(expectedPath);
copySync(join(testPath, '_actual'), expectedPath);
} else {
throw new Error(`Could not find test output in ${testPath}`);
}
Expand Down
4 changes: 2 additions & 2 deletions test/chunking-form/samples/deprecated/emit-asset/_config.js
@@ -1,4 +1,4 @@
const fs = require('fs');
const { readFileSync } = require('fs');
const path = require('path');

module.exports = {
Expand All @@ -21,7 +21,7 @@ module.exports = {
if (id.endsWith('.svg')) {
return `export default import.meta.ROLLUP_ASSET_URL_${this.emitAsset(
path.basename(id),
fs.readFileSync(id)
readFileSync(id)
)};`;
}
}
Expand Down
@@ -1,4 +1,4 @@
const fs = require('fs');
const { readFileSync } = require('fs');
const path = require('path');

module.exports = {
Expand All @@ -21,7 +21,7 @@ module.exports = {
return `export default import.meta.ROLLUP_FILE_URL_${this.emitFile({
type: 'asset',
name: path.basename(id),
source: fs.readFileSync(id)
source: readFileSync(id)
})};`;
}
}
Expand Down
23 changes: 11 additions & 12 deletions test/cli/index.js
@@ -1,7 +1,9 @@
const assert = require('assert');
const { exec } = require('child_process');
const path = require('path');
const sander = require('sander');
const { existsSync, readFileSync } = require('fs');
const { basename, resolve, sep } = require('path');
const process = require('process');
const { copySync, removeSync, statSync } = require('fs-extra');
const {
normaliseOutput,
runTestSuiteWithSamples,
Expand All @@ -11,12 +13,12 @@ const {

const cwd = process.cwd();

sander.rimrafSync(__dirname, 'node_modules');
sander.copydirSync(__dirname, 'node_modules_rename_me').to(__dirname, 'node_modules');
removeSync(resolve(__dirname, 'node_modules'));
copySync(resolve(__dirname, 'node_modules_rename_me'), resolve(__dirname, 'node_modules'));

runTestSuiteWithSamples(
'cli',
path.resolve(__dirname, 'samples'),
resolve(__dirname, 'samples'),
(dir, config) => {
// allow to repeat flaky tests for debugging on CLI
for (let pass = 0; pass < (config.repeat || 1); pass++) {
Expand All @@ -27,7 +29,7 @@ runTestSuiteWithSamples(
);

function runTest(dir, config, pass) {
const name = path.basename(dir) + ': ' + config.description;
const name = basename(dir) + ': ' + config.description;
(config.skip ? it.skip : config.solo ? it.only : it)(
pass > 0 ? `${name} (pass ${pass + 1})` : name,
done => {
Expand All @@ -37,7 +39,7 @@ function runTest(dir, config, pass) {
}
const command = config.command.replace(
/(^| )rollup($| )/g,
`node ${path.resolve(__dirname, '../../dist/bin')}${path.sep}rollup `
`node ${resolve(__dirname, '../../dist/bin')}${sep}rollup `
);

Promise.resolve(config.before && config.before()).then(() => {
Expand Down Expand Up @@ -111,18 +113,15 @@ function runTest(dir, config, pass) {
} catch (err) {
done(err);
}
} else if (
sander.existsSync('_expected') &&
sander.statSync('_expected').isDirectory()
) {
} else if (existsSync('_expected') && statSync('_expected').isDirectory()) {
try {
assertDirectoriesAreEqual('_actual', '_expected');
done();
} catch (err) {
done(err);
}
} else {
const expected = sander.readFileSync('_expected.js').toString();
const expected = readFileSync('_expected.js', 'utf8');
try {
assert.equal(normaliseOutput(code), normaliseOutput(expected));
done();
Expand Down
6 changes: 3 additions & 3 deletions test/cli/samples/config-no-output/_config.js
@@ -1,12 +1,12 @@
const assert = require('assert');
const fs = require('fs');
const { readFileSync, unlinkSync } = require('fs');

module.exports = {
description: 'uses -o from CLI',
command: 'rollup -c -o output.js',
test() {
const output = fs.readFileSync('output.js', 'utf-8');
const output = readFileSync('output.js', 'utf-8');
assert.equal(output.trim(), 'console.log(42);');
fs.unlinkSync('output.js');
unlinkSync('output.js');
}
};
10 changes: 5 additions & 5 deletions test/cli/samples/sourcemap-hidden/_config.js
@@ -1,16 +1,16 @@
const assert = require('assert');
const fs = require('fs');
const { readFileSync, unlinkSync } = require('fs');

module.exports = {
description: 'omits sourcemap comments',
command: 'rollup -i main.js -f es -m hidden -o output.js',
test() {
assert.equal(fs.readFileSync('output.js', 'utf-8').trim(), 'console.log( 42 );');
fs.unlinkSync('output.js');
assert.equal(readFileSync('output.js', 'utf-8').trim(), 'console.log( 42 );');
unlinkSync('output.js');
assert.equal(
fs.readFileSync('output.js.map', 'utf-8').trim(),
readFileSync('output.js.map', 'utf-8').trim(),
'{"version":3,"file":"output.js","sources":["main.js"],"sourcesContent":["console.log( 42 );\\n"],"names":[],"mappings":"AAAA,OAAO,CAAC,GAAG,EAAE,EAAE,EAAE"}'
);
fs.unlinkSync('output.js.map');
unlinkSync('output.js.map');
}
};

0 comments on commit 042933a

Please sign in to comment.