Skip to content
This repository has been archived by the owner on Jul 23, 2019. It is now read-only.

Commit

Permalink
Merge pull request #8 from spryker/develop
Browse files Browse the repository at this point in the history
Version 2.0.0
  • Loading branch information
Alessandro Bellini committed Mar 31, 2016
2 parents 7873f8e + 2278c3c commit 423e229
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 368 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

### 2.0.0
- version test added (check Spryker GUI bundle compatibility - temporary disabled)

### 2.0.0-beta2
- legacy compatibility for v1 projects with `-l|--legacy` flag

### 2.0.0-beta1
- code refactor
- webpack configuration moved ouside the tool
- new provider implementation

### 1.1.3
- new provider implementation

Expand Down
129 changes: 71 additions & 58 deletions lib/automation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ let path = require('path');
let R = require('ramda');
let context = require('./context');
let errors = require('./errors');
let configurator = require('./automation/webpack');
let collector = require('./automation/collector');
let provider = require('./automation/provider');
let compiler = require('./automation/compiler');
let themes = require('./automation/themes');
let cwd = process.cwd();

function processAssets(targetConfigName, configurator, targetPattern, theme) {
function automation(target, targetPattern, theme) {
console.log(`\n${target.toUpperCase()}`.bold.gray);

if (!!theme) {
console.log(`${theme.toUpperCase()} theme`.gray);
}

console.log('- collecting data...'.gray);

return new Promise((resolve, reject) => {
return Promise.all([
collector.entryPoints(targetPattern, theme),
Expand All @@ -26,97 +35,101 @@ function processAssets(targetConfigName, configurator, targetPattern, theme) {
let entryPoints = results[0];
let extensions = results[1];
let manifests = results[2];
let defaultConfig;
let config;
let config = {};

console.log(`\n${targetConfigName.toUpperCase()}`.bold.gray);
function skip(message) {
console.warn('- build'.gray, 'skipped'.yellow, message.gray);
resolve();
}

if (!!theme) {
console.log(`${theme.toUpperCase()} theme`.gray);
function compile() {
compiler.build(config).then(resolve, reject);
}

console.log(`- entry points:`.gray,
console.log(` ${String.fromCharCode(0x2517)} entry points:`.gray,
`${entryPoints.count.valid} valid`.green,
'|'.gray,
`${entryPoints.count.duplicates} duplicates`.yellow,
'|'.gray,
`${entryPoints.count.total} total`.cyan);

try {
defaultConfig = configurator.load(entryPoints.valid, manifests.valid);
} catch (err) {
reject(errors.enrich(err, 'loading default configuration'));
}

if (extensions.count.valid > 0) {
if (extensions.count.valid > 1) {
console.log('- warning: more than one extension for the same application/theme - first will be processed, the others ignored'.yellow);
}
if (extensions.count.valid > 0) {
if (extensions.count.valid > 1) {
console.warn(` ${String.fromCharCode(0x2517)} warning: more than one extension for the same application/theme - first will be processed, the others ignored`.yellow);
}

try {
let extensionName = R.keys(extensions.valid)[0];
let extension = provider.import(path.join(cwd, extensions.valid[extensionName]));
config = R.merge(defaultConfig, extension);
console.log(` ${String.fromCharCode(0x2517)} configuration:`.gray, extensions.valid[extensionName].magenta);

if (!config) {
reject(errors.enrich(new Error('undefined/null/invalid configuration extension returned'), 'extending configuration'));
}
let extension = provider.import(path.join(cwd, extensions.valid[extensionName]), entryPoints.valid, manifests.valid);
config = R.merge(configurator.getDefault(), extension);

console.log('- custom configuration loaded:'.gray, extensions.valid[extensionName].magenta);
} catch (extensionErr) {
config = R.clone(defaultConfig);
console.error('- custom configuration ignored due to error: %s'.red, extensionErr);
}
// LEGACY
if (context.has('legacy') && target === 'yves') {
config = R.merge(configurator.legacy.yves(entryPoints.valid, manifests.valid), extension);
}
// END LEGACY

if (context.has('debug')) {
provider.report();
if (!config) {
throw new Error('undefined/null/invalid configuration returned');
} else {
if (config.antelope && config.antelope.disabled) {
skip('by configuration');
} else {
compile();
}
}
} else {
// LEGACY
if (context.has('legacy') && target === 'zed') {
config = configurator.legacy.zed(entryPoints.valid, manifests.valid);
compile();
} else {
if (target === 'zed') {
console.warn('\n[!] Zed missing configuration may be caused by antelope v2'.yellow);
console.warn(' Run this tool using'.yellow, '-l', 'flag to provide legacy for v1 projects'.yellow, '\n');
}
// END LEGACY

skip('due to missing configuration');
}
}
} else {
config = R.clone(defaultConfig);
console.log('- default configuration loaded'.gray);
}

if (config.antelope && config.antelope.disabled) {
console.log('- build'.gray, 'disabled'.yellow, 'by config'.gray);
resolve();
} else {
compiler.build(config).then(resolve, reject);
} catch (err) {
console.error('- build'.gray, 'aborted'.red);
reject(err);
}
}, reject);
});
}

function automation(targetConfigName, targetPattern, themePattern) {
let configurator = require(`./webpack/configs/${targetConfigName}.js`);

if (targetConfigName === 'zed') {
return processAssets(targetConfigName, configurator, targetPattern, themePattern);
}

return R.reduce((promise, theme) => {
return promise.then(() => {
return processAssets(targetConfigName, configurator, targetPattern, theme);
});
}, Promise.resolve(), themes.getFromPattern(themePattern));
}

module.exports = {
run: () => {
let targetPatterns = context.patterns.target();
let themePatterns = context.patterns.theme();

function processYves() {
return R.reduce((promise, theme) => {
return promise.then(() => {
return automation('yves', targetPatterns.yves, theme);
});
}, Promise.resolve(), themes.getFromPattern(themePatterns.yves));
}

function processZed() {
return automation('zed', targetPatterns.zed);
}

if (context.isAll()) {
return automation('yves', targetPatterns.yves, themePatterns.yves).then(() => {
return automation('zed', targetPatterns.zed);
});
return processYves().then(processZed);
}

if (context.isYves()) {
return automation('yves', targetPatterns.yves, themePatterns.yves);
return processYves();
}

if (context.isZed()) {
return automation('zed', targetPatterns.zed);
return processZed();
}
}
};
3 changes: 2 additions & 1 deletion lib/automation/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ module.exports = {
manifests: () => {
return collect('manifests (package.json)', [
path.join(cwd, `./package.json`),
path.join(cwd, `./vendor/spryker/**/assets/package.json`)
path.join(cwd, `./vendor/spryker/**/assets/*/package.json`)
]);
},
extensions: (targetPattern, themePattern) => {
targetPattern = targetPattern || '';
themePattern = themePattern || '';

return collect('extensions', [
path.join(cwd, `./vendor/spryker/**/assets/*/*${targetPattern}*${themePattern}*${context.patterns.extension()}`),
path.join(cwd, `./assets/**/*${targetPattern}*${themePattern}*${context.patterns.extension()}`)
], context.patterns.extension());
},
Expand Down
28 changes: 20 additions & 8 deletions lib/automation/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ let errors = require('../errors');
let cwd = process.cwd();
let watching = false;

function showModuleNotFoundMessage() {
console.error('\n[!] Something is missing: did you run'.red, 'antelope install', 'before building?'.red);
console.error(' If you did, then it may be a typo...'.red);
}

module.exports = {
build: (config) => {
return new Promise((resolve, reject) => {
Expand All @@ -31,9 +36,13 @@ module.exports = {

if (!!stats.compilation.errors && stats.compilation.errors.length > 0) {
withErrors = true;

R.forEach(function(compilationErr) {
withModuleNotFound = (compilationErr.name === 'ModuleNotFoundError');
console.error(' - %s'.red, compilationErr);
if (compilationErr.name == 'ModuleNotFoundError') {
withModuleNotFound = true;
}

console.error(` ${String.fromCharCode(0x2517)} %s`.red, compilationErr);
}, stats.compilation.errors);

if (!config.watch) {
Expand All @@ -42,22 +51,22 @@ module.exports = {
}

if (!!stats.compilation.fileDependencies) {
console.log(' - built:'.gray, `${stats.compilation.fileDependencies.length}`.green);
console.log(` ${String.fromCharCode(0x2517)} built:`.gray, `${stats.compilation.fileDependencies.length}`.green);

if (context.has('debug') && stats.compilation.fileDependencies.length > 0) {
R.forEach(function(file) {
console.log(' -'.gray, `${path.relative(cwd, file)}`.magenta);
console.log(` ${String.fromCharCode(0x2517)}`.gray, `${path.relative(cwd, file)}`.magenta);
}, stats.compilation.fileDependencies);
}
}

if (!!stats.compilation.missingDependencies) {
console.warn(' - missing:'.gray, `${stats.compilation.missingDependencies.length}`.yellow);
console.warn(` ${String.fromCharCode(0x2517)} missing:`.gray, `${stats.compilation.missingDependencies.length}`.yellow);

if (stats.compilation.missingDependencies.length) {
withWarnings = true;
R.forEach(function(file) {
console.warn(` - ${path.relative(cwd, file)}`.yellow);
console.warn(` ${String.fromCharCode(0x2517)} ${path.relative(cwd, file)}`.yellow);
}, stats.compilation.missingDependencies);
}
}
Expand All @@ -66,15 +75,18 @@ module.exports = {
console.error('- build failed'.gray, 'due to errors'.red);

if (withModuleNotFound) {
console.error(' [!] something is missing: did you run'.red, 'antelope install', 'before building?'.red);
console.error(' if you did, then it may be a typo...'.red);
showModuleNotFoundMessage();
}
} else if (withWarnings) {
console.warn('- build completed'.gray, 'with some warning'.yellow);
} else {
console.log('- build completed'.gray, 'successfully'.green, 'in'.gray, `${(stats.endTime - stats.startTime) / 1000}`.cyan, 'seconds'.gray);
}
} catch (err) {
if (withModuleNotFound) {
showModuleNotFoundMessage();
}

reject(errors.enrich(err, 'compilation report'));
}

Expand Down
54 changes: 34 additions & 20 deletions lib/automation/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,51 @@
let path = require('path');
let fs = require('graceful-fs');
let R = require('ramda');
let context = require('../context');
let cwd = process.cwd();
let report = {};

function extendedRequire(id) {
let requiredModule;

function contextRequire(id) {
try {
requiredModule = require(`${cwd}/node_modules/${id}`);
report[id] = 'project';
return requiredModule;
return require(`${cwd}/node_modules/${id}`);
} catch (err) {
requiredModule = require(id);
report[id] = 'antelope';
return requiredModule;
return require(id);
}
}

function getAntelope(entryPoints, manifests) {
let loadersPaths = process.mainModule.paths.concat([path.join(cwd, './node_modules')]);
let rootPaths = [
cwd,
path.join(cwd, './node_modules')
].concat(R.map((manifest) => {
return path.join(path.dirname(manifest), './node_modules');
}, R.keys(manifests)));

return {
remote: (id) => require(id),
options: context.options(),
entryPoints: entryPoints,
paths: {
root: rootPaths,
loaders: loadersPaths
}
};
}

module.exports = {
import: (id) => {
import: (id, entryPoints, manifest) => {
let context = require('module');
let body = fs.readFileSync(id, 'utf8');
let fn = new Function('module', 'exports', 'require', body);
let fn = new Function('module', 'exports', 'require', '__dirname', 'antelope', body);
let args = [
context,
context.exports,
contextRequire,
path.dirname(id),
getAntelope(entryPoints, manifest)
];

fn.apply(context, [context, context.exports, extendedRequire]);
fn.apply(context, args);
return context.exports;
},
report: () => {
R.forEach((id) => {
console.log(' -'.gray, id.magenta, 'imported from'.gray, report[id].cyan);
}, R.keys(report));

report = {};
}
};
Loading

0 comments on commit 423e229

Please sign in to comment.