Skip to content

Commit

Permalink
fix(Plugin resolution): Use the require resolution to retrieve configs
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Feb 3, 2019
1 parent 9a6fac4 commit 87946c2
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 75 deletions.
21 changes: 7 additions & 14 deletions src/assets.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
const { autoHandler } = require('knifecycle');
const path = require('path');
const {
identity,
buildMetapakModulePath,
mapConfigsSequentially,
} = require('./utils');
const { identity, mapConfigsSequentially } = require('./utils');

module.exports = autoHandler(initBuildPackageAssets);

async function initBuildPackageAssets(
{ PROJECT_DIR, fs, log, glob, require, mkdirp },
{ PROJECT_DIR, fs, log, glob, require, mkdirp, resolveModule },
packageConf,
metapakModulesSequence,
metapakModulesConfigs
Expand All @@ -18,18 +14,15 @@ async function initBuildPackageAssets(
metapakModulesSequence,
metapakModulesConfigs,
(metapakModuleName, metapakModuleConfig) => {
const packageAssetsDir = buildMetapakModulePath(
PROJECT_DIR,
packageConf,
metapakModuleName,
const modulePath = resolveModule(metapakModuleName);
const packageAssetsDir = path.join(
modulePath,
'src',
metapakModuleConfig,
'assets'
);
const packageAssetsTransformerPath = buildMetapakModulePath(
PROJECT_DIR,
packageConf,
metapakModuleName,
const packageAssetsTransformerPath = path.join(
modulePath,
'src',
metapakModuleConfig,
'assets.js'
Expand Down
6 changes: 6 additions & 0 deletions src/assets.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ describe('buildPackageAssets', () => {
})
);
$.register(constant('require', requireStub));
$.register(
constant(
'resolveModule',
moduleName => `project/dir/node_modules/${moduleName}`
)
);
$.register(initBuildPackageAssets);
});

Expand Down
18 changes: 5 additions & 13 deletions src/gitHooks.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
'use strict';

const { autoHandler } = require('knifecycle');
const {
buildMetapakModulePath,
mapConfigsSequentially,
identity,
} = require('./utils');
const path = require('path');
const { autoHandler } = require('knifecycle');
const { mapConfigsSequentially, identity } = require('./utils');

module.exports = autoHandler(initBuildPackageGitHooks);

async function initBuildPackageGitHooks(
{ ENV, PROJECT_DIR, GIT_HOOKS_DIR, fs, os, log, require },
{ ENV, PROJECT_DIR, GIT_HOOKS_DIR, fs, os, log, require, resolveModule },
packageConf,
metapakModulesSequence,
metapakModulesConfigs
Expand All @@ -31,10 +25,8 @@ async function initBuildPackageGitHooks(
metapakModulesSequence,
metapakModulesConfigs,
(metapakModuleName, metapakModuleConfig) => {
const packageHooksPath = buildMetapakModulePath(
PROJECT_DIR,
packageConf,
metapakModuleName,
const packageHooksPath = path.join(
resolveModule(metapakModuleName),
'src',
metapakModuleConfig,
'hooks.js'
Expand Down
6 changes: 6 additions & 0 deletions src/gitHooks.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ describe('buildPackageGitHooks', () => {
})
);
$.register(constant('require', requireStub));
$.register(
constant(
'resolveModule',
moduleName => `/home/whoiam/project/dir/node_modules/${moduleName}`
)
);
$.register(initBuildPackageGitHooks);
});

Expand Down
25 changes: 9 additions & 16 deletions src/metapak.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict';

const path = require('path');
const Promise = require('bluebird');
const YError = require('yerror');
const { autoService } = require('knifecycle');
const { buildMetapakModulePath } = require('./utils');

const MAX_PACKAGE_BUILD_ITERATIONS = 15;

Expand All @@ -19,6 +16,7 @@ async function initMetapak({
buildPackageConf,
buildPackageAssets,
buildPackageGitHooks,
resolveModule,
}) {
return async function metapak() {
return _loadJSONFile({ fs }, path.join(PROJECT_DIR, 'package.json'))
Expand Down Expand Up @@ -49,9 +47,9 @@ async function initMetapak({
fs,
log,
},
packageConf,
metapakModulesSequence,
metapackConfigsSequence
metapackConfigsSequence,
resolveModule
)
.then(metapakModulesConfigs =>
Promise.all([
Expand Down Expand Up @@ -187,23 +185,18 @@ function _reorderMetapakModulesNames(
}

function _getPackageMetapakModulesConfigs(
{ PROJECT_DIR, fs, log },
packageConf,
{ fs, log },
metapakModulesSequence,
metapackConfigsSequence
metapackConfigsSequence,
resolveModule
) {
return Promise.props(
metapakModulesSequence.reduce(
(metapakModulesConfigs, metapakModuleName) => {
const modulePath = path.join(resolveModule(metapakModuleName), 'src');

metapakModulesConfigs[metapakModuleName] = fs
.readdirAsync(
buildMetapakModulePath(
PROJECT_DIR,
packageConf,
metapakModuleName,
'src'
)
)
.readdirAsync(modulePath)
.then(metapakModuleConfigs => {
metapakModuleConfigs = metapackConfigsSequence.filter(
metapakModuleConfig =>
Expand Down
6 changes: 6 additions & 0 deletions src/metapak.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ describe('metapak', () => {
$.register(constant('log', sinon.stub()));
$.register(constant('exit', sinon.stub()));
$.register(constant('PROJECT_DIR', 'project/dir'));
$.register(
constant(
'resolveModule',
moduleName => `project/dir/node_modules/${moduleName}`
)
);
buildPackageConfStub = sinon.stub();
$.register(constant('buildPackageConf', buildPackageConfStub));
buildPackageAssetsStub = sinon.stub();
Expand Down
15 changes: 4 additions & 11 deletions src/packageConf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ const sortKeys = require('sort-keys');
// when supporting only Node 10+
const isDeepStrictEqual = require('deep-strict-equal');
const path = require('path');
const {
buildMetapakModulePath,
mapConfigsSequentially,
identity,
buildDiff,
} = require('./utils');
const { mapConfigsSequentially, identity, buildDiff } = require('./utils');

const METAPAK_SCRIPT = 'metapak';

module.exports = autoHandler(initBuildPackageConf);

async function initBuildPackageConf(
{ ENV, PROJECT_DIR, fs, require, log },
{ ENV, PROJECT_DIR, fs, require, log, resolveModule },
packageConf,
metapakModulesSequence,
metapakModulesConfigs
Expand All @@ -28,10 +23,8 @@ async function initBuildPackageConf(
metapakModulesSequence,
metapakModulesConfigs,
async (metapakModuleName, metapakModuleConfig) => {
const packageTransformPath = buildMetapakModulePath(
PROJECT_DIR,
packageConf,
metapakModuleName,
const packageTransformPath = path.join(
resolveModule(metapakModuleName),
'src',
metapakModuleConfig,
'package.js'
Expand Down
6 changes: 6 additions & 0 deletions src/packageConf.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ describe('buildPackageConf', () => {
})
);
$.register(constant('require', requireStub));
$.register(
constant(
'resolveModule',
moduleName => `project/dir/node_modules/${moduleName}`
)
);
$.register(initBuildPackageConf);
});

Expand Down
15 changes: 15 additions & 0 deletions src/resolveModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const YError = require('yerror');
const path = require('path');
const { autoService } = require('knifecycle');

module.exports = autoService(initResolveModule);

async function initResolveModule() {
return function resolveModule(metapakModuleName) {
try {
return path.dirname(require.resolve(`${metapakModuleName}/package`));
} catch (err) {
throw YError.wrap(err, 'E_MODULE_NOT_FOUND', metapakModuleName);
}
};
}
4 changes: 2 additions & 2 deletions src/runMetapak.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const {
default: Knifecycle,
constant,
Expand All @@ -22,6 +20,7 @@ const initBuildPackageConf = require('../src/packageConf');
const initBuildPackageAssets = require('../src/assets');
const initBuildPackageGitHooks = require('../src/gitHooks');
const initProjectDir = require('../src/projectDir');
const initResolveModule = require('../src/resolveModule');

module.exports = { runMetapak, prepareMetapak };

Expand Down Expand Up @@ -62,6 +61,7 @@ async function prepareMetapak($ = new Knifecycle()) {
$.register(initBuildPackageConf);
$.register(initBuildPackageAssets);
$.register(initBuildPackageGitHooks);
$.register(initResolveModule);

program
.version(require(path.join(__dirname, '..', 'package.json')).version)
Expand Down
19 changes: 0 additions & 19 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
const path = require('path');
const chalk = require('chalk');
const diff = require('diff');

module.exports = {
identity: x => x,
buildMetapakModulePath,
mapConfigsSequentially,
buildDiff,
};

function buildMetapakModulePath(
PROJECT_DIR,
packageConf,
metapakModuleName,
...parts
) {
// Take in count the edge case of self applying metapak module
parts = [PROJECT_DIR]
.concat(
packageConf.name !== metapakModuleName
? ['node_modules', metapakModuleName]
: []
)
.concat(parts);
return path.join(...parts);
}

async function mapConfigsSequentially(
metapakModulesSequence,
metapakModulesConfigs,
Expand Down

0 comments on commit 87946c2

Please sign in to comment.