Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic glob support for assets #87

Merged
merged 2 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@babel/preset-env": "^7.5.5",
"@pika/cli": "latest",
"chalk": "^2.4.2",
"glob": "^7.1.4",
"is-builtin-module": "^3.0.0",
"mkdirp": "^0.5.1",
"ora": "^3.1.0",
Expand Down
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import rimraf from 'rimraf';
import mkdirp from 'mkdirp';
import chalk from 'chalk';
import glob from 'glob';
import ora from 'ora';
import yargs from 'yargs-parser';

Expand Down Expand Up @@ -170,6 +171,19 @@ export async function install(
) {
const knownNamedExports = {...namedExports};
const remotePackageMap = fromEntries(remotePackages);
const globDependencies = arrayOfDeps.filter(dep => glob.hasMagic(dep));
if (globDependencies.length) {
globDependencies.forEach(dep => {
const indexOfDep = arrayOfDeps.indexOf(dep);
arrayOfDeps.splice(indexOfDep, 1);
});
const globResults = globDependencies
.map(dep => path.join(cwd, 'node_modules', dep))
.map(dep => glob.sync(dep))
.flat()
Copy link
Owner

@FredKSchott FredKSchott Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately we still need to support Node 8+, and I believe this was introduced in Node 11.

Here's another way you could write this, half getting rid of flat & half just doing some code golf. The nice thing about using a Set is that now that we get deduplication for free (more important now that we have globs). But feel free to use this however you see fit.

const depsList = new Set();
arrayOfDeps.forEach(dep => {
  if (!glob.hasMagic(dep)) {
    depsList.add(dep);
    return;
  }
  const globLoc = path.join(cwd, 'node_modules', dep);
  glob.sync(globLoc).forEach((globMatchLoc) => {
    const globMatch = path.relative(`${cwd}/node_modules`, globMatch);
    depsList.add(globMatch);
  });
});
// Then, just replace the 1-2 later references to arrayOfDeps with the new depsList

.map(dep => path.relative(`${cwd}/node_modules`, dep));
arrayOfDeps.push(...globResults);
}
for (const filePath of PACKAGES_TO_AUTO_DETECT_EXPORTS) {
knownNamedExports[filePath] = knownNamedExports[filePath] || detectExports(filePath) || [];
}
Expand Down