Skip to content

Commit

Permalink
fix(app): always use POSIX separators when using fast-glob
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Sep 20, 2023
1 parent d378481 commit da9e891
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 87 deletions.
13 changes: 5 additions & 8 deletions app/lib/config/providers/ElementTemplatesProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
*/

const fs = require('fs');
const { globSync } = require('fast-glob');
const parents = require('parents');
const path = require('path');

const { isArray } = require('min-dash');

const { globFiles } = require('../../util/files');

const log = require('../../log')('app:config:element-templates');


Expand Down Expand Up @@ -143,11 +144,7 @@ function getTemplatesForPath(path) {
* @return {Array<string>}
*/
function globTemplates(path) {
const globOptions = {
cwd: path,
onlyFiles: true,
absolute: true
};

return globSync('element-templates/**/*.json', globOptions);
return globFiles('element-templates/**/*.json', {
cwd: path
});
}
5 changes: 2 additions & 3 deletions app/lib/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ class Flags {
config,
files,
errors
} = globJSON({
searchPaths,
pattern: FLAGS_PATTERN
} = globJSON(FLAGS_PATTERN, {
searchPaths
});

log.info('found %O', files);
Expand Down
24 changes: 9 additions & 15 deletions app/lib/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
*/

const path = require('path');
const { globSync } = require('fast-glob');

const log = require('./log')('app:plugins');

const {
globFiles
} = require('./util/files');
const { globFiles } = require('./util/files');

const PLUGINS_PATTERN = 'plugins/*/index.js';

Expand All @@ -42,9 +39,8 @@ class Plugins {

log.info('searching for %s in paths %O', PLUGINS_PATTERN, searchPaths);

const pluginPaths = globFiles({
searchPaths,
pattern: PLUGINS_PATTERN
const pluginPaths = globFiles(PLUGINS_PATTERN, {
searchPaths
});

log.info('found plug-in entries %O', pluginPaths);
Expand Down Expand Up @@ -86,10 +82,9 @@ class Plugins {
};

if (style) {
const stylePath = path.join(base, style);
const styleFiles = globSync(stylePath, {
absolute: true
});
const stylePath = path.posix.join(base, style);

const styleFiles = globFiles(stylePath);

if (!styleFiles.length) {
plugin.error = true;
Expand All @@ -99,10 +94,9 @@ class Plugins {
}

if (script) {
const scriptPath = path.join(base, script);
const scriptFiles = globSync(scriptPath, {
absolute: true
});
const scriptPath = path.posix.join(base, script);

const scriptFiles = globFiles(scriptPath);

if (!scriptFiles.length) {
plugin.error = true;
Expand Down
40 changes: 18 additions & 22 deletions app/lib/util/__tests__/files-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ const path = require('path');

const {
globJSON,
globFiles
globFiles,
toPosixPath
} = require('../files');


describe('files', function() {

describe('#globJSON', function() {

it('should load by name', function() {
it('should read given file name', function() {

// when
const { config } = globJSON({
name: 'config.json',
const { config } = globJSON('config.json', {
searchPaths: [
absPath('files/bar')
toAbsolutePath('files/bar')
]
});

Expand All @@ -35,13 +35,12 @@ describe('files', function() {
});


it('should load by pattern', function() {
it('should read given pattern', function() {

// when
const { config } = globJSON({
name: '**/config.json',
const { config } = globJSON('**/config.json', {
searchPaths: [
absPath('files')
toAbsolutePath('files')
]
});

Expand All @@ -56,10 +55,9 @@ describe('files', function() {
it('should consider defaults', function() {

// when
const { config } = globJSON({
name: 'config.json',
const { config } = globJSON('config.json', {
searchPaths: [
absPath('files/bar')
toAbsolutePath('files/bar')
],
defaults: {
foo: 'default FOO',
Expand All @@ -78,10 +76,9 @@ describe('files', function() {
it('should handle errors gracefully', function() {

// when
const { config } = globJSON({
name: '**/*-json.json',
const { config } = globJSON('**/*-json.json', {
searchPaths: [
absPath('files')
toAbsolutePath('files')
]
});

Expand All @@ -99,17 +96,16 @@ describe('files', function() {
it('should find files by pattern', function() {

// when
const files = globFiles({
pattern: '**/*-json.json',
const files = globFiles('**/*-json.json', {
searchPaths: [
absPath('files')
toAbsolutePath('files')
]
});

// then
expect(files).to.eql([
absPath('files/bar/good-json.json'),
absPath('files/foo/not-json.json')
toAbsolutePath('files/bar/good-json.json'),
toAbsolutePath('files/foo/not-json.json')
]);

});
Expand All @@ -119,6 +115,6 @@ describe('files', function() {
});


function absPath(file) {
return path.resolve(__dirname, file);
function toAbsolutePath(file) {
return toPosixPath(path.resolve(__dirname, file));
}
103 changes: 64 additions & 39 deletions app/lib/util/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,45 @@ const { globSync } = require('fast-glob');
const path = require('path');
const fs = require('fs');

const { merge } = require('min-dash');

/**
* Load JSON configuration from multiple paths.
* Find JSON files in one or more paths. Return the merged result, the files
* that were read and errors that occurred. Optionally, provide defaults that
* will be merged into the result.
*
* @param {string} pattern
* @param {Object} [options]
* @param {string[]} [options.searchPaths]
* @param {Object} [options.defaults]
*
* @example
*
* const {
* config,
* errors,
* files,
* config
* } = globJSON({
* name: 'config.json',
* searchPaths: [ __dirname, process.cwd() ]
* files
* } = globJSON('config.json', {
* searchPaths: [
* __dirname,
* process.cwd()
* ]
* });
*
* @return { config, files, errors }
* @return { {
* config: Object,
* errors: Error[],
* files: string[]
* } }
*/
function globJSON(options) {

function globJSON(pattern, options) {
const {
name,
pattern,
searchPaths,
defaults
defaults = {}
} = options;

const files = globFiles({
searchPaths,
pattern: name || pattern
const files = globFiles(pattern, {
searchPaths
});

const {
Expand All @@ -60,10 +71,6 @@ function globJSON(options) {
module.exports.globJSON = globJSON;


function merge(...objects) {
return Object.assign({}, ...objects);
}

/**
* Read the given files and return { contents: [], errors: [] }.
*
Expand Down Expand Up @@ -99,7 +106,6 @@ module.exports.readJSON = readJSON;


function readFileAsJSON(file) {

try {
const contents = fs.readFileSync(file, { encoding: 'UTF-8' });

Expand All @@ -110,35 +116,54 @@ function readFileAsJSON(file) {
}

/**
* Find files by pattern in a list of search paths.
* Find files matching a given pattern in one or more search paths.
*
* @param {Object} options
* @param {Array<string>} options.searchPaths
* @param {string} options.pattern
* @param {string} pattern
* @param {Object} [options]
* @param {string[]} [options.searchPaths]
* @param {boolean} [options.absolute=true]
* @param {boolean} [options.nodir=true]
*
* @return {Array<string>} list of found, absolute path names
* @return {string[]}
*/
function globFiles(options) {
function globFiles(pattern, options = {}) {
const defaultOptions = {
absolute: true,
onlyFiles: true
};

const {
searchPaths,
pattern
...otherOptions
} = options;

return searchPaths.reduce((paths, searchPath) => {

const newPaths = globSync(pattern, {
cwd: searchPath,
nodir: true,
absolute: true
}).map(p => p.split(path.posix.sep).join(path.sep));
if (searchPaths) {
return searchPaths.reduce((paths, searchPath) => {
return [
...paths,
...globSync(toPosixPath(pattern), {
...defaultOptions,
...otherOptions,
cwd: toPosixPath(searchPath)
}).map(toPosixPath)
];
}, []);
}

return [
...paths,
...newPaths
];
}, []);
if (otherOptions.cwd) {
otherOptions.cwd = toPosixPath(otherOptions.cwd);
}

return globSync(toPosixPath(pattern), {
...defaultOptions,
...otherOptions
}).map(toPosixPath);
}

module.exports.globFiles = globFiles;

function toPosixPath(filePath) {
return filePath.split(path.sep).join(path.posix.sep);
}

module.exports.toPosixPath = toPosixPath;

0 comments on commit da9e891

Please sign in to comment.