Skip to content

Commit

Permalink
fix: transformer loading to use existing packages and use global prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
attilah committed May 4, 2020
1 parent e1621a8 commit 2053463
Show file tree
Hide file tree
Showing 3 changed files with 995 additions and 633 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const inquirer = require('inquirer');
const importGlobal = require('import-global');
const importFrom = require('import-from');
const { DynamoDBModelTransformer } = require('graphql-dynamodb-transformer');
const { ModelAuthTransformer } = require('graphql-auth-transformer');
const { ModelConnectionTransformer } = require('graphql-connection-transformer');
Expand Down Expand Up @@ -76,6 +78,7 @@ function getTransformerFactory(context, resourceDir, authConfig) {
// The loading of transformer can happen multiple ways in the following order:
// - modulePath is an absolute path to an NPM package
// - modulePath is a package name, then it will be loaded from the project's root's node_modules with createRequireFromPath.
// - modulePath is a name of a globally installed package
let importedModule;
const tempModulePath = modulePath.toString();

Expand All @@ -84,11 +87,19 @@ function getTransformerFactory(context, resourceDir, authConfig) {
// Load it by absolute path
importedModule = require(modulePath);
} else {
// 'require' from project path
const projectRootPath = context.amplify.pathManager.searchProjectRootPath();
const { createRequireFromPath } = require('module');
const projectRequire = createRequireFromPath(path.join(projectRootPath, 'noop.js')); // createRequireFromPath doesn't support directory well. we need to add noop file. See https://github.com/nodejs/node/issues/23710 for detail
importedModule = projectRequire(tempModulePath);
const projectNodeModules = path.join(projectRootPath, 'node_modules');

try {
importedModule = importFrom(projectNodeModules, modulePath);
} catch (_) {
// Intentionally left blank to try global
}

// Try global package install
if (!importedModule) {
importedModule = importGlobal(modulePath);
}
}

// At this point we've to have an imported module, otherwise module loader, threw an error.
Expand Down
2 changes: 2 additions & 0 deletions packages/amplify-provider-awscloudformation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"graphql-predictions-transformer": "2.3.3",
"graphql-transformer-core": "6.17.0",
"graphql-versioned-transformer": "4.15.3",
"import-from": "^3.0.0",
"import-global": "^0.1.0",
"ini": "^1.3.5",
"inquirer": "^7.0.3",
"lodash": "^4.17.15",
Expand Down
Loading

0 comments on commit 2053463

Please sign in to comment.