Skip to content

Commit

Permalink
Issue #386 [Enhancement][WIP] Register gulp task via targetConfig.tas…
Browse files Browse the repository at this point in the history
…k(pluginName)
  • Loading branch information
t2ym committed Sep 1, 2020
1 parent e30ce98 commit 9adb2df
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 33 deletions.
57 changes: 56 additions & 1 deletion demo-config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,68 @@
@license https://github.com/t2ym/thin-hook/blob/master/LICENSE.md
Copyright (c) 2020 Tetsuya Mori <t2y3141592@gmail.com>. All rights reserved.
*/
const path = require('path');
const fs = require('fs');
const gulp = require('gulp');

// get thin-hook package path even from within thin-hook package
const hookPath = (() => {
try {
let hookJs = require.resolve('thin-hook');
if (hookJs) {
return path.dirname(hookJs);
}
}
catch (e) {}
let dirname = __dirname;
let packageName;
while (!packageName) {
let packagePath = path.resolve(dirname, 'package.json');
if (fs.existsSync(packagePath)) {
let package = require(packagePath);
packageName = package.name;
break;
}
dirname = path.resolve(dirname, '..');
if (dirname === '/') {
break;
}
}
if (packageName === 'thin-hook') {
return dirname;
}
else {
return path.resolve(__dirname, '..');
}
})();

const basePath = module.parent.path;
const configPath = __dirname;
const plugins = 'plugins';

// register gulp task
// Note: this function must be called via targetConfig.task() so that this is the targetConfig object
const task = function (pluginName) {
const targetConfig = this;
const configuratorPath = path.resolve(targetConfig.path.hook, targetConfig.path.plugins, pluginName, 'configurator.js');
const plugin = require(configuratorPath);
if (plugin.name !== pluginName) {
throw new Error(`targetConfig.task("${pluginName}"): plugin.name === ${plugin.name} does not match`);
}
return gulp.task(pluginName, plugin.configurator(targetConfig));
}

const targetConfig = {
task: task,
path: {
base: basePath,
root: 'demo',
config: 'demo-config',
config: path.relative(basePath, configPath),
backend: 'demo-backend',
frontend: 'demo-frontend',
keys: 'demo-keys',
hook: hookPath,
plugins: plugins,
},
mode: {
enableDebugging: false,
Expand Down
23 changes: 3 additions & 20 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ if (!gulp.series) {
}

const targetConfig = require('./demo-config/config.js');
console.log('targetConfig', targetConfig);

const hook = require('./hook.js');

Expand Down Expand Up @@ -398,27 +399,9 @@ gulp.task('integrity-json', () => {
.pipe(gulp.dest('.'));
});

gulp.task('policy',
require('./plugins/policy/configurator.js')
.configurator(
path.resolve(__dirname, targetConfig.path.config, 'policy'), // configPath
path.resolve(__dirname, targetConfig.path.root), // destPath
{
enableDebugging: targetConfig.mode.enableDebugging ? true : false,
},
)
);
targetConfig.task('policy');

gulp.task('disable-devtools',
require('./plugins/disable-devtools/configurator.js')
.configurator(
null, // configPath
path.resolve(__dirname, targetConfig.path.root), // destPath
{
devtoolsDisabled: targetConfig.mode.devtoolsDisabled ? true : false,
},
)
);
targetConfig.task('disable-devtools');

// server secret for cache-automation.js
const serverSecret = crypto.randomFillSync(Buffer.alloc(32)).toString('hex');
Expand Down
20 changes: 14 additions & 6 deletions plugins/disable-devtools/configurator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ const { preprocess } = require('preprocess');
const through = require('through2');
const gulp = require('gulp');

const configurator = (configPath, destPath, {
sourceFile = 'disable-devtools.js',
devtoolsDisabled = 'true',
} = {}) =>
() => gulp.src([ path.resolve(__dirname, sourceFile) ])
const pluginName = 'disable-devtools';

const configurator = (targetConfig) => {
const configPath = path.resolve(targetConfig.path.base, targetConfig.path.config, pluginName);
const destPath = path.resolve(targetConfig.path.base, targetConfig.path.root);
const devtoolsDisabled = targetConfig.mode.devtoolsDisabled;
const pluginDirname = __dirname;
const sourceFile = targetConfig[pluginName] && targetConfig[pluginName].sourceFile
? targetConfig[pluginName].sourceFile
: 'disable-devtools.js';
return () => gulp.src([ path.resolve(pluginDirname, sourceFile) ])
// 1st pass
.pipe(through.obj((file, enc, callback) => {
let script = String(file.contents);
Expand Down Expand Up @@ -43,15 +49,17 @@ const configurator = (configPath, destPath, {
},
{
type: 'js',
srcDir: configPath, // in demo-config/disable-devtools/ ; Note: unused
srcDir: configPath, // in demo-config/policy/
}
);
file.contents = Buffer.from(script);
callback(null, file);
}))
*/
.pipe(gulp.dest(destPath));
}

module.exports = {
configurator,
name: pluginName,
};
20 changes: 14 additions & 6 deletions plugins/policy/configurator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ const { preprocess } = require('preprocess');
const through = require('through2');
const gulp = require('gulp');

const configurator = (configPath, destPath, {
sourceFile = 'hook-callback.js',
enableDebugging = 'false',
} = {}) =>
() => gulp.src([ path.resolve(__dirname, sourceFile) ])
const pluginName = 'policy';

const configurator = (targetConfig) => {
const configPath = path.resolve(targetConfig.path.base, targetConfig.path.config, pluginName);
const destPath = path.resolve(targetConfig.path.base, targetConfig.path.root);
const enableDebugging = targetConfig.mode.enableDebugging;
const pluginDirname = __dirname;
const sourceFile = targetConfig[pluginName] && targetConfig[pluginName].sourceFile
? targetConfig[pluginName].sourceFile
: 'hook-callback.js';
return () => gulp.src([ path.resolve(pluginDirname, sourceFile) ])
// 1st pass
.pipe(through.obj((file, enc, callback) => {
let script = String(file.contents);
Expand All @@ -24,7 +30,7 @@ const configurator = (configPath, destPath, {
},
{
type: 'js',
srcDir: __dirname, // in plugin/policy/
srcDir: pluginDirname, // in plugins/policy/
}
);
script = script.replace(/\/\* #include /g, '/* @include ');
Expand All @@ -49,7 +55,9 @@ const configurator = (configPath, destPath, {
callback(null, file);
}))
.pipe(gulp.dest(destPath));
}

module.exports = {
configurator,
name: pluginName,
};

0 comments on commit 9adb2df

Please sign in to comment.