Skip to content

Commit

Permalink
Adapt to using file pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Apr 7, 2024
1 parent 98e087b commit 8f179c7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
49 changes: 34 additions & 15 deletions lib/extra-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,41 @@ const OsHelpers = require('./os-helpers');

/**
* Collect the extra files requested by the rules.
* @param {Options} options
* @param {Path[]} requestedFiles
* @param {{ files : { pattern: string, included: boolean }[], excludedFolders: string[] }[]} requests
* @returns {Promise<ExtraFile[]>}
*/
async function collect(options, requestedFiles) {
async function collect(requests) {
const {globby} = await import('globby');
const files = await globby(
requestedFiles.map((file) => OsHelpers.makePathOsAgnostic(file)),
const files = await Promise.all(
requests.map((request) => getFiles(globby, request))
)
.then((files) => files.reduce((acc, items) => acc.concat(items), []))
.then(unique);

return Promise.all(
files.map(async (filePath) => {
const content = await FS.readFile(filePath);
return {
path: filePath,
content: content
};
})
);
}

/** .
* @param {any} globby
* @param {{ files : { pattern: string, included: boolean }[], excludedFolders: string[] }} request
* @returns {Promise<ExtraFile[]>}
*/
function getFiles(globby, request) {
return globby(
request.files.map(
(file) =>
(file.included ? '' : '!') + OsHelpers.makePathOsAgnostic(file.pattern)
),
{
ignore: request.excludedFolders,
followSymbolicLinks: true,
expandDirectories: false,
caseSensitiveMatch: true,
Expand All @@ -26,17 +52,10 @@ async function collect(options, requestedFiles) {
extglob: false
}
);
console.log(files.sort())
}

return Promise.all(
files.sort().map(async (filePath) => {
const content = await FS.readFile(filePath);
return {
path: filePath,
content: content
};
})
);
function unique(array) {
return [...new Set(array)];
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async function initializeApp(options, elmModulePath, reviewElmJson, appHash) {
let resolve = null;
const requestedFilesP = new Promise((r) => {
resolve = r;
}).then((files) => ExtraFiles.collect(options, files));
}).then(ExtraFiles.collect);
AppState.subscribe(app.ports.requestReadingFiles, resolve);

ModuleCache.subscribe(options, app);
Expand Down
4 changes: 2 additions & 2 deletions template/src/Elm/Review/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Set exposing (Set)
-- PORTS


port requestReadingFiles : List String -> Cmd msg
port requestReadingFiles : List { files : List { pattern : String, included : Bool }, excludedFolders : List String } -> Cmd msg


port collectFile : (Decode.Value -> msg) -> Sub msg
Expand Down Expand Up @@ -302,7 +302,7 @@ I recommend you take a look at the following documents:
[ cmd

-- TODO Don't trigger when the other cmd is `abort`
, rules |> List.concatMap Rule.ruleRequestedFiles |> Set.fromList |> Set.toList |> requestReadingFiles
, rules |> List.concatMap Rule.ruleRequestedFiles |> requestReadingFiles
]

configurationErrors ->
Expand Down

0 comments on commit 8f179c7

Please sign in to comment.