Skip to content
This repository has been archived by the owner on Mar 14, 2022. It is now read-only.

Commit

Permalink
use fs/promises instead of making promisied versions ourselves
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeChampion committed Jun 18, 2021
1 parent 5f36fcc commit 7894525
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 54 deletions.
3 changes: 1 addition & 2 deletions lib/helpers/construct-polyfill-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

const path = require('path');
const globby = require('globby');
const denodeify = require('util').promisify;
const readFile = denodeify(require('fs-extra').readFile);
const { readFile } = require('fs/promises');

async function constructBrowserDeps() {
const dependencies = await globby(['node_modules/*/*/origami.json', 'node_modules/*/origami.json', 'origami.json']);
Expand Down
5 changes: 2 additions & 3 deletions lib/helpers/files.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';

const fs = require('fs-extra');
const { readFile, open } = require('fs/promises');
const path = require('path');
const denodeify = require('util').promisify;
const deglob = denodeify(require('deglob'));

const fileExists = (file) => denodeify(fs.open)(file, 'r').then(() => true).catch(() => false);
const readFile = denodeify(fs.readFile);
const fileExists = (file) => open(file, 'r').then(() => true).catch(() => false);

function getBuildFolderPath(cwd) {
cwd = cwd || process.cwd();
Expand Down
4 changes: 1 addition & 3 deletions lib/tasks/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
const Listr = require('listr');
const ListrRenderer = require('../helpers/listr-renderer');
const path = require('path');
const fs = require('fs');
const denodeify = require('util').promisify;
const readFile = denodeify(fs.readFile);
const {readFile} = require('fs/promises');
const buildDemo = require('./demo-build');

module.exports = function (cfg) {
Expand Down
6 changes: 2 additions & 4 deletions lib/tasks/test-sass-compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ const ListrRenderer = require('../helpers/listr-renderer');
const isCI = require('is-ci');
const { camelCase } = require('lodash');
const ftSass = require('@financial-times/sass');
const denodeify = require('util').promisify;
const fsOpen = denodeify(require('fs').open);
const { readFile } = require('fs/promises');
const { readFile, open } = require('fs/promises');
const execa = require('execa');

const fileExists = file => fsOpen(file, 'r').then(() => true).catch(() => false);
const fileExists = file => open(file, 'r').then(() => true).catch(() => false);
async function compilationTest(cwd, { silent, brand } = {
silent: false,
brand: false
Expand Down
5 changes: 1 addition & 4 deletions lib/tasks/test-sass.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
"use strict";

const commandLine = require("../helpers/command-line");
const denodeify = require("util").promisify;
const files = require("../helpers/files");
const fs = require("fs");
const { writeFile, unlink } = require("fs/promises");
const path = require("path");
const writeFile = denodeify(fs.writeFile);
const unlink = denodeify(fs.unlink);

const trueTest = function(config, task) {
const testRunner = path.join(config.cwd, "test/scss/test-runner.js");
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/verify-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const path = require('path');
const ESLint = require('eslint').ESLint;
const denodeify = require('util').promisify;
const deglob = denodeify(require('deglob'));
const fs = require('fs-extra');
const { open } = require('fs/promises');
const log = require('../helpers/log');
const isCI = require('is-ci');

const fileExists = file => denodeify(fs.open)(file, 'r').then(() => true).catch(() => false);
const fileExists = file => open(file, 'r').then(() => true).catch(() => false);

async function lint (config) {
const hasJS = await projectHasJavaScriptFiles(config);
Expand Down
6 changes: 2 additions & 4 deletions lib/tasks/verify-origami-json.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';

const process = require('process');
const fs = require('fs');
const { readFile, open} = require('fs/promises');
const path = require('path');
const denodeify = require('util').promisify;
const isCI = require('is-ci');

const fileExists = file => denodeify(fs.open)(file, 'r').then(() => true).catch(() => false);
const readFile = denodeify(fs.readFile);
const fileExists = file => open(file, 'r').then(() => true).catch(() => false);

// https://origami.ft.com/docs/manifests/origami-json/#origamitype
// "component": A front-end component that follows the component specification
Expand Down
14 changes: 6 additions & 8 deletions lib/tasks/verify-package-json.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict';

const process = require('process');
const fs = require('fs');
const { readFile, open } = require('fs/promises');
const path = require('path');
const denodeify = require('util').promisify;
const isCI = require('is-ci');
const semver = require('semver');

const fileExists = file => denodeify(fs.open)(file, 'r').then(() => true).catch(() => false);
const readFile = denodeify(fs.readFile);
const fileExists = file => open(file, 'r').then(() => true).catch(() => false);

/**
* Checks whether description conforms to the origami package.json description specification.
Expand Down Expand Up @@ -64,11 +62,11 @@ function validEngines(engines) {
* Checks whether browser conforms to the origami package.json browser specification.
* @param {any} manifest The manifest to check
* @param {string} workingDirectory The directory which contains the component to check
* @returns {string|void} If valid, returns undefined, otherwise returns a string which explains why it is not valid
* @returns {Promise<string|void>} If valid, returns undefined, otherwise returns a string which explains why it is not valid
*/
function validJavaScriptEntryPoint(manifest, workingDirectory) {
async function validJavaScriptEntryPoint(manifest, workingDirectory) {
const mainJavaScriptExpectedPath = path.resolve(path.join(workingDirectory, '/main.js'));
const mainJavaScriptFileExists = fs.existsSync(mainJavaScriptExpectedPath);
const mainJavaScriptFileExists = await fileExists(mainJavaScriptExpectedPath);
if (mainJavaScriptFileExists) {
if (typeof manifest.browser === 'string') {
const browserPathIsCorrect = path.resolve(manifest.browser) === mainJavaScriptExpectedPath;
Expand Down Expand Up @@ -129,7 +127,7 @@ async function packageJson(config) {
result.push('The engines property is required. It must have the npm property set to a SemVer range which disallows versions lower than 7.0.0.');
}

const invalidExplanation = validJavaScriptEntryPoint(packageJson, config.cwd);
const invalidExplanation = await validJavaScriptEntryPoint(packageJson, config.cwd);
if (invalidExplanation) {
result.push(invalidExplanation);
}
Expand Down
12 changes: 4 additions & 8 deletions lib/tasks/verify-readme.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
'use strict';

const process = require('process');
const {
promises: {readFile},
} = require('fs');
const { readFile, open } = require('fs/promises');
const path = require('path');
const isCI = require('is-ci');
const vfile = require('vfile');
const fs = require('fs-extra');
const remark = require('remark');
const remarkLint = require('remark-lint');
const report = require('vfile-reporter');
const log = require('../helpers/log');
const denodeify = require('util').promisify;

const fileExists = file => denodeify(fs.open)(file, 'r').then(() => true).catch(() => false);
const fileExists = file => open(file, 'r').then(() => true).catch(() => false);

async function origamiJson(config) {
// Error if there is no readme to verify.
const uppercaseReadmePath = path.join(config.cwd, '/README.md');
const hasUppercaseReadme = await fs.exists(uppercaseReadmePath);
const hasUppercaseReadme = await fileExists(uppercaseReadmePath);
const lowercaseReadmePath = path.join(config.cwd, '/readme.md');
const hasLowercaseReadme = await fs.exists(lowercaseReadmePath);
const hasLowercaseReadme = await fileExists(lowercaseReadmePath);
if (!hasUppercaseReadme && !hasLowercaseReadme) {
throw new Error('Components require a README.md with documentation.');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/verify-sass.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const process = require('process');
const path = require('path');
const denodeify = require('util').promisify;
const deglob = denodeify(require('deglob'));
const fs = require('fs-extra');
const { open } = require('fs/promises');
const isCI = require('is-ci');
const execa = require('execa');

const fileExists = file => denodeify(fs.open)(file, 'r').then(() => true).catch(() => false);
const fileExists = file => open(file, 'r').then(() => true).catch(() => false);
async function sassLint(config) {
const hasScss = await projectHasScssFiles(config);

Expand Down
3 changes: 1 addition & 2 deletions test/integration/helpers/fileExists.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const denodeify = require('util').promisify;
const open = denodeify(require('fs-extra').open);
const open = require('fs/promises').open;

/**
* Node.JS no longer has an fs.exists method.
Expand Down
4 changes: 1 addition & 3 deletions test/integration/helpers/obtpath.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const denodeify = require('util').promisify;
const fs = require('fs-extra');
const { readFile } = require('fs/promises');
const path = require('path');

const readFile = denodeify(fs.readFile);

module.exports = function obtBin() {
return readFile(path.join(__dirname, '../../../package.json'))
Expand Down
8 changes: 4 additions & 4 deletions test/integration/verify/verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const process = require('process');
const proclaim = require('proclaim');
const obtBinPath = require('../helpers/obtpath');
const rimraf = require('../helpers/delete');
const fs = require('fs');
const { promisify } = require('util');
const writeFile = promisify(fs.writeFile);
const { writeFile, open } = require('fs/promises');
const tmpdir = require('../helpers/tmpdir');

const fileExists = file => open(file, 'r').then(() => true).catch(() => false);

describe('obt verify', function () {
let obt;

Expand Down Expand Up @@ -94,7 +94,7 @@ describe('obt verify', function () {
const folder = await tmpdir('obt-verify-task-');
const filePath = path.join(folder, 'testFilesystemCaseSensitivity.txt');
await writeFile(path.join(folder, 'testFilesystemCaseSensitivity.txt'), "hello", "utf8");
const caseSensitiveFileSystem = fs.existsSync(filePath.toUpperCase());
const caseSensitiveFileSystem = await fileExists(filePath.toUpperCase());
// Do not run this test on case-insensitive filesystems because it will fail.
if (!caseSensitiveFileSystem) {
return execa(obt, ['verify'])
Expand Down
6 changes: 1 addition & 5 deletions test/unit/helpers/construct-polyfill-url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@ describe('construct-polyfill-url', function() {
let globby;
let constructPolyfillUrl;
let fs;
let denodeify;
beforeEach(function() {
fs = {
readFile: sinon.stub()
};
globby = sinon.stub();
denodeify = sinon.stub().returnsArg(0);
mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
});

mockery.registerMock('fs-extra', fs);

mockery.registerMock('util', {promisify:denodeify});
mockery.registerMock('fs/promises', fs);

mockery.registerMock('globby', globby);

Expand Down

0 comments on commit 7894525

Please sign in to comment.