Skip to content

Commit

Permalink
Minor improvements on the usage of fs utilities - unify all the fs fu…
Browse files Browse the repository at this point in the history
…nctions into the same util/fs to simplify things (#3749)
  • Loading branch information
kabirbaidhya committed Mar 24, 2020
1 parent ec2351b commit a655155
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 34 deletions.
23 changes: 8 additions & 15 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const tildify = require('tildify');
const commander = require('commander');
const color = require('colorette');
const argv = require('getopts')(process.argv.slice(2));
const fs = require('fs');
const { promisify } = require('util');
const cliPkg = require('../package');
const {
mkConfigObj,
Expand All @@ -20,14 +18,10 @@ const {
getSeedExtension,
getStubPath,
} = require('./utils/cli-config-utils');
const { readFile, writeFile } = require('./../lib/util/fs');

const { listMigrations } = require('./utils/migrationsLister');

const fsPromised = {
readFile: promisify(fs.readFile),
writeFile: promisify(fs.writeFile),
};

function openKnexfile(configPath) {
const config = require(configPath);

Expand Down Expand Up @@ -118,15 +112,14 @@ function invoke(env) {
}
checkLocalModule(env);
const stubPath = `./knexfile.${type}`;
pending = fsPromised
.readFile(
path.dirname(env.modulePath) +
'/lib/migrate/stub/knexfile-' +
type +
'.stub'
)
pending = readFile(
path.dirname(env.modulePath) +
'/lib/migrate/stub/knexfile-' +
type +
'.stub'
)
.then((code) => {
return fsPromised.writeFile(stubPath, code);
return writeFile(stubPath, code);
})
.then(() => {
success(color.green(`Created ${stubPath}`));
Expand Down
6 changes: 2 additions & 4 deletions lib/migrate/sources/fs-migrations.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const { sortBy, filter } = require('lodash');

const readDirAsync = promisify(fs.readdir);
const { readdir } = require('../../util/fs');

const DEFAULT_LOAD_EXTENSIONS = Object.freeze([
'.co',
Expand Down Expand Up @@ -35,7 +33,7 @@ class FsMigrations {
// Get a list of files in all specified migration directories
const readMigrationsPromises = this.migrationsPaths.map((configDir) => {
const absoluteDir = path.resolve(process.cwd(), configDir);
return readDirAsync(absoluteDir).then((files) => ({
return readdir(absoluteDir).then((files) => ({
files,
configDir,
absoluteDir,
Expand Down
6 changes: 2 additions & 4 deletions lib/seed/Seeder.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Seeder
// -------

const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const { filter, includes, extend } = require('lodash');
const { ensureDirectoryExists } = require('../util/fs');
const { readdir, ensureDirectoryExists } = require('../util/fs');
const { writeJsFileUsingTemplate } = require('../util/template');

// The new seeds we're performing, typically called from the `knex.seed`
Expand Down Expand Up @@ -42,7 +40,7 @@ class Seeder {
async _listAll(config) {
this.config = this.setConfig(config);
const loadExtensions = this.config.loadExtensions;
return promisify(fs.readdir)(this._absoluteConfigDir()).then((seeds) =>
return readdir(this._absoluteConfigDir()).then((seeds) =>
filter(seeds, (value) => {
const extension = path.extname(value);
return includes(loadExtensions, extension);
Expand Down
13 changes: 7 additions & 6 deletions lib/util/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ const path = require('path');
const { promisify } = require('util');
const mkdirp = require('mkdirp');

/**
* Promisified version of fs.stat() function.
*
* @param {string} path
* @returns {Promise}
*/
// Promisify common fs functions.
const stat = promisify(fs.stat);
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const readdir = promisify(fs.readdir);

/**
* Creates a temporary directory and returns it path.
Expand All @@ -35,6 +33,9 @@ function ensureDirectoryExists(dir) {

module.exports = {
stat,
readdir,
readFile,
writeFile,
createTemp,
ensureDirectoryExists,
};
7 changes: 2 additions & 5 deletions lib/util/template.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { template } = require('lodash');
const { promisify } = require('util');
const fs = require('fs');

const { readFile, writeFile } = require('./fs');

/**
* Light wrapper over lodash templates making it safer to be used with javascript source code.
Expand All @@ -16,9 +16,6 @@ const jsSourceTemplate = (content, options) =>
...options,
});

const readFile = promisify(fs.readFile, { context: fs });
const writeFile = promisify(fs.writeFile, { context: fs });

/**
* Compile the contents of specified (javascript) file as a lodash template
*
Expand Down
48 changes: 48 additions & 0 deletions test/unit/util/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const path = require('path');
const { expect } = require('chai');
const {
stat,
readdir,
readFile,
writeFile,
createTemp,
ensureDirectoryExists,
} = require('../../../lib/util/fs');
Expand All @@ -26,6 +29,51 @@ describe('FS functions', () => {
});
});

describe('readFile', async () => {
it('should be able to read from the given file path.', async () => {
const tmpPath = await createTemp();
const filePath = path.join(tmpPath, `${Date.now()}1.txt`);

fs.writeFileSync(filePath, 'Hello World!');

const contents = await readFile(filePath);

expect(contents.toString()).to.equal('Hello World!');
});
});

describe('writeFile', async () => {
it('should be able to write to the given file path.', async () => {
const tmpPath = await createTemp();
const filePath = path.join(tmpPath, `${Date.now()}2.txt`);

await writeFile(filePath, 'Foo Bar');

expect(fs.readFileSync(filePath).toString()).to.equal('Foo Bar');
});
});

describe('readdir', async () => {
it('should read a directory and return a list of files under it.', async () => {
const directory = await createTemp();

// Create files under the directory.
fs.writeFileSync(path.join(directory, 'testfile1.txt'), 'testfile1');
fs.writeFileSync(path.join(directory, 'testfile2.txt'), 'testfile2');
fs.writeFileSync(path.join(directory, 'testfile3.txt'), 'testfile3');
fs.writeFileSync(path.join(directory, 'testfile4.txt'), 'testfile4');

const result = await readdir(directory);

expect(result).to.deep.equal([
'testfile1.txt',
'testfile2.txt',
'testfile3.txt',
'testfile4.txt',
]);
});
});

describe('ensureDirectoryExists', () => {
it('should resolve successfully if the directory already exists.', async () => {
const directoryThatExists = await createTemp();
Expand Down

0 comments on commit a655155

Please sign in to comment.