mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
70
lib/config/custom-loaders/file-loader-multi.js
Normal file
70
lib/config/custom-loaders/file-loader-multi.js
Normal file
@@ -0,0 +1,70 @@
|
||||
var path = require('path');
|
||||
var loaderUtils = require('loader-utils');
|
||||
|
||||
module.exports = function(content) {
|
||||
this.cacheable && this.cacheable();
|
||||
if(!this.emitFile) throw new Error('emitFile is required from module system');
|
||||
|
||||
var query = loaderUtils.getOptions(this) || {};
|
||||
var configKey = query.config || 'multiFileLoader';
|
||||
var options = this.options[configKey] || {};
|
||||
var config = {
|
||||
publicPath: false,
|
||||
useRelativePath: false,
|
||||
name: '[hash].[ext]'
|
||||
};
|
||||
|
||||
// options takes precedence over config
|
||||
Object.keys(options).forEach(function(attr) {
|
||||
config[attr] = options[attr];
|
||||
});
|
||||
|
||||
// query takes precedence over config and options
|
||||
Object.keys(query).forEach(function(attr) {
|
||||
config[attr] = query[attr];
|
||||
});
|
||||
|
||||
var context = config.context || this.options.context;
|
||||
var url = loaderUtils.interpolateName(this, config.name, {
|
||||
context: context,
|
||||
content: content,
|
||||
regExp: config.regExp
|
||||
});
|
||||
var path = loaderUtils.interpolateName(this, '[path]', {
|
||||
context: context,
|
||||
content: content,
|
||||
regExp: config.regExp
|
||||
});
|
||||
|
||||
var outputPath = '';
|
||||
|
||||
if (config.outputPath) {
|
||||
outputPath = (
|
||||
typeof config.outputPath === 'function'
|
||||
? config.outputPath(url, path)
|
||||
: config.outputPath + url
|
||||
);
|
||||
} else {
|
||||
outputPath = url;
|
||||
}
|
||||
|
||||
var publicPath = JSON.stringify(url);
|
||||
|
||||
if (config.publicPath) {
|
||||
publicPath = JSON.stringify(
|
||||
typeof config.publicPath === 'function'
|
||||
? config.publicPath(url, path)
|
||||
: config.publicPath + url
|
||||
);
|
||||
}
|
||||
|
||||
publicPath = '__webpack_public_path__ + ' + publicPath;
|
||||
|
||||
if (query.emitFile === undefined || query.emitFile) {
|
||||
this.emitFile(outputPath, content);
|
||||
}
|
||||
|
||||
return 'module.exports = ' + publicPath + ';';
|
||||
};
|
||||
|
||||
module.exports.raw = true;
|
155
lib/config/custom-loaders/generateListComponent.js
Normal file
155
lib/config/custom-loaders/generateListComponent.js
Normal file
@@ -0,0 +1,155 @@
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
|
||||
var erase = true;
|
||||
var readmeContent = null;
|
||||
var readmeFilePath = '';
|
||||
|
||||
function isFileEmpty(fileContents) {
|
||||
return fileContents.toString('utf8').trim() === '';
|
||||
}
|
||||
|
||||
function writeFile(file, newValue) {
|
||||
fs.writeFileSync(file, newValue, 'utf-8');
|
||||
}
|
||||
|
||||
function readFile(file) {
|
||||
return fs.readFileSync(file, 'utf8');
|
||||
}
|
||||
|
||||
function eraseContentList() {
|
||||
if (erase) {
|
||||
erase = false;
|
||||
|
||||
var businessRegex = /(?:<!-- BUSINESS START-->)([\s\S]*?)(?:<!-- BUSINESS END-->)/;
|
||||
var contentRegex = /(?:<!-- CONTENT START-->)([\s\S]*?)(?:<!-- CONTENT END-->)/;
|
||||
var coreRegex = /(?:<!-- CORE START-->)([\s\S]*?)(?:<!-- CORE END-->)/;
|
||||
var businessRegexDirective = /(?:<!-- BUSINESS DIRECTIVE START-->)([\s\S]*?)(?:<!-- BUSINESS DIRECTIVE END-->)/;
|
||||
var contentRegexDirective = /(?:<!-- CONTENT DIRECTIVE START-->)([\s\S]*?)(?:<!-- CONTENT DIRECTIVE END-->)/;
|
||||
var coreRegexDirective = /(?:<!-- CORE DIRECTIVE START-->)([\s\S]*?)(?:<!-- CORE DIRECTIVE END-->)/;
|
||||
var servicessRegex = /(?:<!-- SERVICES START-->)([\s\S]*?)(?:<!-- SERVICES END-->)/;
|
||||
|
||||
|
||||
readmeContent = readmeContent.replace(businessRegex, '<!-- BUSINESS START--><!-- BUSINESS END-->');
|
||||
readmeContent = readmeContent.replace(contentRegex, '<!-- CONTENT START--><!-- CONTENT END-->');
|
||||
readmeContent = readmeContent.replace(coreRegex, '<!-- CORE START--><!-- CORE END-->');
|
||||
|
||||
readmeContent = readmeContent.replace(businessRegexDirective, '<!-- BUSINESS DIRECTIVE START--><!-- BUSINESS DIRECTIVE END-->');
|
||||
readmeContent = readmeContent.replace(contentRegexDirective, '<!-- CONTENT DIRECTIVE START--><!-- CONTENT DIRECTIVE END-->');
|
||||
readmeContent = readmeContent.replace(coreRegexDirective, '<!-- CORE DIRECTIVE START--><!-- CORE DIRECTIVE END-->');
|
||||
|
||||
readmeContent = readmeContent.replace(servicessRegex, '<!-- SERVICES START--><!-- SERVICES END-->');
|
||||
|
||||
writeFile(readmeFilePath, readmeContent)
|
||||
}
|
||||
}
|
||||
|
||||
function generateListComponent(currentFileContent, webpackInstance) {
|
||||
if (!isFileEmpty(currentFileContent)) {
|
||||
|
||||
var componentReg = /(@Component)(\s?)\((\s?){(\s?)((.|[\n\r])*)}(\s?)\)/gm;
|
||||
var componentSection = componentReg.exec(currentFileContent);
|
||||
|
||||
if (componentSection) {
|
||||
|
||||
var selectorReg = /(adf)([a-zA-Z]|-)+((?!,)|(?! ))/g;
|
||||
var selector = selectorReg.exec(componentSection[0]);
|
||||
|
||||
if (selector) {
|
||||
var rawPath = webpackInstance.resourcePath.replace(/\\/g, "/");
|
||||
var removeRoot = rawPath.substr(rawPath.indexOf('/ng2-components') + 16, rawPath.length);
|
||||
var url = removeRoot.substr(0, removeRoot.indexOf('src')) + 'README.md';
|
||||
|
||||
var link = '- [' + selector[0] + '](' + url + ')';
|
||||
|
||||
if (webpackInstance.resourcePath.match('ng2-alfresco-core')) {
|
||||
readmeContent = readmeContent.replace('<!-- CORE START-->', '<!-- CORE START-->\n' + link);
|
||||
} else if (webpackInstance.resourcePath.match('ng2-alfresco-')) {
|
||||
readmeContent = readmeContent.replace('<!-- CONTENT START-->', '<!-- CONTENT START-->\n' + link);
|
||||
} else if (webpackInstance.resourcePath.match('ng2-activiti-')) {
|
||||
readmeContent = readmeContent.replace('<!-- BUSINESS START-->', '<!-- BUSINESS START-->\n' + link);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
var directiveReg = /(@Directive)(\s?)\((\s?){(\s?)((.|[\r\n])*)}(\s?)\)/gm;
|
||||
var directiveSection = directiveReg.exec(currentFileContent);
|
||||
|
||||
if (directiveSection) {
|
||||
var selectorReg = /(adf)([a-zA-Z]|-)+((?!,)|(?! ))/g;
|
||||
var selector = selectorReg.exec(directiveSection[0]);
|
||||
|
||||
if (selector) {
|
||||
var selector = selector[0].replace("selector: '[", "").replace("']", '').replace("]", '').replace("selector: '", "").replace("'", '');
|
||||
|
||||
var rawPath = webpackInstance.resourcePath.replace(/\\/g, "/");
|
||||
var removeRoot = rawPath.substr(rawPath.indexOf('/ng2-components') + 16, rawPath.length);
|
||||
var url = removeRoot.substr(0, removeRoot.indexOf('src')) + 'README.md';
|
||||
|
||||
var link = '- [' + selector + '](' + url + ')';
|
||||
|
||||
if (webpackInstance.resourcePath.match('ng2-alfresco-core')) {
|
||||
readmeContent = readmeContent.replace('<!-- CORE DIRECTIVE START-->', '<!-- CORE DIRECTIVE START-->\n' + link);
|
||||
}
|
||||
//else if (webpackInstance.resourcePath.match('ng2-alfresco-')) {
|
||||
// readmeContent = readmeContent.replace('<!-- CONTENT DIRECTIVE START-->', '<!-- CONTENT DIRECTIVE START-->\n' + link);
|
||||
//}
|
||||
//else if (webpackInstance.resourcePath.match('ng2-activiti-')) {
|
||||
// readmeContent = readmeContent.replace('<!-- BUSINESS DIRECTIVE START-->', '<!-- BUSINESS DIRECTIVE START-->\n' + link);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
writeFile(readmeFilePath, readmeContent);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function generateListservices(currentFileContent, webpackInstance) {
|
||||
if (!isFileEmpty(currentFileContent)) {
|
||||
|
||||
var servicesReg = /(@Injectable\(\))(([a-zA-Z ]|[\r\n])*)/gm;
|
||||
var servicesSection = servicesReg.exec(currentFileContent);
|
||||
|
||||
if (servicesSection) {
|
||||
|
||||
var selectorReg = /([a-zA-Z])+Service/g;
|
||||
var selector = selectorReg.exec(servicesSection[0]);
|
||||
|
||||
if (selector) {
|
||||
var rawPath = webpackInstance.resourcePath.replace(/\\/g, "/");
|
||||
var url = rawPath.substr(rawPath.indexOf('/ng2-components') + 16, rawPath.length);
|
||||
|
||||
var link = '- [' + selector[0] + '](' + url + ')';
|
||||
|
||||
readmeContent = readmeContent.replace('<!-- SERVICES START-->', '<!-- SERVICES START-->\n' + link);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
writeFile(readmeFilePath, readmeContent);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function (input, map) {
|
||||
this.cacheable && this.cacheable();
|
||||
var callback = this.async();
|
||||
|
||||
readmeFilePath = path.resolve(__dirname, '../../README.md');
|
||||
|
||||
if (!readmeContent) {
|
||||
readmeContent = readFile(readmeFilePath);
|
||||
}
|
||||
|
||||
if (readmeContent) {
|
||||
eraseContentList();
|
||||
generateListComponent(input, this);
|
||||
generateListservices(input, this);
|
||||
}
|
||||
callback(null, input, map);
|
||||
}
|
67
lib/config/custom-loaders/license-check.js
Normal file
67
lib/config/custom-loaders/license-check.js
Normal file
@@ -0,0 +1,67 @@
|
||||
var path = require('path');
|
||||
var loaderUtils = require('loader-utils');
|
||||
var fs = require('fs');
|
||||
|
||||
var licenseFileUtf8Store = undefined;
|
||||
|
||||
function readLicenseHeaderFile(licenseFilePath) {
|
||||
if (licenseFileUtf8Store) {
|
||||
return licenseFileUtf8Store;
|
||||
}
|
||||
|
||||
if (fs.existsSync(licenseFilePath)) {
|
||||
licenseFileUtf8Store = fs.readFileSync(licenseFilePath, 'utf8').split(/\r?\n/);
|
||||
return licenseFileUtf8Store;
|
||||
}
|
||||
|
||||
throw new Error('The license header file path is wrong ' + licenseFilePath);
|
||||
}
|
||||
|
||||
function isFileEmpty(fileContents) {
|
||||
return fileContents.toString('utf8').trim() === '';
|
||||
}
|
||||
|
||||
function readCurrentFile(fileContent) {
|
||||
return fileContent.toString('utf8').split(/\r?\n/);
|
||||
}
|
||||
|
||||
function isLicenseHeaderPresent(currentFileContent, licenseFilePath) {
|
||||
if (!isFileEmpty(currentFileContent)) {
|
||||
var currentFileUtf8 = readCurrentFile(currentFileContent),
|
||||
licenseFileUtf8 = readLicenseHeaderFile(licenseFilePath);
|
||||
skipStrict = 0;
|
||||
|
||||
if(currentFileUtf8[0] === '"use strict";' ) {
|
||||
skipStrict = 1;
|
||||
}
|
||||
|
||||
for (var i = skipStrict; i < licenseFileUtf8.length; i++) {
|
||||
if (currentFileUtf8[i + skipStrict] !== licenseFileUtf8[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function report(hasHeader, emitter, filename) {
|
||||
if (hasHeader) return;
|
||||
emitter('Missing license header file : ' + filename);
|
||||
}
|
||||
|
||||
function licenseCheck(webpackInstance, input, options) {
|
||||
var isLicensePresent = isLicenseHeaderPresent(input, options.licenseFile);
|
||||
|
||||
var emitter = options.emitErrors ? webpackInstance.emitError : webpackInstance.emitWarning;
|
||||
|
||||
report(isLicensePresent, emitter, webpackInstance.resourcePath);
|
||||
}
|
||||
|
||||
module.exports = function(input, map) {
|
||||
this.cacheable && this.cacheable();
|
||||
var callback = this.async();
|
||||
|
||||
var options = loaderUtils.getOptions(this);
|
||||
licenseCheck(this, input, options);
|
||||
callback(null, input, map);
|
||||
};
|
Reference in New Issue
Block a user