Skip to content

Commit

Permalink
fix: 🐛 add more meaningful log to importAny
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisermann committed Jul 11, 2020
1 parent 4c2d9d1 commit 2f7053e
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 64 deletions.
3 changes: 1 addition & 2 deletions src/autoProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
Transformers,
Options,
} from './types';
import { hasDepInstalled } from './modules/hasDepInstalled';
import { concat } from './modules/concat';
import { hasDepInstalled, concat } from './modules/utils';
import { getTagInfo } from './modules/tagInfo';
import {
addLanguageAlias,
Expand Down
9 changes: 0 additions & 9 deletions src/modules/concat.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/modules/getIncludePaths.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/modules/hasDepInstalled.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/modules/importAny.ts

This file was deleted.

58 changes: 58 additions & 0 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { dirname } from 'path';

export async function importAny(...modules: string[]) {
try {
const mod = await modules.reduce(
(acc, moduleName) => acc.catch(() => import(moduleName)),
Promise.reject(),
);

return mod;
} catch (e) {
throw new Error(`Cannot find any of modules: ${modules}\n\n${e}`);
}
}

export function concat(...arrs: any[]): any[] {
return arrs.reduce((acc: [], a) => {
if (a) {
return acc.concat(a);
}

return acc;
}, []);
}

/** Paths used by preprocessors to resolve @imports */
export function getIncludePaths(fromFilename: string, base: string[] = []) {
return [
...new Set([...base, 'node_modules', process.cwd(), dirname(fromFilename)]),
];
}

const cachedResult: Record<string, boolean> = {};

/**
* Checks if a package is installed.
*
* @export
* @param {string} dep
* @returns boolean
*/
export async function hasDepInstalled(dep: string) {
if (cachedResult[dep] != null) {
return cachedResult[dep];
}

let result = false;

try {
await import(dep);

result = true;
} catch (e) {
result = false;
}

return (cachedResult[dep] = result);
}
2 changes: 1 addition & 1 deletion src/processors/babel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PreprocessorGroup, Options } from '../types';
import { concat } from '../modules/concat';
import { concat } from '../modules/utils';
import { getTagInfo } from '../modules/tagInfo';
import { prepareContent } from '../modules/prepareContent';

Expand Down
2 changes: 1 addition & 1 deletion src/processors/coffeescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PreprocessorGroup, Options } from '../types';
import { getTagInfo } from '../modules/tagInfo';
import { concat } from '../modules/concat';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

export default (options?: Options.Coffeescript): PreprocessorGroup => ({
Expand Down
2 changes: 1 addition & 1 deletion src/processors/less.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PreprocessorGroup, Options } from '../types';
import { getTagInfo } from '../modules/tagInfo';
import { concat } from '../modules/concat';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

export default (options?: Options.Less): PreprocessorGroup => ({
Expand Down
2 changes: 1 addition & 1 deletion src/processors/postcss.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PreprocessorGroup, Options } from '../types';
import { getTagInfo } from '../modules/tagInfo';
import { concat } from '../modules/concat';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

/** Adapted from https://github.com/TehShrike/svelte-preprocess-postcss */
Expand Down
2 changes: 1 addition & 1 deletion src/processors/scss.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PreprocessorGroup, Options } from '../types';
import { getTagInfo } from '../modules/tagInfo';
import { concat } from '../modules/concat';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

export default (options?: Options.Sass): PreprocessorGroup => ({
Expand Down
2 changes: 1 addition & 1 deletion src/processors/stylus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Options, PreprocessorGroup } from '../types';
import { getTagInfo } from '../modules/tagInfo';
import { concat } from '../modules/concat';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

export default (options?: Options.Stylus): PreprocessorGroup => ({
Expand Down
2 changes: 1 addition & 1 deletion src/processors/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Options, PreprocessorGroup } from '../types';
import { getTagInfo } from '../modules/tagInfo';
import { concat } from '../modules/concat';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

export default (options?: Options.Typescript): PreprocessorGroup => ({
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/less.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import less from 'less';

import { getIncludePaths } from '../modules/getIncludePaths';
import { getIncludePaths } from '../modules/utils';
import { Transformer, Options } from '../types';

const transformer: Transformer<Options.Less> = async ({
Expand Down
3 changes: 1 addition & 2 deletions src/transformers/scss.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Result } from 'sass';

import { Transformer, Processed, Options } from '../types';
import { getIncludePaths } from '../modules/getIncludePaths';
import { importAny } from '../modules/importAny';
import { getIncludePaths, importAny } from '../modules/utils';

let sass: Options.Sass['implementation'];

Expand Down
2 changes: 1 addition & 1 deletion src/transformers/stylus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';

import stylus from 'stylus';

import { getIncludePaths } from '../modules/getIncludePaths';
import { getIncludePaths } from '../modules/utils';
import { Transformer, Options } from '../types';

const transformer: Transformer<Options.Stylus> = ({
Expand Down
8 changes: 5 additions & 3 deletions test/modules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { resolve } from 'path';

import { getTestAppFilename, getFixtureContent } from './utils';
import { getTagInfo } from '../src/modules/tagInfo';
import { importAny } from '../src/modules/importAny';
import { getIncludePaths } from '../src/modules/getIncludePaths';
import {
importAny,
getIncludePaths,
hasDepInstalled,
} from '../src/modules/utils';
import { globalifySelector } from '../src/modules/globalifySelector';
import { hasDepInstalled } from '../src/modules/hasDepInstalled';

describe('importAny', () => {
it('should throw error when none exist', () => {
Expand Down

0 comments on commit 2f7053e

Please sign in to comment.