const task = require('./task/index');
const folder = require('./config/folder/index');
const _ = require('lodash');
const utils = require('corifeus-utils');
const fs = require('fs');
class loader {
constructor(grunt) {
this.grunt = grunt
this.configJit = {
jshint: 'grunt-contrib-jshint',
mochaTest: 'grunt-mocha-test',
mocha_istanbul: 'grunt-mocha-istanbul',
clean: 'grunt-contrib-clean',
watch: 'grunt-contrib-watch',
jsdoc: 'grunt-contrib-jsdoc',
};
}
load(options) {
const grunt = this.grunt;
options = options || {};
options.jit = _.merge(this.configJit, options.jit || {})
options.config = _.merge(require('./config/grunt/js/index')(grunt), options.config)
const config = options.config;
if (options.empty ) {
delete options.config['mocha_istanbul'];
options.replacer = {
type: 'p3x',
}
}
options.replacer = options.replacer || {}
let replacer = 'corifeus';
if (typeof(options.replacer) === 'string') {
replacer = options.replacer;
} else if (options.replacer.hasOwnProperty('type')) {
replacer = options.replacer.type;
}
const replaceFiles = [
'artifacts/**/*.md',
'docs/**/*.md',
'*.md',
'!node_modules',
'!build',
'!LICENSE.md',
'!readme.md',
'!README.md',
]
const defaultHeader = {
header: true,
replace: `
## \${pkg.description}
---
`,
files: replaceFiles
}
const defaultFooter = {
footer: true,
replace: `
---
[**\${pkg.name.toUpperCase()}**](https://pages.corifeus.com/\${git.repo}) Build v\${pkg.version}
[](https://www.facebook.com/corifeus.software) [](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=LFRV89WPRMMVE&lc=HU&item_name=Patrik%20Laszlo&item_number=patrikx3¤cy_code=HUF&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted) [](https://www.patrikx3.com/en/front/contact)
## Sponsor
[](https://www.jetbrains.com/)
`,
files: replaceFiles
};
let angularVersion = '';
const angularPkgPath = `${process.cwd()}/node_modules/@angular/common/package.json`;
if (fs.existsSync(angularPkgPath)) {
const angularPkg = JSON.parse(fs.readFileSync(angularPkgPath).toString());
angularVersion = `
# Built on Angular
\`\`\`${angularPkg.version}\`\`\`
`
}
const hideNodeVersion = options.hasOwnProperty('replacer') && options.replacer.node === false;
let hideBuild = options.hasOwnProperty('replacer') && options.replacer.build === false;
let build = hideBuild ? '' : `[](https://travis-ci.org/patrikx3/\${git.repo}) [](https://scrutinizer-ci.com/g/patrikx3/\${git.repo}/?branch=master) [](https://scrutinizer-ci.com/g/patrikx3/\${git.repo}/?branch=master)`
let nodeVersion = hideNodeVersion ? `# \${pkg.description} v\${pkg.version}
# Description
` : `# \${pkg.description} v\${pkg.version}
This is an open source project. Just code. If you like this code, please add a star in GitHub and you really like, you can donate as well. Thanks you so much!
Given, I have my own server, with dynamic IP address, it could happen that the server for about max 5 minutes can not be reachable for the dynamic DNS or very rarely I backup with Clonzilla the SSD or something with the electricity (too much hoovering or cleaning - but I worked on it, so should not happen again), but for some reason, it is not reachable please hang on for 5-30 minutes and it will be back for sure.
All my domains (patrikx3.com and corifeus.com) could have errors right now, since I am developing in my free time and you can catch glitches, but usually it is stable (imagine updating everything always, which is weird).
### Node Version Requirement
\`\`\`
\${pkg.engines.node}
\`\`\`
### Built on Node
\`\`\`
${process.version}
\`\`\`
The \`\`\`async\`\`\` and \`\`\`await\`\`\` keywords are required.
Install NodeJs:
https://nodejs.org/en/download/package-manager/
${angularVersion}
# Description
`
const footerMain = _.clone(defaultFooter);
footerMain.files = [
'readme.md',
'README.md',
];
switch(replacer) {
case 'corifeus':
options.config['cory-replace'] = {
headerMain: {
header: true,
replace: `
${build}
---
${nodeVersion}
`,
files: [
'readme.md',
'README.md',
]
},
footerMain: footerMain,
header: defaultHeader,
footer: defaultFooter
}
break;
case 'p3x':
let nonEmptyP3x = '';
if (!options.empty) {
nonEmptyP3x = `${build}
---
`
}
options.config['cory-replace'] = {
headerMain: {
header: true,
/* [](https://trello.com/b/gqKHzZGy/p3x)
*/
replace: `
${nonEmptyP3x}
${nodeVersion}
`,
files: [
'readme.md',
'README.md',
]
},
footerMain: footerMain,
header: defaultHeader,
footer: defaultFooter
};
break;
case 'build':
case 'lede':
let nonEmptyLede = '';
if (!options.empty) {
nonEmptyLede = `[](https://travis-ci.org/patrikx3/\${git.repo}) [](https://scrutinizer-ci.com/g/patrikx3/\${git.repo}/?branch=master) [](https://scrutinizer-ci.com/g/patrikx3/\${git.repo}/?branch=master)
# \${pkg.description}
`
}
options.config['cory-replace'] = {
headerMain: {
header: true,
/* [](https://trello.com/b/gqKHzZGy/p3x)
*/
replace: `
${nonEmptyLede}
`,
files: [
'readme.md',
'README.md',
]
},
footerMain: footerMain,
header: defaultHeader,
footer: defaultFooter
};
break;
case 'home':
options.config['cory-replace'] = {
header: {
header: true,
replace: `
## \${pkg.description}
---
`,
files: [
'readme.md',
'README.md',
]
},
footer: defaultFooter,
}
break;
default:
throw new Error(`unknown replacer ${options.replacer}`)
}
if (options.replacer.hasOwnProperty('npmio') && options.replacer.npmio === true && options.config['cory-replace'].hasOwnProperty('headerMain')) {
const replace = options.config['cory-replace'].headerMain.replace;
const append = replace.substring(replace.indexOf('---'));
options.config['cory-replace'].headerMain.replace = replace.substring(0, replace.indexOf('---')) + `
[](https://www.npmjs.com/package/\${pkg.name}/)
` + append
}
grunt.config.merge(config);
Object.keys(task).forEach((taskItem) => task[taskItem](grunt))
grunt.registerTask('cory-test', (target) => {
switch(target) {
case 'js-mocha':
grunt.task.run([
'mochaTest',
'watch:cory-js-test'
])
break;
case 'angular-protractor':
grunt.task.run([
'connect:cory-angular',
'protractor:cory-angular-chrome',
]);
break;
case 'angular-karma':
grunt.task.run([
'karma:cory-angular-run',
'watch:cory-angular-karma'
]);
break;
}
});
grunt.registerTask('cory-build-run', (target) => {
switch(target) {
case 'angular':
grunt.task.run(config.task.run.angular);
break;
case 'js':
grunt.task.run(config.task.run.js);
break;
}
});
// should be at the end
require('jit-grunt')(grunt, options.jit);
require('time-grunt')(grunt);
}
js(options) {
options = options || {};
this.load(options);
}
empty(options) {
options = options || {};
options.empty = true;
this.load(options);
}
ts(options) {
options = options || {};
options.config = _.merge(require('./config/grunt/ts/index')(), options.config || {}),
this.load(options);
}
}
module.exports = loader;