ej2-javascript-samples/gulpfile.js

469 строки
17 KiB
JavaScript
Исходник Постоянная ссылка Обычный вид История

var glob = require('glob');
2019-10-06 15:35:27 +03:00
var fs = require('fs');
2023-12-19 08:07:01 +03:00
var gulp = require('gulp');
var path = require('path');
var shelljs = global.shelljs || require('shelljs');
2023-12-19 08:07:01 +03:00
var webpackGulp = require('webpack-stream');
var webpack = require('webpack');
var runSequence = require('gulp4-run-sequence');
var platform = 'typescript';
var sampleList;
var commonConfig = require('./config.json');
var defRegex = /(this.|export |\(window as any\).)default (= |)\(\)(: void|) => {/g;
var loadRegex = /loadCultureFiles\(\);/
var impCultureRegex = /import \{ loadCultureFiles \} from \'\.\.\/common\/culture-loader\';/;
var impCulture = "import { loadCultureFiles } from '../common/culture-loader';";
2019-10-06 15:35:27 +03:00
2023-12-19 08:07:01 +03:00
gulp.task('bundle', function (done) {
if (commonConfig.platform === 'javascript') {
runSequence('ship-deps', function () {
bundle(commonConfig.platform, done);
});
} else {
bundle(commonConfig.platform, done);
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
});
2019-10-06 15:35:27 +03:00
2023-12-19 08:07:01 +03:00
function bundle(platform, done) {
var webpackConfig = require(fs.realpathSync('./webpack.config.js'));
return gulp.src('.')
.pipe(webpackGulp(webpackConfig, webpack))
.pipe(gulp.dest('.'))
.on('end', function () {
done();
})
.on('error', function (e) {
done(e);
process.exit(1);
});
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
gulp.task('ship-deps', function (done) {
gulp.src(['./node_modules/@syncfusion/ej2/*.css',
'./node_modules/@syncfusion/ej2/dist/*{.js,.map}', './node_modules/fuse.js/dist/fuse.min.js'
])
.pipe(gulp.dest('./dist/'))
.on('end', function () {
done();
});
});
2019-10-06 15:35:27 +03:00
2023-12-19 08:07:01 +03:00
gulp.task('pdfium-wasm', function (done) {
const filePath = process.cwd() + '/package.json';
const fileContent = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(fileContent)
var platformName = jsonData.name;
let Folder = glob.sync('./node_modules/@syncfusion/ej2-pdfviewer/dist/ej2-pdfviewer-lib/*.{js,wasm}')
if (platformName == '@syncfusion/ej2-javascript-samples') {
return gulp.src(Folder)
.pipe(gulp.dest('./dist/ej2-pdfviewer-lib'));
} else if (platformName == '@syncfusion/ej2-samples') {
return gulp.src(Folder)
.pipe(gulp.dest('./src/ej2-pdfviewer-lib'));
} else if (platformName == '@syncfusion/ej2-react-samples') {
return gulp.src(Folder)
.pipe(gulp.dest('./ej2-pdfviewer-lib'));
} else if (platformName === 'ej2-vue-samples') {
const sourcePath = './node_modules/@syncfusion/ej2-pdfviewer/dist/ej2-pdfviewer-lib';
const destpath = './public/js/ej2-pdfviewer-lib';
if (!fs.existsSync(destpath)) {
shelljs.mkdir('-p', destpath);
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
shelljs.cp('-R', sourcePath + '/*', destpath);
console.log("File moved to Destination")
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
done();
});
2019-10-06 15:35:27 +03:00
2023-12-19 08:07:01 +03:00
function preProcess() {
var googleAnalyticsScript = `
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-W8WD8WN');</script>
<!-- End Google Tag Manager -->`
var bodyScript = "ej2-samples" ? `
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-W8WD8WN"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<div hidden id="sync-analytics" data-queue="EJ2 - Javascript - Demos"></div>
` : `
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-W8WD8WN"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<div hidden id="sync-analytics" data-queue="EJ2 - react - Demos"></div>
`
var hFiles = glob.sync('./samples/**/index.html');
for (var hFile of hFiles) {
var htmlCont = fs.readFileSync(hFile, 'utf8');
var $ = cheerio.load(htmlCont);
var headContent = $('head').html();
if (headContent.indexOf('gtm.start') === -1) {
var nHtml = `<!DOCTYPE html>\n<html lang="en">\n<head> ${googleAnalyticsScript} ${headContent} \n</head>\n<body class="ej2-new">\n ${bodyScript + $('body').html()}\n</body>\n</html>`;
fs.writeFileSync(hFile, nHtml, 'utf8');
}
}
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
exports.preProcess = preProcess;
2019-10-06 15:35:27 +03:00
2023-12-19 08:07:01 +03:00
// replace-this
gulp.task('replace-this', function (done) {
if (shelljs.exec('node --max-old-space-size=7168 ./node_modules/gulp/bin/gulp.js replace-this-files').code !== 0) {
process.exit(1);
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
done();
});
gulp.task('replace-this-files', function (done) {
var glob = require('glob');
var fs = require('fs');
var files = glob.sync('./src/**/*.ts');
for (let file of files) {
var content = fs.readFileSync(file, 'utf-8').replace(/this.default/, '(window as any).default');
if (defRegex.test(content)) {
if (!loadRegex.test(content)) {
content = content.replace(defRegex, '(window as any).default = (): void => {\n loadCultureFiles();');
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
if (!impCultureRegex.test(content)) {
content = impCulture + '\n' + content;
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
}
fs.writeFileSync(file, content);
2019-10-06 15:35:27 +03:00
2023-12-19 08:07:01 +03:00
}
done();
});
2019-10-06 15:35:27 +03:00
2023-12-19 08:07:01 +03:00
gulp.task('build', function(done){
preProcess();
var fCode = shelljs.exec('gulp pdfium-wasm && gulp create-locale && gulp combine-samplelist && gulp combine-seo-list && gulp bundle && gulp next-prev');
if (fCode.code !== 0) {
2019-10-06 15:35:27 +03:00
process.exit(1);
2023-12-19 08:07:01 +03:00
} else {
2019-10-06 15:35:27 +03:00
done();
}
});
2023-12-19 08:07:01 +03:00
gulp.task('combine-seo-list', function (done) {
combineSampleList(commonConfig.platform, true, done);
2019-10-06 15:35:27 +03:00
});
2023-12-19 08:07:01 +03:00
gulp.task('next-prev', function (done) {
var files = glob.sync('./*/*', {
silent: true,
ignore: ['./src/**', './.vscode/**', './node_modules/**', './samples/**', './spec/**', './styles/**']
});
var samplesAr = require(fs.realpathSync('./src/common/sampleListSeo.js'));
var samplePathArray = getSamplePathArray(samplesAr.samplesList);
var next = "";
var prev = "";
var anchorPrev = "";
var anchorNext = "";
var disablenext = "";
var disableprev = "";
var platform;
for (var i = 0; i < files.length; i++) {
var url = files[i].split('/')[2];
var currentComp = files[i].split('/')[1];
var curlocation = currentComp + '/' + url;
var inx = samplePathArray.indexOf(curlocation);
if (inx == 0) {
next = samplePathArray[inx + 1];
platform = '/javascript';
anchorNext = `href="https://ej2.syncfusion.com${platform}/demos/${next}/"`;
anchorPrev = '';
disableprev = 'e-disabled';
disablenext = '';
2019-10-06 15:35:27 +03:00
2023-12-19 08:07:01 +03:00
} else if (inx == samplePathArray.length - 1) {
prev = samplePathArray[inx - 1];
platform = '/javascript';
anchorPrev = `href="https://ej2.syncfusion.com/${platform}demos/${prev}/"`;
anchorNext = '';
disablenext = 'e-disabled';
disableprev = '';
} else if (inx !== 0 || samplePathArray.length - 1) {
if (inx !== -1) {
next = samplePathArray[inx + 1];
prev = samplePathArray[inx - 1];
platform = '/javascript';
anchorNext = `href="https://ej2.syncfusion.com${platform}/demos/${next}/"`;
anchorPrev = `href="https://ej2.syncfusion.com${platform}/demos/${prev}/"`;
disablenext = '';
disableprev = '';
}
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
if (fs.existsSync('./' + curlocation + '/index.html')) {
var pHtml = fs.readFileSync('./' + curlocation + '/index.html', 'utf8');
pHtml = pHtml.replace(/{{:anchor-next}}/g, anchorNext);
pHtml = pHtml.replace(/{{:anchor-prev}}/g, anchorPrev);
pHtml = pHtml.replace(/{{:disabled-next}}/g, disablenext);
pHtml = pHtml.replace(/{{:disabled-prev}}/, disableprev);
pHtml = pHtml.replace(/<link rel="canonical" href=.+/, '');
fs.writeFileSync('./' + curlocation + '/index.html', pHtml, 'utf-8');
2019-10-06 15:35:27 +03:00
}
}
2023-12-19 08:07:01 +03:00
done();
});
function getSamplePathArray(samplesAr) {
var samplesPathArray = [];
for (var sampleList of samplesAr) {
var dir = sampleList.directory;
for (var sample of sampleList.samples) {
samplesPathArray.push(dir + '/' + sample.url);
}
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
return samplesPathArray;
2019-10-06 15:35:27 +03:00
}
var jsoncombine = require('gulp-jsoncombine');
var elasticlunr = require('elasticlunr');
var sampleOrder = '';
var sampleList;
gulp.task('combine-samplelist', function (done) {
if (shelljs.exec('node --max-old-space-size=7168 ./node_modules/gulp/bin/gulp.js combine-samplelists').code !== 0) {
process.exit(1);
}
2023-12-19 08:07:01 +03:00
done();
2019-10-06 15:35:27 +03:00
});
gulp.task('combine-samplelists', function (done) {
2023-12-19 08:07:01 +03:00
combineSampleList(commonConfig.platform, false, done);
2019-10-06 15:35:27 +03:00
});
2023-12-19 08:07:01 +03:00
function combineSampleList(platform, seo, done) {
var filename = platform === 'javascript' ? 'samplelist.js' : 'sampleList.ts';
console.log('Combining sample list started', filename);
if (seo) {
filename = 'sampleListSeo.js';
}
2019-10-06 15:35:27 +03:00
sampleOrder = JSON.parse(fs.readFileSync(`./src/common/sampleOrder.json`));
var sampleListPath = `./src/common/`;
var apiReference = {};
if (sampleList && sampleList.length) {
var controls = getControls();
sampleOrder = getSampleOrder(controls);
}
2023-12-19 08:07:01 +03:00
return gulp.src(commonConfig.samplejson)
2019-10-06 15:35:27 +03:00
.pipe(jsoncombine(`${filename}`, function (data) {
var result = [];
var subCategory = [];
var intId = 0;
2019-10-06 15:35:27 +03:00
var curDirectory = '';
var addUID = function (pid, dt) {
for (var i = 0; i < dt.length; i++) {
dt[i].uid = pid + i;
if (dt[i].hasOwnProperty('samples')) {
curDirectory = dt[i].directory;
subCategory = [];
addUID('00' + intId + i, dt[i].samples);
intId++;
} else {
var index = subCategory.indexOf(dt[i].category);
if (index !== -1) {
dt[i].order = index;
} else {
subCategory.push(dt[i].category);
dt[i].order = subCategory.length - 1;
}
}
}
2019-10-06 15:35:27 +03:00
};
var orderKeys = Object.keys(sampleOrder);
for (var i = 0; i < orderKeys.length; i++) {
2019-10-06 15:35:27 +03:00
console.log('Category : ' + orderKeys[i]);
var components = sampleOrder[orderKeys[i]];
let category = orderKeys[i];
for (var j = 0; j < components.length; j++) {
2019-10-06 15:35:27 +03:00
console.log('Component : ' + components[j]);
var currentData = getSamples(data, components[j], category);
2019-10-06 15:35:27 +03:00
currentData.order = i;
result.push(currentData);
}
}
2019-10-06 15:35:27 +03:00
addUID('0', result);
2023-12-19 08:07:01 +03:00
generateSearchIndex(result, sampleListPath, platform);
var configProps = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
var commonChunkSkip = configProps.cssComponent || [];
var sList = `${platform === 'javascript' ?
'window.samplesList =' : 'export let samplesList : any ='}` + JSON.stringify(result) + ';\n\n' +
`${platform === 'javascript' ? 'window.apiList =' : 'export let apiList:any='}` + JSON.stringify(apiReference) +
`${platform === 'typescript' ? `\n\n export let skipCommonChunk: string[] = ${JSON.stringify(commonChunkSkip)};` : ''}`;
2019-10-06 15:35:27 +03:00
var seoList = 'var samplesList =' + JSON.stringify(result) + ';\nexports.samplesList = samplesList;';
2023-12-19 08:07:01 +03:00
if (seo) {
return new Buffer(seoList);
}
2019-10-06 15:35:27 +03:00
return new Buffer(sList);
}))
2019-10-06 15:35:27 +03:00
.pipe(gulp.dest(sampleListPath))
.on('end', function () {
done();
})
.on('error', function (e) {
done(e);
process.exit(1);
});
}
function getControls() {
var controlList = sampleList;
var controls = controlList.split(',');
for (var i = 0; i < controls.length; i++) {
controls[i] = controls[i].replace(/^\s*/, '').replace(/\s*$/, '');
}
return controls;
}
function getSampleOrder(controls) {
var controlWiseSampleOrder = {};
var orderKeys = Object.keys(sampleOrder);
for (var i = 0; i < orderKeys.length; i++) {
var keys = orderKeys[i];
//get the controlslist
var components = sampleOrder[keys];
2019-10-06 15:35:27 +03:00
for (let j = 0; j < controls.length; j++) {
// get the index of controls
var componentIndex = components.indexOf(controls[j]);
2019-10-06 15:35:27 +03:00
if (componentIndex !== -1) {
if (!controlWiseSampleOrder[keys]) {
controlWiseSampleOrder[keys] = [components[componentIndex]];
} else {
controlWiseSampleOrder[keys].push(components[componentIndex]);
}
}
}
}
return controlWiseSampleOrder;
}
function getSamples(data, component, category) {
2023-12-19 08:07:01 +03:00
var dataList = Object.keys(data);
for (var i = 0; i < dataList.length; i++) {
var currentData = data[dataList[i]];
if (component === currentData.name && category === currentData.category) {
2023-12-19 08:07:01 +03:00
return currentData;
}
}
}
function generateSearchIndex(sampleArray, samplelistpath, platform) {
var searchindexpath = platform === 'javascript' ? `${samplelistpath}search-index.js` :
`${samplelistpath}search-index.json`;
elasticlunr.clearStopWords();
var instance = elasticlunr(function () {
this.addField('component');
this.addField('name');
this.setRef('uid');
});
2019-10-06 15:35:27 +03:00
for (let sampleCollection of sampleArray) {
var component = sampleCollection.name;
var directory = sampleCollection.directory;
var puid = sampleCollection.uid;
2019-10-06 15:35:27 +03:00
var hideOnDevice = sampleCollection.hideOnDevice;
for (let sample of sampleCollection.samples) {
sample.component = component;
sample.dir = directory;
sample.parentId = puid;
2023-12-19 08:07:01 +03:00
sample.hideOnDevice = hideOnDevice ? hideOnDevice : sample.hideOnDevice;
instance.addDoc(sample);
}
}
2023-12-19 08:07:01 +03:00
fs.writeFileSync(searchindexpath, `${platform === 'javascript' ? 'window.searchIndex =' : ''}` + JSON.stringify(instance.toJSON()));
}
2023-12-19 08:07:01 +03:00
gulp.task('create-locale', function (done) {
createLocale(platform, done);
});
2023-12-19 08:07:01 +03:00
function createLocale(platform, done) {
var fileExt = '.js';
var localePath = `./dist`;
if (!fs.existsSync(localePath)) {
shelljs.mkdir('-p', localePath);
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
var localeJson = glob.sync('./src/**/locale.json', {
silent: true
2019-10-06 15:35:27 +03:00
});
2023-12-19 08:07:01 +03:00
if (localeJson.length) {
// baseUtil;
var obj = {};
for (var i = 0; i < localeJson.length; i++) {
var compentLocale = JSON.parse(fs.readFileSync(localeJson[i]));
obj = extend({}, obj, compentLocale, true);
}
2023-12-19 08:07:01 +03:00
fs.writeFileSync(`${localePath}/locale-string${fileExt}`, 'window.Locale=' + JSON.stringify(obj) + ';');
done();
} else {
fs.writeFileSync(`${localePath}/locale-string${fileExt}`,'window.Locale=');
done();
}
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
function extend(copied, first, second, deep) {
var result = copied || {};
var length = arguments.length;
if (deep) {
length = length - 1;
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
var _loop_1 = function (i) {
if (!arguments_1[i]) {
return 'continue';
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
var obj1 = arguments_1[i];
Object.keys(obj1).forEach(function (key) {
var src = result[key];
var copy = obj1[key];
var clone;
if (deep && isObject(copy)) {
clone = isObject(src) ? src : {};
result[key] = extend({}, clone, copy, true);
}
else {
result[key] = copy;
}
});
};
var arguments_1 = arguments;
for (var i = 1; i < length; i++) {
_loop_1(i);
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
return result;
2019-10-06 15:35:27 +03:00
}
2023-12-19 08:07:01 +03:00
function isObject(obj) {
var objCon = {};
return (!isNullOrUndefined(obj) && obj.constructor === objCon.constructor);
}
2019-10-06 15:35:27 +03:00
2023-12-19 08:07:01 +03:00
function isNullOrUndefined(value) {
return value === undefined || value === null;
}
2019-10-06 15:35:27 +03:00
2023-12-19 08:07:01 +03:00
gulp.task('serve', function(done) {
runSequence('build',function() {
var browserSync = require('browser-sync');
var bs = browserSync.create('Essential TypeScript');
var options = {
server: {
baseDir: './'
},
ui: false
};
bs.init(options, done);
});
});