mirror of
https://github.com/knex/knex.git
synced 2025-12-28 23:48:58 +00:00
Minor improvements on the usage of fs utilities - unify all the fs functions into the same util/fs to simplify things (#3749)
This commit is contained in:
parent
ec2351b7a8
commit
a6551559f0
23
bin/cli.js
23
bin/cli.js
@ -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,
|
||||
@ -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);
|
||||
|
||||
@ -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}`));
|
||||
|
||||
@ -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',
|
||||
@ -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,
|
||||
|
||||
@ -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`
|
||||
@ -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);
|
||||
|
||||
@ -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.
|
||||
@ -35,6 +33,9 @@ function ensureDirectoryExists(dir) {
|
||||
|
||||
module.exports = {
|
||||
stat,
|
||||
readdir,
|
||||
readFile,
|
||||
writeFile,
|
||||
createTemp,
|
||||
ensureDirectoryExists,
|
||||
};
|
||||
|
||||
@ -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.
|
||||
@ -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
|
||||
*
|
||||
|
||||
@ -4,6 +4,9 @@ const path = require('path');
|
||||
const { expect } = require('chai');
|
||||
const {
|
||||
stat,
|
||||
readdir,
|
||||
readFile,
|
||||
writeFile,
|
||||
createTemp,
|
||||
ensureDirectoryExists,
|
||||
} = require('../../../lib/util/fs');
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user